Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / src / javajs / util / CU.java
1 package javajs.util;
2
3 import java.util.Hashtable;
4 import java.util.Map;
5
6 import javajs.api.GenericColor;
7
8 /**
9  * ColorUtility 
10  * 
11  */
12
13 public class CU {
14
15   public static String toRGBHexString(GenericColor c) {
16     int rgb = c.getRGB();    
17     if (rgb == 0)
18       return "000000";
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);
25     return r + g + b;
26   }
27
28   public static String toCSSString(GenericColor c) {
29     int opacity = c.getOpacity255();
30     if (opacity == 255)
31       return "#" + toRGBHexString(c);
32     int rgb = c.getRGB();
33     return "rgba(" + ((rgb>>16)&0xFF) + "," + ((rgb>>8)&0xff) + "," + (rgb&0xff) + "," + opacity/255f  + ")"; 
34   }
35   
36   private final static String[] colorNames = {
37     "black",                // 000000
38     "pewhite",              // ffffff
39     "pecyan",               // 00ffff
40     "pepurple",             // d020ff
41     "pegreen",              // 00ff00
42     "peblue",               // 6060ff
43     "peviolet",             // ff80c0
44     "pebrown",              // a42028
45     "pepink",               // ffd8d8
46     "peyellow",             // ffff00
47     "pedarkgreen",          // 00c000
48     "peorange",             // ffb000
49     "pelightblue",          // b0b0ff
50     "pedarkcyan",           // 00a0a0
51     "pedarkgray",           // 606060
52   
53     "aliceblue",            // F0F8FF
54     "antiquewhite",         // FAEBD7
55     "aqua",                 // 00FFFF
56     "aquamarine",           // 7FFFD4
57     "azure",                // F0FFFF
58     "beige",                // F5F5DC
59     "bisque",               // FFE4C4
60     "blanchedalmond",       // FFEBCD
61     "blue",                 // 0000FF
62     "blueviolet",           // 8A2BE2
63     "brown",                // A52A2A
64     "burlywood",            // DEB887
65     "cadetblue",            // 5F9EA0
66     "chartreuse",           // 7FFF00
67     "chocolate",            // D2691E
68     "coral",                // FF7F50
69     "cornflowerblue",       // 6495ED
70     "cornsilk",             // FFF8DC
71     "crimson",              // DC143C
72     "cyan",                 // 00FFFF
73     "darkblue",             // 00008B
74     "darkcyan",             // 008B8B
75     "darkgoldenrod",        // B8860B
76     "darkgray",             // A9A9A9
77     "darkgreen",            // 006400
78     "darkkhaki",            // BDB76B
79     "darkmagenta",          // 8B008B
80     "darkolivegreen",       // 556B2F
81     "darkorange",           // FF8C00
82     "darkorchid",           // 9932CC
83     "darkred",              // 8B0000
84     "darksalmon",           // E9967A
85     "darkseagreen",         // 8FBC8F
86     "darkslateblue",        // 483D8B
87     "darkslategray",        // 2F4F4F
88     "darkturquoise",        // 00CED1
89     "darkviolet",           // 9400D3
90     "deeppink",             // FF1493
91     "deepskyblue",          // 00BFFF
92     "dimgray",              // 696969
93     "dodgerblue",           // 1E90FF
94     "firebrick",            // B22222
95     "floralwhite",          // FFFAF0 16775920
96     "forestgreen",          // 228B22
97     "fuchsia",              // FF00FF
98     "gainsboro",            // DCDCDC
99     "ghostwhite",           // F8F8FF
100     "gold",                 // FFD700
101     "goldenrod",            // DAA520
102     "gray",                 // 808080
103     "green",                // 008000
104     "greenyellow",          // ADFF2F
105     "honeydew",             // F0FFF0
106     "hotpink",              // FF69B4
107     "indianred",            // CD5C5C
108     "indigo",               // 4B0082
109     "ivory",                // FFFFF0
110     "khaki",                // F0E68C
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
128     "lime",                 // 00FF00
129     "limegreen",            // 32CD32
130     "linen",                // FAF0E6
131     "magenta",              // FF00FF
132     "maroon",               // 800000
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
147     "navy",                 // 000080
148     "oldlace",              // FDF5E6
149     "olive",                // 808000
150     "olivedrab",            // 6B8E23
151     "orange",               // FFA500
152     "orangered",            // FF4500
153     "orchid",               // DA70D6
154     "palegoldenrod",        // EEE8AA
155     "palegreen",            // 98FB98
156     "paleturquoise",        // AFEEEE
157     "palevioletred",        // DB7093
158     "papayawhip",           // FFEFD5
159     "peachpuff",            // FFDAB9
160     "peru",                 // CD853F
161     "pink",                 // FFC0CB
162     "plum",                 // DDA0DD
163     "powderblue",           // B0E0E6
164     "purple",               // 800080
165     "red",                  // FF0000
166     "rosybrown",            // BC8F8F
167     "royalblue",            // 4169E1
168     "saddlebrown",          // 8B4513
169     "salmon",               // FA8072
170     "sandybrown",           // F4A460
171     "seagreen",             // 2E8B57
172     "seashell",             // FFF5EE
173     "sienna",               // A0522D
174     "silver",               // C0C0C0
175     "skyblue",              // 87CEEB
176     "slateblue",            // 6A5ACD
177     "slategray",            // 708090
178     "snow",                 // FFFAFA 16775930
179     "springgreen",          // 00FF7F
180     "steelblue",            // 4682B4
181     "tan",                  // D2B48C
182     "teal",                 // 008080
183     "thistle",              // D8BFD8
184     "tomato",               // FF6347
185     "turquoise",            // 40E0D0
186     "violet",               // EE82EE
187     "wheat",                // F5DEB3
188     "white",                // FFFFFF 16777215
189     "whitesmoke",           // F5F5F5
190     "yellow",               // FFFF00
191     "yellowgreen",          // 9ACD32
192     // plus a few rasmol names/values
193     "bluetint",             // AFD7FF
194     "greenblue",            // 2E8B57
195     "greentint",            // 98FFB3
196     "grey",                 // 808080
197     "pinktint",             // FFABBB
198     "redorange",            // FF4500
199     "yellowtint",           // F6F675
200   };
201   
202   private final static int[] colorArgbs = {
203   //#FFFFC3 hover
204     0xFF000000, // black
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
223     0xFF00FFFF, // aqua
224     0xFF7FFFD4, // aquamarine
225     0xFFF0FFFF, // azure
226     0xFFF5F5DC, // beige
227     0xFFFFE4C4, // bisque
228     0xFFFFEBCD, // blanchedalmond
229     0xFF0000FF, // blue
230     0xFF8A2BE2, // blueviolet
231     0xFFA52A2A, // brown
232     0xFFDEB887, // burlywood
233     0xFF5F9EA0, // cadetblue
234     0xFF7FFF00, // chartreuse
235     0xFFD2691E, // chocolate
236     0xFFFF7F50, // coral
237     0xFF6495ED, // cornflowerblue
238     0xFFFFF8DC, // cornsilk
239     0xFFDC143C, // crimson
240     0xFF00FFFF, // cyan
241     0xFF00008B, // darkblue
242     0xFF008B8B, // darkcyan
243     0xFFB8860B, // darkgoldenrod
244     0xFFA9A9A9, // darkgray
245     0xFF006400, // darkgreen
246   
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
269     0xFFFFD700, // gold
270     0xFFDAA520, // goldenrod
271     0xFF808080, // gray
272     0xFF008000, // green
273     0xFFADFF2F, // greenyellow
274     0xFFF0FFF0, // honeydew
275     0xFFFF69B4, // hotpink
276     0xFFCD5C5C, // indianred
277     0xFF4B0082, // indigo
278     0xFFFFFFF0, // ivory
279     0xFFF0E68C, // khaki
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
297     0xFF00FF00, // lime
298     0xFF32CD32, // limegreen
299     0xFFFAF0E6, // linen
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
316     0xFF000080, // navy
317     0xFFFDF5E6, // oldlace
318     0xFF808000, // olive
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
329     0xFFCD853F, // peru
330     0xFFFFC0CB, // pink
331     0xFFDDA0DD, // plum
332     0xFFB0E0E6, // powderblue
333     0xFF800080, // purple
334     0xFFFF0000, // red
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
347     0xFFFFFAFA, // snow
348     0xFF00FF7F, // springgreen
349     0xFF4682B4, // steelblue
350     0xFFD2B48C, // tan
351     0xFF008080, // teal
352     0xFFD8BFD8, // thistle
353     0xFFFF6347, // tomato
354     0xFF40E0D0, // turquoise
355     0xFFEE82EE, // violet
356     0xFFF5DEB3, // wheat
357     0xFFFFFFFF, // white
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
365     0xFF808080, // grey
366     0xFFFFABBB, // pinktint
367     0xFFFF4500, // redorange
368     0xFFF6F675, // yellowtint
369   };
370
371   private static final Map<String, Integer> mapJavaScriptColors = new Hashtable<String, Integer>();
372
373   static {
374     for (int i = colorNames.length; --i >= 0; )
375       mapJavaScriptColors.put(colorNames[i], Integer.valueOf(colorArgbs[i]));
376   }
377
378   /**
379    * accepts [xRRGGBB] or [0xRRGGBB] or [0xFFRRGGBB] or #RRGGBB or
380    * [red,green,blue] or a valid JavaScript color
381    * 
382    * @param strColor
383    * @return 0 if invalid or integer color
384    */
385   public static int getArgbFromString(String strColor) {
386     int len = 0;
387     if (strColor == null || (len = strColor.length()) == 0)
388       return 0;
389     if (strColor.charAt(0) == '[' && strColor.charAt(len - 1) == ']') {
390       String check;
391       if (strColor.indexOf(",") >= 0) {
392         String[] tokens = PT.split(strColor.substring(1, strColor
393             .length() - 1), ",");
394         if (tokens.length != 3)
395           return 0;
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);
400       }
401       switch (len) {
402       case 9:
403         check = "x";
404         break;
405       case 10:
406         check = "0x";
407         break;
408       default:
409         return 0;
410       }
411       if (strColor.indexOf(check) != 1)
412         return 0;
413       strColor = "#" + strColor.substring(len - 7, len - 1);
414       len = 7;
415     }
416     if (len == 7 && strColor.charAt(0) == '#') {
417       try {
418         return PT.parseIntRadix(strColor.substring(1, 7), 16) | 0xFF000000;
419       } catch (Exception e) {
420         return 0;
421       }
422     }
423     Integer boxedArgb = mapJavaScriptColors.get(strColor.toLowerCase());
424     return (boxedArgb == null ? 0 : boxedArgb.intValue());
425   }
426
427   public static int colorTriadToFFRGB(float x, float y, float z) {
428     if (x <= 1 && y <= 1 && z <= 1) {
429       if (x > 0)
430         x = x * 256 - 1;
431       if (y > 0)
432         y = y * 256 - 1;
433       if (z > 0)
434         z = z * 256 - 1;
435     }
436     return rgb((int) x, (int) y, (int) z);
437   }
438
439   public static int rgb(int red, int grn, int blu) {
440     return 0xFF000000 | (red << 16) | (grn << 8) | blu;
441   }
442
443   public final static P3 colorPtFromString(String colorName) {
444     return colorPtFromInt(getArgbFromString(colorName), null);
445   }
446
447   public final static P3 colorPtFromInt(int color, P3 pt) {
448     if (pt == null)
449       pt = new P3();
450     pt.set((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF);
451     return pt;
452   }
453
454   public static int colorPtToFFRGB(T3 pt) {
455     return colorTriadToFFRGB(pt.x, pt.y, pt.z);
456   }
457
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;
462   }
463
464   /**
465    * Return a greyscale rgb value 0-FF using NTSC color lightness algorithm
466    *<p>
467    * the alpha component is set to 0xFF. If you want a value in the
468    * range 0-255 then & the result with 0xFF;
469    *
470    * @param rgb the rgb value
471    * @return a grayscale value in the range 0 - 255 decimal
472    */
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);
478   }
479   
480   
481   /**
482    * Convert RGB values to HSL (hue/saturation/lightness)
483    * 
484    * @param rgb
485    *        range 255 255 255
486    * @param doRound
487    *        set to false when just using this for 
488    *        for RGB -- HSL -- HSL' -- RGB' conversion
489    * 
490    * @return the HSL as P3 range 360 100 100
491    * @author hansonr
492    */
493
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));
502
503     //  lightness is just p * 50
504
505     float p = (max + min);
506     float q = (max - min);
507
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;
510
511     float s = q / (q == 0 ? 1 : p <= 1 ? p : 2 - p);
512
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));
517   }
518
519   /**
520    * Convert HSL (hue/saturation/luninance) values to RGB
521    *
522    * @param hsl in the range 360, 100, 100
523    * @return the RGB as P3 range 0 to 255
524    * @author hansonr
525    */
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
529     
530     // highly condensed
531     
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;
535
536     float p = l - (l < 0.5 ? l : 1 - l) * s;    
537     float q = 2 * (l - p); 
538         
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));
543   }
544
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);
548   }
549
550 }