Most visited

Recently visited

Added in API level 1

Color

public class Color
extends Object

java.lang.Object
   ↳ android.graphics.Color


The Color class defines methods for creating and converting color ints. Colors are represented as packed ints, made up of 4 bytes: alpha, red, green, blue. The values are unpremultiplied, meaning any transparency is stored solely in the alpha component, and not in the color components. The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue. Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution. Thus opaque-black would be 0xFF000000 (100% opaque but no contributions from red, green, or blue), and opaque-white would be 0xFFFFFFFF

Summary

Constants

int BLACK

int BLUE

int CYAN

int DKGRAY

int GRAY

int GREEN

int LTGRAY

int MAGENTA

int RED

int TRANSPARENT

int WHITE

int YELLOW

Public constructors

Color()

Public methods

static int HSVToColor(float[] hsv)

Convert HSV components to an ARGB color.

static int HSVToColor(int alpha, float[] hsv)

Convert HSV components to an ARGB color.

static void RGBToHSV(int red, int green, int blue, float[] hsv)

Convert RGB components to HSV.

static int alpha(int color)

Return the alpha component of a color int.

static int argb(int alpha, int red, int green, int blue)

Return a color-int from alpha, red, green, blue components.

static int blue(int color)

Return the blue component of a color int.

static void colorToHSV(int color, float[] hsv)

Convert the argb color to its HSV components.

static int green(int color)

Return the green component of a color int.

static float luminance(int color)

Returns the relative luminance of a color.

static int parseColor(String colorString)

Parse the color string, and return the corresponding color-int.

static int red(int color)

Return the red component of a color int.

static int rgb(int red, int green, int blue)

Return a color-int from red, green, blue components.

Inherited methods

From class java.lang.Object

Constants

BLACK

Added in API level 1
int BLACK

Constant Value: -16777216 (0xff000000)

BLUE

Added in API level 1
int BLUE

Constant Value: -16776961 (0xff0000ff)

CYAN

Added in API level 1
int CYAN

Constant Value: -16711681 (0xff00ffff)

DKGRAY

Added in API level 1
int DKGRAY

Constant Value: -12303292 (0xff444444)

GRAY

Added in API level 1
int GRAY

Constant Value: -7829368 (0xff888888)

GREEN

Added in API level 1
int GREEN

Constant Value: -16711936 (0xff00ff00)

LTGRAY

Added in API level 1
int LTGRAY

Constant Value: -3355444 (0xffcccccc)

MAGENTA

Added in API level 1
int MAGENTA

Constant Value: -65281 (0xffff00ff)

RED

Added in API level 1
int RED

Constant Value: -65536 (0xffff0000)

TRANSPARENT

Added in API level 1
int TRANSPARENT

Constant Value: 0 (0x00000000)

WHITE

Added in API level 1
int WHITE

Constant Value: -1 (0xffffffff)

YELLOW

Added in API level 1
int YELLOW

Constant Value: -256 (0xffffff00)

Public constructors

Color

Added in API level 1
Color ()

Public methods

HSVToColor

Added in API level 1
int HSVToColor (float[] hsv)

Convert HSV components to an ARGB color. Alpha set to 0xFF. hsv[0] is Hue [0 .. 360) hsv[1] is Saturation [0...1] hsv[2] is Value [0...1] If hsv values are out of range, they are pinned.

Parameters
hsv float: 3 element array which holds the input HSV components.
Returns
int the resulting argb color

HSVToColor

Added in API level 1
int HSVToColor (int alpha, 
                float[] hsv)

Convert HSV components to an ARGB color. The alpha component is passed through unchanged. hsv[0] is Hue [0 .. 360) hsv[1] is Saturation [0...1] hsv[2] is Value [0...1] If hsv values are out of range, they are pinned.

Parameters
alpha int: the alpha component of the returned argb color.
hsv float: 3 element array which holds the input HSV components.
Returns
int the resulting argb color

RGBToHSV

Added in API level 1
void RGBToHSV (int red, 
                int green, 
                int blue, 
                float[] hsv)

Convert RGB components to HSV. hsv[0] is Hue [0 .. 360) hsv[1] is Saturation [0...1] hsv[2] is Value [0...1]

Parameters
red int: red component value [0..255]
green int: green component value [0..255]
blue int: blue component value [0..255]
hsv float: 3 element array which holds the resulting HSV components.

alpha

Added in API level 1
int alpha (int color)

Return the alpha component of a color int. This is the same as saying color >>> 24

Parameters
color int
Returns
int

argb

Added in API level 1
int argb (int alpha, 
                int red, 
                int green, 
                int blue)

Return a color-int from alpha, red, green, blue components. These component values should be [0..255], but there is no range check performed, so if they are out of range, the returned color is undefined.

Parameters
alpha int: Alpha component [0..255] of the color
red int: Red component [0..255] of the color
green int: Green component [0..255] of the color
blue int: Blue component [0..255] of the color
Returns
int

blue

Added in API level 1
int blue (int color)

Return the blue component of a color int. This is the same as saying color & 0xFF

Parameters
color int
Returns
int

colorToHSV

Added in API level 1
void colorToHSV (int color, 
                float[] hsv)

Convert the argb color to its HSV components. hsv[0] is Hue [0 .. 360) hsv[1] is Saturation [0...1] hsv[2] is Value [0...1]

Parameters
color int: the argb color to convert. The alpha component is ignored.
hsv float: 3 element array which holds the resulting HSV components.

green

Added in API level 1
int green (int color)

Return the green component of a color int. This is the same as saying (color >> 8) & 0xFF

Parameters
color int
Returns
int

luminance

Added in API level 24
float luminance (int color)

Returns the relative luminance of a color.

Assumes sRGB encoding. Based on the formula for relative luminance defined in WCAG 2.0, W3C Recommendation 11 December 2008.

Parameters
color int
Returns
float a value between 0 (darkest black) and 1 (lightest white)

parseColor

Added in API level 1
int parseColor (String colorString)

Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. Supported formats are: #RRGGBB #AARRGGBB or one of the following names: 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey', 'aqua', 'fuchsia', 'lime', 'maroon', 'navy', 'olive', 'purple', 'silver', 'teal'.

Parameters
colorString String
Returns
int

red

Added in API level 1
int red (int color)

Return the red component of a color int. This is the same as saying (color >> 16) & 0xFF

Parameters
color int
Returns
int

rgb

Added in API level 1
int rgb (int red, 
                int green, 
                int blue)

Return a color-int from red, green, blue components. The alpha component is implicity 255 (fully opaque). These component values should be [0..255], but there is no range check performed, so if they are out of range, the returned color is undefined.

Parameters
red int: Red component [0..255] of the color
green int: Green component [0..255] of the color
blue int: Blue component [0..255] of the color
Returns
int

Hooray!