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; 20a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guyimport com.android.internal.util.XmlUtils; 21a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.HashMap; 239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.Locale; 249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/** 269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * The Color class defines methods for creating and converting color ints. 279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Colors are represented as packed ints, made up of 4 bytes: alpha, red, 289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * green, blue. The values are unpremultiplied, meaning any transparency is 299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * stored solely in the alpha component, and not in the color components. The 309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * components are stored as follows (alpha << 24) | (red << 16) | 319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * (green << 8) | blue. Each component ranges between 0..255 with 0 329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * meaning no contribution for that component, and 255 meaning 100% 339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * contribution. Thus opaque-black would be 0xFF000000 (100% opaque but 345f3445dc609fb3dd64b023c1ba9c1ee3ba95b868David Hoover * no contributions from red, green, or blue), and opaque-white would be 355f3445dc609fb3dd64b023c1ba9c1ee3ba95b868David Hoover * 0xFFFFFFFF 369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class Color { 389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int BLACK = 0xFF000000; 399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int DKGRAY = 0xFF444444; 409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int GRAY = 0xFF888888; 419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int LTGRAY = 0xFFCCCCCC; 429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int WHITE = 0xFFFFFFFF; 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int RED = 0xFFFF0000; 449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int GREEN = 0xFF00FF00; 459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int BLUE = 0xFF0000FF; 469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int YELLOW = 0xFFFFFF00; 479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int CYAN = 0xFF00FFFF; 489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int MAGENTA = 0xFFFF00FF; 499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final int TRANSPARENT = 0; 509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Return the alpha component of a color int. This is the same as saying 539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * color >>> 24 549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static int alpha(int color) { 569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return color >>> 24; 579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Return the red component of a color int. This is the same as saying 619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * (color >> 16) & 0xFF 629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static int red(int color) { 649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return (color >> 16) & 0xFF; 659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Return the green component of a color int. This is the same as saying 699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * (color >> 8) & 0xFF 709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static int green(int color) { 729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return (color >> 8) & 0xFF; 739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Return the blue component of a color int. This is the same as saying 779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * color & 0xFF 789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static int blue(int color) { 809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return color & 0xFF; 819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Return a color-int from red, green, blue components. 859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * The alpha component is implicity 255 (fully opaque). 869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * These component values should be [0..255], but there is no 879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * range check performed, so if they are out of range, the 889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * returned color is undefined. 899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param red Red component [0..255] of the color 909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param green Green component [0..255] of the color 919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param blue Blue component [0..255] of the color 929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static int rgb(int red, int green, int blue) { 949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return (0xFF << 24) | (red << 16) | (green << 8) | blue; 959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Return a color-int from alpha, red, green, blue components. 999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * These component values should be [0..255], but there is no 1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * range check performed, so if they are out of range, the 1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * returned color is undefined. 1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param alpha Alpha component [0..255] of the color 1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param red Red component [0..255] of the color 1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param green Green component [0..255] of the color 1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param blue Blue component [0..255] of the color 1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static int argb(int alpha, int red, int green, int blue) { 1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return (alpha << 24) | (red << 16) | (green << 8) | blue; 1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 112a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * Returns the hue component of a color int. 113a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * 114a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @return A value between 0.0f and 1.0f 115a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * 116a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @hide Pending API council 117a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy */ 118a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy public static float hue(int color) { 119a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int r = (color >> 16) & 0xFF; 120a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int g = (color >> 8) & 0xFF; 121a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int b = color & 0xFF; 122a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 123a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int V = Math.max(b, Math.max(r, g)); 124a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int temp = Math.min(b, Math.min(r, g)); 125a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 126a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy float H; 127a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 128a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy if (V == temp) { 129a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy H = 0; 130a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } else { 131a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy final float vtemp = (float) (V - temp); 132a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy final float cr = (V - r) / vtemp; 133a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy final float cg = (V - g) / vtemp; 134a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy final float cb = (V - b) / vtemp; 135a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 136a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy if (r == V) { 137a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy H = cb - cg; 138a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } else if (g == V) { 139a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy H = 2 + cr - cb; 140a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } else { 141a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy H = 4 + cg - cr; 142a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } 143a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 144a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy H /= 6.f; 145a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy if (H < 0) { 146a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy H++; 147a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } 148a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } 149a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 150a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy return H; 151a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } 152a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 153a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy /** 154a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * Returns the saturation component of a color int. 155a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * 156a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @return A value between 0.0f and 1.0f 157a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * 158a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @hide Pending API council 159a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy */ 160a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy public static float saturation(int color) { 161a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int r = (color >> 16) & 0xFF; 162a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int g = (color >> 8) & 0xFF; 163a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int b = color & 0xFF; 164a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 165a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 166a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int V = Math.max(b, Math.max(r, g)); 167a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int temp = Math.min(b, Math.min(r, g)); 168a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 169a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy float S; 170a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 171a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy if (V == temp) { 172a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy S = 0; 173a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } else { 174a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy S = (V - temp) / (float) V; 175a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } 176a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 177a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy return S; 178a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } 179a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 180a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy /** 181a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * Returns the brightness component of a color int. 182a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy * 183a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @return A value between 0.0f and 1.0f 184a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy * 185a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @hide Pending API council 186a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy */ 187a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy public static float brightness(int color) { 188a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int r = (color >> 16) & 0xFF; 189a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int g = (color >> 8) & 0xFF; 190a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int b = color & 0xFF; 191a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 192a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy int V = Math.max(b, Math.max(r, g)); 193a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 194a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy return (V / 255.f); 195a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } 196a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 197a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy /** 1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Parse the color string, and return the corresponding color-int. 1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * If the string cannot be parsed, throws an IllegalArgumentException 2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * exception. Supported formats are: 2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * #RRGGBB 2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * #AARRGGBB 2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 204a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy * 'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey', 205a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy * 'aqua', 'fuschia', 'lime', 'maroon', 'navy', 'olive', 'purple', 206a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy * 'silver', 'teal' 2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static int parseColor(String colorString) { 2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (colorString.charAt(0) == '#') { 2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Use a long to avoid rollovers on #ffXXXXXX 2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project long color = Long.parseLong(colorString.substring(1), 16); 2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (colorString.length() == 7) { 2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Set the alpha value 2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project color |= 0x00000000ff000000; 2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } else if (colorString.length() != 9) { 2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("Unknown color"); 2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return (int)color; 2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } else { 220aeb60fb86fc572500239c6f6684d1eb71e3ae35bElliott Hughes Integer color = sColorNameMap.get(colorString.toLowerCase(Locale.ROOT)); 2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (color != null) { 2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return color; 2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("Unknown color"); 2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 229a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * Convert HSB components to an ARGB color. Alpha set to 0xFF. 230a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * hsv[0] is Hue [0 .. 1) 231a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * hsv[1] is Saturation [0...1] 232a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * hsv[2] is Value [0...1] 233a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * If hsv values are out of range, they are pinned. 234a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @param hsb 3 element array which holds the input HSB components. 235a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @return the resulting argb color 236a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * 237a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @hide Pending API council 238a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy */ 239a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy public static int HSBtoColor(float[] hsb) { 240a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy return HSBtoColor(hsb[0], hsb[1], hsb[2]); 241a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } 242a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 243a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy /** 244a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * Convert HSB components to an ARGB color. Alpha set to 0xFF. 245a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * hsv[0] is Hue [0 .. 1) 246a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * hsv[1] is Saturation [0...1] 247a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * hsv[2] is Value [0...1] 248a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * If hsv values are out of range, they are pinned. 249a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @param h Hue component 250a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @param s Saturation component 251a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @param b Brightness component 252a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @return the resulting argb color 253a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * 254a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy * @hide Pending API council 255a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy */ 256a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy public static int HSBtoColor(float h, float s, float b) { 257a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy h = MathUtils.constrain(h, 0.0f, 1.0f); 258a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy s = MathUtils.constrain(s, 0.0f, 1.0f); 259a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy b = MathUtils.constrain(b, 0.0f, 1.0f); 260a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 261a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy float red = 0.0f; 262a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy float green = 0.0f; 263a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy float blue = 0.0f; 264a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 265a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy final float hf = (h - (int) h) * 6.0f; 266a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy final int ihf = (int) hf; 267a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy final float f = hf - ihf; 268a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy final float pv = b * (1.0f - s); 269a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy final float qv = b * (1.0f - s * f); 270a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy final float tv = b * (1.0f - s * (1.0f - f)); 271a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 272a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy switch (ihf) { 273a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy case 0: // Red is the dominant color 274a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy red = b; 275a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy green = tv; 276a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy blue = pv; 277a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy break; 278a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy case 1: // Green is the dominant color 279a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy red = qv; 280a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy green = b; 281a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy blue = pv; 282a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy break; 283a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy case 2: 284a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy red = pv; 285a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy green = b; 286a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy blue = tv; 287a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy break; 288a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy case 3: // Blue is the dominant color 289a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy red = pv; 290a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy green = qv; 291a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy blue = b; 292a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy break; 293a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy case 4: 294a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy red = tv; 295a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy green = pv; 296a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy blue = b; 297a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy break; 298a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy case 5: // Red is the dominant color 299a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy red = b; 300a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy green = pv; 301a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy blue = qv; 302a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy break; 303a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } 304a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 305a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy return 0xFF000000 | (((int) (red * 255.0f)) << 16) | 306a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy (((int) (green * 255.0f)) << 8) | ((int) (blue * 255.0f)); 307a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy } 308a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy 309a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy /** 3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Convert RGB components to HSV. 3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hsv[0] is Hue [0 .. 360) 3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hsv[1] is Saturation [0...1] 3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hsv[2] is Value [0...1] 3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param red red component value [0..255] 3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param green green component value [0..255] 3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param blue blue component value [0..255] 3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param hsv 3 element array which holds the resulting HSV components. 3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static void RGBToHSV(int red, int green, int blue, float hsv[]) { 3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (hsv.length < 3) { 3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new RuntimeException("3 components required for hsv"); 3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project nativeRGBToHSV(red, green, blue, hsv); 3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Convert the argb color to its HSV components. 3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hsv[0] is Hue [0 .. 360) 3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hsv[1] is Saturation [0...1] 3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hsv[2] is Value [0...1] 3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param color the argb color to convert. The alpha component is ignored. 3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param hsv 3 element array which holds the resulting HSV components. 3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static void colorToHSV(int color, float hsv[]) { 3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project RGBToHSV((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, hsv); 3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Convert HSV components to an ARGB color. Alpha set to 0xFF. 3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hsv[0] is Hue [0 .. 360) 3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hsv[1] is Saturation [0...1] 3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hsv[2] is Value [0...1] 3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * If hsv values are out of range, they are pinned. 3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param hsv 3 element array which holds the input HSV components. 3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return the resulting argb color 3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static int HSVToColor(float hsv[]) { 3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return HSVToColor(0xFF, hsv); 3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Convert HSV components to an ARGB color. The alpha component is passed 3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * through unchanged. 3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hsv[0] is Hue [0 .. 360) 3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hsv[1] is Saturation [0...1] 3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hsv[2] is Value [0...1] 3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * If hsv values are out of range, they are pinned. 3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param alpha the alpha component of the returned argb color. 3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param hsv 3 element array which holds the input HSV components. 3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return the resulting argb color 3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static int HSVToColor(int alpha, float hsv[]) { 3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (hsv.length < 3) { 3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new RuntimeException("3 components required for hsv"); 3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return nativeHSVToColor(alpha, hsv); 3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 369a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy private static native void nativeRGBToHSV(int red, int greed, int blue, float hsv[]); 3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private static native int nativeHSVToColor(int alpha, float hsv[]); 3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 372a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy /** 373a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy * Converts an HTML color (named or numeric) to an integer RGB value. 374a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy * 375a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy * @param color Non-null color string. 376a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy * 377a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy * @return A color value, or {@code -1} if the color string could not be interpreted. 378a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy * 379a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy * @hide 380a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy */ 381a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy public static int getHtmlColor(String color) { 382aeb60fb86fc572500239c6f6684d1eb71e3ae35bElliott Hughes Integer i = sColorNameMap.get(color.toLowerCase(Locale.ROOT)); 383a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy if (i != null) { 384a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy return i; 385a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy } else { 386a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy try { 387a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy return XmlUtils.convertValueToInt(color, -1); 388a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy } catch (NumberFormatException nfe) { 389a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy return -1; 390a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy } 391a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy } 392a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy } 393a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy 3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private static final HashMap<String, Integer> sColorNameMap; 3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project static { 397a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy sColorNameMap = new HashMap<String, Integer>(); 398a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy sColorNameMap.put("black", BLACK); 399a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy sColorNameMap.put("darkgray", DKGRAY); 400a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy sColorNameMap.put("gray", GRAY); 401a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy sColorNameMap.put("lightgray", LTGRAY); 402a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy sColorNameMap.put("white", WHITE); 403a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy sColorNameMap.put("red", RED); 404a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy sColorNameMap.put("green", GREEN); 405a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy sColorNameMap.put("blue", BLUE); 406a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy sColorNameMap.put("yellow", YELLOW); 407a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy sColorNameMap.put("cyan", CYAN); 408a32d100b34d048cf0c765d8f31d87b81ab88d1ebRomain Guy sColorNameMap.put("magenta", MAGENTA); 40986d3e03d381bff8c107e3def31cf37f4851f827cRomain Guy sColorNameMap.put("aqua", 0xFF00FFFF); 41086d3e03d381bff8c107e3def31cf37f4851f827cRomain Guy sColorNameMap.put("fuchsia", 0xFFFF00FF); 411a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy sColorNameMap.put("darkgrey", DKGRAY); 412a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy sColorNameMap.put("grey", GRAY); 413a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy sColorNameMap.put("lightgrey", LTGRAY); 41486d3e03d381bff8c107e3def31cf37f4851f827cRomain Guy sColorNameMap.put("lime", 0xFF00FF00); 41586d3e03d381bff8c107e3def31cf37f4851f827cRomain Guy sColorNameMap.put("maroon", 0xFF800000); 41686d3e03d381bff8c107e3def31cf37f4851f827cRomain Guy sColorNameMap.put("navy", 0xFF000080); 41786d3e03d381bff8c107e3def31cf37f4851f827cRomain Guy sColorNameMap.put("olive", 0xFF808000); 41886d3e03d381bff8c107e3def31cf37f4851f827cRomain Guy sColorNameMap.put("purple", 0xFF800080); 41986d3e03d381bff8c107e3def31cf37f4851f827cRomain Guy sColorNameMap.put("silver", 0xFFC0C0C0); 42086d3e03d381bff8c107e3def31cf37f4851f827cRomain Guy sColorNameMap.put("teal", 0xFF008080); 421a8f6d5f0720f400b6f59b0809aaefea83c5f51d4Romain Guy 4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 424