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 "lightpink", // FFB6C1
122 "lightsalmon", // FFA07A
123 "lightseagreen", // 20B2AA
124 "lightskyblue", // 87CEFA
125 "lightslategray", // 778899
126 "lightsteelblue", // B0C4DE
127 "lightyellow", // FFFFE0
129 "limegreen", // 32CD32
133 "mediumaquamarine", // 66CDAA
134 "mediumblue", // 0000CD
135 "mediumorchid", // BA55D3
136 "mediumpurple", // 9370DB
137 "mediumseagreen", // 3CB371
138 "mediumslateblue", // 7B68EE
139 "mediumspringgreen", // 00FA9A
140 "mediumturquoise", // 48D1CC
141 "mediumvioletred", // C71585
142 "midnightblue", // 191970
143 "mintcream", // F5FFFA
144 "mistyrose", // FFE4E1
145 "moccasin", // FFE4B5
146 "navajowhite", // FFDEAD
150 "olivedrab", // 6B8E23
152 "orangered", // FF4500
154 "palegoldenrod", // EEE8AA
155 "palegreen", // 98FB98
156 "paleturquoise", // AFEEEE
157 "palevioletred", // DB7093
158 "papayawhip", // FFEFD5
159 "peachpuff", // FFDAB9
163 "powderblue", // B0E0E6
166 "rosybrown", // BC8F8F
167 "royalblue", // 4169E1
168 "saddlebrown", // 8B4513
170 "sandybrown", // F4A460
171 "seagreen", // 2E8B57
172 "seashell", // FFF5EE
176 "slateblue", // 6A5ACD
177 "slategray", // 708090
178 "snow", // FFFAFA 16775930
179 "springgreen", // 00FF7F
180 "steelblue", // 4682B4
185 "turquoise", // 40E0D0
188 "white", // FFFFFF 16777215
189 "whitesmoke", // F5F5F5
191 "yellowgreen", // 9ACD32
192 // plus a few rasmol names/values
193 "bluetint", // AFD7FF
194 "greenblue", // 2E8B57
195 "greentint", // 98FFB3
197 "pinktint", // FFABBB
198 "redorange", // FF4500
199 "yellowtint", // F6F675
202 private final static int[] colorArgbs = {
205 // plus the PE chain colors
206 0xFFffffff, // pewhite
207 0xFF00ffff, // pecyan
208 0xFFd020ff, // pepurple
209 0xFF00ff00, // pegreen
210 0xFF6060ff, // peblue
211 0xFFff80c0, // peviolet
212 0xFFa42028, // pebrown
213 0xFFffd8d8, // pepink
214 0xFFffff00, // peyellow
215 0xFF00c000, // pedarkgreen
216 0xFFffb000, // peorange
217 0xFFb0b0ff, // pelightblue
218 0xFF00a0a0, // pedarkcyan
219 0xFF606060, // pedarkgray
220 // standard JavaScript
221 0xFFF0F8FF, // aliceblue
222 0xFFFAEBD7, // antiquewhite
224 0xFF7FFFD4, // aquamarine
227 0xFFFFE4C4, // bisque
228 0xFFFFEBCD, // blanchedalmond
230 0xFF8A2BE2, // blueviolet
232 0xFFDEB887, // burlywood
233 0xFF5F9EA0, // cadetblue
234 0xFF7FFF00, // chartreuse
235 0xFFD2691E, // chocolate
237 0xFF6495ED, // cornflowerblue
238 0xFFFFF8DC, // cornsilk
239 0xFFDC143C, // crimson
241 0xFF00008B, // darkblue
242 0xFF008B8B, // darkcyan
243 0xFFB8860B, // darkgoldenrod
244 0xFFA9A9A9, // darkgray
245 0xFF006400, // darkgreen
247 0xFFBDB76B, // darkkhaki
248 0xFF8B008B, // darkmagenta
249 0xFF556B2F, // darkolivegreen
250 0xFFFF8C00, // darkorange
251 0xFF9932CC, // darkorchid
252 0xFF8B0000, // darkred
253 0xFFE9967A, // darksalmon
254 0xFF8FBC8F, // darkseagreen
255 0xFF483D8B, // darkslateblue
256 0xFF2F4F4F, // darkslategray
257 0xFF00CED1, // darkturquoise
258 0xFF9400D3, // darkviolet
259 0xFFFF1493, // deeppink
260 0xFF00BFFF, // deepskyblue
261 0xFF696969, // dimgray
262 0xFF1E90FF, // dodgerblue
263 0xFFB22222, // firebrick
264 0xFFFFFAF0, // floralwhite
265 0xFF228B22, // forestgreen
266 0xFFFF00FF, // fuchsia
267 0xFFDCDCDC, // gainsboro
268 0xFFF8F8FF, // ghostwhite
270 0xFFDAA520, // goldenrod
273 0xFFADFF2F, // greenyellow
274 0xFFF0FFF0, // honeydew
275 0xFFFF69B4, // hotpink
276 0xFFCD5C5C, // indianred
277 0xFF4B0082, // indigo
280 0xFFE6E6FA, // lavender
281 0xFFFFF0F5, // lavenderblush
282 0xFF7CFC00, // lawngreen
283 0xFFFFFACD, // lemonchiffon
284 0xFFADD8E6, // lightblue
285 0xFFF08080, // lightcoral
286 0xFFE0FFFF, // lightcyan
287 0xFFFAFAD2, // lightgoldenrodyellow
288 0xFF90EE90, // lightgreen
289 0xFFD3D3D3, // lightgrey
290 0xFFFFB6C1, // lightpink
291 0xFFFFA07A, // lightsalmon
292 0xFF20B2AA, // lightseagreen
293 0xFF87CEFA, // lightskyblue
294 0xFF778899, // lightslategray
295 0xFFB0C4DE, // lightsteelblue
296 0xFFFFFFE0, // lightyellow
298 0xFF32CD32, // limegreen
300 0xFFFF00FF, // magenta
301 0xFF800000, // maroon
302 0xFF66CDAA, // mediumaquamarine
303 0xFF0000CD, // mediumblue
304 0xFFBA55D3, // mediumorchid
305 0xFF9370DB, // mediumpurple
306 0xFF3CB371, // mediumseagreen
307 0xFF7B68EE, // mediumslateblue
308 0xFF00FA9A, // mediumspringgreen
309 0xFF48D1CC, // mediumturquoise
310 0xFFC71585, // mediumvioletred
311 0xFF191970, // midnightblue
312 0xFFF5FFFA, // mintcream
313 0xFFFFE4E1, // mistyrose
314 0xFFFFE4B5, // moccasin
315 0xFFFFDEAD, // navajowhite
317 0xFFFDF5E6, // oldlace
319 0xFF6B8E23, // olivedrab
320 0xFFFFA500, // orange
321 0xFFFF4500, // orangered
322 0xFFDA70D6, // orchid
323 0xFFEEE8AA, // palegoldenrod
324 0xFF98FB98, // palegreen
325 0xFFAFEEEE, // paleturquoise
326 0xFFDB7093, // palevioletred
327 0xFFFFEFD5, // papayawhip
328 0xFFFFDAB9, // peachpuff
332 0xFFB0E0E6, // powderblue
333 0xFF800080, // purple
335 0xFFBC8F8F, // rosybrown
336 0xFF4169E1, // royalblue
337 0xFF8B4513, // saddlebrown
338 0xFFFA8072, // salmon
339 0xFFF4A460, // sandybrown
340 0xFF2E8B57, // seagreen
341 0xFFFFF5EE, // seashell
342 0xFFA0522D, // sienna
343 0xFFC0C0C0, // silver
344 0xFF87CEEB, // skyblue
345 0xFF6A5ACD, // slateblue
346 0xFF708090, // slategray
348 0xFF00FF7F, // springgreen
349 0xFF4682B4, // steelblue
352 0xFFD8BFD8, // thistle
353 0xFFFF6347, // tomato
354 0xFF40E0D0, // turquoise
355 0xFFEE82EE, // violet
358 0xFFF5F5F5, // whitesmoke
359 0xFFFFFF00, // yellow
360 0xFF9ACD32, // yellowgreen
361 // plus a few rasmol names/values
362 0xFFAFD7FF, // bluetint
363 0xFF2E8B57, // greenblue
364 0xFF98FFB3, // greentint
366 0xFFFFABBB, // pinktint
367 0xFFFF4500, // redorange
368 0xFFF6F675, // yellowtint
371 private static final Map<String, Integer> mapJavaScriptColors = new Hashtable<String, Integer>();
374 for (int i = colorNames.length; --i >= 0; )
375 mapJavaScriptColors.put(colorNames[i], Integer.valueOf(colorArgbs[i]));
379 * accepts [xRRGGBB] or [0xRRGGBB] or [0xFFRRGGBB] or #RRGGBB or
380 * [red,green,blue] or a valid JavaScript color
383 * @return 0 if invalid or integer color
385 public static int getArgbFromString(String strColor) {
387 if (strColor == null || (len = strColor.length()) == 0)
389 if (strColor.charAt(0) == '[' && strColor.charAt(len - 1) == ']') {
391 if (strColor.indexOf(",") >= 0) {
392 String[] tokens = PT.split(strColor.substring(1, strColor
393 .length() - 1), ",");
394 if (tokens.length != 3)
396 float red = PT.parseFloat(tokens[0]);
397 float grn = PT.parseFloat(tokens[1]);
398 float blu = PT.parseFloat(tokens[2]);
399 return colorTriadToFFRGB(red, grn, blu);
411 if (strColor.indexOf(check) != 1)
413 strColor = "#" + strColor.substring(len - 7, len - 1);
416 if (len == 7 && strColor.charAt(0) == '#') {
418 return PT.parseIntRadix(strColor.substring(1, 7), 16) | 0xFF000000;
419 } catch (Exception e) {
423 Integer boxedArgb = mapJavaScriptColors.get(strColor.toLowerCase());
424 return (boxedArgb == null ? 0 : boxedArgb.intValue());
427 public static int colorTriadToFFRGB(float x, float y, float z) {
428 if (x <= 1 && y <= 1 && z <= 1) {
436 return rgb((int) x, (int) y, (int) z);
439 public static int rgb(int red, int grn, int blu) {
440 return 0xFF000000 | (red << 16) | (grn << 8) | blu;
443 public final static P3 colorPtFromString(String colorName) {
444 return colorPtFromInt(getArgbFromString(colorName), null);
447 public final static P3 colorPtFromInt(int color, P3 pt) {
450 pt.set((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF);
454 public static int colorPtToFFRGB(T3 pt) {
455 return colorTriadToFFRGB(pt.x, pt.y, pt.z);
458 public static void toRGB3f(int c, float[] f) {
459 f[0] = ((c >> 16) & 0xFF) / 255f; // red
460 f[1] = ((c >> 8) & 0xFF) / 255f;
461 f[2] = (c & 0xFF) / 255f;
465 * Return a greyscale rgb value 0-FF using NTSC color lightness algorithm
467 * the alpha component is set to 0xFF. If you want a value in the
468 * range 0-255 then & the result with 0xFF;
470 * @param rgb the rgb value
471 * @return a grayscale value in the range 0 - 255 decimal
473 public static int toFFGGGfromRGB(int rgb) {
474 int grey = (((2989 * ((rgb >> 16) & 0xFF)) +
475 (5870 * ((rgb >> 8) & 0xFF)) +
476 (1140 * (rgb & 0xFF)) + 5000) / 10000) & 0xFFFFFF;
477 return rgb(grey, grey, grey);
482 * Convert RGB values to HSL (hue/saturation/lightness)
487 * set to false when just using this for
488 * for RGB -- HSL -- HSL' -- RGB' conversion
490 * @return the HSL as P3 range 360 100 100
494 public static P3 rgbToHSL(P3 rgb, boolean doRound) {
495 // adapted from http://tips4java.wordpress.com/2009/07/05/hsl-color/
496 // see http://en.wikipedia.org/wiki/HSL_color_space
497 float r = rgb.x / 255;
498 float g = rgb.y / 255;
499 float b = rgb.z / 255;
500 float min = Math.min(r, Math.min(g, b));
501 float max = Math.max(r, Math.max(g, b));
503 // lightness is just p * 50
505 float p = (max + min);
506 float q = (max - min);
508 float h = (60 * ((q == 0 ? 0 : max == r ? ((g - b) / q + 6)
509 : max == g ? (b - r) / q + 2 : (r - g) / q + 4))) % 360;
511 float s = q / (q == 0 ? 1 : p <= 1 ? p : 2 - p);
513 // we round to tenths for HSL so that we can return enough
514 // precision to get back 1-255 in RGB
515 return (doRound ? P3.new3(Math.round(h*10)/10f, Math.round(s * 1000)/10f,
516 Math.round(p * 500)/10f) : P3.new3(h, s * 100, p * 50));
520 * Convert HSL (hue/saturation/luninance) values to RGB
522 * @param hsl in the range 360, 100, 100
523 * @return the RGB as P3 range 0 to 255
526 public static P3 hslToRGB(P3 hsl) {
527 // adapted from http://tips4java.wordpress.com/2009/07/05/hsl-color/
528 // see http://en.wikipedia.org/wiki/HSL_color_space
532 float h = Math.max(0, Math.min(360, hsl.x)) / 60;
533 float s = Math.max(0, Math.min(100, hsl.y)) / 100;
534 float l = Math.max(0, Math.min(100, hsl.z)) / 100;
536 float p = l - (l < 0.5 ? l : 1 - l) * s;
537 float q = 2 * (l - p);
539 float r = toRGB(p, q, h + 2);
540 float g = toRGB(p, q, h);
541 float b = toRGB(p, q, h - 2);
542 return P3.new3(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255));
545 private static float toRGB(float p, float q, float h) {
546 return ((h = (h + (h < 0 ? 6 : h > 6 ? -6 : 0))) < 1 ? p + q * h
547 : h < 3 ? p + q : h < 4 ? p + q * (4 - h) : p);