Color.java revision 5f3445dc609fb3dd64b023c1ba9c1ee3ba95b868
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.graphics;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guyimport android.util.MathUtils;
20a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.HashMap;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.Locale;
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * The Color class defines methods for creating and converting color ints.
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Colors are represented as packed ints, made up of 4 bytes: alpha, red,
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * green, blue. The values are unpremultiplied, meaning any transparency is
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * stored solely in the alpha component, and not in the color components. The
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * components are stored as follows (alpha << 24) | (red << 16) |
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * (green << 8) | blue. Each component ranges between 0..255 with 0
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * meaning no contribution for that component, and 255 meaning 100%
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * contribution. Thus opaque-black would be 0xFF000000 (100% opaque but
335f3445dc609fb3dd64b023c1ba9c1ee3ba95b868David Hoover * no contributions from red, green, or blue), and opaque-white would be
345f3445dc609fb3dd64b023c1ba9c1ee3ba95b868David Hoover * 0xFFFFFFFF
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class Color {
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int BLACK       = 0xFF000000;
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int DKGRAY      = 0xFF444444;
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int GRAY        = 0xFF888888;
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int LTGRAY      = 0xFFCCCCCC;
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int WHITE       = 0xFFFFFFFF;
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int RED         = 0xFFFF0000;
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int GREEN       = 0xFF00FF00;
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int BLUE        = 0xFF0000FF;
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int YELLOW      = 0xFFFFFF00;
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int CYAN        = 0xFF00FFFF;
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int MAGENTA     = 0xFFFF00FF;
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int TRANSPARENT = 0;
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Return the alpha component of a color int. This is the same as saying
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * color >>> 24
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static int alpha(int color) {
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return color >>> 24;
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Return the red component of a color int. This is the same as saying
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * (color >> 16) & 0xFF
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static int red(int color) {
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (color >> 16) & 0xFF;
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Return the green component of a color int. This is the same as saying
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * (color >> 8) & 0xFF
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static int green(int color) {
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (color >> 8) & 0xFF;
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Return the blue component of a color int. This is the same as saying
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * color & 0xFF
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static int blue(int color) {
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return color & 0xFF;
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Return a color-int from red, green, blue components.
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The alpha component is implicity 255 (fully opaque).
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * These component values should be [0..255], but there is no
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * range check performed, so if they are out of range, the
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * returned color is undefined.
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param red  Red component [0..255] of the color
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param green Green component [0..255] of the color
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param blue  Blue component [0..255] of the color
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static int rgb(int red, int green, int blue) {
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (0xFF << 24) | (red << 16) | (green << 8) | blue;
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Return a color-int from alpha, red, green, blue components.
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * These component values should be [0..255], but there is no
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * range check performed, so if they are out of range, the
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * returned color is undefined.
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param alpha Alpha component [0..255] of the color
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param red   Red component [0..255] of the color
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param green Green component [0..255] of the color
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param blue  Blue component [0..255] of the color
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static int argb(int alpha, int red, int green, int blue) {
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (alpha << 24) | (red << 16) | (green << 8) | blue;
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
111a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * Returns the hue component of a color int.
112a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *
113a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @return A value between 0.0f and 1.0f
114a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *
115a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @hide Pending API council
116a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     */
117a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    public static float hue(int color) {
118a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int r = (color >> 16) & 0xFF;
119a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int g = (color >> 8) & 0xFF;
120a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int b = color & 0xFF;
121a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
122a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int V = Math.max(b, Math.max(r, g));
123a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int temp = Math.min(b, Math.min(r, g));
124a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
125a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        float H;
126a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
127a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        if (V == temp) {
128a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            H = 0;
129a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        } else {
130a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            final float vtemp = (float) (V - temp);
131a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            final float cr = (V - r) / vtemp;
132a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            final float cg = (V - g) / vtemp;
133a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            final float cb = (V - b) / vtemp;
134a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
135a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            if (r == V) {
136a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                H = cb - cg;
137a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            } else if (g == V) {
138a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                H = 2 + cr - cb;
139a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            } else {
140a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                H = 4 + cg - cr;
141a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            }
142a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
143a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            H /= 6.f;
144a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            if (H < 0) {
145a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                H++;
146a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            }
147a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        }
148a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
149a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        return H;
150a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    }
151a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
152a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    /**
153a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * Returns the saturation component of a color int.
154a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *
155a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @return A value between 0.0f and 1.0f
156a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *
157a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @hide Pending API council
158a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     */
159a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    public static float saturation(int color) {
160a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int r = (color >> 16) & 0xFF;
161a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int g = (color >> 8) & 0xFF;
162a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int b = color & 0xFF;
163a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
164a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
165a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int V = Math.max(b, Math.max(r, g));
166a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int temp = Math.min(b, Math.min(r, g));
167a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
168a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        float S;
169a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
170a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        if (V == temp) {
171a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            S = 0;
172a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        } else {
173a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            S = (V - temp) / (float) V;
174a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        }
175a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
176a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        return S;
177a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    }
178a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
179a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    /**
180a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * Returns the brightness component of a color int.
181a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *
182a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @return A value between 0.0f and 1.0f
183a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *
184a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @hide Pending API council
185a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     */
186a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    public static float brightness(int color) {
187a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int r = (color >> 16) & 0xFF;
188a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int g = (color >> 8) & 0xFF;
189a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int b = color & 0xFF;
190a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
191a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        int V = Math.max(b, Math.max(r, g));
192a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
193a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        return (V / 255.f);
194a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    }
195a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
196a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    /**
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Parse the color string, and return the corresponding color-int.
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If the string cannot be parsed, throws an IllegalArgumentException
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * exception. Supported formats are:
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * #RRGGBB
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * #AARRGGBB
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta',
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * 'yellow', 'lightgray', 'darkgray'
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static int parseColor(String colorString) {
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (colorString.charAt(0) == '#') {
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            // Use a long to avoid rollovers on #ffXXXXXX
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            long color = Long.parseLong(colorString.substring(1), 16);
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (colorString.length() == 7) {
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                // Set the alpha value
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                color |= 0x00000000ff000000;
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else if (colorString.length() != 9) {
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                throw new IllegalArgumentException("Unknown color");
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (int)color;
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Integer color = sColorNameMap.get(colorString.toLowerCase(Locale.US));
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (color != null) {
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return color;
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        throw new IllegalArgumentException("Unknown color");
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
226a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * Convert HSB components to an ARGB color. Alpha set to 0xFF.
227a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *     hsv[0] is Hue [0 .. 1)
228a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *     hsv[1] is Saturation [0...1]
229a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *     hsv[2] is Value [0...1]
230a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * If hsv values are out of range, they are pinned.
231a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @param hsb  3 element array which holds the input HSB components.
232a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @return the resulting argb color
233a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *
234a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @hide Pending API council
235a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     */
236a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    public static int HSBtoColor(float[] hsb) {
237a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        return HSBtoColor(hsb[0], hsb[1], hsb[2]);
238a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    }
239a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
240a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    /**
241a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * Convert HSB components to an ARGB color. Alpha set to 0xFF.
242a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *     hsv[0] is Hue [0 .. 1)
243a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *     hsv[1] is Saturation [0...1]
244a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *     hsv[2] is Value [0...1]
245a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * If hsv values are out of range, they are pinned.
246a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @param h Hue component
247a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @param s Saturation component
248a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @param b Brightness component
249a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @return the resulting argb color
250a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     *
251a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     * @hide Pending API council
252a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy     */
253a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    public static int HSBtoColor(float h, float s, float b) {
254a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        h = MathUtils.constrain(h, 0.0f, 1.0f);
255a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        s = MathUtils.constrain(s, 0.0f, 1.0f);
256a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        b = MathUtils.constrain(b, 0.0f, 1.0f);
257a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
258a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        float red = 0.0f;
259a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        float green = 0.0f;
260a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        float blue = 0.0f;
261a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
262a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        final float hf = (h - (int) h) * 6.0f;
263a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        final int ihf = (int) hf;
264a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        final float f = hf - ihf;
265a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        final float pv = b * (1.0f - s);
266a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        final float qv = b * (1.0f - s * f);
267a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        final float tv = b * (1.0f - s * (1.0f - f));
268a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
269a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        switch (ihf) {
270a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            case 0:         // Red is the dominant color
271a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                red = b;
272a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                green = tv;
273a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                blue = pv;
274a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                break;
275a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            case 1:         // Green is the dominant color
276a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                red = qv;
277a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                green = b;
278a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                blue = pv;
279a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                break;
280a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            case 2:
281a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                red = pv;
282a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                green = b;
283a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                blue = tv;
284a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                break;
285a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            case 3:         // Blue is the dominant color
286a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                red = pv;
287a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                green = qv;
288a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                blue = b;
289a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                break;
290a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            case 4:
291a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                red = tv;
292a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                green = pv;
293a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                blue = b;
294a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                break;
295a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy            case 5:         // Red is the dominant color
296a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                red = b;
297a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                green = pv;
298a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                blue = qv;
299a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                break;
300a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        }
301a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
302a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        return 0xFF000000 | (((int) (red * 255.0f)) << 16) |
303a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy                (((int) (green * 255.0f)) << 8) | ((int) (blue * 255.0f));
304a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    }
305a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy
306a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    /**
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Convert RGB components to HSV.
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *     hsv[0] is Hue [0 .. 360)
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *     hsv[1] is Saturation [0...1]
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *     hsv[2] is Value [0...1]
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param red  red component value [0..255]
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param green  green component value [0..255]
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param blue  blue component value [0..255]
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param hsv  3 element array which holds the resulting HSV components.
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static void RGBToHSV(int red, int green, int blue, float hsv[]) {
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (hsv.length < 3) {
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("3 components required for hsv");
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        nativeRGBToHSV(red, green, blue, hsv);
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Convert the argb color to its HSV components.
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *     hsv[0] is Hue [0 .. 360)
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *     hsv[1] is Saturation [0...1]
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *     hsv[2] is Value [0...1]
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param color the argb color to convert. The alpha component is ignored.
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param hsv  3 element array which holds the resulting HSV components.
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static void colorToHSV(int color, float hsv[]) {
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        RGBToHSV((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, hsv);
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Convert HSV components to an ARGB color. Alpha set to 0xFF.
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *     hsv[0] is Hue [0 .. 360)
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *     hsv[1] is Saturation [0...1]
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *     hsv[2] is Value [0...1]
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If hsv values are out of range, they are pinned.
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param hsv  3 element array which holds the input HSV components.
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the resulting argb color
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    */
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static int HSVToColor(float hsv[]) {
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return HSVToColor(0xFF, hsv);
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Convert HSV components to an ARGB color. The alpha component is passed
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * through unchanged.
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *     hsv[0] is Hue [0 .. 360)
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *     hsv[1] is Saturation [0...1]
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *     hsv[2] is Value [0...1]
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If hsv values are out of range, they are pinned.
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param alpha the alpha component of the returned argb color.
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param hsv  3 element array which holds the input HSV components.
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the resulting argb color
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    */
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static int HSVToColor(int alpha, float hsv[]) {
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (hsv.length < 3) {
3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("3 components required for hsv");
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return nativeHSVToColor(alpha, hsv);
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
366a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy    private static native void nativeRGBToHSV(int red, int greed, int blue, float hsv[]);
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static native int nativeHSVToColor(int alpha, float hsv[]);
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final HashMap<String, Integer> sColorNameMap;
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static {
372a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        sColorNameMap = new HashMap<String, Integer>();
373a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        sColorNameMap.put("black", BLACK);
374a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        sColorNameMap.put("darkgray", DKGRAY);
375a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        sColorNameMap.put("gray", GRAY);
376a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        sColorNameMap.put("lightgray", LTGRAY);
377a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        sColorNameMap.put("white", WHITE);
378a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        sColorNameMap.put("red", RED);
379a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        sColorNameMap.put("green", GREEN);
380a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        sColorNameMap.put("blue", BLUE);
381a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        sColorNameMap.put("yellow", YELLOW);
382a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        sColorNameMap.put("cyan", CYAN);
383a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy        sColorNameMap.put("magenta", MAGENTA);
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
387