Colorpublic class Color extends Object 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 |
Fields Summary |
---|
public static final int | BLACK | public static final int | DKGRAY | public static final int | GRAY | public static final int | LTGRAY | public static final int | WHITE | public static final int | RED | public static final int | GREEN | public static final int | BLUE | public static final int | YELLOW | public static final int | CYAN | public static final int | MAGENTA | public static final int | TRANSPARENT | private static final HashMap | sColorNameMap |
Methods Summary |
---|
public static int | HSBtoColor(float[] hsb)Convert HSB components to an ARGB color. Alpha set to 0xFF.
hsv[0] is Hue [0 .. 1)
hsv[1] is Saturation [0...1]
hsv[2] is Value [0...1]
If hsv values are out of range, they are pinned.
return HSBtoColor(hsb[0], hsb[1], hsb[2]);
| public static int | HSBtoColor(float h, float s, float b)Convert HSB components to an ARGB color. Alpha set to 0xFF.
hsv[0] is Hue [0 .. 1)
hsv[1] is Saturation [0...1]
hsv[2] is Value [0...1]
If hsv values are out of range, they are pinned.
h = MathUtils.constrain(h, 0.0f, 1.0f);
s = MathUtils.constrain(s, 0.0f, 1.0f);
b = MathUtils.constrain(b, 0.0f, 1.0f);
float red = 0.0f;
float green = 0.0f;
float blue = 0.0f;
final float hf = (h - (int) h) * 6.0f;
final int ihf = (int) hf;
final float f = hf - ihf;
final float pv = b * (1.0f - s);
final float qv = b * (1.0f - s * f);
final float tv = b * (1.0f - s * (1.0f - f));
switch (ihf) {
case 0: // Red is the dominant color
red = b;
green = tv;
blue = pv;
break;
case 1: // Green is the dominant color
red = qv;
green = b;
blue = pv;
break;
case 2:
red = pv;
green = b;
blue = tv;
break;
case 3: // Blue is the dominant color
red = pv;
green = qv;
blue = b;
break;
case 4:
red = tv;
green = pv;
blue = b;
break;
case 5: // Red is the dominant color
red = b;
green = pv;
blue = qv;
break;
}
return 0xFF000000 | (((int) (red * 255.0f)) << 16) |
(((int) (green * 255.0f)) << 8) | ((int) (blue * 255.0f));
| public static 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.
return HSVToColor(0xFF, hsv);
| public static 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.
if (hsv.length < 3) {
throw new RuntimeException("3 components required for hsv");
}
return nativeHSVToColor(alpha, hsv);
| public static 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]
if (hsv.length < 3) {
throw new RuntimeException("3 components required for hsv");
}
nativeRGBToHSV(red, green, blue, hsv);
| public static int | alpha(int color)Return the alpha component of a color int. This is the same as saying
color >>> 24
return color >>> 24;
| public static 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.
return (alpha << 24) | (red << 16) | (green << 8) | blue;
| public static int | blue(int color)Return the blue component of a color int. This is the same as saying
color & 0xFF
return color & 0xFF;
| public static float | brightness(int color)Returns the brightness component of a color int.
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
int V = Math.max(b, Math.max(r, g));
return (V / 255.f);
| public static 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]
RGBToHSV((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, hsv);
| public static int | getHtmlColor(java.lang.String color)Converts an HTML color (named or numeric) to an integer RGB value.
Integer i = sColorNameMap.get(color.toLowerCase(Locale.ROOT));
if (i != null) {
return i;
} else {
try {
return XmlUtils.convertValueToInt(color, -1);
} catch (NumberFormatException nfe) {
return -1;
}
}
| public static int | green(int color)Return the green component of a color int. This is the same as saying
(color >> 8) & 0xFF
return (color >> 8) & 0xFF;
| public static float | hue(int color)Returns the hue component of a color int.
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
int V = Math.max(b, Math.max(r, g));
int temp = Math.min(b, Math.min(r, g));
float H;
if (V == temp) {
H = 0;
} else {
final float vtemp = (float) (V - temp);
final float cr = (V - r) / vtemp;
final float cg = (V - g) / vtemp;
final float cb = (V - b) / vtemp;
if (r == V) {
H = cb - cg;
} else if (g == V) {
H = 2 + cr - cb;
} else {
H = 4 + cg - cr;
}
H /= 6.f;
if (H < 0) {
H++;
}
}
return H;
| private static native int | nativeHSVToColor(int alpha, float[] hsv)
| private static native void | nativeRGBToHSV(int red, int greed, int blue, float[] hsv)
| public static int | parseColor(java.lang.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
'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta',
'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey',
'aqua', 'fuschia', 'lime', 'maroon', 'navy', 'olive', 'purple',
'silver', 'teal'
if (colorString.charAt(0) == '#") {
// Use a long to avoid rollovers on #ffXXXXXX
long color = Long.parseLong(colorString.substring(1), 16);
if (colorString.length() == 7) {
// Set the alpha value
color |= 0x00000000ff000000;
} else if (colorString.length() != 9) {
throw new IllegalArgumentException("Unknown color");
}
return (int)color;
} else {
Integer color = sColorNameMap.get(colorString.toLowerCase(Locale.ROOT));
if (color != null) {
return color;
}
}
throw new IllegalArgumentException("Unknown color");
| public static int | red(int color)Return the red component of a color int. This is the same as saying
(color >> 16) & 0xFF
return (color >> 16) & 0xFF;
| public static 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.
return (0xFF << 24) | (red << 16) | (green << 8) | blue;
| public static float | saturation(int color)Returns the saturation component of a color int.
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
int V = Math.max(b, Math.max(r, g));
int temp = Math.min(b, Math.min(r, g));
float S;
if (V == temp) {
S = 0;
} else {
S = (V - temp) / (float) V;
}
return S;
|
|