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 17 package android.graphics; 18 19 import android.annotation.ColorInt; 20 import android.annotation.Size; 21 import android.util.MathUtils; 22 import com.android.internal.util.XmlUtils; 23 24 import java.util.HashMap; 25 import java.util.Locale; 26 27 /** 28 * The Color class defines methods for creating and converting color ints. 29 * Colors are represented as packed ints, made up of 4 bytes: alpha, red, 30 * green, blue. The values are unpremultiplied, meaning any transparency is 31 * stored solely in the alpha component, and not in the color components. The 32 * components are stored as follows (alpha << 24) | (red << 16) | 33 * (green << 8) | blue. Each component ranges between 0..255 with 0 34 * meaning no contribution for that component, and 255 meaning 100% 35 * contribution. Thus opaque-black would be 0xFF000000 (100% opaque but 36 * no contributions from red, green, or blue), and opaque-white would be 37 * 0xFFFFFFFF 38 */ 39 public class Color { 40 @ColorInt public static final int BLACK = 0xFF000000; 41 @ColorInt public static final int DKGRAY = 0xFF444444; 42 @ColorInt public static final int GRAY = 0xFF888888; 43 @ColorInt public static final int LTGRAY = 0xFFCCCCCC; 44 @ColorInt public static final int WHITE = 0xFFFFFFFF; 45 @ColorInt public static final int RED = 0xFFFF0000; 46 @ColorInt public static final int GREEN = 0xFF00FF00; 47 @ColorInt public static final int BLUE = 0xFF0000FF; 48 @ColorInt public static final int YELLOW = 0xFFFFFF00; 49 @ColorInt public static final int CYAN = 0xFF00FFFF; 50 @ColorInt public static final int MAGENTA = 0xFFFF00FF; 51 @ColorInt public static final int TRANSPARENT = 0; 52 53 /** 54 * Return the alpha component of a color int. This is the same as saying 55 * color >>> 24 56 */ alpha(int color)57 public static int alpha(int color) { 58 return color >>> 24; 59 } 60 61 /** 62 * Return the red component of a color int. This is the same as saying 63 * (color >> 16) & 0xFF 64 */ red(int color)65 public static int red(int color) { 66 return (color >> 16) & 0xFF; 67 } 68 69 /** 70 * Return the green component of a color int. This is the same as saying 71 * (color >> 8) & 0xFF 72 */ green(int color)73 public static int green(int color) { 74 return (color >> 8) & 0xFF; 75 } 76 77 /** 78 * Return the blue component of a color int. This is the same as saying 79 * color & 0xFF 80 */ blue(int color)81 public static int blue(int color) { 82 return color & 0xFF; 83 } 84 85 /** 86 * Return a color-int from red, green, blue components. 87 * The alpha component is implicity 255 (fully opaque). 88 * These component values should be [0..255], but there is no 89 * range check performed, so if they are out of range, the 90 * returned color is undefined. 91 * @param red Red component [0..255] of the color 92 * @param green Green component [0..255] of the color 93 * @param blue Blue component [0..255] of the color 94 */ 95 @ColorInt rgb(int red, int green, int blue)96 public static int rgb(int red, int green, int blue) { 97 return (0xFF << 24) | (red << 16) | (green << 8) | blue; 98 } 99 100 /** 101 * Return a color-int from alpha, red, green, blue components. 102 * These component values should be [0..255], but there is no 103 * range check performed, so if they are out of range, the 104 * returned color is undefined. 105 * @param alpha Alpha component [0..255] of the color 106 * @param red Red component [0..255] of the color 107 * @param green Green component [0..255] of the color 108 * @param blue Blue component [0..255] of the color 109 */ 110 @ColorInt argb(int alpha, int red, int green, int blue)111 public static int argb(int alpha, int red, int green, int blue) { 112 return (alpha << 24) | (red << 16) | (green << 8) | blue; 113 } 114 115 /** 116 * Returns the relative luminance of a color. 117 * <p> 118 * Assumes sRGB encoding. Based on the formula for relative luminance 119 * defined in WCAG 2.0, W3C Recommendation 11 December 2008. 120 * 121 * @return a value between 0 (darkest black) and 1 (lightest white) 122 */ luminance(@olorInt int color)123 public static float luminance(@ColorInt int color) { 124 double red = Color.red(color) / 255.0; 125 red = red < 0.03928 ? red / 12.92 : Math.pow((red + 0.055) / 1.055, 2.4); 126 double green = Color.green(color) / 255.0; 127 green = green < 0.03928 ? green / 12.92 : Math.pow((green + 0.055) / 1.055, 2.4); 128 double blue = Color.blue(color) / 255.0; 129 blue = blue < 0.03928 ? blue / 12.92 : Math.pow((blue + 0.055) / 1.055, 2.4); 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 */ HSVToColor(@ize3) float hsv[])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 */ HSVToColor(int alpha, @Size(3) float hsv[])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 nativeRGBToHSV(int red, int greed, int blue, float hsv[])226 private static native void nativeRGBToHSV(int red, int greed, int blue, float hsv[]); nativeHSVToColor(int alpha, 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 getHtmlColor(String color)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 254 static { 255 sColorNameMap = new HashMap<String, Integer>(); 256 sColorNameMap.put("black", BLACK); 257 sColorNameMap.put("darkgray", DKGRAY); 258 sColorNameMap.put("gray", GRAY); 259 sColorNameMap.put("lightgray", LTGRAY); 260 sColorNameMap.put("white", WHITE); 261 sColorNameMap.put("red", RED); 262 sColorNameMap.put("green", GREEN); 263 sColorNameMap.put("blue", BLUE); 264 sColorNameMap.put("yellow", YELLOW); 265 sColorNameMap.put("cyan", CYAN); 266 sColorNameMap.put("magenta", MAGENTA); 267 sColorNameMap.put("aqua", 0xFF00FFFF); 268 sColorNameMap.put("fuchsia", 0xFFFF00FF); 269 sColorNameMap.put("darkgrey", DKGRAY); 270 sColorNameMap.put("grey", GRAY); 271 sColorNameMap.put("lightgrey", LTGRAY); 272 sColorNameMap.put("lime", 0xFF00FF00); 273 sColorNameMap.put("maroon", 0xFF800000); 274 sColorNameMap.put("navy", 0xFF000080); 275 sColorNameMap.put("olive", 0xFF808000); 276 sColorNameMap.put("purple", 0xFF800080); 277 sColorNameMap.put("silver", 0xFFC0C0C0); 278 sColorNameMap.put("teal", 0xFF008080); 279 280 } 281 } 282