3 import java.util.Hashtable;
6 import javajs.api.GenericColor;
15 public static String toRGBHexString(GenericColor c) {
19 String r = "00" + Integer.toHexString((rgb >> 16) & 0xFF);
20 r = r.substring(r.length() - 2);
21 String g = "00" + Integer.toHexString((rgb >> 8) & 0xFF);
22 g = g.substring(g.length() - 2);
23 String b = "00" + Integer.toHexString(rgb & 0xFF);
24 b = b.substring(b.length() - 2);
28 public static String toCSSString(GenericColor c) {
29 int opacity = c.getOpacity255();
31 return "#" + toRGBHexString(c);
33 return "rgba(" + ((rgb>>16)&0xFF) + "," + ((rgb>>8)&0xff) + "," + (rgb&0xff) + "," + opacity/255f + ")";
36 private final static String[] colorNames = {
47 "pedarkgreen", // 00c000
49 "pelightblue", // b0b0ff
50 "pedarkcyan", // 00a0a0
51 "pedarkgray", // 606060
53 "aliceblue", // F0F8FF
54 "antiquewhite", // FAEBD7
56 "aquamarine", // 7FFFD4
60 "blanchedalmond", // FFEBCD
62 "blueviolet", // 8A2BE2
64 "burlywood", // DEB887
65 "cadetblue", // 5F9EA0
66 "chartreuse", // 7FFF00
67 "chocolate", // D2691E
69 "cornflowerblue", // 6495ED
75 "darkgoldenrod", // B8860B
77 "darkgreen", // 006400
78 "darkkhaki", // BDB76B
79 "darkmagenta", // 8B008B
80 "darkolivegreen", // 556B2F
81 "darkorange", // FF8C00
82 "darkorchid", // 9932CC
84 "darksalmon", // E9967A
85 "darkseagreen", // 8FBC8F
86 "darkslateblue", // 483D8B
87 "darkslategray", // 2F4F4F
88 "darkturquoise", // 00CED1
89 "darkviolet", // 9400D3
91 "deepskyblue", // 00BFFF
93 "dodgerblue", // 1E90FF
94 "firebrick", // B22222
95 "floralwhite", // FFFAF0 16775920
96 "forestgreen", // 228B22
98 "gainsboro", // DCDCDC
99 "ghostwhite", // F8F8FF
101 "goldenrod", // DAA520
104 "greenyellow", // ADFF2F
105 "honeydew", // F0FFF0
107 "indianred", // CD5C5C
111 "lavender", // E6E6FA
112 "lavenderblush", // FFF0F5
113 "lawngreen", // 7CFC00
114 "lemonchiffon", // FFFACD
115 "lightblue", // ADD8E6
116 "lightcoral", // F08080
117 "lightcyan", // E0FFFF
118 "lightgoldenrodyellow", // FAFAD2
119 "lightgreen", // 90EE90
120 "lightgrey", // D3D3D3
121 "lightgray", // D3D3D3
122 "lightpink", // FFB6C1
123 "lightsalmon", // FFA07A
124 "lightseagreen", // 20B2AA
125 "lightskyblue", // 87CEFA
126 "lightslategray", // 778899
127 "lightsteelblue", // B0C4DE
128 "lightyellow", // FFFFE0
130 "limegreen", // 32CD32
134 "mediumaquamarine", // 66CDAA
135 "mediumblue", // 0000CD
136 "mediumorchid", // BA55D3
137 "mediumpurple", // 9370DB
138 "mediumseagreen", // 3CB371
139 "mediumslateblue", // 7B68EE
140 "mediumspringgreen", // 00FA9A
141 "mediumturquoise", // 48D1CC
142 "mediumvioletred", // C71585
143 "midnightblue", // 191970
144 "mintcream", // F5FFFA
145 "mistyrose", // FFE4E1
146 "moccasin", // FFE4B5
147 "navajowhite", // FFDEAD
151 "olivedrab", // 6B8E23
153 "orangered", // FF4500
155 "palegoldenrod", // EEE8AA
156 "palegreen", // 98FB98
157 "paleturquoise", // AFEEEE
158 "palevioletred", // DB7093
159 "papayawhip", // FFEFD5
160 "peachpuff", // FFDAB9
164 "powderblue", // B0E0E6
167 "rosybrown", // BC8F8F
168 "royalblue", // 4169E1
169 "saddlebrown", // 8B4513
171 "sandybrown", // F4A460
172 "seagreen", // 2E8B57
173 "seashell", // FFF5EE
177 "slateblue", // 6A5ACD
178 "slategray", // 708090
179 "snow", // FFFAFA 16775930
180 "springgreen", // 00FF7F
181 "steelblue", // 4682B4
186 "turquoise", // 40E0D0
189 "white", // FFFFFF 16777215
190 "whitesmoke", // F5F5F5
192 "yellowgreen", // 9ACD32
193 // plus a few rasmol names/values
194 "bluetint", // AFD7FF
195 "greenblue", // 2E8B57
196 "greentint", // 98FFB3
199 "pinktint", // FFABBB
200 "redorange", // FF4500
201 "yellowtint", // F6F675
204 private final static int[] colorArgbs = {
207 // plus the PE chain colors
208 0xFFffffff, // pewhite
209 0xFF00ffff, // pecyan
210 0xFFd020ff, // pepurple
211 0xFF00ff00, // pegreen
212 0xFF6060ff, // peblue
213 0xFFff80c0, // peviolet
214 0xFFa42028, // pebrown
215 0xFFffd8d8, // pepink
216 0xFFffff00, // peyellow
217 0xFF00c000, // pedarkgreen
218 0xFFffb000, // peorange
219 0xFFb0b0ff, // pelightblue
220 0xFF00a0a0, // pedarkcyan
221 0xFF606060, // pedarkgray
222 // standard JavaScript
223 0xFFF0F8FF, // aliceblue
224 0xFFFAEBD7, // antiquewhite
226 0xFF7FFFD4, // aquamarine
229 0xFFFFE4C4, // bisque
230 0xFFFFEBCD, // blanchedalmond
232 0xFF8A2BE2, // blueviolet
234 0xFFDEB887, // burlywood
235 0xFF5F9EA0, // cadetblue
236 0xFF7FFF00, // chartreuse
237 0xFFD2691E, // chocolate
239 0xFF6495ED, // cornflowerblue
240 0xFFFFF8DC, // cornsilk
241 0xFFDC143C, // crimson
243 0xFF00008B, // darkblue
244 0xFF008B8B, // darkcyan
245 0xFFB8860B, // darkgoldenrod
246 0xFFA9A9A9, // darkgray
247 0xFF006400, // darkgreen
249 0xFFBDB76B, // darkkhaki
250 0xFF8B008B, // darkmagenta
251 0xFF556B2F, // darkolivegreen
252 0xFFFF8C00, // darkorange
253 0xFF9932CC, // darkorchid
254 0xFF8B0000, // darkred
255 0xFFE9967A, // darksalmon
256 0xFF8FBC8F, // darkseagreen
257 0xFF483D8B, // darkslateblue
258 0xFF2F4F4F, // darkslategray
259 0xFF00CED1, // darkturquoise
260 0xFF9400D3, // darkviolet
261 0xFFFF1493, // deeppink
262 0xFF00BFFF, // deepskyblue
263 0xFF696969, // dimgray
264 0xFF1E90FF, // dodgerblue
265 0xFFB22222, // firebrick
266 0xFFFFFAF0, // floralwhite
267 0xFF228B22, // forestgreen
268 0xFFFF00FF, // fuchsia
269 0xFFDCDCDC, // gainsboro
270 0xFFF8F8FF, // ghostwhite
272 0xFFDAA520, // goldenrod
275 0xFFADFF2F, // greenyellow
276 0xFFF0FFF0, // honeydew
277 0xFFFF69B4, // hotpink
278 0xFFCD5C5C, // indianred
279 0xFF4B0082, // indigo
282 0xFFE6E6FA, // lavender
283 0xFFFFF0F5, // lavenderblush
284 0xFF7CFC00, // lawngreen
285 0xFFFFFACD, // lemonchiffon
286 0xFFADD8E6, // lightblue
287 0xFFF08080, // lightcoral
288 0xFFE0FFFF, // lightcyan
289 0xFFFAFAD2, // lightgoldenrodyellow
290 0xFF90EE90, // lightgreen
291 0xFFD3D3D3, // lightgrey
292 0xFFD3D3D3, // lightgray
293 0xFFFFB6C1, // lightpink
294 0xFFFFA07A, // lightsalmon
295 0xFF20B2AA, // lightseagreen
296 0xFF87CEFA, // lightskyblue
297 0xFF778899, // lightslategray
298 0xFFB0C4DE, // lightsteelblue
299 0xFFFFFFE0, // lightyellow
301 0xFF32CD32, // limegreen
303 0xFFFF00FF, // magenta
304 0xFF800000, // maroon
305 0xFF66CDAA, // mediumaquamarine
306 0xFF0000CD, // mediumblue
307 0xFFBA55D3, // mediumorchid
308 0xFF9370DB, // mediumpurple
309 0xFF3CB371, // mediumseagreen
310 0xFF7B68EE, // mediumslateblue
311 0xFF00FA9A, // mediumspringgreen
312 0xFF48D1CC, // mediumturquoise
313 0xFFC71585, // mediumvioletred
314 0xFF191970, // midnightblue
315 0xFFF5FFFA, // mintcream
316 0xFFFFE4E1, // mistyrose
317 0xFFFFE4B5, // moccasin
318 0xFFFFDEAD, // navajowhite
320 0xFFFDF5E6, // oldlace
322 0xFF6B8E23, // olivedrab
323 0xFFFFA500, // orange
324 0xFFFF4500, // orangered
325 0xFFDA70D6, // orchid
326 0xFFEEE8AA, // palegoldenrod
327 0xFF98FB98, // palegreen
328 0xFFAFEEEE, // paleturquoise
329 0xFFDB7093, // palevioletred
330 0xFFFFEFD5, // papayawhip
331 0xFFFFDAB9, // peachpuff
335 0xFFB0E0E6, // powderblue
336 0xFF800080, // purple
338 0xFFBC8F8F, // rosybrown
339 0xFF4169E1, // royalblue
340 0xFF8B4513, // saddlebrown
341 0xFFFA8072, // salmon
342 0xFFF4A460, // sandybrown
343 0xFF2E8B57, // seagreen
344 0xFFFFF5EE, // seashell
345 0xFFA0522D, // sienna
346 0xFFC0C0C0, // silver
347 0xFF87CEEB, // skyblue
348 0xFF6A5ACD, // slateblue
349 0xFF708090, // slategray
351 0xFF00FF7F, // springgreen
352 0xFF4682B4, // steelblue
355 0xFFD8BFD8, // thistle
356 0xFFFF6347, // tomato
357 0xFF40E0D0, // turquoise
358 0xFFEE82EE, // violet
361 0xFFF5F5F5, // whitesmoke
362 0xFFFFFF00, // yellow
363 0xFF9ACD32, // yellowgreen
364 // plus a few rasmol names/values
365 0xFFAFD7FF, // bluetint
366 0xFF2E8B57, // greenblue
367 0xFF98FFB3, // greentint
370 0xFFFFABBB, // pinktint
371 0xFFFF4500, // redorange
372 0xFFF6F675, // yellowtint
375 private static final Map<String, Integer> mapJavaScriptColors = new Hashtable<String, Integer>();
378 for (int i = colorNames.length; --i >= 0; )
379 mapJavaScriptColors.put(colorNames[i], Integer.valueOf(colorArgbs[i]));
383 * accepts [xRRGGBB] or [0xRRGGBB] or [0xFFRRGGBB] or #RRGGBB or
384 * [red,green,blue] or a valid JavaScript color
387 * @return 0 if invalid or integer color
389 public static int getArgbFromString(String strColor) {
391 if (strColor == null || (len = strColor.length()) == 0)
393 strColor = strColor.toLowerCase();
394 if (strColor.charAt(0) == '[' && strColor.charAt(len - 1) == ']') {
396 if (strColor.indexOf(",") >= 0) {
397 String[] tokens = PT.split(strColor.substring(1, strColor
398 .length() - 1), ",");
399 if (tokens.length != 3)
401 float red = PT.parseFloat(tokens[0]);
402 float grn = PT.parseFloat(tokens[1]);
403 float blu = PT.parseFloat(tokens[2]);
404 return colorTriadToFFRGB(red, grn, blu);
416 if (strColor.indexOf(check) != 1)
418 strColor = "#" + strColor.substring(len - 7, len - 1);
421 if (len == 7 && strColor.charAt(0) == '#') {
423 return PT.parseIntRadix(strColor.substring(1, 7), 16) | 0xFF000000;
424 } catch (Exception e) {
428 Integer boxedArgb = mapJavaScriptColors.get(strColor);
429 return (boxedArgb == null ? 0 : boxedArgb.intValue());
432 public static int colorTriadToFFRGB(float x, float y, float z) {
433 if (x <= 1 && y <= 1 && z <= 1) {
441 return rgb((int) x, (int) y, (int) z);
444 public static int rgb(int red, int grn, int blu) {
445 return 0xFF000000 | (red << 16) | (grn << 8) | blu;
448 public final static P3 colorPtFromString(String colorName) {
449 return colorPtFromInt(getArgbFromString(colorName), null);
452 public final static P3 colorPtFromInt(int color, P3 pt) {
455 pt.set((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF);
459 public static int colorPtToFFRGB(T3 pt) {
460 return colorTriadToFFRGB(pt.x, pt.y, pt.z);
463 public static void toRGB3f(int c, float[] f) {
464 f[0] = ((c >> 16) & 0xFF) / 255f; // red
465 f[1] = ((c >> 8) & 0xFF) / 255f;
466 f[2] = (c & 0xFF) / 255f;
470 * Return a greyscale rgb value 0-FF using NTSC color lightness algorithm
472 * the alpha component is set to 0xFF. If you want a value in the
473 * range 0-255 then & the result with 0xFF;
475 * @param rgb the rgb value
476 * @return a grayscale value in the range 0 - 255 decimal
478 public static int toFFGGGfromRGB(int rgb) {
479 int grey = (((2989 * ((rgb >> 16) & 0xFF)) +
480 (5870 * ((rgb >> 8) & 0xFF)) +
481 (1140 * (rgb & 0xFF)) + 5000) / 10000) & 0xFFFFFF;
482 return rgb(grey, grey, grey);
487 * Convert RGB values to HSL (hue/saturation/lightness)
492 * set to false when just using this for
493 * for RGB -- HSL -- HSL' -- RGB' conversion
495 * @return the HSL as P3 range 360 100 100
499 public static P3 rgbToHSL(P3 rgb, boolean doRound) {
500 // adapted from http://tips4java.wordpress.com/2009/07/05/hsl-color/
501 // see http://en.wikipedia.org/wiki/HSL_color_space
502 float r = rgb.x / 255;
503 float g = rgb.y / 255;
504 float b = rgb.z / 255;
505 float min = Math.min(r, Math.min(g, b));
506 float max = Math.max(r, Math.max(g, b));
508 // lightness is just p * 50
510 float p = (max + min);
511 float q = (max - min);
513 float h = (60 * ((q == 0 ? 0 : max == r ? ((g - b) / q + 6)
514 : max == g ? (b - r) / q + 2 : (r - g) / q + 4))) % 360;
516 float s = q / (q == 0 ? 1 : p <= 1 ? p : 2 - p);
518 // we round to tenths for HSL so that we can return enough
519 // precision to get back 1-255 in RGB
520 return (doRound ? P3.new3(Math.round(h*10)/10f, Math.round(s * 1000)/10f,
521 Math.round(p * 500)/10f) : P3.new3(h, s * 100, p * 50));
525 * Convert HSL (hue/saturation/luninance) values to RGB
527 * @param hsl in the range 360, 100, 100
528 * @return the RGB as P3 range 0 to 255
531 public static P3 hslToRGB(P3 hsl) {
532 // adapted from http://tips4java.wordpress.com/2009/07/05/hsl-color/
533 // see http://en.wikipedia.org/wiki/HSL_color_space
537 float h = Math.max(0, Math.min(360, hsl.x)) / 60;
538 float s = Math.max(0, Math.min(100, hsl.y)) / 100;
539 float l = Math.max(0, Math.min(100, hsl.z)) / 100;
541 float p = l - (l < 0.5 ? l : 1 - l) * s;
542 float q = 2 * (l - p);
544 float r = toRGB(p, q, h + 2);
545 float g = toRGB(p, q, h);
546 float b = toRGB(p, q, h - 2);
547 return P3.new3(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255));
550 private static float toRGB(float p, float q, float h) {
551 return ((h = (h + (h < 0 ? 6 : h > 6 ? -6 : 0))) < 1 ? p + q * h
552 : h < 3 ? p + q : h < 4 ? p + q * (4 - h) : p);