# ColorUtils

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  
belongs to Maven artifact com.android.support:support-compat:28.0.0-alpha1  
Summary:[Methods](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#)  

# ColorUtils

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public

final

class
ColorUtils
`  
`

extends Object


``


`

|---|----------------------------------------|
| java.lang.Object                          ||
| ↳ | android.support.v4.graphics.ColorUtils |

<br />

*** ** * ** ***

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

## Summary

|                                                                                                                                                                                         ### Public methods                                                                                                                                                                                          ||
|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` static int`    | ` `[HSLToColor](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#HSLToColor(float[]))`(float[] hsl) ` Convert HSL (hue-saturation-lightness) components to a RGB color.                                                                                                                                                                             |
| ` static int`    | ` `[LABToColor](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#LABToColor(double, double, double))`(double l, double a, double b) ` Converts a color from CIE Lab to its RGB representation.                                                                                                                                                      |
| ` static void`   | ` `[LABToXYZ](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#LABToXYZ(double, double, double, double[]))`(double l, double a, double b, double[] outXyz) ` Converts a color from CIE Lab to CIE XYZ representation.                                                                                                                               |
| ` static void`   | ` `[RGBToHSL](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#RGBToHSL(int, int, int, float[]))`(int r, int g, int b, float[] outHsl) ` Convert RGB components to HSL (hue-saturation-lightness).                                                                                                                                                  |
| ` static void`   | ` `[RGBToLAB](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#RGBToLAB(int, int, int, double[]))`(int r, int g, int b, double[] outLab) ` Convert RGB components to its CIE Lab representative components.                                                                                                                                         |
| ` static void`   | ` `[RGBToXYZ](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#RGBToXYZ(int, int, int, double[]))`(int r, int g, int b, double[] outXyz) ` Convert RGB components to its CIE XYZ representative components.                                                                                                                                         |
| ` static int`    | ` `[XYZToColor](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#XYZToColor(double, double, double))`(double x, double y, double z) ` Converts a color from CIE XYZ to its RGB representation.                                                                                                                                                      |
| ` static void`   | ` `[XYZToLAB](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#XYZToLAB(double, double, double, double[]))`(double x, double y, double z, double[] outLab) ` Converts a color from CIE XYZ to CIE Lab representation.                                                                                                                               |
| ` static int`    | ` `[blendARGB](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#blendARGB(int, int, float))`(int color1, int color2, float ratio) ` Blend between two ARGB colors using the given ratio.                                                                                                                                                            |
| ` static void`   | ` `[blendHSL](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#blendHSL(float[], float[], float, float[]))`(float[] hsl1, float[] hsl2, float ratio, float[] outResult) ` Blend between`hsl1`and`hsl2`using the given ratio.                                                                                                                        |
| ` static void`   | ` `[blendLAB](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#blendLAB(double[], double[], double, double[]))`(double[] lab1, double[] lab2, double ratio, double[] outResult) ` Blend between two CIE-LAB colors using the given ratio.                                                                                                           |
| ` static double` | ` `[calculateContrast](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#calculateContrast(int, int))`(int foreground, int background) ` Returns the contrast ratio between`foreground`and`background`.                                                                                                                                              |
| ` static double` | ` `[calculateLuminance](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#calculateLuminance(int))`(int color) ` Returns the luminance of a color as a float between`0.0`and`1.0`.                                                                                                                                                                   |
| ` static int`    | ` `[calculateMinimumAlpha](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#calculateMinimumAlpha(int, int, float))`(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](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#colorToHSL(int, float[]))`(int color, float[] outHsl) ` Convert the ARGB color to its HSL (hue-saturation-lightness) components.                                                                                                                                                   |
| ` static void`   | ` `[colorToLAB](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#colorToLAB(int, double[]))`(int color, double[] outLab) ` Convert the ARGB color to its CIE Lab representative components.                                                                                                                                                         |
| ` static void`   | ` `[colorToXYZ](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#colorToXYZ(int, double[]))`(int color, double[] outXyz) ` Convert the ARGB color to its CIE XYZ representative components.                                                                                                                                                         |
| ` static int`    | ` `[compositeColors](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#compositeColors(int, int))`(int foreground, int background) ` Composite two potentially translucent colors over each other and returns the result.                                                                                                                            |
| ` static double` | ` `[distanceEuclidean](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#distanceEuclidean(double[], double[]))`(double[] labX, double[] labY) ` Returns the euclidean distance between two LAB colors.                                                                                                                                              |
| ` static int`    | ` `[setAlphaComponent](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#setAlphaComponent(int, int))`(int color, int alpha) ` Set the alpha component of`color`to be`alpha`.                                                                                                                                                                        |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils#)From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||

## Public methods

### HSLToColor

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
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.

<br />

<br />

|                                 Parameters                                  ||
|-------|----------------------------------------------------------------------|
| `hsl` | `float`: 3-element array which holds the input HSL components <br /> |

|                Returns                ||
|-------|--------------------------------|
| `int` | the resulting RGB color <br /> |

### LABToColor

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Converts a color from CIE Lab to its RGB representation.

<br />

|                                          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 <br /> |

### LABToXYZ

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
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)

<br />

<br />

|                                            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 <br />              |

### RGBToHSL

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void RGBToHSL (int r, 
                int g, 
                int b, 
                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\]

<br />

<br />

|                                     Parameters                                     ||
|----------|--------------------------------------------------------------------------|
| `r`      | `int`: red component value \[0..255\] Value is 255 or less.              |
| `g`      | `int`: green component value \[0..255\] Value is 255 or less.            |
| `b`      | `int`: blue component value \[0..255\] Value is 255 or less.             |
| `outHsl` | `float`: 3-element array which holds the resulting HSL components <br /> |

### RGBToLAB

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Convert RGB components to its CIE Lab representative components.

- outLab\[0\] is L \[0 ...1)
- outLab\[1\] is a \[-128...127)
- outLab\[2\] is b \[-128...127)

<br />

<br />

|                                     Parameters                                      ||
|----------|---------------------------------------------------------------------------|
| `r`      | `int`: red component value \[0..255\] Value is 255 or less.               |
| `g`      | `int`: green component value \[0..255\] Value is 255 or less.             |
| `b`      | `int`: blue component value \[0..255\] Value is 255 or less.              |
| `outLab` | `double`: 3-element array which holds the resulting LAB components <br /> |

### RGBToXYZ

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void RGBToXYZ (int r, 
                int g, 
                int b, 
                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)

<br />

<br />

|                                     Parameters                                      ||
|----------|---------------------------------------------------------------------------|
| `r`      | `int`: red component value \[0..255\] Value is 255 or less.               |
| `g`      | `int`: green component value \[0..255\] Value is 255 or less.             |
| `b`      | `int`: blue component value \[0..255\] Value is 255 or less.              |
| `outXyz` | `double`: 3-element array which holds the resulting XYZ components <br /> |

### XYZToColor

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int XYZToColor (double x, 
                double y, 
                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).

<br />

<br />

|                                         Parameters                                          ||
|-----|----------------------------------------------------------------------------------------|
| `x` | `double`: X component value \[0...95.047) Value is between 0.0 and 95.047 inclusive.   |
| `y` | `double`: Y component value \[0...100) Value is between 0.0 and 100.0 inclusive.       |
| `z` | `double`: Z component value \[0...108.883) Value is between 0.0 and 108.883 inclusive. |

|                       Returns                       ||
|-------|----------------------------------------------|
| `int` | int containing the RGB representation <br /> |

### XYZToLAB

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void XYZToLAB (double x, 
                double y, 
                double z, 
                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 ...1)
- outLab\[1\] is a \[-128...127)
- outLab\[2\] is b \[-128...127)

<br />

<br />

|                                            Parameters                                            ||
|----------|----------------------------------------------------------------------------------------|
| `x`      | `double`: X component value \[0...95.047) Value is between 0.0 and 95.047 inclusive.   |
| `y`      | `double`: Y component value \[0...100) Value is between 0.0 and 100.0 inclusive.       |
| `z`      | `double`: Z component value \[0...108.883) Value is between 0.0 and 108.883 inclusive. |
| `outLab` | `double`: 3-element array which holds the resulting Lab components <br />              |

### blendARGB

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int blendARGB (int color1, 
                int color2, 
                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`.

<br />

<br />

|                                           Parameters                                            ||
|----------|---------------------------------------------------------------------------------------|
| `color1` | `int`: the first ARGB color <br />                                                    |
| `color2` | `int`: the second ARGB color <br />                                                   |
| `ratio`  | `float`: the blend ratio of`color1`to`color2` Value is between 0.0 and 1.0 inclusive. |

|    Returns    ||
|-------|--------|
| `int` | <br /> |

### blendHSL

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void blendHSL (float[] hsl1, 
                float[] hsl2, 
                float ratio, 
                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`.

<br />

<br />

|                                           Parameters                                           ||
|-------------|-----------------------------------------------------------------------------------|
| `hsl1`      | `float`: 3-element array which holds the first HSL color <br />                   |
| `hsl2`      | `float`: 3-element array which holds the second HSL color <br />                  |
| `ratio`     | `float`: the blend ratio of`hsl1`to`hsl2` Value is between 0.0 and 1.0 inclusive. |
| `outResult` | `float`: 3-element array which holds the resulting HSL components <br />          |

### blendLAB

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void blendLAB (double[] lab1, 
                double[] lab2, 
                double ratio, 
                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`.

<br />

<br />

|                                           Parameters                                            ||
|-------------|------------------------------------------------------------------------------------|
| `lab1`      | `double`: 3-element array which holds the first LAB color <br />                   |
| `lab2`      | `double`: 3-element array which holds the second LAB color <br />                  |
| `ratio`     | `double`: the blend ratio of`lab1`to`lab2` Value is between 0.0 and 1.0 inclusive. |
| `outResult` | `double`: 3-element array which holds the resulting LAB components <br />          |

### calculateContrast

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
double calculateContrast (int foreground, 
                int background)
```

Returns the contrast ratio between`foreground`and`background`.`background`must be opaque.

Formula defined[here](http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef).

<br />

|         Parameters         ||
|--------------|--------------|
| `foreground` | `int` <br /> |
| `background` | `int` <br /> |

|     Returns      ||
|----------|--------|
| `double` | <br /> |

### calculateLuminance

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
double calculateLuminance (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`.

<br />

<br />

|      Parameters       ||
|---------|--------------|
| `color` | `int` <br /> |

|                      Returns                      ||
|----------|-----------------------------------------|
| `double` | Value is between 0.0 and 1.0 inclusive. |

### calculateMinimumAlpha

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
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`.

<br />

|                           Parameters                           ||
|--------------------|--------------------------------------------|
| `foreground`       | `int`: the foreground color <br />         |
| `background`       | `int`: the opaque background color <br />  |
| `minContrastRatio` | `float`: the minimum contrast ratio <br /> |

|                                         Returns                                         ||
|-------|----------------------------------------------------------------------------------|
| `int` | the alpha value in the range 0-255, or -1 if no value could be calculated <br /> |

### colorToHSL

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void colorToHSL (int color, 
                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\]

<br />

<br />

|                                     Parameters                                     ||
|----------|--------------------------------------------------------------------------|
| `color`  | `int`: the ARGB color to convert. The alpha component is ignored <br />  |
| `outHsl` | `float`: 3-element array which holds the resulting HSL components <br /> |

### colorToLAB

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Convert the ARGB color to its CIE Lab representative components.

<br />

|                                     Parameters                                      ||
|----------|---------------------------------------------------------------------------|
| `color`  | `int`: the ARGB color to convert. The alpha component is ignored <br />   |
| `outLab` | `double`: 3-element array which holds the resulting LAB components <br /> |

### colorToXYZ

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void colorToXYZ (int color, 
                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)

<br />

<br />

|                                     Parameters                                      ||
|----------|---------------------------------------------------------------------------|
| `color`  | `int`: the ARGB color to convert. The alpha component is ignored <br />   |
| `outXyz` | `double`: 3-element array which holds the resulting LAB components <br /> |

### compositeColors

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int compositeColors (int foreground, 
                int background)
```

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

<br />

|         Parameters         ||
|--------------|--------------|
| `foreground` | `int` <br /> |
| `background` | `int` <br /> |

|    Returns    ||
|-------|--------|
| `int` | <br /> |

### distanceEuclidean

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Returns the euclidean distance between two LAB colors.

<br />

|       Parameters        ||
|--------|-----------------|
| `labX` | `double` <br /> |
| `labY` | `double` <br /> |

|     Returns      ||
|----------|--------|
| `double` | <br /> |

### setAlphaComponent

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int setAlphaComponent (int color, 
                int alpha)
```

Set the alpha component of`color`to be`alpha`.

<br />

|              Parameters              ||
|---------|-----------------------------|
| `color` | `int` <br />                |
| `alpha` | `int` Value is 255 or less. |

|    Returns    ||
|-------|--------|
| `int` | <br /> |

-

  ## Classes

  - [BitmapCompat](https://developer.android.com/reference/android/support/v4/graphics/BitmapCompat)
  - [ColorUtils](https://developer.android.com/reference/android/support/v4/graphics/ColorUtils)
  - [PaintCompat](https://developer.android.com/reference/android/support/v4/graphics/PaintCompat)