@SdkSuppress(minSdkVersion = 26) @Test public void addColorsSameColorSpace() { Color result = ColorUtils.compositeColors(Color.valueOf(0x7f007f00), Color.valueOf(0x7f7f0000)); assertEquals(0.16f, result.red(), 1e-2f); assertEquals(0.33f, result.green(), 1e-2f); assertEquals(0.00f, result.blue(), 1e-2f); assertEquals(0.75f, result.alpha(), 1e-2f); }
public static int compositeColors(@ColorInt int foreground, @ColorInt int background) { int bgAlpha = Color.alpha(background); int fgAlpha = Color.alpha(foreground); int a = compositeAlpha(fgAlpha, bgAlpha); int r = compositeComponent(Color.red(foreground), fgAlpha, Color.red(background), bgAlpha, a); int g = compositeComponent(Color.green(foreground), fgAlpha, Color.green(background), bgAlpha, a); int b = compositeComponent(Color.blue(foreground), fgAlpha, Color.blue(background), bgAlpha, a); return Color.argb(a, r, g, b); }
ColorUtils { public static int compositeColors(@ColorInt int foreground, @ColorInt int background) { int bgAlpha = Color.alpha(background); int fgAlpha = Color.alpha(foreground); int a = compositeAlpha(fgAlpha, bgAlpha); int r = compositeComponent(Color.red(foreground), fgAlpha, Color.red(background), bgAlpha, a); int g = compositeComponent(Color.green(foreground), fgAlpha, Color.green(background), bgAlpha, a); int b = compositeComponent(Color.blue(foreground), fgAlpha, Color.blue(background), bgAlpha, a); return Color.argb(a, r, g, b); } }
ColorUtils { public static int compositeColors(@ColorInt int foreground, @ColorInt int background) { int bgAlpha = Color.alpha(background); int fgAlpha = Color.alpha(foreground); int a = compositeAlpha(fgAlpha, bgAlpha); int r = compositeComponent(Color.red(foreground), fgAlpha, Color.red(background), bgAlpha, a); int g = compositeComponent(Color.green(foreground), fgAlpha, Color.green(background), bgAlpha, a); int b = compositeComponent(Color.blue(foreground), fgAlpha, Color.blue(background), bgAlpha, a); return Color.argb(a, r, g, b); } private  ColorUtils(); }
ColorUtils { public static int compositeColors(@ColorInt int foreground, @ColorInt int background) { int bgAlpha = Color.alpha(background); int fgAlpha = Color.alpha(foreground); int a = compositeAlpha(fgAlpha, bgAlpha); int r = compositeComponent(Color.red(foreground), fgAlpha, Color.red(background), bgAlpha, a); int g = compositeComponent(Color.green(foreground), fgAlpha, Color.green(background), bgAlpha, a); int b = compositeComponent(Color.blue(foreground), fgAlpha, Color.blue(background), bgAlpha, a); return Color.argb(a, r, g, b); } private  ColorUtils(); static int compositeColors(@ColorInt int foreground, @ColorInt int background); @RequiresApi(26) @NonNull static Color compositeColors(@NonNull Color foreground, @NonNull Color background); @FloatRange(from = 0.0, to = 1.0) static double calculateLuminance(@ColorInt int color); static double calculateContrast(@ColorInt int foreground, @ColorInt int background); static int calculateMinimumAlpha(@ColorInt int foreground, @ColorInt int background,
            float minContrastRatio); static void RGBToHSL(@IntRange(from = 0x0, to = 0xFF) int r,
            @IntRange(from = 0x0, to = 0xFF) int g, @IntRange(from = 0x0, to = 0xFF) int b,
            @NonNull float[] outHsl); static void colorToHSL(@ColorInt int color, @NonNull float[] outHsl); @ColorInt static int HSLToColor(@NonNull float[] hsl); @ColorInt static int setAlphaComponent(@ColorInt int color,
            @IntRange(from = 0x0, to = 0xFF) int alpha); static void colorToLAB(@ColorInt int color, @NonNull double[] outLab); static void RGBToLAB(@IntRange(from = 0x0, to = 0xFF) int r,
            @IntRange(from = 0x0, to = 0xFF) int g, @IntRange(from = 0x0, to = 0xFF) int b,
            @NonNull double[] outLab); static void colorToXYZ(@ColorInt int color, @NonNull double[] outXyz); static void RGBToXYZ(@IntRange(from = 0x0, to = 0xFF) int r,
            @IntRange(from = 0x0, to = 0xFF) int g, @IntRange(from = 0x0, to = 0xFF) int b,
            @NonNull double[] outXyz); static void XYZToLAB(@FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_X) double x,
            @FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_Y) double y,
            @FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_Z) double z,
            @NonNull double[] outLab); static void LABToXYZ(@FloatRange(from = 0f, to = 100) final double l,
            @FloatRange(from = -128, to = 127) final double a,
            @FloatRange(from = -128, to = 127) final double b,
            @NonNull double[] outXyz); @ColorInt static int XYZToColor(@FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_X) double x,
            @FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_Y) double y,
            @FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_Z) double z); @ColorInt static int LABToColor(@FloatRange(from = 0f, to = 100) final double l,
            @FloatRange(from = -128, to = 127) final double a,
            @FloatRange(from = -128, to = 127) final double b); static double distanceEuclidean(@NonNull double[] labX, @NonNull double[] labY); @ColorInt static int blendARGB(@ColorInt int color1, @ColorInt int color2,
            @FloatRange(from = 0.0, to = 1.0) float ratio); static void blendHSL(@NonNull float[] hsl1, @NonNull float[] hsl2,
            @FloatRange(from = 0.0, to = 1.0) float ratio, @NonNull float[] outResult); static void blendLAB(@NonNull double[] lab1, @NonNull double[] lab2,
            @FloatRange(from = 0.0, to = 1.0) double ratio, @NonNull double[] outResult); }
ColorUtils { public static int compositeColors(@ColorInt int foreground, @ColorInt int background) { int bgAlpha = Color.alpha(background); int fgAlpha = Color.alpha(foreground); int a = compositeAlpha(fgAlpha, bgAlpha); int r = compositeComponent(Color.red(foreground), fgAlpha, Color.red(background), bgAlpha, a); int g = compositeComponent(Color.green(foreground), fgAlpha, Color.green(background), bgAlpha, a); int b = compositeComponent(Color.blue(foreground), fgAlpha, Color.blue(background), bgAlpha, a); return Color.argb(a, r, g, b); } private  ColorUtils(); static int compositeColors(@ColorInt int foreground, @ColorInt int background); @RequiresApi(26) @NonNull static Color compositeColors(@NonNull Color foreground, @NonNull Color background); @FloatRange(from = 0.0, to = 1.0) static double calculateLuminance(@ColorInt int color); static double calculateContrast(@ColorInt int foreground, @ColorInt int background); static int calculateMinimumAlpha(@ColorInt int foreground, @ColorInt int background,
            float minContrastRatio); static void RGBToHSL(@IntRange(from = 0x0, to = 0xFF) int r,
            @IntRange(from = 0x0, to = 0xFF) int g, @IntRange(from = 0x0, to = 0xFF) int b,
            @NonNull float[] outHsl); static void colorToHSL(@ColorInt int color, @NonNull float[] outHsl); @ColorInt static int HSLToColor(@NonNull float[] hsl); @ColorInt static int setAlphaComponent(@ColorInt int color,
            @IntRange(from = 0x0, to = 0xFF) int alpha); static void colorToLAB(@ColorInt int color, @NonNull double[] outLab); static void RGBToLAB(@IntRange(from = 0x0, to = 0xFF) int r,
            @IntRange(from = 0x0, to = 0xFF) int g, @IntRange(from = 0x0, to = 0xFF) int b,
            @NonNull double[] outLab); static void colorToXYZ(@ColorInt int color, @NonNull double[] outXyz); static void RGBToXYZ(@IntRange(from = 0x0, to = 0xFF) int r,
            @IntRange(from = 0x0, to = 0xFF) int g, @IntRange(from = 0x0, to = 0xFF) int b,
            @NonNull double[] outXyz); static void XYZToLAB(@FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_X) double x,
            @FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_Y) double y,
            @FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_Z) double z,
            @NonNull double[] outLab); static void LABToXYZ(@FloatRange(from = 0f, to = 100) final double l,
            @FloatRange(from = -128, to = 127) final double a,
            @FloatRange(from = -128, to = 127) final double b,
            @NonNull double[] outXyz); @ColorInt static int XYZToColor(@FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_X) double x,
            @FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_Y) double y,
            @FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_Z) double z); @ColorInt static int LABToColor(@FloatRange(from = 0f, to = 100) final double l,
            @FloatRange(from = -128, to = 127) final double a,
            @FloatRange(from = -128, to = 127) final double b); static double distanceEuclidean(@NonNull double[] labX, @NonNull double[] labY); @ColorInt static int blendARGB(@ColorInt int color1, @ColorInt int color2,
            @FloatRange(from = 0.0, to = 1.0) float ratio); static void blendHSL(@NonNull float[] hsl1, @NonNull float[] hsl2,
            @FloatRange(from = 0.0, to = 1.0) float ratio, @NonNull float[] outResult); static void blendLAB(@NonNull double[] lab1, @NonNull double[] lab2,
            @FloatRange(from = 0.0, to = 1.0) double ratio, @NonNull double[] outResult);  }