Color.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics;
18
19import java.util.HashMap;
20import java.util.Locale;
21
22/**
23 * The Color class defines methods for creating and converting color ints.
24 * Colors are represented as packed ints, made up of 4 bytes: alpha, red,
25 * green, blue. The values are unpremultiplied, meaning any transparency is
26 * stored solely in the alpha component, and not in the color components. The
27 * components are stored as follows (alpha << 24) | (red << 16) |
28 * (green << 8) | blue. Each component ranges between 0..255 with 0
29 * meaning no contribution for that component, and 255 meaning 100%
30 * contribution. Thus opaque-black would be 0xFF000000 (100% opaque but
31 * no contributes from red, gree, blue, and opaque-white would be 0xFFFFFFFF
32 */
33public class Color {
34    public static final int BLACK       = 0xFF000000;
35    public static final int DKGRAY      = 0xFF444444;
36    public static final int GRAY        = 0xFF888888;
37    public static final int LTGRAY      = 0xFFCCCCCC;
38    public static final int WHITE       = 0xFFFFFFFF;
39    public static final int RED         = 0xFFFF0000;
40    public static final int GREEN       = 0xFF00FF00;
41    public static final int BLUE        = 0xFF0000FF;
42    public static final int YELLOW      = 0xFFFFFF00;
43    public static final int CYAN        = 0xFF00FFFF;
44    public static final int MAGENTA     = 0xFFFF00FF;
45    public static final int TRANSPARENT = 0;
46
47    /**
48     * Return the alpha component of a color int. This is the same as saying
49     * color >>> 24
50     */
51    public static int alpha(int color) {
52        return color >>> 24;
53    }
54
55    /**
56     * Return the red component of a color int. This is the same as saying
57     * (color >> 16) & 0xFF
58     */
59    public static int red(int color) {
60        return (color >> 16) & 0xFF;
61    }
62
63    /**
64     * Return the green component of a color int. This is the same as saying
65     * (color >> 8) & 0xFF
66     */
67    public static int green(int color) {
68        return (color >> 8) & 0xFF;
69    }
70
71    /**
72     * Return the blue component of a color int. This is the same as saying
73     * color & 0xFF
74     */
75    public static int blue(int color) {
76        return color & 0xFF;
77    }
78
79    /**
80     * Return a color-int from red, green, blue components.
81     * The alpha component is implicity 255 (fully opaque).
82     * These component values should be [0..255], but there is no
83     * range check performed, so if they are out of range, the
84     * returned color is undefined.
85     * @param red  Red component [0..255] of the color
86     * @param green Green component [0..255] of the color
87     * @param blue  Blue component [0..255] of the color
88     */
89    public static int rgb(int red, int green, int blue) {
90        return (0xFF << 24) | (red << 16) | (green << 8) | blue;
91    }
92
93    /**
94     * Return a color-int from alpha, red, green, blue components.
95     * These component values should be [0..255], but there is no
96     * range check performed, so if they are out of range, the
97     * returned color is undefined.
98     * @param alpha Alpha component [0..255] of the color
99     * @param red   Red component [0..255] of the color
100     * @param green Green component [0..255] of the color
101     * @param blue  Blue component [0..255] of the color
102     */
103    public static int argb(int alpha, int red, int green, int blue) {
104        return (alpha << 24) | (red << 16) | (green << 8) | blue;
105    }
106
107    /**
108     * Parse the color string, and return the corresponding color-int.
109     * If the string cannot be parsed, throws an IllegalArgumentException
110     * exception. Supported formats are:
111     * #RRGGBB
112     * #AARRGGBB
113     * 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta',
114     * 'yellow', 'lightgray', 'darkgray'
115     */
116    public static int parseColor(String colorString) {
117        if (colorString.charAt(0) == '#') {
118            // Use a long to avoid rollovers on #ffXXXXXX
119            long color = Long.parseLong(colorString.substring(1), 16);
120            if (colorString.length() == 7) {
121                // Set the alpha value
122                color |= 0x00000000ff000000;
123            } else if (colorString.length() != 9) {
124                throw new IllegalArgumentException("Unknown color");
125            }
126            return (int)color;
127        } else {
128            Integer color = sColorNameMap.get(colorString.toLowerCase(Locale.US));
129            if (color != null) {
130                return color;
131            }
132        }
133        throw new IllegalArgumentException("Unknown color");
134    }
135
136    /**
137     * Convert RGB components to HSV.
138     *     hsv[0] is Hue [0 .. 360)
139     *     hsv[1] is Saturation [0...1]
140     *     hsv[2] is Value [0...1]
141     * @param red  red component value [0..255]
142     * @param green  green component value [0..255]
143     * @param blue  blue component value [0..255]
144     * @param hsv  3 element array which holds the resulting HSV components.
145     */
146    public static void RGBToHSV(int red, int green, int blue, float hsv[]) {
147        if (hsv.length < 3) {
148            throw new RuntimeException("3 components required for hsv");
149        }
150        nativeRGBToHSV(red, green, blue, hsv);
151    }
152
153    /**
154     * Convert the argb color to its HSV components.
155     *     hsv[0] is Hue [0 .. 360)
156     *     hsv[1] is Saturation [0...1]
157     *     hsv[2] is Value [0...1]
158     * @param color the argb color to convert. The alpha component is ignored.
159     * @param hsv  3 element array which holds the resulting HSV components.
160     */
161    public static void colorToHSV(int color, float hsv[]) {
162        RGBToHSV((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, hsv);
163    }
164
165    /**
166     * Convert HSV components to an ARGB color. Alpha set to 0xFF.
167     *     hsv[0] is Hue [0 .. 360)
168     *     hsv[1] is Saturation [0...1]
169     *     hsv[2] is Value [0...1]
170     * If hsv values are out of range, they are pinned.
171     * @param hsv  3 element array which holds the input HSV components.
172     * @return the resulting argb color
173    */
174    public static int HSVToColor(float hsv[]) {
175        return HSVToColor(0xFF, hsv);
176    }
177
178    /**
179     * Convert HSV components to an ARGB color. The alpha component is passed
180     * through unchanged.
181     *     hsv[0] is Hue [0 .. 360)
182     *     hsv[1] is Saturation [0...1]
183     *     hsv[2] is Value [0...1]
184     * If hsv values are out of range, they are pinned.
185     * @param alpha the alpha component of the returned argb color.
186     * @param hsv  3 element array which holds the input HSV components.
187     * @return the resulting argb color
188    */
189    public static int HSVToColor(int alpha, float hsv[]) {
190        if (hsv.length < 3) {
191            throw new RuntimeException("3 components required for hsv");
192        }
193        return nativeHSVToColor(alpha, hsv);
194    }
195
196    private static native void nativeRGBToHSV(int red, int greed, int blue,
197                                              float hsv[]);
198    private static native int nativeHSVToColor(int alpha, float hsv[]);
199
200    private static final HashMap<String, Integer> sColorNameMap;
201
202    static {
203        sColorNameMap = new HashMap();
204        sColorNameMap.put("black", Integer.valueOf(BLACK));
205        sColorNameMap.put("darkgray", Integer.valueOf(DKGRAY));
206        sColorNameMap.put("gray", Integer.valueOf(GRAY));
207        sColorNameMap.put("lightgray", Integer.valueOf(LTGRAY));
208        sColorNameMap.put("white", Integer.valueOf(WHITE));
209        sColorNameMap.put("red", Integer.valueOf(RED));
210        sColorNameMap.put("green", Integer.valueOf(GREEN));
211        sColorNameMap.put("blue", Integer.valueOf(BLUE));
212        sColorNameMap.put("yellow", Integer.valueOf(YELLOW));
213        sColorNameMap.put("cyan", Integer.valueOf(CYAN));
214        sColorNameMap.put("magenta", Integer.valueOf(MAGENTA));
215    }
216}
217
218