Merge branch 'features/r2_11_2/JAL-3821_reinstate_patch' into develop
[jalview.git] / unused / 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     "lightgray",            // D3D3D3
122     "lightpink",            // FFB6C1
123     "lightsalmon",          // FFA07A
124     "lightseagreen",        // 20B2AA
125     "lightskyblue",         // 87CEFA
126     "lightslategray",       // 778899
127     "lightsteelblue",       // B0C4DE
128     "lightyellow",          // FFFFE0
129     "lime",                 // 00FF00
130     "limegreen",            // 32CD32
131     "linen",                // FAF0E6
132     "magenta",              // FF00FF
133     "maroon",               // 800000
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
148     "navy",                 // 000080
149     "oldlace",              // FDF5E6
150     "olive",                // 808000
151     "olivedrab",            // 6B8E23
152     "orange",               // FFA500
153     "orangered",            // FF4500
154     "orchid",               // DA70D6
155     "palegoldenrod",        // EEE8AA
156     "palegreen",            // 98FB98
157     "paleturquoise",        // AFEEEE
158     "palevioletred",        // DB7093
159     "papayawhip",           // FFEFD5
160     "peachpuff",            // FFDAB9
161     "peru",                 // CD853F
162     "pink",                 // FFC0CB
163     "plum",                 // DDA0DD
164     "powderblue",           // B0E0E6
165     "purple",               // 800080
166     "red",                  // FF0000
167     "rosybrown",            // BC8F8F
168     "royalblue",            // 4169E1
169     "saddlebrown",          // 8B4513
170     "salmon",               // FA8072
171     "sandybrown",           // F4A460
172     "seagreen",             // 2E8B57
173     "seashell",             // FFF5EE
174     "sienna",               // A0522D
175     "silver",               // C0C0C0
176     "skyblue",              // 87CEEB
177     "slateblue",            // 6A5ACD
178     "slategray",            // 708090
179     "snow",                 // FFFAFA 16775930
180     "springgreen",          // 00FF7F
181     "steelblue",            // 4682B4
182     "tan",                  // D2B48C
183     "teal",                 // 008080
184     "thistle",              // D8BFD8
185     "tomato",               // FF6347
186     "turquoise",            // 40E0D0
187     "violet",               // EE82EE
188     "wheat",                // F5DEB3
189     "white",                // FFFFFF 16777215
190     "whitesmoke",           // F5F5F5
191     "yellow",               // FFFF00
192     "yellowgreen",          // 9ACD32
193     // plus a few rasmol names/values
194     "bluetint",             // AFD7FF
195     "greenblue",            // 2E8B57
196     "greentint",            // 98FFB3
197     "grey",                 // 808080
198     "gray",                 
199     "pinktint",             // FFABBB
200     "redorange",            // FF4500
201     "yellowtint",           // F6F675
202   };
203   
204   private final static int[] colorArgbs = {
205   //#FFFFC3 hover
206     0xFF000000, // black
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
225     0xFF00FFFF, // aqua
226     0xFF7FFFD4, // aquamarine
227     0xFFF0FFFF, // azure
228     0xFFF5F5DC, // beige
229     0xFFFFE4C4, // bisque
230     0xFFFFEBCD, // blanchedalmond
231     0xFF0000FF, // blue
232     0xFF8A2BE2, // blueviolet
233     0xFFA52A2A, // brown
234     0xFFDEB887, // burlywood
235     0xFF5F9EA0, // cadetblue
236     0xFF7FFF00, // chartreuse
237     0xFFD2691E, // chocolate
238     0xFFFF7F50, // coral
239     0xFF6495ED, // cornflowerblue
240     0xFFFFF8DC, // cornsilk
241     0xFFDC143C, // crimson
242     0xFF00FFFF, // cyan
243     0xFF00008B, // darkblue
244     0xFF008B8B, // darkcyan
245     0xFFB8860B, // darkgoldenrod
246     0xFFA9A9A9, // darkgray
247     0xFF006400, // darkgreen
248   
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
271     0xFFFFD700, // gold
272     0xFFDAA520, // goldenrod
273     0xFF808080, // gray
274     0xFF008000, // green
275     0xFFADFF2F, // greenyellow
276     0xFFF0FFF0, // honeydew
277     0xFFFF69B4, // hotpink
278     0xFFCD5C5C, // indianred
279     0xFF4B0082, // indigo
280     0xFFFFFFF0, // ivory
281     0xFFF0E68C, // khaki
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
300     0xFF00FF00, // lime
301     0xFF32CD32, // limegreen
302     0xFFFAF0E6, // linen
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
319     0xFF000080, // navy
320     0xFFFDF5E6, // oldlace
321     0xFF808000, // olive
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
332     0xFFCD853F, // peru
333     0xFFFFC0CB, // pink
334     0xFFDDA0DD, // plum
335     0xFFB0E0E6, // powderblue
336     0xFF800080, // purple
337     0xFFFF0000, // red
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
350     0xFFFFFAFA, // snow
351     0xFF00FF7F, // springgreen
352     0xFF4682B4, // steelblue
353     0xFFD2B48C, // tan
354     0xFF008080, // teal
355     0xFFD8BFD8, // thistle
356     0xFFFF6347, // tomato
357     0xFF40E0D0, // turquoise
358     0xFFEE82EE, // violet
359     0xFFF5DEB3, // wheat
360     0xFFFFFFFF, // white
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
368     0xFF808080, // grey
369     0xFF808080, // gray
370     0xFFFFABBB, // pinktint
371     0xFFFF4500, // redorange
372     0xFFF6F675, // yellowtint
373   };
374
375   private static final Map<String, Integer> mapJavaScriptColors = new Hashtable<String, Integer>();
376
377   static {
378     for (int i = colorNames.length; --i >= 0; )
379       mapJavaScriptColors.put(colorNames[i], Integer.valueOf(colorArgbs[i]));
380   }
381
382   /**
383    * accepts [xRRGGBB] or [0xRRGGBB] or [0xFFRRGGBB] or #RRGGBB or
384    * [red,green,blue] or a valid JavaScript color
385    * 
386    * @param strColor
387    * @return 0 if invalid or integer color
388    */
389   public static int getArgbFromString(String strColor) {
390     int len = 0;
391     if (strColor == null || (len = strColor.length()) == 0)
392       return 0;
393     strColor = strColor.toLowerCase();
394     if (strColor.charAt(0) == '[' && strColor.charAt(len - 1) == ']') {
395       String check;
396       if (strColor.indexOf(",") >= 0) {
397         String[] tokens = PT.split(strColor.substring(1, strColor
398             .length() - 1), ",");
399         if (tokens.length != 3)
400           return 0;
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);
405       }
406       switch (len) {
407       case 9:
408         check = "x";
409         break;
410       case 10:
411         check = "0x";
412         break;
413       default:
414         return 0;
415       }
416       if (strColor.indexOf(check) != 1)
417         return 0;
418       strColor = "#" + strColor.substring(len - 7, len - 1);
419       len = 7;
420     }
421     if (len == 7 && strColor.charAt(0) == '#') {
422       try {
423         return PT.parseIntRadix(strColor.substring(1, 7), 16) | 0xFF000000;
424       } catch (Exception e) {
425         return 0;
426       }
427     }
428     Integer boxedArgb = mapJavaScriptColors.get(strColor);
429     return (boxedArgb == null ? 0 : boxedArgb.intValue());
430   }
431
432   public static int colorTriadToFFRGB(float x, float y, float z) {
433     if (x <= 1 && y <= 1 && z <= 1) {
434       if (x > 0)
435         x = x * 256 - 1;
436       if (y > 0)
437         y = y * 256 - 1;
438       if (z > 0)
439         z = z * 256 - 1;
440     }
441     return rgb((int) x, (int) y, (int) z);
442   }
443
444   public static int rgb(int red, int grn, int blu) {
445     return 0xFF000000 | (red << 16) | (grn << 8) | blu;
446   }
447
448   public final static P3 colorPtFromString(String colorName) {
449     return colorPtFromInt(getArgbFromString(colorName), null);
450   }
451
452   public final static P3 colorPtFromInt(int color, P3 pt) {
453     if (pt == null)
454       pt = new P3();
455     pt.set((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF);
456     return pt;
457   }
458
459   public static int colorPtToFFRGB(T3 pt) {
460     return colorTriadToFFRGB(pt.x, pt.y, pt.z);
461   }
462
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;
467   }
468
469   /**
470    * Return a greyscale rgb value 0-FF using NTSC color lightness algorithm
471    *<p>
472    * the alpha component is set to 0xFF. If you want a value in the
473    * range 0-255 then & the result with 0xFF;
474    *
475    * @param rgb the rgb value
476    * @return a grayscale value in the range 0 - 255 decimal
477    */
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);
483   }
484   
485   
486   /**
487    * Convert RGB values to HSL (hue/saturation/lightness)
488    * 
489    * @param rgb
490    *        range 255 255 255
491    * @param doRound
492    *        set to false when just using this for 
493    *        for RGB -- HSL -- HSL' -- RGB' conversion
494    * 
495    * @return the HSL as P3 range 360 100 100
496    * @author hansonr
497    */
498
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));
507
508     //  lightness is just p * 50
509
510     float p = (max + min);
511     float q = (max - min);
512
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;
515
516     float s = q / (q == 0 ? 1 : p <= 1 ? p : 2 - p);
517
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));
522   }
523
524   /**
525    * Convert HSL (hue/saturation/luninance) values to RGB
526    *
527    * @param hsl in the range 360, 100, 100
528    * @return the RGB as P3 range 0 to 255
529    * @author hansonr
530    */
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
534     
535     // highly condensed
536     
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;
540
541     float p = l - (l < 0.5 ? l : 1 - l) * s;    
542     float q = 2 * (l - p); 
543         
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));
548   }
549
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);
553   }
554
555 }