ColorUtils

public final class ColorUtils
extends Object

java.lang.Object
   ↳ androidx.core.graphics.ColorUtils


A set of color-related utility methods, building upon those available in Color.

Summary

Public methods

static int HSLToColor(float[] hsl)

Convert HSL (hue-saturation-lightness) components to a RGB color.

static int LABToColor(double l, double a, double b)

Converts a color from CIE Lab to its RGB representation.

static void LABToXYZ(double l, double a, double b, double[] outXyz)

Converts a color from CIE Lab to CIE XYZ representation.

static void RGBToHSL(int r, int g, int b, float[] outHsl)

Convert RGB components to HSL (hue-saturation-lightness).

static void RGBToLAB(int r, int g, int b, double[] outLab)

Convert RGB components to its CIE Lab representative components.

static void RGBToXYZ(int r, int g, int b, double[] outXyz)

Convert RGB components to its CIE XYZ representative components.

static int XYZToColor(double x, double y, double z)

Converts a color from CIE XYZ to its RGB representation.

static void XYZToLAB(double x, double y, double z, double[] outLab)

Converts a color from CIE XYZ to CIE Lab representation.

static int blendARGB(int color1, int color2, float ratio)

Blend between two ARGB colors using the given ratio.

static void blendHSL(float[] hsl1, float[] hsl2, float ratio, float[] outResult)

Blend between hsl1 and hsl2 using the given ratio.

static void blendLAB(double[] lab1, double[] lab2, double ratio, double[] outResult)

Blend between two CIE-LAB colors using the given ratio.

static double calculateContrast(int foreground, int background)

Returns the contrast ratio between foreground and background.

static double calculateLuminance(int color)

Returns the luminance of a color as a float between 0.0 and 1.0.

static int calculateMinimumAlpha(int foreground, 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.

static void colorToHSL(int color, float[] outHsl)

Convert the ARGB color to its HSL (hue-saturation-lightness) components.

static void colorToLAB(int color, double[] outLab)

Convert the ARGB color to its CIE Lab representative components.

static void colorToXYZ(int color, double[] outXyz)

Convert the ARGB color to its CIE XYZ representative components.

static Color compositeColors(Color foreground, Color background)

Composites two translucent colors together.

static int compositeColors(int foreground, int background)

Composite two potentially translucent colors over each other and returns the result.

static double distanceEuclidean(double[] labX, double[] labY)

Returns the euclidean distance between two LAB colors.

static int setAlphaComponent(int color, int alpha)

Set the alpha component of color to be alpha.

Inherited methods

Public methods

HSLToColor

public static int HSLToColor (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]
If hsv values are out of range, they are pinned.

Parameters
hsl float: 3-element array which holds the input HSL components

Returns
int the resulting RGB color

LABToColor

public static int LABToColor (double l, 
                double a, 
                double b)

Converts a color from CIE Lab to its RGB representation.

Parameters
l double: L component value [0...100]

Value is between 0.0 and 100.0 inclusive.

a double: A component value [-128...127]

Value is between -128.0 and 127.0 inclusive.

b double: B component value [-128...127]

Value is between -128.0 and 127.0 inclusive.

Returns
int int containing the RGB representation

LABToXYZ

public static void LABToXYZ (double l, 
                double a, 
                double b, 
                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
l double: L component value [0...100)

Value is between 0.0 and 100.0 inclusive.

a double: A component value [-128...127)

Value is between -128.0 and 127.0 inclusive.

b double: B component value [-128...127)

Value is between -128.0 and 127.0 inclusive.

outXyz double: 3-element array which holds the resulting XYZ components

RGBToHSL

public static void RGBToHSL (int r, 
                int g, 
                int b, 
                float[] outHsl)