001    /*
002     * ColorTable.java
003     *
004     *
005     *  The Salamander Project - 2D and 3D graphics libraries in Java
006     *  Copyright (C) 2004 Mark McKay
007     *
008     *  This library is free software; you can redistribute it and/or
009     *  modify it under the terms of the GNU Lesser General Public
010     *  License as published by the Free Software Foundation; either
011     *  version 2.1 of the License, or (at your option) any later version.
012     *
013     *  This library is distributed in the hope that it will be useful,
014     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
015     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016     *  Lesser General Public License for more details.
017     *
018     *  You should have received a copy of the GNU Lesser General Public
019     *  License along with this library; if not, write to the Free Software
020     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021     *
022     *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
023     *  projects can be found at http://www.kitfox.com
024     *
025     * Created on January 26, 2004, 4:34 AM
026     */
027    
028    package com.kitfox.svg.xml;
029    
030    import java.awt.*;
031    import java.util.*;
032    import java.util.regex.Matcher;
033    import java.util.regex.Pattern;
034    
035    /**
036     * @author Mark McKay
037     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
038     */
039    public class ColorTable 
040    {
041    
042        static final Map colorTable;
043        static {
044            HashMap table = new HashMap();
045    
046            //We really should be interpreting the currentColor keyword as 
047            // a reference to the referring node's color, but this quick hack 
048            // will stop the program from crashing.
049            table.put("currentcolor", new Color(0x0));
050    
051            table.put("aliceblue", new Color(0xf0f8ff));
052            table.put("antiquewhite", new Color(0xfaebd7));
053            table.put("aqua", new Color(0x00ffff));
054            table.put("aquamarine", new Color(0x7fffd4));
055            table.put("azure", new Color(0xf0ffff));
056            table.put("beige", new Color(0xf5f5dc));
057            table.put("bisque", new Color(0xffe4c4));
058            table.put("black", new Color(0x000000));
059            table.put("blanchedalmond", new Color(0xffebcd));
060            table.put("blue", new Color(0x0000ff));
061            table.put("blueviolet", new Color(0x8a2be2));
062            table.put("brown", new Color(0xa52a2a));
063            table.put("burlywood", new Color(0xdeb887));
064            table.put("cadetblue", new Color(0x5f9ea0));
065            table.put("chartreuse", new Color(0x7fff00));
066            table.put("chocolate", new Color(0xd2691e));
067            table.put("coral", new Color(0xff7f50));
068            table.put("cornflowerblue", new Color(0x6495ed));
069            table.put("cornsilk", new Color(0xfff8dc));
070            table.put("crimson", new Color(0xdc143c));
071            table.put("cyan", new Color(0x00ffff));
072            table.put("darkblue", new Color(0x00008b));
073            table.put("darkcyan", new Color(0x008b8b));
074            table.put("darkgoldenrod", new Color(0xb8860b));
075            table.put("darkgray", new Color(0xa9a9a9));
076            table.put("darkgreen", new Color(0x006400));
077            table.put("darkkhaki", new Color(0xbdb76b));
078            table.put("darkmagenta", new Color(0x8b008b));
079            table.put("darkolivegreen", new Color(0x556b2f));
080            table.put("darkorange", new Color(0xff8c00));
081            table.put("darkorchid", new Color(0x9932cc));
082            table.put("darkred", new Color(0x8b0000));
083            table.put("darksalmon", new Color(0xe9967a));
084            table.put("darkseagreen", new Color(0x8fbc8f));
085            table.put("darkslateblue", new Color(0x483d8b));
086            table.put("darkslategray", new Color(0x2f4f4f));
087            table.put("darkturquoise", new Color(0x00ced1));
088            table.put("darkviolet", new Color(0x9400d3));
089            table.put("deeppink", new Color(0xff1493));
090            table.put("deepskyblue", new Color(0x00bfff));
091            table.put("dimgray", new Color(0x696969));
092            table.put("dodgerblue", new Color(0x1e90ff));
093            table.put("feldspar", new Color(0xd19275));
094            table.put("firebrick", new Color(0xb22222));
095            table.put("floralwhite", new Color(0xfffaf0));
096            table.put("forestgreen", new Color(0x228b22));
097            table.put("fuchsia", new Color(0xff00ff));
098            table.put("gainsboro", new Color(0xdcdcdc));
099            table.put("ghostwhite", new Color(0xf8f8ff));
100            table.put("gold", new Color(0xffd700));
101            table.put("goldenrod", new Color(0xdaa520));
102            table.put("gray", new Color(0x808080));
103            table.put("green", new Color(0x008000));
104            table.put("greenyellow", new Color(0xadff2f));
105            table.put("honeydew", new Color(0xf0fff0));
106            table.put("hotpink", new Color(0xff69b4));
107            table.put("indianred", new Color(0xcd5c5c));
108            table.put("indigo", new Color(0x4b0082));
109            table.put("ivory", new Color(0xfffff0));
110            table.put("khaki", new Color(0xf0e68c));
111            table.put("lavender", new Color(0xe6e6fa));
112            table.put("lavenderblush", new Color(0xfff0f5));
113            table.put("lawngreen", new Color(0x7cfc00));
114            table.put("lemonchiffon", new Color(0xfffacd));
115            table.put("lightblue", new Color(0xadd8e6));
116            table.put("lightcoral", new Color(0xf08080));
117            table.put("lightcyan", new Color(0xe0ffff));
118            table.put("lightgoldenrodyellow", new Color(0xfafad2));
119            table.put("lightgrey", new Color(0xd3d3d3));
120            table.put("lightgreen", new Color(0x90ee90));
121            table.put("lightpink", new Color(0xffb6c1));
122            table.put("lightsalmon", new Color(0xffa07a));
123            table.put("lightseagreen", new Color(0x20b2aa));
124            table.put("lightskyblue", new Color(0x87cefa));
125            table.put("lightslateblue", new Color(0x8470ff));
126            table.put("lightslategray", new Color(0x778899));
127            table.put("lightsteelblue", new Color(0xb0c4de));
128            table.put("lightyellow", new Color(0xffffe0));
129            table.put("lime", new Color(0x00ff00));
130            table.put("limegreen", new Color(0x32cd32));
131            table.put("linen", new Color(0xfaf0e6));
132            table.put("magenta", new Color(0xff00ff));
133            table.put("maroon", new Color(0x800000));
134            table.put("mediumaquamarine", new Color(0x66cdaa));
135            table.put("mediumblue", new Color(0x0000cd));
136            table.put("mediumorchid", new Color(0xba55d3));
137            table.put("mediumpurple", new Color(0x9370d8));
138            table.put("mediumseagreen", new Color(0x3cb371));
139            table.put("mediumslateblue", new Color(0x7b68ee));
140            table.put("mediumspringgreen", new Color(0x00fa9a));
141            table.put("mediumturquoise", new Color(0x48d1cc));
142            table.put("mediumvioletred", new Color(0xc71585));
143            table.put("midnightblue", new Color(0x191970));
144            table.put("mintcream", new Color(0xf5fffa));
145            table.put("mistyrose", new Color(0xffe4e1));
146            table.put("moccasin", new Color(0xffe4b5));
147            table.put("navajowhite", new Color(0xffdead));
148            table.put("navy", new Color(0x000080));
149            table.put("oldlace", new Color(0xfdf5e6));
150            table.put("olive", new Color(0x808000));
151            table.put("olivedrab", new Color(0x6b8e23));
152            table.put("orange", new Color(0xffa500));
153            table.put("orangered", new Color(0xff4500));
154            table.put("orchid", new Color(0xda70d6));
155            table.put("palegoldenrod", new Color(0xeee8aa));
156            table.put("palegreen", new Color(0x98fb98));
157            table.put("paleturquoise", new Color(0xafeeee));
158            table.put("palevioletred", new Color(0xd87093));
159            table.put("papayawhip", new Color(0xffefd5));
160            table.put("peachpuff", new Color(0xffdab9));
161            table.put("peru", new Color(0xcd853f));
162            table.put("pink", new Color(0xffc0cb));
163            table.put("plum", new Color(0xdda0dd));
164            table.put("powderblue", new Color(0xb0e0e6));
165            table.put("purple", new Color(0x800080));
166            table.put("red", new Color(0xff0000));
167            table.put("rosybrown", new Color(0xbc8f8f));
168            table.put("royalblue", new Color(0x4169e1));
169            table.put("saddlebrown", new Color(0x8b4513));
170            table.put("salmon", new Color(0xfa8072));
171            table.put("sandybrown", new Color(0xf4a460));
172            table.put("seagreen", new Color(0x2e8b57));
173            table.put("seashell", new Color(0xfff5ee));
174            table.put("sienna", new Color(0xa0522d));
175            table.put("silver", new Color(0xc0c0c0));
176            table.put("skyblue", new Color(0x87ceeb));
177            table.put("slateblue", new Color(0x6a5acd));
178            table.put("slategray", new Color(0x708090));
179            table.put("snow", new Color(0xfffafa));
180            table.put("springgreen", new Color(0x00ff7f));
181            table.put("steelblue", new Color(0x4682b4));
182            table.put("tan", new Color(0xd2b48c));
183            table.put("teal", new Color(0x008080));
184            table.put("thistle", new Color(0xd8bfd8));
185            table.put("tomato", new Color(0xff6347));
186            table.put("turquoise", new Color(0x40e0d0));
187            table.put("violet", new Color(0xee82ee));
188            table.put("violetred", new Color(0xd02090));
189            table.put("wheat", new Color(0xf5deb3));
190            table.put("white", new Color(0xffffff));
191            table.put("whitesmoke", new Color(0xf5f5f5));
192            table.put("yellow", new Color(0xffff00));
193            table.put("yellowgreen", new Color(0x9acd32));
194            
195            colorTable = Collections.unmodifiableMap(table);
196        }
197    
198        static ColorTable singleton = new ColorTable();
199    
200        /** Creates a new instance of ColorTable */
201        protected ColorTable() {
202    //        buildColorList();
203        }
204    
205        static public ColorTable instance() { return singleton; }
206    
207        public Color lookupColor(String name) {
208            Object obj = colorTable.get(name.toLowerCase());
209            if (obj == null) return null;
210    
211            return (Color)obj;
212        }
213    
214        public static Color parseColor(String val)
215        {
216            Color retVal = null;
217    
218            if (val.charAt(0) == '#')
219            {
220                String hexStrn = val.substring(1);
221                
222                if (hexStrn.length() == 3)
223                {
224                    hexStrn = "" + hexStrn.charAt(0) + hexStrn.charAt(0) + hexStrn.charAt(1) + hexStrn.charAt(1) + hexStrn.charAt(2) + hexStrn.charAt(2);
225                }
226                int hexVal = parseHex(hexStrn);
227    
228                retVal = new Color(hexVal);
229            }
230            else
231            {
232                final String number = "\\s*(((\\d+)(\\.\\d*)?)|(\\.\\d+))(%)?\\s*";
233                final Matcher rgbMatch = Pattern.compile("rgb\\(" + number + "," + number + "," + number + "\\)", Pattern.CASE_INSENSITIVE).matcher("");
234    
235                rgbMatch.reset(val);
236                if (rgbMatch.matches())
237                {
238                    float rr = Float.parseFloat(rgbMatch.group(1));
239                    float gg = Float.parseFloat(rgbMatch.group(7));
240                    float bb = Float.parseFloat(rgbMatch.group(13));
241                    rr /= "%".equals(rgbMatch.group(6)) ? 100 : 255;
242                    gg /= "%".equals(rgbMatch.group(12)) ? 100 : 255;
243                    bb /= "%".equals(rgbMatch.group(18)) ? 100 : 255;
244                    retVal = new Color(rr, gg, bb);
245                }
246                else
247                {
248                    Color lookupCol = ColorTable.instance().lookupColor(val);
249                    if (lookupCol != null) retVal = lookupCol;
250                }
251            }
252    
253            return retVal;
254        }
255    
256        public static int parseHex(String val)
257        {
258            int retVal = 0;
259            
260            for (int i = 0; i < val.length(); i++)
261            {
262                retVal <<= 4;
263                
264                char ch = val.charAt(i);
265                if (ch >= '0' && ch <= '9')
266                {
267                    retVal |= ch - '0';
268                }
269                else if (ch >= 'a' && ch <= 'z')
270                {
271                    retVal |= ch - 'a' + 10;
272                }
273                else if (ch >= 'A' && ch <= 'Z')
274                {
275                    retVal |= ch - 'A' + 10;
276                }
277                else throw new RuntimeException();
278            }
279            
280            return retVal;
281        }
282    
283    }