Google se compromete a impulsar la igualdad racial para las comunidades afrodescendientes. Obtén información al respecto.

ColorUtils

class ColorUtils
kotlin.Any
   ↳ androidx.core.graphics.ColorUtils

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

Summary

Public methods
static Int
HSLToColor(@NonNull hsl: FloatArray)

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

static Int
LABToColor(@FloatRange(0.0, 100) l: Double, @FloatRange(-128, 127) a: Double, @FloatRange(-128, 127) b: Double)

Converts a color from CIE Lab to its RGB representation.

static Unit
LABToXYZ(@FloatRange(0.0, 100) l: Double, @FloatRange(-128, 127) a: Double, @FloatRange(-128, 127) b: Double, @NonNull outXyz: DoubleArray)

Converts a color from CIE Lab to CIE XYZ representation.

static Unit
RGBToHSL(@IntRange(0, 255) r: Int, @IntRange(0, 255) g: Int, @IntRange(0, 255) b: Int, @NonNull outHsl: FloatArray)

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

static Unit
RGBToLAB(@IntRange(0, 255) r: Int, @IntRange(0, 255) g: Int, @IntRange(0, 255) b: Int, @NonNull outLab: DoubleArray)

Convert RGB components to its CIE Lab representative components.

static Unit
RGBToXYZ(@IntRange(0, 255) r: Int, @IntRange(0, 255) g: Int, @IntRange(0, 255) b: Int, @NonNull outXyz: DoubleArray)

Convert RGB components to its CIE XYZ representative components.

static Int
XYZToColor(@FloatRange(0.0, 95.047) x: Double, @FloatRange(0.0, 100.0) y: Double, @FloatRange(0.0, 108.883) z: Double)

Converts a color from CIE XYZ to its RGB representation.

static Unit
XYZToLAB(@FloatRange(0.0, 95.047) x: Double, @FloatRange(0.0, 100.0) y: Double, @FloatRange(0.0, 108.883) z: Double, @NonNull outLab: DoubleArray)

Converts a color from CIE XYZ to CIE Lab representation.

static Int
blendARGB(@ColorInt color1: Int, @ColorInt color2: Int, @FloatRange(0.0, 1.0) ratio: Float)

Blend between two ARGB colors using the given ratio.

static Unit
blendHSL(@NonNull hsl1: FloatArray, @NonNull hsl2: FloatArray, @FloatRange(0.0, 1.0) ratio: Float, @NonNull outResult: FloatArray)

Blend between hsl1 and hsl2 using the given ratio.

static Unit
blendLAB(@NonNull lab1: DoubleArray, @NonNull lab2: DoubleArray, @FloatRange(0.0, 1.0) ratio: Double, @NonNull outResult: DoubleArray)

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

static Double
calculateContrast(@ColorInt foreground: Int, @ColorInt background: Int)

Returns the contrast ratio between foreground and background.

static Double
calculateLuminance(@ColorInt color: Int)

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

static Int
calculateMinimumAlpha(@ColorInt foreground: Int, @ColorInt background: Int, minContrastRatio: Float)

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 Unit
colorToHSL(@ColorInt color: Int, @NonNull outHsl: FloatArray)

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

static Unit
colorToLAB(@ColorInt color: Int, @NonNull outLab: DoubleArray)

Convert the ARGB color to its CIE Lab representative components.

static Unit
colorToXYZ(@ColorInt color: Int, @NonNull outXyz: DoubleArray)

Convert the ARGB color to its CIE XYZ representative components.

static Int
compositeColors(@ColorInt foreground: Int, @ColorInt background: Int)

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

static Color
compositeColors(@NonNull foreground: Color, @NonNull background: Color)

Composites two translucent colors together.

static Double
distanceEuclidean(@NonNull labX: DoubleArray, @NonNull labY: DoubleArray)

Returns the euclidean distance between two LAB colors.

static Int
setAlphaComponent(@ColorInt color: Int, @IntRange(0, 255) alpha: Int)

Set the alpha component of color to be alpha.

Public methods

HSLToColor

@ColorInt static fun HSLToColor(@NonNull hsl: FloatArray): Int

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 FloatArray: 3-element array which holds the input HSL components
Return
Int the resulting RGB color

LABToColor

@ColorInt static fun LABToColor(
    @FloatRange(0.0, 100) l: Double,
    @FloatRange(-128, 127) a: Double,
    @FloatRange(-128, 127) b: Double
): Int

Converts a color from CIE Lab to its RGB representation.

Parameters
l Double: L component value [0...100]
a Double: A component value [-128...127]
b Double: B component value [-128...127]
Return
Int int containing the RGB representation

LABToXYZ

static fun LABToXYZ(
    @FloatRange(0.0, 100) l: Double,
    @FloatRange(-128, 127) a: Double,
    @FloatRange(-128, 127) b: Double,
    @NonNull outXyz: DoubleArray
): Unit

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)
a Double: A component value [-128...127)
b Double: B component value [-128...127)
outXyz DoubleArray: 3-element array which holds the re