ColorUtils
public final class ColorUtils
A set of color-related utility methods, building upon those available in Color.
Summary
Public methods |
|
|---|---|
static @ColorInt int |
HSLToColor(@NonNull float[] hsl)Convert HSL (hue-saturation-lightness) components to a RGB color. |
static @ColorInt int |
LABToColor(Converts a color from CIE Lab to its RGB representation. |
static void |
LABToXYZ(Converts a color from CIE Lab to CIE XYZ representation. |
static @ColorInt int |
M3HCTToColor(Generate an ARGB color using M3HCT color parameters. |
static void |
RGBToHSL(Convert RGB components to HSL (hue-saturation-lightness). |
static void |
RGBToLAB(Convert RGB components to its CIE Lab representative components. |
static void |
RGBToXYZ(Convert RGB components to its CIE XYZ representative components. |
static @ColorInt int |
XYZToColor(Converts a color from CIE XYZ to its RGB representation. |
static void |
XYZToLAB(Converts a color from CIE XYZ to CIE Lab representation. |
static @ColorInt int |
blendARGB(Blend between two ARGB colors using the given ratio. |
static void |
blendHSL(Blend between |
static void |
blendLAB(Blend between two CIE-LAB colors using the given ratio. |
static double |
calculateContrast(@ColorInt int foreground, @ColorInt int background)Returns the contrast ratio between |
static @FloatRange(from = 0.0, to = 1.0) double |
calculateLuminance(@ColorInt int color)Returns the luminance of a color as a float between |
static int |
calculateMinimumAlpha(Calculates the minimum alpha value which can be applied to |
static void |
colorToHSL(@ColorInt int color, @NonNull float[] outHsl)Convert the ARGB color to its HSL (hue-saturation-lightness) components. |
static void |
colorToLAB(@ColorInt int color, @NonNull double[] outLab)Convert the ARGB color to its CIE Lab representative components. |
static void |
colorToM3HCT(@ColorInt int color, @Size(value = 3) @NonNull float[] outM3HCT)Generate a M3HCT color from an ARGB color. |
static void |
colorToXYZ(@ColorInt int color, @NonNull double[] outXyz)Convert the ARGB color to its CIE XYZ representative components. |
static @NonNull Color |
@RequiresApi(value = 26)Composites two translucent colors together. |
static int |
compositeColors(@ColorInt int foreground, @ColorInt int background)Composite two potentially translucent colors over each other and returns the result. |
static double |
distanceEuclidean(@NonNull double[] labX, @NonNull double[] labY)Returns the euclidean distance between two LAB colors. |
static @ColorInt int |
setAlphaComponent(@ColorInt int color, @IntRange(from = 0, to = 255) int alpha)Set the alpha component of |
Public methods
HSLToColor
public static @ColorInt int HSLToColor(@NonNull float[] hsl)
Convert HSL (hue-saturation-lightness) components to a RGB color.
- hsl[0] is Hue [0, 360)
- hsl[1] is Saturation [0, 1]
- hsl[2] is Lightness [0, 1]
| Parameters | |
|---|---|
@NonNull float[] hsl |
3-element array which holds the input HSL components |
| Returns | |
|---|---|
@ColorInt int |
the resulting RGB color |
LABToColor
public static @ColorInt int LABToColor(
@FloatRange(from = 0.0, to = 100) double l,
@FloatRange(from = "-128", to = 127) double a,
@FloatRange(from = "-128", to = 127) double b
)
Converts a color from CIE Lab to its RGB representation.
| Parameters | |
|---|---|
@FloatRange(from = 0.0, to = 100) double l |
L component value [0, 100] |
@FloatRange(from = "-128", to = 127) double a |
A component value [-128, 127] |
@FloatRange(from = "-128", to = 127) double b |
B component value [-128, 127] |
| Returns | |
|---|---|
@ColorInt int |
int containing the RGB representation |
LABToXYZ
public static void LABToXYZ(
@FloatRange(from = 0.0, to = 100) double l,
@FloatRange(from = "-128", to = 127) double a,
@FloatRange(from = "-128", to = 127) double b,
@NonNull double[] outXyz
)
Converts a color from CIE Lab to CIE XYZ representation.
The resulting XYZ representation will use the D65 illuminant and the CIE 2° Standard Observer (1931).
- outXyz[0] is X [0, 95.047)
- outXyz[1] is Y [0, 100)
- outXyz[2] is Z [0, 108.883)
| Parameters | |
|---|---|
@FloatRange(from = 0.0, to = 100) double l |
L component value [0, 100] |
@FloatRange(from = "-128", to = 127) double a |
A component value [-128, 127) |
@FloatRange(from = "-128", to = 127) double b |
B component value [-128, 127) |
@NonNull double[] outXyz |
3-element array which holds the resulting XYZ components |
M3HCTToColor
public static @ColorInt int M3HCTToColor(
@FloatRange(from = 0.0, to = 360, toInclusive = false) float hue,
@FloatRange(from = 0.0, to = Double.POSITIVE_INFINITY, toInclusive = false) float chroma,
@FloatRange(from = 0.0, to = 100) float tone
)
Generate an ARGB color using M3HCT color parameters. HCT color space is a new color space proposed in Material Design 3
| Parameters | |
|---|---|
@FloatRange(from = 0.0, to = 360, toInclusive = false) float hue |
is Hue in M3HCT [0, 360); invalid values are corrected. |
@FloatRange(from = 0.0, to = Double.POSITIVE_INFINITY, toInclusive = false) float chroma |
is Chroma in M3HCT [0, ?); Chroma may decrease because chroma has a different maximum for any given hue and tone. |
@FloatRange(from = 0.0, to = 100) float tone |
is Tone in M3HCT [0, 100]; invalid values are corrected. |
| See also | |
|---|---|
About Color Spaces |
RGBToHSL
public static void RGBToHSL(
@IntRange(from = 0, to = 255) int r,
@IntRange(from = 0, to = 255) int g,
@IntRange(from = 0, to = 255) int b,
@NonNull float[] outHsl
)
Convert RGB components to HSL (hue-saturation-lightness).
- outHsl[0] is Hue [0, 360)
- outHsl[1] is Saturation [0, 1]
- outHsl[2] is Lightness [0, 1]
RGBToLAB
public static void RGBToLAB(
@IntRange(from = 0, to = 255) int r,
@IntRange(from = 0, to = 255) int g,
@IntRange(from = 0, to = 255) int b,
@NonNull double[] outLab
)
Convert RGB components to its CIE Lab representative components.
- outLab[0] is L [0, 100]
- outLab[1] is a [-128, 127)
- outLab[2] is b [-128, 127)
RGBToXYZ
public static void RGBToXYZ(
@IntRange(from = 0, to = 255) int r,
@IntRange(from = 0, to = 255) int g,
@IntRange(from = 0, to = 255) int b,
@NonNull double[] outXyz
)
Convert RGB components to its CIE XYZ representative components.
The resulting XYZ representation will use the D65 illuminant and the CIE 2° Standard Observer (1931).
- outXyz[0] is X [0, 95.047)
- outXyz[1] is Y [0, 100)
- outXyz[2] is Z [0, 108.883)
XYZToColor
public static @ColorInt int XYZToColor(
@FloatRange(from = 0.0, to = 95.047) double x,
@FloatRange(from = 0.0, to = 100.0) double y,
@FloatRange(from = 0.0, to = 108.883) double z
)
Converts a color from CIE XYZ to its RGB representation.
This method expects the XYZ representation to use the D65 illuminant and the CIE 2° Standard Observer (1931).
| Parameters | |
|---|---|
@FloatRange(from = 0.0, to = 95.047) double x |
X component value [0, 95.047) |
@FloatRange(from = 0.0, to = 100.0) double y |
Y component value [0, 100) |
@FloatRange(from = 0.0, to = 108.883) double z |
Z component value [0, 108.883) |
| Returns | |
|---|---|
@ColorInt int |
int containing the RGB representation |
XYZToLAB
public static void XYZToLAB(
@FloatRange(from = 0.0, to = 95.047) double x,
@FloatRange(from = 0.0, to = 100.0) double y,
@FloatRange(from = 0.0, to = 108.883) double z,
@NonNull double[] outLab
)
Converts a color from CIE XYZ to CIE Lab representation.
This method expects the XYZ representation to use the D65 illuminant and the CIE 2° Standard Observer (1931).
- outLab[0] is L [0, 100]
- outLab[1] is a [-128, 127)
- outLab[2] is b [-128, 127)
| Parameters | |
|---|---|
@FloatRange(from = 0.0, to = 95.047) double x |
X component value [0, 95.047) |
@FloatRange(from = 0.0, to = 100.0) double y |
Y component value [0, 100) |
@FloatRange(from = 0.0, to = 108.883) double z |
Z component value [0, 108.883) |
@NonNull double[] outLab |
3-element array which holds the resulting Lab components |
blendARGB
public static @ColorInt int blendARGB(
@ColorInt int color1,
@ColorInt int color2,
@FloatRange(from = 0.0, to = 1.0) float ratio
)
Blend between two ARGB colors using the given ratio.
A blend ratio of 0.0 will result in color1, 0.5 will give an even blend, 1.0 will result in color2.
| Parameters | |
|---|---|
@ColorInt int color1 |
the first ARGB color |
@ColorInt int color2 |
the second ARGB color |
@FloatRange(from = 0.0, to = 1.0) float ratio |
the blend ratio of |
blendHSL
public static void blendHSL(
@NonNull float[] hsl1,
@NonNull float[] hsl2,
@FloatRange(from = 0.0, to = 1.0) float ratio,
@NonNull float[] outResult
)
Blend between hsl1 and hsl2 using the given ratio. This will interpolate the hue using the shortest angle.
A blend ratio of 0.0 will result in hsl1, 0.5 will give an even blend, 1.0 will result in hsl2.
| Parameters | |
|---|---|
@NonNull float[] hsl1 |
3-element array which holds the first HSL color |
@NonNull float[] hsl2 |
3-element array which holds the second HSL color |
@FloatRange(from = 0.0, to = 1.0) float ratio |
the blend ratio of |
@NonNull float[] outResult |
3-element array which holds the resulting HSL components |
blendLAB
public static void blendLAB(
@NonNull double[] lab1,
@NonNull double[] lab2,
@FloatRange(from = 0.0, to = 1.0) double ratio,
@NonNull double[] outResult
)
Blend between two CIE-LAB colors using the given ratio.
A blend ratio of 0.0 will result in lab1, 0.5 will give an even blend, 1.0 will result in lab2.
| Parameters | |
|---|---|
@NonNull double[] lab1 |
3-element array which holds the first LAB color |
@NonNull double[] lab2 |
3-element array which holds the second LAB color |
@FloatRange(from = 0.0, to = 1.0) double ratio |
the blend ratio of |
@NonNull double[] outResult |
3-element array which holds the resulting LAB components |
calculateContrast
public static double calculateContrast(@ColorInt int foreground, @ColorInt int background)
Returns the contrast ratio between foreground and background. background must be opaque.
Formula defined here.
calculateLuminance
public static @FloatRange(from = 0.0, to = 1.0) double calculateLuminance(@ColorInt int color)
Returns the luminance of a color as a float between 0.0 and 1.0.
Defined as the Y component in the XYZ representation of color.
calculateMinimumAlpha
public static int calculateMinimumAlpha(
@ColorInt int foreground,
@ColorInt int background,
float minContrastRatio
)
Calculates the minimum alpha value which can be applied to foreground so that would have a contrast value of at least minContrastRatio when compared to background.
| Parameters | |
|---|---|
@ColorInt int foreground |
the foreground color |
@ColorInt int background |
the opaque background color |
float minContrastRatio |
the minimum contrast ratio |
| Returns | |
|---|---|
int |
the alpha value in the range [0, 255] or -1 if no value could be calculated |
colorToHSL
public static void colorToHSL(@ColorInt int color, @NonNull float[] outHsl)
Convert the ARGB color to its HSL (hue-saturation-lightness) components.
- outHsl[0] is Hue [0, 360)
- outHsl[1] is Saturation [0, 1]
- outHsl[2] is Lightness [0, 1]
colorToLAB
public static void colorToLAB(@ColorInt int color, @NonNull double[] outLab)
Convert the ARGB color to its CIE Lab representative components.
colorToM3HCT
public static void colorToM3HCT(@ColorInt int color, @Size(value = 3) @NonNull float[] outM3HCT)
Generate a M3HCT color from an ARGB color. HCT color space is a new color space proposed in Material Design 3
| Parameters | |
|---|---|
@ColorInt int color |
is the ARGB color value we use to get its respective M3HCT values. |
@Size(value = 3) @NonNull float[] outM3HCT |
3-element array which holds the resulting M3HCT components (Hue, Chroma, Tone). |
| See also | |
|---|---|
About Color Spaces |
|
colorToXYZ
public static void colorToXYZ(@ColorInt int color, @NonNull double[] outXyz)
Convert the ARGB color to its CIE XYZ representative components.
The resulting XYZ representation will use the D65 illuminant and the CIE 2° Standard Observer (1931).
- outXyz[0] is X [0, 95.047)
- outXyz[1] is Y [0, 100)
- outXyz[2] is Z [0, 108.883)
compositeColors
@RequiresApi(value = 26)
public static @NonNull Color compositeColors(@NonNull Color foreground, @NonNull Color background)
Composites two translucent colors together. More specifically, adds two colors using the source over blending mode. The colors must not be pre-multiplied and the result is a non pre-multiplied color.
If the two colors have different color spaces, the foreground color is converted to the color space of the background color.
The following example creates a purple color by blending opaque blue with semi-translucent red:
Color purple = ColorUtils.compositeColors(
Color.valueOf(1f, 0f, 0f, 0.5f),
Color.valueOf(0f, 0f, 1f));| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if the |
compositeColors
public static int compositeColors(@ColorInt int foreground, @ColorInt int background)
Composite two potentially translucent colors over each other and returns the result.
distanceEuclidean
public static double distanceEuclidean(@NonNull double[] labX, @NonNull double[] labY)
Returns the euclidean distance between two LAB colors.
setAlphaComponent
public static @ColorInt int setAlphaComponent(@ColorInt int color, @IntRange(from = 0, to = 255) int alpha)
Set the alpha component of color to be alpha.