blob: 3fc391c42b649d35ff76583d2797fb600e499867 [file] [log] [blame]
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.graphics;
import java.util.HashMap;
import java.util.Locale;
/**
* 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 contributes from red, gree, blue, and opaque-white would be 0xFFFFFFFF
*/
public class Color {
public static final int BLACK = 0xFF000000;
public static final int DKGRAY = 0xFF444444;
public static final int GRAY = 0xFF888888;
public static final int LTGRAY = 0xFFCCCCCC;
public static final int WHITE = 0xFFFFFFFF;
public static final int RED = 0xFFFF0000;
public static final int GREEN = 0xFF00FF00;
public static final int BLUE = 0xFF0000FF;
public static final int YELLOW = 0xFFFFFF00;
public static final int CYAN = 0xFF00FFFF;
public static final int MAGENTA = 0xFFFF00FF;
public static final int TRANSPARENT = 0;
/**
* Return the alpha component of a color int. This is the same as saying
* color >>> 24
*/
public static int alpha(int color) {
return color >>> 24;
}
/**
* Return the red component of a color int. This is the same as saying
* (color >> 16) & 0xFF
*/
public static int red(int color) {
return (color >> 16) & 0xFF;
}
/**
* Return the green component of a color int. This is the same as saying
* (color >> 8) & 0xFF
*/
public static int green(int color) {
return (color >> 8) & 0xFF;
}
/**
* Return the blue component of a color int. This is the same as saying
* color & 0xFF
*/
public static int blue(int color) {
return color & 0xFF;
}
/**
* 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.
* @param red Red component [0..255] of the color
* @param green Green component [0..255] of the color
* @param blue Blue component [0..255] of the color
*/
public static int rgb(int red, int green, int blue) {
return (0xFF << 24) | (red << 16) | (green << 8) | 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.
* @param alpha Alpha component [0..255] of the color
* @param red Red component [0..255] of the color
* @param green Green component [0..255] of the color
* @param blue Blue component [0..255] of the color
*/
public static int argb(int alpha, int red, int green, int blue) {
return (alpha << 24) | (red << 16) | (green << 8) | blue;
}
/**
* 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'
*/
public static int parseColor(String colorString) {
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.US));
if (color != null) {
return color;
}
}
throw new IllegalArgumentException("Unknown color");
}
/**
* Convert RGB components to HSV.
* hsv[0] is Hue [0 .. 360)
* hsv[1] is Saturation [0...1]
* hsv[2] is Value [0...1]
* @param red red component value [0..255]
* @param green green component value [0..255]
* @param blue blue component value [0..255]
* @param hsv 3 element array which holds the resulting HSV components.
*/
public static void RGBToHSV(int red, int green, int blue, float hsv[]) {
if (hsv.length < 3) {
throw new RuntimeException("3 components required for hsv");
}
nativeRGBToHSV(red, green, blue, 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]
* @param color the argb color to convert. The alpha component is ignored.
* @param hsv 3 element array which holds the resulting HSV components.
*/
public static void colorToHSV(int color, float hsv[]) {
RGBToHSV((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, 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.
* @param hsv 3 element array which holds the input HSV components.
* @return the resulting argb color
*/
public static int HSVToColor(float hsv[]) {
return HSVToColor(0xFF, 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.
* @param alpha the alpha component of the returned argb color.
* @param hsv 3 element array which holds the input HSV components.
* @return the resulting argb color
*/
public static int HSVToColor(int alpha, float hsv[]) {
if (hsv.length < 3) {
throw new RuntimeException("3 components required for hsv");
}
return nativeHSVToColor(alpha, hsv);
}
private static native void nativeRGBToHSV(int red, int greed, int blue,
float hsv[]);
private static native int nativeHSVToColor(int alpha, float hsv[]);
private static final HashMap<String, Integer> sColorNameMap;
static {
sColorNameMap = new HashMap();
sColorNameMap.put("black", Integer.valueOf(BLACK));
sColorNameMap.put("darkgray", Integer.valueOf(DKGRAY));
sColorNameMap.put("gray", Integer.valueOf(GRAY));
sColorNameMap.put("lightgray", Integer.valueOf(LTGRAY));
sColorNameMap.put("white", Integer.valueOf(WHITE));
sColorNameMap.put("red", Integer.valueOf(RED));
sColorNameMap.put("green", Integer.valueOf(GREEN));
sColorNameMap.put("blue", Integer.valueOf(BLUE));
sColorNameMap.put("yellow", Integer.valueOf(YELLOW));
sColorNameMap.put("cyan", Integer.valueOf(CYAN));
sColorNameMap.put("magenta", Integer.valueOf(MAGENTA));
}
}