Color.java revision 68bd5fdd1ad3cf0b74c225b31adf1f68393bfbb6
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 android.annotation.ColorInt;
20import android.annotation.Size;
21
22import com.android.internal.util.XmlUtils;
23
24import java.util.HashMap;
25import java.util.Locale;
26import java.util.function.DoubleUnaryOperator;
27
28/**
29 * The Color class defines methods for creating and converting color ints.
30 * Colors are represented as packed ints, made up of 4 bytes: alpha, red,
31 * green, blue. The values are unpremultiplied, meaning any transparency is
32 * stored solely in the alpha component, and not in the color components. The
33 * components are stored as follows (alpha << 24) | (red << 16) |
34 * (green << 8) | blue. Each component ranges between 0..255 with 0
35 * meaning no contribution for that component, and 255 meaning 100%
36 * contribution. Thus opaque-black would be 0xFF000000 (100% opaque but
37 * no contributions from red, green, or blue), and opaque-white would be
38 * 0xFFFFFFFF
39 */
40public class Color {
41    @ColorInt public static final int BLACK       = 0xFF000000;
42    @ColorInt public static final int DKGRAY      = 0xFF444444;
43    @ColorInt public static final int GRAY        = 0xFF888888;
44    @ColorInt public static final int LTGRAY      = 0xFFCCCCCC;
45    @ColorInt public static final int WHITE       = 0xFFFFFFFF;
46    @ColorInt public static final int RED         = 0xFFFF0000;
47    @ColorInt public static final int GREEN       = 0xFF00FF00;
48    @ColorInt public static final int BLUE        = 0xFF0000FF;
49    @ColorInt public static final int YELLOW      = 0xFFFFFF00;
50    @ColorInt public static final int CYAN        = 0xFF00FFFF;
51    @ColorInt public static final int MAGENTA     = 0xFFFF00FF;
52    @ColorInt public static final int TRANSPARENT = 0;
53
54    /**
55     * Return the alpha component of a color int. This is the same as saying
56     * color >>> 24
57     */
58    public static int alpha(int color) {
59        return color >>> 24;
60    }
61
62    /**
63     * Return the red component of a color int. This is the same as saying
64     * (color >> 16) & 0xFF
65     */
66    public static int red(int color) {
67        return (color >> 16) & 0xFF;
68    }
69
70    /**
71     * Return the green component of a color int. This is the same as saying
72     * (color >> 8) & 0xFF
73     */
74    public static int green(int color) {
75        return (color >> 8) & 0xFF;
76    }
77
78    /**
79     * Return the blue component of a color int. This is the same as saying
80     * color & 0xFF
81     */
82    public static int blue(int color) {
83        return color & 0xFF;
84    }
85
86    /**
87     * Return a color-int from red, green, blue components.
88     * The alpha component is implicity 255 (fully opaque).
89     * These component values should be [0..255], but there is no
90     * range check performed, so if they are out of range, the
91     * returned color is undefined.
92     * @param red  Red component [0..255] of the color
93     * @param green Green component [0..255] of the color
94     * @param blue  Blue component [0..255] of the color
95     */
96    @ColorInt
97    public static int rgb(int red, int green, int blue) {
98        return 0xff000000 | (red << 16) | (green << 8) | blue;
99    }
100
101    /**
102     * Return a color-int from alpha, red, green, blue components.
103     * These component values should be [0..255], but there is no
104     * range check performed, so if they are out of range, the
105     * returned color is undefined.
106     * @param alpha Alpha component [0..255] of the color
107     * @param red   Red component [0..255] of the color
108     * @param green Green component [0..255] of the color
109     * @param blue  Blue component [0..255] of the color
110     */
111    @ColorInt
112    public static int argb(int alpha, int red, int green, int blue) {
113        return (alpha << 24) | (red << 16) | (green << 8) | blue;
114    }
115
116    /**
117     * Returns the relative luminance of a color.
118     * <p>
119     * Assumes sRGB encoding. Based on the formula for relative luminance
120     * defined in WCAG 2.0, W3C Recommendation 11 December 2008.
121     *
122     * @return a value between 0 (darkest black) and 1 (lightest white)
123     */
124    public static float luminance(@ColorInt int color) {
125        ColorSpace.Rgb cs = (ColorSpace.Rgb) ColorSpace.get(ColorSpace.Named.SRGB);
126        DoubleUnaryOperator eotf = cs.getEotf();
127        double red = eotf.applyAsDouble(Color.red(color) / 255.0);
128        double green = eotf.applyAsDouble(Color.green(color) / 255.0);
129        double blue = eotf.applyAsDouble(Color.blue(color) / 255.0);
130        return (float) ((0.2126 * red) + (0.7152 * green) + (0.0722 * blue));
131    }
132
133    /**
134     * Parse the color string, and return the corresponding color-int.
135     * If the string cannot be parsed, throws an IllegalArgumentException
136     * exception. Supported formats are:
137     * #RRGGBB
138     * #AARRGGBB
139     * or one of the following names:
140     * 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta',
141     * 'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey',
142     * 'aqua', 'fuchsia', 'lime', 'maroon', 'navy', 'olive', 'purple',
143     * 'silver', 'teal'.
144     */
145    @ColorInt
146    public static int parseColor(@Size(min=1) String colorString) {
147        if (colorString.charAt(0) == '#') {
148            // Use a long to avoid rollovers on #ffXXXXXX
149            long color = Long.parseLong(colorString.substring(1), 16);
150            if (colorString.length() == 7) {
151                // Set the alpha value
152                color |= 0x00000000ff000000;
153            } else if (colorString.length() != 9) {
154                throw new IllegalArgumentException("Unknown color");
155            }
156            return (int)color;
157        } else {
158            Integer color = sColorNameMap.get(colorString.toLowerCase(Locale.ROOT));
159            if (color != null) {
160                return color;
161            }
162        }
163        throw new IllegalArgumentException("Unknown color");
164    }
165
166    /**
167     * Convert RGB components to HSV.
168     *     hsv[0] is Hue [0 .. 360)
169     *     hsv[1] is Saturation [0...1]
170     *     hsv[2] is Value [0...1]
171     * @param red  red component value [0..255]
172     * @param green  green component value [0..255]
173     * @param blue  blue component value [0..255]
174     * @param hsv  3 element array which holds the resulting HSV components.
175     */
176    public static void RGBToHSV(int red, int green, int blue, @Size(3) float hsv[]) {
177        if (hsv.length < 3) {
178            throw new RuntimeException("3 components required for hsv");
179        }
180        nativeRGBToHSV(red, green, blue, hsv);
181    }
182
183    /**
184     * Convert the argb color to its HSV components.
185     *     hsv[0] is Hue [0 .. 360)
186     *     hsv[1] is Saturation [0...1]
187     *     hsv[2] is Value [0...1]
188     * @param color the argb color to convert. The alpha component is ignored.
189     * @param hsv  3 element array which holds the resulting HSV components.
190     */
191    public static void colorToHSV(@ColorInt int color, @Size(3) float hsv[]) {
192        RGBToHSV((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, hsv);
193    }
194
195    /**
196     * Convert HSV components to an ARGB color. Alpha set to 0xFF.
197     *     hsv[0] is Hue [0 .. 360)
198     *     hsv[1] is Saturation [0...1]
199     *     hsv[2] is Value [0...1]
200     * If hsv values are out of range, they are pinned.
201     * @param hsv  3 element array which holds the input HSV components.
202     * @return the resulting argb color
203    */
204    public static int HSVToColor(@Size(3) float hsv[]) {
205        return HSVToColor(0xFF, hsv);
206    }
207
208    /**
209     * Convert HSV components to an ARGB color. The alpha component is passed
210     * through unchanged.
211     *     hsv[0] is Hue [0 .. 360)
212     *     hsv[1] is Saturation [0...1]
213     *     hsv[2] is Value [0...1]
214     * If hsv values are out of range, they are pinned.
215     * @param alpha the alpha component of the returned argb color.
216     * @param hsv  3 element array which holds the input HSV components.
217     * @return the resulting argb color
218    */
219    public static int HSVToColor(int alpha, @Size(3) float hsv[]) {
220        if (hsv.length < 3) {
221            throw new RuntimeException("3 components required for hsv");
222        }
223        return nativeHSVToColor(alpha, hsv);
224    }
225
226    private static native void nativeRGBToHSV(int red, int greed, int blue, float hsv[]);
227    private static native int nativeHSVToColor(int alpha, float hsv[]);
228
229    /**
230     * Converts an HTML color (named or numeric) to an integer RGB value.
231     *
232     * @param color Non-null color string.
233     *
234     * @return A color value, or {@code -1} if the color string could not be interpreted.
235     *
236     * @hide
237     */
238    @ColorInt
239    public static int getHtmlColor(String color) {
240        Integer i = sColorNameMap.get(color.toLowerCase(Locale.ROOT));
241        if (i != null) {
242            return i;
243        } else {
244            try {
245                return XmlUtils.convertValueToInt(color, -1);
246            } catch (NumberFormatException nfe) {
247                return -1;
248            }
249        }
250    }
251
252    private static final HashMap<String, Integer> sColorNameMap;
253    static {
254        sColorNameMap = new HashMap<>();
255        sColorNameMap.put("black", BLACK);
256        sColorNameMap.put("darkgray", DKGRAY);
257        sColorNameMap.put("gray", GRAY);
258        sColorNameMap.put("lightgray", LTGRAY);
259        sColorNameMap.put("white", WHITE);
260        sColorNameMap.put("red", RED);
261        sColorNameMap.put("green", GREEN);
262        sColorNameMap.put("blue", BLUE);
263        sColorNameMap.put("yellow", YELLOW);
264        sColorNameMap.put("cyan", CYAN);
265        sColorNameMap.put("magenta", MAGENTA);
266        sColorNameMap.put("aqua", 0xFF00FFFF);
267        sColorNameMap.put("fuchsia", 0xFFFF00FF);
268        sColorNameMap.put("darkgrey", DKGRAY);
269        sColorNameMap.put("grey", GRAY);
270        sColorNameMap.put("lightgrey", LTGRAY);
271        sColorNameMap.put("lime", 0xFF00FF00);
272        sColorNameMap.put("maroon", 0xFF800000);
273        sColorNameMap.put("navy", 0xFF000080);
274        sColorNameMap.put("olive", 0xFF808000);
275        sColorNameMap.put("purple", 0xFF800080);
276        sColorNameMap.put("silver", 0xFFC0C0C0);
277        sColorNameMap.put("teal", 0xFF008080);
278
279    }
280}
281