1 24 package org.riotfamily.common.util; 25 26 import java.awt.Color ; 27 import java.util.HashMap ; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 30 31 35 public final class ColorUtils { 36 37 private static final Pattern RGB_PATTERN = Pattern.compile( 38 "rgb\\(\\s?(\\d{1,3})%?\\s?,\\s?(\\d{1,3})%?\\s?," + 39 "\\s?(\\d{1,3})(%?)\\s?\\)"); 40 41 private static HashMap namedColors = new HashMap (); 42 43 static { 44 namedColors.put("aliceblue", new Color (0xf0, 0xf8, 0xff)); 45 namedColors.put("antiquewhite", new Color (0xfa, 0xeb, 0xd7)); 46 namedColors.put("aqua", new Color (0x00, 0xff, 0xff)); 47 namedColors.put("aquamarine", new Color (0x7f, 0xff, 0xd4)); 48 namedColors.put("azure", new Color (0xf0, 0xff, 0xff)); 49 namedColors.put("beige", new Color (0xf5, 0xf5, 0xdc)); 50 namedColors.put("bisque", new Color (0xff, 0xe4, 0xc4)); 51 namedColors.put("black", new Color (0x00, 0x00, 0x00)); 52 namedColors.put("blanchedalmond", new Color (0xff, 0xeb, 0xcd)); 53 namedColors.put("blue", new Color (0x00, 0x00, 0xff)); 54 namedColors.put("blueviolet", new Color (0x8a, 0x2b, 0xe2)); 55 namedColors.put("brown", new Color (0xa5, 0x2a, 0x2a)); 56 namedColors.put("burlywood", new Color (0xde, 0xb8, 0x87)); 57 namedColors.put("cadetblue", new Color (0x5f, 0x9e, 0xa0)); 58 namedColors.put("chartreuse", new Color (0x7f, 0xff, 0x00)); 59 namedColors.put("chocolate", new Color (0xd2, 0x69, 0x1e)); 60 namedColors.put("coral", new Color (0xff, 0x7f, 0x50)); 61 namedColors.put("cornflowerblue", new Color (0x64, 0x95, 0xed)); 62 namedColors.put("cornsilk", new Color (0xff, 0xf8, 0xdc)); 63 namedColors.put("crimson", new Color (0xdc, 0x14, 0x3c)); 64 namedColors.put("cyan", new Color (0x00, 0xff, 0xff)); 65 namedColors.put("darkblue", new Color (0x00, 0x00, 0x8b)); 66 namedColors.put("darkcyan", new Color (0x00, 0x8b, 0x8b)); 67 namedColors.put("darkgoldenrod", new Color (0xb8, 0x86, 0x0b)); 68 namedColors.put("darkgray", new Color (0xa9, 0xa9, 0xa9)); 69 namedColors.put("darkgrey", new Color (0xa9, 0xa9, 0xa9)); 70 namedColors.put("darkgreen", new Color (0x00, 0x64, 0x00)); 71 namedColors.put("darkkhaki", new Color (0xbd, 0xb7, 0x6b)); 72 namedColors.put("darkmagenta", new Color (0x8b, 0x00, 0x8b)); 73 namedColors.put("darkolivegreen", new Color (0x55, 0x6b, 0x2f)); 74 namedColors.put("darkorange", new Color (0xff, 0x8c, 0x00)); 75 namedColors.put("darkorchid", new Color (0x99, 0x32, 0xcc)); 76 namedColors.put("darkred", new Color (0x8b, 0x00, 0x00)); 77 namedColors.put("darksalmon", new Color (0xe9, 0x96, 0x7a)); 78 namedColors.put("darkseagreen", new Color (0x8f, 0xbc, 0x8f)); 79 namedColors.put("darkslateblue", new Color (0x48, 0x3d, 0x8b)); 80 namedColors.put("darkslategray", new Color (0x2f, 0x4f, 0x4f)); 81 namedColors.put("darkslategrey", new Color (0x2f, 0x4f, 0x4f)); 82 namedColors.put("darkturquoise", new Color (0x00, 0xce, 0xd1)); 83 namedColors.put("darkviolet", new Color (0x94, 0x00, 0xd3)); 84 namedColors.put("deeppink", new Color (0xff, 0x14, 0x93)); 85 namedColors.put("deepskyblue", new Color (0x00, 0xbf, 0xff)); 86 namedColors.put("dimgray", new Color (0x69, 0x69, 0x69)); 87 namedColors.put("dimgrey", new Color (0x69, 0x69, 0x69)); 88 namedColors.put("dodgerblue", new Color (0x1e, 0x90, 0xff)); 89 namedColors.put("firebrick", new Color (0xb2, 0x22, 0x22)); 90 namedColors.put("floralwhite", new Color (0xff, 0xfa, 0xf0)); 91 namedColors.put("forestgreen", new Color (0x22, 0x8b, 0x22)); 92 namedColors.put("fuchsia", new Color (0xff, 0x00, 0xff)); 93 namedColors.put("gainsboro", new Color (0xdc, 0xdc, 0xdc)); 94 namedColors.put("ghostwhite", new Color (0xf8, 0xf8, 0xff)); 95 namedColors.put("gold", new Color (0xff, 0xd7, 0x00)); 96 namedColors.put("goldenrod", new Color (0xda, 0xa5, 0x20)); 97 namedColors.put("gray", new Color (0x80, 0x80, 0x80)); 98 namedColors.put("grey", new Color (0x80, 0x80, 0x80)); 99 namedColors.put("green", new Color (0x00, 0x80, 0x00)); 100 namedColors.put("greenyellow", new Color (0xad, 0xff, 0x2f)); 101 namedColors.put("honeydew", new Color (0xf0, 0xff, 0xf0)); 102 namedColors.put("hotpink", new Color (0xff, 0x69, 0xb4)); 103 namedColors.put("indianred", new Color (0xcd, 0x5c, 0x5c)); 104 namedColors.put("indigo", new Color (0x4b, 0x00, 0x82)); 105 namedColors.put("ivory", new Color (0xff, 0xff, 0xf0)); 106 namedColors.put("khaki", new Color (0xf0, 0xe6, 0x8c)); 107 namedColors.put("lavender", new Color (0xe6, 0xe6, 0xfa)); 108 namedColors.put("lavenderblush", new Color (0xff, 0xf0, 0xf5)); 109 namedColors.put("lawngreen", new Color (0x7c, 0xfc, 0x00)); 110 namedColors.put("lemonchiffon", new Color (0xff, 0xfa, 0xcd)); 111 namedColors.put("lightblue", new Color (0xad, 0xd8, 0xe6)); 112 namedColors.put("lightcoral", new Color (0xf0, 0x80, 0x80)); 113 namedColors.put("lightcyan", new Color (0xe0, 0xff, 0xff)); 114 namedColors.put("lightgoldenrodyellow", new Color (0xfa, 0xfa, 0xd2)); 115 namedColors.put("lightgray", new Color (0xd3, 0xd3, 0xd3)); 116 namedColors.put("lightgrey", new Color (0xd3, 0xd3, 0xd3)); 117 namedColors.put("lightgreen", new Color (0x90, 0xee, 0x90)); 118 namedColors.put("lightpink", new Color (0xff, 0xb6, 0xc1)); 119 namedColors.put("lightsalmon", new Color (0xff, 0xa0, 0x7a)); 120 namedColors.put("lightseagreen", new Color (0x20, 0xb2, 0xaa)); 121 namedColors.put("lightskyblue", new Color (0x87, 0xce, 0xfa)); 122 namedColors.put("lightslategray", new Color (0x77, 0x88, 0x99)); 123 namedColors.put("lightslategrey", new Color (0x77, 0x88, 0x99)); 124 namedColors.put("lightsteelblue", new Color (0xb0, 0xc4, 0xde)); 125 namedColors.put("lightyellow", new Color (0xff, 0xff, 0xe0)); 126 namedColors.put("lime", new Color (0x00, 0xff, 0x00)); 127 namedColors.put("limegreen", new Color (0x32, 0xcd, 0x32)); 128 namedColors.put("linen", new Color (0xfa, 0xf0, 0xe6)); 129 namedColors.put("magenta", new Color (0xff, 0x00, 0xff)); 130 namedColors.put("maroon", new Color (0x80, 0x00, 0x00)); 131 namedColors.put("mediumaquamarine", new Color (0x66, 0xcd, 0xaa)); 132 namedColors.put("mediumblue", new Color (0x00, 0x00, 0xcd)); 133 namedColors.put("mediumorchid", new Color (0xba, 0x55, 0xd3)); 134 namedColors.put("mediumpurple", new Color (0x93, 0x70, 0xd8)); 135 namedColors.put("mediumseagreen", new Color (0x3c, 0xb3, 0x71)); 136 namedColors.put("mediumslateblue", new Color (0x7b, 0x68, 0xee)); 137 namedColors.put("mediumspringgreen", new Color (0x00, 0xfa, 0x9a)); 138 namedColors.put("mediumturquoise", new Color (0x48, 0xd1, 0xcc)); 139 namedColors.put("mediumvioletred", new Color (0xc7, 0x15, 0x85)); 140 namedColors.put("midnightblue", new Color (0x19, 0x19, 0x70)); 141 namedColors.put("mintcream", new Color (0xf5, 0xff, 0xfa)); 142 namedColors.put("mistyrose", new Color (0xff, 0xe4, 0xe1)); 143 namedColors.put("moccasin", new Color (0xff, 0xe4, 0xb5)); 144 namedColors.put("navajowhite", new Color (0xff, 0xde, 0xad)); 145 namedColors.put("navy", new Color (0x00, 0x00, 0x80)); 146 namedColors.put("oldlace", new Color (0xfd, 0xf5, 0xe6)); 147 namedColors.put("olive", new Color (0x80, 0x80, 0x00)); 148 namedColors.put("olivedrab", new Color (0x6b, 0x8e, 0x23)); 149 namedColors.put("orange", new Color (0xff, 0xa5, 0x00)); 150 namedColors.put("orangered", new Color (0xff, 0x45, 0x00)); 151 namedColors.put("orchid", new Color (0xda, 0x70, 0xd6)); 152 namedColors.put("palegoldenrod", new Color (0xee, 0xe8, 0xaa)); 153 namedColors.put("palegreen", new Color (0x98, 0xfb, 0x98)); 154 namedColors.put("paleturquoise", new Color (0xaf, 0xee, 0xee)); 155 namedColors.put("palevioletred", new Color (0xd8, 0x70, 0x93)); 156 namedColors.put("papayawhip", new Color (0xff, 0xef, 0xd5)); 157 namedColors.put("peachpuff", new Color (0xff, 0xda, 0xb9)); 158 namedColors.put("peru", new Color (0xcd, 0x85, 0x3f)); 159 namedColors.put("pink", new Color (0xff, 0xc0, 0xcb)); 160 namedColors.put("plum", new Color (0xdd, 0xa0, 0xdd)); 161 namedColors.put("powderblue", new Color (0xb0, 0xe0, 0xe6)); 162 namedColors.put("purple", new Color (0x80, 0x00, 0x80)); 163 namedColors.put("red", new Color (0xff, 0x00, 0x00)); 164 namedColors.put("rosybrown", new Color (0xbc, 0x8f, 0x8f)); 165 namedColors.put("royalblue", new Color (0x41, 0x69, 0xe1)); 166 namedColors.put("saddlebrown", new Color (0x8b, 0x45, 0x13)); 167 namedColors.put("salmon", new Color (0xfa, 0x80, 0x72)); 168 namedColors.put("sandybrown", new Color (0xf4, 0xa4, 0x60)); 169 namedColors.put("seagreen", new Color (0x2e, 0x8b, 0x57)); 170 namedColors.put("seashell", new Color (0xff, 0xf5, 0xee)); 171 namedColors.put("sienna", new Color (0xa0, 0x52, 0x2d)); 172 namedColors.put("silver", new Color (0xc0, 0xc0, 0xc0)); 173 namedColors.put("skyblue", new Color (0x87, 0xce, 0xeb)); 174 namedColors.put("slateblue", new Color (0x6a, 0x5a, 0xcd)); 175 namedColors.put("slategray", new Color (0x70, 0x80, 0x90)); 176 namedColors.put("slategrey", new Color (0x70, 0x80, 0x90)); 177 namedColors.put("snow", new Color (0xff, 0xfa, 0xfa)); 178 namedColors.put("springgreen", new Color (0x00, 0xff, 0x7f)); 179 namedColors.put("steelblue", new Color (0x46, 0x82, 0xb4)); 180 namedColors.put("tan", new Color (0xd2, 0xb4, 0x8c)); 181 namedColors.put("teal", new Color (0x00, 0x80, 0x80)); 182 namedColors.put("thistle", new Color (0xd8, 0xbf, 0xd8)); 183 namedColors.put("tomato", new Color (0xff, 0x63, 0x47)); 184 namedColors.put("turquoise", new Color (0x40, 0xe0, 0xd0)); 185 namedColors.put("violet", new Color (0xee, 0x82, 0xee)); 186 namedColors.put("wheat", new Color (0xf5, 0xde, 0xb3)); 187 namedColors.put("white", new Color (0xff, 0xff, 0xff)); 188 namedColors.put("whitesmoke", new Color (0xf5, 0xf5, 0xf5)); 189 namedColors.put("yellow", new Color (0xff, 0xff, 0x00)); 190 namedColors.put("yellowgreen", new Color (0x9a, 0xcd, 0x32)); 191 } 192 193 204 public static Color parseColor(String s) throws IllegalArgumentException { 205 int r, g, b; 206 if (s.startsWith("#")) { 207 if (s.length() == 4) { 208 r = Integer.parseInt(s.substring(1, 2) + s.charAt(1), 16); 209 g = Integer.parseInt(s.substring(2, 3) + s.charAt(2), 16); 210 b = Integer.parseInt(s.substring(3) + s.charAt(3), 16); 211 return new Color (r, g, b); 212 } 213 if (s.length() == 7) { 214 r = Integer.parseInt(s.substring(1, 3), 16); 215 g = Integer.parseInt(s.substring(3, 5), 16); 216 b = Integer.parseInt(s.substring(5), 16); 217 return new Color (r, g, b); 218 } 219 } 220 else { 221 Matcher m = RGB_PATTERN.matcher(s); 222 if (m.matches()) { 223 r = Integer.parseInt(m.group(1)); 224 g = Integer.parseInt(m.group(2)); 225 b = Integer.parseInt(m.group(3)); 226 if ("%".equals(m.group(4))) { 227 r = 255 * (r / 100); 228 g = 255 * (g / 100); 229 b = 255 * (b / 100); 230 } 231 return new Color (r, g, b); 232 } 233 else { 234 Color color = (Color ) namedColors.get(s.toLowerCase()); 235 if (color != null) { 236 return color; 237 } 238 } 239 } 240 throw new IllegalArgumentException ("Invalid color format."); 241 } 242 243 248 public static Color brightness(Color c, float scale) { 249 int r = c.getRed(); 250 int g = c.getGreen(); 251 int b = c.getBlue(); 252 if (r == 0 && g == 0 && b == 0 && scale > 0) { 253 r = g = b = Math.min(255, (int) (255 * scale)); 254 } 255 else { 256 r = Math.max(0, (int) (r * scale)); 257 g = Math.max(0, (int) (g * scale)); 258 b = Math.max(0, (int) (b * scale)); 259 if (r > 255 || g > 255 || b > 255) { 260 return c; 261 } 262 } 263 return new Color (r, g, b); 264 } 265 266 271 public static Color saturation(Color c, float scale) { 272 int r = c.getRed(); 273 int g = c.getGreen(); 274 int b = c.getBlue(); 275 float[] hsb = Color.RGBtoHSB(r, g, b, null); 276 float s = hsb[1] * scale; 277 if (s < 0) { 278 s = 0; 279 }; 280 if (s > 1) { 281 s = 1; 282 }; 283 return Color.getHSBColor(hsb[0], s, hsb[2]); 284 } 285 286 public static String toHex(Color color) { 287 String r = Integer.toHexString(color.getRed()); 288 String g = Integer.toHexString(color.getGreen()); 289 String b = Integer.toHexString(color.getBlue()); 290 if (r.length() == 1) { 291 r = "0" + r; 292 } 293 if (g.length() == 1) { 294 g = "0" + g; 295 } 296 if (b.length() == 1) { 297 b = "0" + b; 298 } 299 return ("#" + r + g + b).toUpperCase(); 300 } 301 } 302 303 | Popular Tags |