Android APIs
public class

ColorUtils

extends Object
java.lang.Object
   ↳ android.support.v4.graphics.ColorUtils

Class Overview

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 void RGBToHSL(int r, int g, int b, float[] hsl)
Convert RGB components to HSL (hue-saturation-lightness).
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.
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[] hsl)
Convert the ARGB color to its HSL (hue-saturation-lightness) components.
static int compositeColors(int foreground, int background)
Composite two potentially translucent colors over each other and returns the result.
static int setAlphaComponent(int color, int alpha)
Set the alpha component of color to be alpha.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

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 3 element array which holds the input HSL components.
Returns
  • the resulting RGB color

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

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

  • hsl[0] is Hue [0 .. 360)
  • hsl[1] is Saturation [0...1]
  • hsl[2] is Lightness [0...1]

Parameters
r red component value [0..255]
g green component value [0..255]
b blue component value [0..255]
hsl 3 element array which holds the resulting HSL components.

public static double calculateContrast (int foreground, int background)

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

Formula defined here.

public static double calculateLuminance (int color)

Returns the luminance of a color. Formula defined here: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef

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

Parameters
foreground the foreground color.
background the background color. Should be opaque.
minContrastRatio the minimum contrast ratio.
Returns
  • the alpha value in the range 0-255, or -1 if no value could be calculated.

public static void colorToHSL (int color, float[] hsl)

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

  • hsl[0] is Hue [0 .. 360)
  • hsl[1] is Saturation [0...1]
  • hsl[2] is Lightness [0...1]

Parameters
color the ARGB color to convert. The alpha component is ignored.
hsl 3 element array which holds the resulting HSL components.

public static int compositeColors (int foreground, int background)

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

public static int setAlphaComponent (int color, int alpha)

Set the alpha component of color to be alpha.