001    package railo.commons.color;
002    
003    import java.awt.Color;
004    
005    import railo.commons.lang.NumberUtil;
006    import railo.commons.lang.StringUtil;
007    import railo.runtime.exp.ExpressionException;
008    import railo.runtime.op.Caster;
009    import railo.runtime.type.List;
010    
011    public final class ColorCaster {
012    
013            /**
014             * calculate the contrast between 2 colors
015             * @param left
016             * @param right
017             * @return a int between 0 (badest) and 510 (best)
018             */
019            public static int contrast(Color left, Color right) {
020                    return
021                    (Math.max(left.getRed(), right.getRed())                - Math.min(left.getRed(), right.getRed())) + 
022                    (Math.max(left.getGreen(), right.getGreen())    - Math.min(left.getGreen(), right.getGreen())) + 
023                    (Math.max(left.getBlue(), right.getBlue())              - Math.max(left.getBlue(), right.getBlue()));
024            }
025    
026            public static Color toColor(String strColor) throws ExpressionException {
027                    if(StringUtil.isEmpty(strColor,true))
028                            throw new ExpressionException("can't cast empty string to a color Object");
029                    
030                    strColor=strColor.toLowerCase().trim();
031                    char first=strColor.charAt(0);
032                    
033                    switch(first) {
034                            case 'a':
035                                    if("aqua".equals(strColor))             return new Color(0,0xFF,0xFF); 
036                                    if("aliceblue".equals(strColor))        return new Color(0xF0,0xF8,0xFF); 
037                                    if("antiquewhite".equals(strColor)) return new Color(0xFA,0xEB,0xD7); 
038                                    if("quamarine".equals(strColor)) return new Color(0x7F,0xFF,0xD4); 
039                                    if("azure".equals(strColor)) return new Color(0xF0,0xFF,0xFF); 
040                            break;
041                            case 'b':
042                                    if("black".equals(strColor))            return Color.BLACK;
043                                    if("blue".equals(strColor))             return Color.BLUE;
044                                    if("blue".equals(strColor))             return Color.CYAN;
045                                    if("beige".equals(strColor))            return new Color(0xF5,0xF5,0xDC); 
046                                    if("blueviolet".equals(strColor))       return new Color(0x8A,0x2B,0xE2); 
047                                    if("brown".equals(strColor))            return new Color(0xA5,0x2A,0x2A); 
048                                    if("burlywood".equals(strColor))        return new Color(0xDE,0xB8,0x87); 
049                            break;
050                            case 'c':
051                                    if("cyan".equals(strColor))             return Color.CYAN;
052                                    if("cadetblue".equals(strColor))        return new Color(0x5F,0x9E,0xA0); 
053                                    if("chartreuse".equals(strColor))       return new Color(0x7F,0xFF,0x00); 
054                                    if("chocolate".equals(strColor))        return new Color(0xD2,0x69,0x1E); 
055                                    if("coral".equals(strColor))            return new Color(0xFF,0x7F,0x50); 
056                                    if("cornflowerblue".equals(strColor))return new Color(0x64,0x95,0xED); 
057                                    if("cornsilk".equals(strColor))         return new Color(0xFF,0xF8,0xDC); 
058                                    if("crimson".equals(strColor))          return new Color(0xDC,0x14,0x3C); 
059                            break;
060                            case 'd':
061                                    if("darkgray".equals(strColor))         return Color.DARK_GRAY;
062                                    if("darkblue".equals(strColor))         return new Color(0x00,0x00,0x8B); 
063                                    if("darkcyan".equals(strColor))         return new Color(0x00,0x8B,0x8B); 
064                                    if("darkgoldenrod".equals(strColor))return new Color(0xB8,0x86,0x0B); 
065                                    if("darkgreen".equals(strColor))        return new Color(0x00,0x64,0x00); 
066                                    if("darkkhaki".equals(strColor))        return new Color(0xBD,0xB7,0x6B); 
067                                    if("darkmagenta".equals(strColor))      return new Color(0x8B,0x00,0x8B); 
068                                    if("darkolivegreen".equals(strColor))return new Color(0x55,0x6B,0x2F); 
069                                    if("darkorange".equals(strColor))       return new Color(0xFF,0x8C,0x00); 
070                                    if("darkorchid".equals(strColor))       return new Color(0x99,0x32,0xcc); 
071                                    if("darkred".equals(strColor))          return new Color(0x8B,0x00,0x00); 
072                                    if("darksalmon".equals(strColor))       return new Color(0xE9,0x96,0x7A); 
073                                    if("darkseagreen".equals(strColor)) return new Color(0x8F,0xBC,0x8F); 
074                                    if("darkslateblue".equals(strColor))return new Color(0x2F,0x4F,0x4F); 
075                                    if("darkslategray".equals(strColor))return new Color(0x48,0x3D,0x8B); 
076                                    if("darkturquoise".equals(strColor))return new Color(0x00,0xCE,0xD1); 
077                                    if("darkviolet".equals(strColor))       return new Color(0x94,0x00,0xD3); 
078                                    if("deeppink".equals(strColor))         return new Color(0xFF,0x14,0x93); 
079                                    if("deepskyblue".equals(strColor))      return new Color(0x00,0xBF,0xFF); 
080                                    if("dimgray".equals(strColor))          return new Color(0x69,0x69,0x69); 
081                                    if("dodgerblue".equals(strColor))       return new Color(0x1E,0x90,0xFF); 
082                            break;
083                            case 'f':
084                                    if("fuchsia".equals(strColor))          return new Color(0xFF,0,0xFF); 
085                                    if("firebrick".equals(strColor))        return new Color(0xB2,0x22,0x22); 
086                                    if("floralwhite".equals(strColor))      return new Color(0xFF,0xFA,0xF0); 
087                                    if("forestgreen".equals(strColor))      return new Color(0x22,0x8B,0x22); 
088                            break;
089                            case 'g':
090                                    if("gray".equals(strColor))             return Color.GRAY;
091                                    if("green".equals(strColor))            return Color.GREEN;
092                                    if("gainsboro".equals(strColor))        return new Color(0xDC,0xDC,0xDC); 
093                                    if("ghostwhite".equals(strColor))       return new Color(0xF8,0xF8,0xFF); 
094                                    if("gold".equals(strColor))             return new Color(0xFF,0xD7,0x00); 
095                                    if("goldenrod".equals(strColor))        return new Color(0xDA,0xA5,0x20); 
096                                    if("greenyellow".equals(strColor))      return new Color(0xAD,0xFF,0x2F); 
097    
098                            break;
099                            case 'h':
100                                    if("honeydew".equals(strColor))         return new Color(0xF0,0xFF,0xF0); 
101                                    if("hotpink".equals(strColor))          return new Color(0xFF,0x69,0xB4); 
102                            break;
103                            case 'i':
104                                    if("indianred".equals(strColor))        return new Color(0xCD,0x5C,0x5C); 
105                                    if("indigo".equals(strColor))           return new Color(0x4B,0x00,0x82); 
106                                    if("ivory".equals(strColor))            return new Color(0xFF,0xFF,0xF0); 
107                            break;
108                            case 'k':
109                                    if("khaki".equals(strColor))            return new Color(0xF0,0xE6,0x8C); 
110                            break;
111                            case 'l':
112                                    if("lightgray".equals(strColor))        return Color.lightGray; 
113                                    if("lime".equals(strColor))             return new Color(0,0xFF,0);
114                                    if("lavender".equals(strColor))         return new Color(0xE6,0xE6,0xFA); 
115                                    if("lavenderblush".equals(strColor))return new Color(0xFF,0xF0,0xF5); 
116                                    if("lawngreen".equals(strColor))        return new Color(0x7C,0xFC,0x00); 
117                                    if("lemonchiffon".equals(strColor)) return new Color(0xFF,0xFA,0xCD); 
118                                    if("lightblue".equals(strColor))        return new Color(0xAD,0xD8,0xE6);
119                                    if("lightcoral".equals(strColor))       return new Color(0xF0,0x80,0x80); 
120                                    if("lightcyan".equals(strColor))        return new Color(0xE0,0xFF,0xFF); 
121                                    if("lightgoldenrodyellow".equals(strColor)) return new Color(0xFA,0xFA,0xD2); 
122                                    if("lightgreen".equals(strColor))       return new Color(0x90,0xEE,0x90); 
123                                    if("lightgrey".equals(strColor))        return new Color(0xD3,0xD3,0xD3);
124                                    if("lightpink".equals(strColor))        return new Color(0xFF,0xB6,0xC1); 
125                                    if("lightsalmon".equals(strColor))      return new Color(0xFF,0xA0,0x7A); 
126                                    if("lightseagreen".equals(strColor))return new Color(0x20,0xB2,0xAA); 
127                                    if("lightskyblue".equals(strColor)) return new Color(0x87,0xCE,0xFA); 
128                                    if("lightslategray".equals(strColor))return new Color(0x77,0x88,0x99);
129                                    if("lightsteelblue".equals(strColor))return new Color(0xB0,0xC4,0xDE); 
130                                    if("lightyellow".equals(strColor))      return new Color(0xFF,0xFF,0xE0); 
131                                    if("limegreen".equals(strColor))        return new Color(0x32,0xCD,0x32);
132                                    if("linen".equals(strColor))            return new Color(0xFA,0xF0,0xE6); 
133                                    
134                            break;
135                            case 'm':
136                                    if("magenta".equals(strColor))          return Color.MAGENTA; 
137                                    if("maroon".equals(strColor))           return new Color(0X80,0,0);
138                                    if("mediumaquamarine".equals(strColor)) return new Color(0x66,0xCD,0xAA); 
139                                    if("mediumblue".equals(strColor)) return new Color(0x00,0x00,0xCD);
140                                    if("mediumorchid".equals(strColor)) return new Color(0xBA,0x55,0xD3); 
141                                    if("mediumpurple".equals(strColor)) return new Color(0x93,0x70,0xDB); 
142                                    if("mediumseagreen".equals(strColor)) return new Color(0x3C,0xB3,0x71);
143                                    if("mediumslateblue".equals(strColor)) return new Color(0x7B,0x68,0xEE); 
144                                    if("mediumspringgreen".equals(strColor)) return new Color(0x00,0xFA,0x9A);
145                                    if("mediumturquoise".equals(strColor)) return new Color(0x48,0xD1,0xCC); 
146                                    if("mediumvioletred".equals(strColor)) return new Color(0xC7,0x15,0x85); 
147                                    if("midnightblue".equals(strColor)) return new Color(0x19,0x19,0x70);
148                                    if("mintcream".equals(strColor)) return new Color(0xF5,0xFF,0xFA); 
149                                    if("mistyrose".equals(strColor)) return new Color(0xFF,0xE4,0xE1);
150                                    if("moccasin".equals(strColor)) return new Color(0xFF,0xE4,0xB5); 
151                            break;
152                            case 'n':
153                                    if("navy".equals(strColor))             return new Color(0,0,0X80);
154                                    if("navajowhite".equals(strColor)) return new Color(0xFF,0xDE,0xAD); 
155                            break;
156                            case 'o':
157                                    if("orange".equals(strColor))           return Color.ORANGE; 
158                                    if("olive".equals(strColor))            return new Color(0X80,0X80,0);
159                                    if("oldlace".equals(strColor)) return new Color(0xFD,0xF5,0xE6);
160                                    if("olivedrab".equals(strColor)) return new Color(0x6B,0x8E,0x23); 
161                                    if("orangered".equals(strColor)) return new Color(0xFF,0x45,0x00);
162                                    if("orchid".equals(strColor)) return new Color(0xDA,0x70,0xD6); 
163                            break;
164                            case 'p':
165                                    if("pink".equals(strColor))             return Color.PINK; 
166                                    if("purple".equals(strColor))           return new Color(0X80,0,0X80);
167                                    if("palegoldenrod".equals(strColor)) return new Color(0xEE,0xE8,0xAA);
168                                    if("palegreen".equals(strColor)) return new Color(0x98,0xFB,0x98); 
169                                    if("paleturquoise".equals(strColor)) return new Color(0xAF,0xEE,0xEE);
170                                    if("palevioletred".equals(strColor)) return new Color(0xDB,0x70,0x93); 
171                                    if("papayawhip".equals(strColor)) return new Color(0xFF,0xEF,0xD5);
172                                    if("peachpuff".equals(strColor)) return new Color(0xFF,0xDA,0xB9); 
173                                    if("peru".equals(strColor)) return new Color(0xCD,0x85,0x3F);
174                                    if("pink".equals(strColor)) return new Color(0xFF,0xC0,0xCB); 
175                                    if("plum".equals(strColor)) return new Color(0xDD,0xA0,0xDD);
176                                    if("powderblue".equals(strColor)) return new Color(0xB0,0xE0,0xE6);                             
177                            break;
178                            case 'r':
179                                    if("red".equals(strColor))              return Color.RED; 
180                                    if("rosybrown".equals(strColor)) return new Color(0xBC,0x8F,0x8F);
181                                    if("royalblue".equals(strColor)) return new Color(0x41,0x69,0xE1); 
182                            break;
183                            case 's':
184                                    if("silver".equals(strColor))           return new Color(0XC0,0XC0,0XC0);
185                                    if("saddlebrown".equals(strColor)) return new Color(0x8B,0x45,0x13);
186                                    if("salmon".equals(strColor)) return new Color(0xFA,0x80,0x72); 
187                                    if("sandybrown".equals(strColor)) return new Color(0xF4,0xA4,0x60);
188                                    if("seagreen".equals(strColor)) return new Color(0x2E,0x8B,0x57); 
189                                    if("seashell".equals(strColor)) return new Color(0xFF,0xF5,0xEE);
190                                    if("sienna".equals(strColor)) return new Color(0xA0,0x52,0x2D); 
191                                    if("skyblue".equals(strColor)) return new Color(0x87,0xCE,0xEB);
192                                    if("slateblue".equals(strColor)) return new Color(0x6A,0x5A,0xCD);
193                                    if("slategray".equals(strColor)) return new Color(0x70,0x80,0x90); 
194                                    if("snow".equals(strColor)) return new Color(0xFF,0xFA,0xFA);
195                                    if("springgreen".equals(strColor)) return new Color(0x00,0xFF,0x7F); 
196                                    if("steelblue".equals(strColor)) return new Color(0x46,0x82,0xB4);
197                            break;
198                            case 't':
199                                    if("teal".equals(strColor))             return new Color(0,0X80,0X80);
200                                    if("tan".equals(strColor)) return new Color(0xD2,0xB4,0x8C); 
201                                    if("thistle".equals(strColor)) return new Color(0xD8,0xBF,0xD8);
202                                    if("tomato".equals(strColor)) return new Color(0xFF,0x63,0x47);
203                                    if("turquoise".equals(strColor)) return new Color(0x40,0xE0,0xD0); 
204                                    //if("".equals(strColor)) return new Color(0x,0x,0x);
205                            break;
206                            case 'v': 
207                                    if("violet".equals(strColor)) return new Color(0xEE,0x82,0xEE);
208                            break;
209                            case 'w':
210                                    if("white".equals(strColor))            return Color.WHITE;  
211                                    if("wheat".equals(strColor)) return new Color(0xF5,0xDE,0xB3); 
212                                    if("whitesmoke".equals(strColor)) return new Color(0xF5,0xF5,0xF5);
213                            break;
214                            case 'y':
215                                    if("yellow".equals(strColor))           return Color.YELLOW;  
216                                    if("yellowgreen".equals(strColor)) return new Color(0x9A,0xCD,0x32);
217                            break;
218                    }
219    
220                    
221                    if(first=='#') {
222                            String strColor2 = strColor.substring(1);
223                            // #fff
224                            if(strColor2.length()==3) {
225                                    char c1=strColor2.charAt(0);
226                                    char c2=strColor2.charAt(1);
227                                    char c3=strColor2.charAt(2);
228                                    return new Color(NumberUtil.hexToInt(""+c1+c1),NumberUtil.hexToInt(""+c2+c2),NumberUtil.hexToInt(""+c3+c3));
229                            }
230                            // #ffffff
231                            if(strColor2.length()==6) {
232                                    String s1=strColor2.substring(0,2);
233                                    String s2=strColor2.substring(2,4);
234                                    String s3=strColor2.substring(4,6);
235                                    return new Color(NumberUtil.hexToInt(s1),NumberUtil.hexToInt(s2),NumberUtil.hexToInt(s3));
236                            }
237                            // #ffffffff
238                            if(strColor2.length()==8) {
239                                    String s1=strColor2.substring(0,2);
240                                    String s2=strColor2.substring(2,4);
241                                    String s3=strColor2.substring(4,6);
242                                    String s4=strColor2.substring(6,8);
243                                    return new Color(NumberUtil.hexToInt(s1),NumberUtil.hexToInt(s2),NumberUtil.hexToInt(s3),NumberUtil.hexToInt(s4));
244                            }
245                    }
246                    
247                    // rgb(255,0,0)
248                    if(strColor.startsWith("rgb(") && strColor.endsWith(")")) {
249                            String strColor2 = strColor.substring(4,strColor.length()-1).trim();
250                            String[] arr = List.listToStringArray(strColor2,',');
251                            if(arr.length==3) {
252                                    int i1 = Caster.toIntValue(arr[0]);
253                                    int i2 = Caster.toIntValue(arr[1]);
254                                    int i3 = Caster.toIntValue(arr[2]);
255                                    return new Color(i1,i2,i3);
256                            }
257                    }
258                    
259                    
260                    
261    
262                    // fff
263                    if(strColor.length()==3) {
264                            char c1=strColor.charAt(0);
265                            char c2=strColor.charAt(1);
266                            char c3=strColor.charAt(2);
267                            int i1=NumberUtil.hexToInt(""+c1+c1,-1);
268                            int i2=NumberUtil.hexToInt(""+c2+c2,-1);
269                            int i3=NumberUtil.hexToInt(""+c3+c3,-1);
270                            if(i1!=-1 && i2!=-1 && i3!=-1) return new Color(i1,i2,i3);
271                    }
272                    // ffffff
273                    else if(strColor.length()==6) {
274                            String s1=strColor.substring(0,2);
275                            String s2=strColor.substring(2,4);
276                            String s3=strColor.substring(4,6);
277                            int i1=NumberUtil.hexToInt(s1,-1);
278                            int i2=NumberUtil.hexToInt(s2,-1);
279                            int i3=NumberUtil.hexToInt(s3,-1);
280                            if(i1!=-1 && i2!=-1 && i3!=-1) return new Color(i1,i2,i3);
281                    }
282                    // ffffffff
283                    else if(strColor.length()==8) {
284                            String s1=strColor.substring(0,2);
285                            String s2=strColor.substring(2,4);
286                            String s3=strColor.substring(4,6);
287                            String s4=strColor.substring(6,8);
288                            int i1=NumberUtil.hexToInt(s1,-1);
289                            int i2=NumberUtil.hexToInt(s2,-1);
290                            int i3=NumberUtil.hexToInt(s3,-1);
291                            int i4=NumberUtil.hexToInt(s4,-1);
292                            if(i1!=-1 && i2!=-1 && i3!=-1 && i4!=-1) return new Color(i1,i2,i3,i4);
293                    }
294                    
295                    // 255,0,0
296                    String[] arr = List.listToStringArray(strColor, ',');
297                    if(arr.length==3){
298                            int i1 = Caster.toIntValue(arr[0],-1);
299                            int i2 = Caster.toIntValue(arr[1],-1);
300                            int i3 = Caster.toIntValue(arr[2],-1);
301                            if(i1>-1 && i2>-1 && i3>-1) return new Color(i1,i2,i3);
302                    }
303                    
304                    
305                    throw new ExpressionException("invalid color definition ["+strColor+"]",
306                                    "color must be a know constant label (blue,green,yellow ...), a hexadecimal value (#ffffff) or a RGB value (rgb(255,255,255)), 255,255,255");
307            }
308        /*private static int hexToInt(String s, boolean throwException) throws ExpressionException {
309            int[] n = new int[s.length()];
310            char c;
311            int sum = 0;
312            int koef = 1;
313            for(int i=n.length-1; i>=0; i--) {
314                c = s.charAt(i);
315                
316                if(!((c>='0' && c<='9') || (c>='a' && c<='f'))) {
317                    if(!throwException) return -1;
318                    throw new ExpressionException("invalid hex constant ["+c+"], hex constants are [0-9,a-f]");
319                }
320                    
321                //System.out.println(c);
322                switch (c) {
323                    case 48:
324                        n[i] = 0;
325                        break;
326                    case 49:
327                        n[i] = 1;
328                        break;
329                    case 50:
330                        n[i] = 2;
331                        break;
332                    case 51:
333                        n[i] = 3;
334                        break;
335                    case 52:
336                        n[i] = 4;
337                        break;
338                    case 53:
339                        n[i] = 5;
340                        break;
341                    case 54:
342                        n[i] = 6;
343                        break;
344                    case 55:
345                        n[i] = 7;
346                        break;
347                    case 56:
348                        n[i] = 8;
349                        break;
350                    case 57:
351                        n[i] = 9;
352                        break;                      
353                    case 97:
354                        n[i] = 10;
355                        break;
356                    case 98:
357                        n[i] = 11;
358                        break;
359                    case 99:
360                        n[i] = 12;
361                        break;
362                    case 100:
363                        n[i] = 13;
364                        break;
365                    case 101:
366                        n[i] = 14;
367                        break;
368                    case 102:
369                        n[i] = 15;
370                        break;
371                }
372                
373                sum = sum + n[i]*koef;
374                koef=koef*16;
375            }
376            return sum;
377        } */
378        
379        public static void main(String[] args) throws ExpressionException {
380            System.out.println(toColor("White"));
381            System.out.println(toColor("#fff"));
382            System.out.println(toColor("#ffffff"));
383            System.out.println(toColor("#ffffffff"));
384            System.out.println(toColor("fff"));
385            System.out.println(toColor("ffffff"));
386            System.out.println(toColor("rgb(255,255,255)"));
387            }
388    
389            public static String toHexString(Color color) {
390                    return "#"+toHexString(color.getRed())+
391                                    toHexString(color.getGreen())+
392                                    toHexString(color.getBlue());
393                    
394            }
395            private static String toHexString(int clr) {
396                    String str = Integer.toHexString(clr);
397                    if(str.length()==1)return "0"+str;
398                    return str;
399            }
400    
401    }