Android Color Sum ColorSum(int color, int color2)

Here you can find the source of ColorSum(int color, int color2)

Description

Returns the sum of the two provided colors.

Parameter

Parameter Description
color a parameter
color2 a parameter

Declaration

public static int ColorSum(int color, int color2) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w ww  . j av  a  2 s.c  o  m*/
     * Returns the sum of the two provided colors.
     *
     * @param color
     * @param color2
     * @return
     */
    public static int ColorSum(int color, int color2) {
        return ColorSum(color, color2, false);
    }

    /**
     * Returns the sum of one color and another. If indicated by the subtract param, will return the
     * difference of the first color and the second instead, with a floor on each color part of 0, and
     * a ceiling of 255.
     *
     * @param color
     * @param color2
     * @param subtract
     * @return
     */
    public static int ColorSum(int color, int color2, boolean subtract) {
        final int a = (color >>> 24);
        final int r = (color >> 16) & 0xFF;
        final int g = (color >> 8) & 0xFF;
        final int b = (color) & 0xFF;

        final int a2 = (subtract) ? -(color2 >>> 24) : (color2 >>> 24);
        final int r2 = (subtract) ? -((color2 >> 16) & 0xFF)
                : ((color2 >> 16) & 0xFF);
        final int g2 = (subtract) ? -((color2 >> 8) & 0xFF)
                : ((color2 >> 8) & 0xFF);
        final int b2 = (subtract) ? -((color2) & 0xFF) : ((color2) & 0xFF);

        return (ClippedColorPart(a + a2) << 24)
                + (ClippedColorPart(r + r2) << 16)
                + (ClippedColorPart(g + g2) << 8)
                + (ClippedColorPart(b + b2));
    }

    public static int ClippedColorPart(int color) {
        if (color < 0) {
            return 0;
        } else if (color > 0xFF) {
            return 0xFF;
        }
        return color;
    }
}

Related

  1. ColorSum(int color, int color2, boolean subtract)