1 | /*
|
---|
2 | * SVG Salamander
|
---|
3 | * Copyright (c) 2004, Mark McKay
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or
|
---|
7 | * without modification, are permitted provided that the following
|
---|
8 | * conditions are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above
|
---|
11 | * copyright notice, this list of conditions and the following
|
---|
12 | * disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above
|
---|
14 | * copyright notice, this list of conditions and the following
|
---|
15 | * disclaimer in the documentation and/or other materials
|
---|
16 | * provided with the distribution.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
---|
22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
---|
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
---|
25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
---|
27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
---|
29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
30 | *
|
---|
31 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
---|
32 | * projects can be found at http://www.kitfox.com
|
---|
33 | *
|
---|
34 | * Created on January 26, 2004, 4:34 AM
|
---|
35 | */
|
---|
36 |
|
---|
37 | package com.kitfox.svg.xml;
|
---|
38 |
|
---|
39 | import java.awt.*;
|
---|
40 | import java.util.*;
|
---|
41 | import java.util.regex.Matcher;
|
---|
42 | import java.util.regex.Pattern;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * @author Mark McKay
|
---|
46 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
47 | */
|
---|
48 | public class ColorTable
|
---|
49 | {
|
---|
50 |
|
---|
51 | static final Map<String, Color> colorTable;
|
---|
52 | static {
|
---|
53 | HashMap<String, Color> table = new HashMap<>();
|
---|
54 |
|
---|
55 | //We really should be interpreting the currentColor keyword as
|
---|
56 | // a reference to the referring node's color, but this quick hack
|
---|
57 | // will stop the program from crashing.
|
---|
58 | table.put("currentcolor", new Color(0x0));
|
---|
59 |
|
---|
60 | table.put("aliceblue", new Color(0xf0f8ff));
|
---|
61 | table.put("antiquewhite", new Color(0xfaebd7));
|
---|
62 | table.put("aqua", new Color(0x00ffff));
|
---|
63 | table.put("aquamarine", new Color(0x7fffd4));
|
---|
64 | table.put("azure", new Color(0xf0ffff));
|
---|
65 | table.put("beige", new Color(0xf5f5dc));
|
---|
66 | table.put("bisque", new Color(0xffe4c4));
|
---|
67 | table.put("black", new Color(0x000000));
|
---|
68 | table.put("blanchedalmond", new Color(0xffebcd));
|
---|
69 | table.put("blue", new Color(0x0000ff));
|
---|
70 | table.put("blueviolet", new Color(0x8a2be2));
|
---|
71 | table.put("brown", new Color(0xa52a2a));
|
---|
72 | table.put("burlywood", new Color(0xdeb887));
|
---|
73 | table.put("cadetblue", new Color(0x5f9ea0));
|
---|
74 | table.put("chartreuse", new Color(0x7fff00));
|
---|
75 | table.put("chocolate", new Color(0xd2691e));
|
---|
76 | table.put("coral", new Color(0xff7f50));
|
---|
77 | table.put("cornflowerblue", new Color(0x6495ed));
|
---|
78 | table.put("cornsilk", new Color(0xfff8dc));
|
---|
79 | table.put("crimson", new Color(0xdc143c));
|
---|
80 | table.put("cyan", new Color(0x00ffff));
|
---|
81 | table.put("darkblue", new Color(0x00008b));
|
---|
82 | table.put("darkcyan", new Color(0x008b8b));
|
---|
83 | table.put("darkgoldenrod", new Color(0xb8860b));
|
---|
84 | table.put("darkgray", new Color(0xa9a9a9));
|
---|
85 | table.put("darkgreen", new Color(0x006400));
|
---|
86 | table.put("darkkhaki", new Color(0xbdb76b));
|
---|
87 | table.put("darkmagenta", new Color(0x8b008b));
|
---|
88 | table.put("darkolivegreen", new Color(0x556b2f));
|
---|
89 | table.put("darkorange", new Color(0xff8c00));
|
---|
90 | table.put("darkorchid", new Color(0x9932cc));
|
---|
91 | table.put("darkred", new Color(0x8b0000));
|
---|
92 | table.put("darksalmon", new Color(0xe9967a));
|
---|
93 | table.put("darkseagreen", new Color(0x8fbc8f));
|
---|
94 | table.put("darkslateblue", new Color(0x483d8b));
|
---|
95 | table.put("darkslategray", new Color(0x2f4f4f));
|
---|
96 | table.put("darkturquoise", new Color(0x00ced1));
|
---|
97 | table.put("darkviolet", new Color(0x9400d3));
|
---|
98 | table.put("deeppink", new Color(0xff1493));
|
---|
99 | table.put("deepskyblue", new Color(0x00bfff));
|
---|
100 | table.put("dimgray", new Color(0x696969));
|
---|
101 | table.put("dodgerblue", new Color(0x1e90ff));
|
---|
102 | table.put("feldspar", new Color(0xd19275));
|
---|
103 | table.put("firebrick", new Color(0xb22222));
|
---|
104 | table.put("floralwhite", new Color(0xfffaf0));
|
---|
105 | table.put("forestgreen", new Color(0x228b22));
|
---|
106 | table.put("fuchsia", new Color(0xff00ff));
|
---|
107 | table.put("gainsboro", new Color(0xdcdcdc));
|
---|
108 | table.put("ghostwhite", new Color(0xf8f8ff));
|
---|
109 | table.put("gold", new Color(0xffd700));
|
---|
110 | table.put("goldenrod", new Color(0xdaa520));
|
---|
111 | table.put("gray", new Color(0x808080));
|
---|
112 | table.put("green", new Color(0x008000));
|
---|
113 | table.put("greenyellow", new Color(0xadff2f));
|
---|
114 | table.put("honeydew", new Color(0xf0fff0));
|
---|
115 | table.put("hotpink", new Color(0xff69b4));
|
---|
116 | table.put("indianred", new Color(0xcd5c5c));
|
---|
117 | table.put("indigo", new Color(0x4b0082));
|
---|
118 | table.put("ivory", new Color(0xfffff0));
|
---|
119 | table.put("khaki", new Color(0xf0e68c));
|
---|
120 | table.put("lavender", new Color(0xe6e6fa));
|
---|
121 | table.put("lavenderblush", new Color(0xfff0f5));
|
---|
122 | table.put("lawngreen", new Color(0x7cfc00));
|
---|
123 | table.put("lemonchiffon", new Color(0xfffacd));
|
---|
124 | table.put("lightblue", new Color(0xadd8e6));
|
---|
125 | table.put("lightcoral", new Color(0xf08080));
|
---|
126 | table.put("lightcyan", new Color(0xe0ffff));
|
---|
127 | table.put("lightgoldenrodyellow", new Color(0xfafad2));
|
---|
128 | table.put("lightgrey", new Color(0xd3d3d3));
|
---|
129 | table.put("lightgreen", new Color(0x90ee90));
|
---|
130 | table.put("lightpink", new Color(0xffb6c1));
|
---|
131 | table.put("lightsalmon", new Color(0xffa07a));
|
---|
132 | table.put("lightseagreen", new Color(0x20b2aa));
|
---|
133 | table.put("lightskyblue", new Color(0x87cefa));
|
---|
134 | table.put("lightslateblue", new Color(0x8470ff));
|
---|
135 | table.put("lightslategray", new Color(0x778899));
|
---|
136 | table.put("lightsteelblue", new Color(0xb0c4de));
|
---|
137 | table.put("lightyellow", new Color(0xffffe0));
|
---|
138 | table.put("lime", new Color(0x00ff00));
|
---|
139 | table.put("limegreen", new Color(0x32cd32));
|
---|
140 | table.put("linen", new Color(0xfaf0e6));
|
---|
141 | table.put("magenta", new Color(0xff00ff));
|
---|
142 | table.put("maroon", new Color(0x800000));
|
---|
143 | table.put("mediumaquamarine", new Color(0x66cdaa));
|
---|
144 | table.put("mediumblue", new Color(0x0000cd));
|
---|
145 | table.put("mediumorchid", new Color(0xba55d3));
|
---|
146 | table.put("mediumpurple", new Color(0x9370d8));
|
---|
147 | table.put("mediumseagreen", new Color(0x3cb371));
|
---|
148 | table.put("mediumslateblue", new Color(0x7b68ee));
|
---|
149 | table.put("mediumspringgreen", new Color(0x00fa9a));
|
---|
150 | table.put("mediumturquoise", new Color(0x48d1cc));
|
---|
151 | table.put("mediumvioletred", new Color(0xc71585));
|
---|
152 | table.put("midnightblue", new Color(0x191970));
|
---|
153 | table.put("mintcream", new Color(0xf5fffa));
|
---|
154 | table.put("mistyrose", new Color(0xffe4e1));
|
---|
155 | table.put("moccasin", new Color(0xffe4b5));
|
---|
156 | table.put("navajowhite", new Color(0xffdead));
|
---|
157 | table.put("navy", new Color(0x000080));
|
---|
158 | table.put("oldlace", new Color(0xfdf5e6));
|
---|
159 | table.put("olive", new Color(0x808000));
|
---|
160 | table.put("olivedrab", new Color(0x6b8e23));
|
---|
161 | table.put("orange", new Color(0xffa500));
|
---|
162 | table.put("orangered", new Color(0xff4500));
|
---|
163 | table.put("orchid", new Color(0xda70d6));
|
---|
164 | table.put("palegoldenrod", new Color(0xeee8aa));
|
---|
165 | table.put("palegreen", new Color(0x98fb98));
|
---|
166 | table.put("paleturquoise", new Color(0xafeeee));
|
---|
167 | table.put("palevioletred", new Color(0xd87093));
|
---|
168 | table.put("papayawhip", new Color(0xffefd5));
|
---|
169 | table.put("peachpuff", new Color(0xffdab9));
|
---|
170 | table.put("peru", new Color(0xcd853f));
|
---|
171 | table.put("pink", new Color(0xffc0cb));
|
---|
172 | table.put("plum", new Color(0xdda0dd));
|
---|
173 | table.put("powderblue", new Color(0xb0e0e6));
|
---|
174 | table.put("purple", new Color(0x800080));
|
---|
175 | table.put("red", new Color(0xff0000));
|
---|
176 | table.put("rosybrown", new Color(0xbc8f8f));
|
---|
177 | table.put("royalblue", new Color(0x4169e1));
|
---|
178 | table.put("saddlebrown", new Color(0x8b4513));
|
---|
179 | table.put("salmon", new Color(0xfa8072));
|
---|
180 | table.put("sandybrown", new Color(0xf4a460));
|
---|
181 | table.put("seagreen", new Color(0x2e8b57));
|
---|
182 | table.put("seashell", new Color(0xfff5ee));
|
---|
183 | table.put("sienna", new Color(0xa0522d));
|
---|
184 | table.put("silver", new Color(0xc0c0c0));
|
---|
185 | table.put("skyblue", new Color(0x87ceeb));
|
---|
186 | table.put("slateblue", new Color(0x6a5acd));
|
---|
187 | table.put("slategray", new Color(0x708090));
|
---|
188 | table.put("snow", new Color(0xfffafa));
|
---|
189 | table.put("springgreen", new Color(0x00ff7f));
|
---|
190 | table.put("steelblue", new Color(0x4682b4));
|
---|
191 | table.put("tan", new Color(0xd2b48c));
|
---|
192 | table.put("teal", new Color(0x008080));
|
---|
193 | table.put("thistle", new Color(0xd8bfd8));
|
---|
194 | table.put("tomato", new Color(0xff6347));
|
---|
195 | table.put("turquoise", new Color(0x40e0d0));
|
---|
196 | table.put("violet", new Color(0xee82ee));
|
---|
197 | table.put("violetred", new Color(0xd02090));
|
---|
198 | table.put("wheat", new Color(0xf5deb3));
|
---|
199 | table.put("white", new Color(0xffffff));
|
---|
200 | table.put("whitesmoke", new Color(0xf5f5f5));
|
---|
201 | table.put("yellow", new Color(0xffff00));
|
---|
202 | table.put("yellowgreen", new Color(0x9acd32));
|
---|
203 |
|
---|
204 | colorTable = Collections.unmodifiableMap(table);
|
---|
205 | }
|
---|
206 |
|
---|
207 | static ColorTable singleton = new ColorTable();
|
---|
208 |
|
---|
209 | /** Creates a new instance of ColorTable */
|
---|
210 | protected ColorTable() {
|
---|
211 | // buildColorList();
|
---|
212 | }
|
---|
213 |
|
---|
214 | static public ColorTable instance() { return singleton; }
|
---|
215 |
|
---|
216 | public Color lookupColor(String name) {
|
---|
217 | Object obj = colorTable.get(name.toLowerCase());
|
---|
218 | if (obj == null) return null;
|
---|
219 |
|
---|
220 | return (Color)obj;
|
---|
221 | }
|
---|
222 |
|
---|
223 | public static Color parseColor(String val)
|
---|
224 | {
|
---|
225 | Color retVal = null;
|
---|
226 |
|
---|
227 | if ("".equals(val))
|
---|
228 | {
|
---|
229 | return null;
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (val.charAt(0) == '#')
|
---|
233 | {
|
---|
234 | String hexStrn = val.substring(1);
|
---|
235 |
|
---|
236 | if (hexStrn.length() == 3)
|
---|
237 | {
|
---|
238 | hexStrn = "" + hexStrn.charAt(0) + hexStrn.charAt(0) + hexStrn.charAt(1) + hexStrn.charAt(1) + hexStrn.charAt(2) + hexStrn.charAt(2);
|
---|
239 | }
|
---|
240 | int hexVal = parseHex(hexStrn);
|
---|
241 |
|
---|
242 | retVal = new Color(hexVal);
|
---|
243 | }
|
---|
244 | else
|
---|
245 | {
|
---|
246 | final String number = "\\s*(((\\d+)(\\.\\d*)?)|(\\.\\d+))(%)?\\s*";
|
---|
247 | final Matcher rgbMatch = Pattern.compile("rgb\\(" + number + "," + number + "," + number + "\\)", Pattern.CASE_INSENSITIVE).matcher("");
|
---|
248 |
|
---|
249 | rgbMatch.reset(val);
|
---|
250 | if (rgbMatch.matches())
|
---|
251 | {
|
---|
252 | float rr = Float.parseFloat(rgbMatch.group(1));
|
---|
253 | float gg = Float.parseFloat(rgbMatch.group(7));
|
---|
254 | float bb = Float.parseFloat(rgbMatch.group(13));
|
---|
255 | rr /= "%".equals(rgbMatch.group(6)) ? 100 : 255;
|
---|
256 | gg /= "%".equals(rgbMatch.group(12)) ? 100 : 255;
|
---|
257 | bb /= "%".equals(rgbMatch.group(18)) ? 100 : 255;
|
---|
258 | retVal = new Color(rr, gg, bb);
|
---|
259 | }
|
---|
260 | else
|
---|
261 | {
|
---|
262 | Color lookupCol = ColorTable.instance().lookupColor(val);
|
---|
263 | if (lookupCol != null) retVal = lookupCol;
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | return retVal;
|
---|
268 | }
|
---|
269 |
|
---|
270 | public static int parseHex(String val)
|
---|
271 | {
|
---|
272 | int retVal = 0;
|
---|
273 |
|
---|
274 | for (int i = 0; i < val.length(); i++)
|
---|
275 | {
|
---|
276 | retVal <<= 4;
|
---|
277 |
|
---|
278 | char ch = val.charAt(i);
|
---|
279 | if (ch >= '0' && ch <= '9')
|
---|
280 | {
|
---|
281 | retVal |= ch - '0';
|
---|
282 | }
|
---|
283 | else if (ch >= 'a' && ch <= 'z')
|
---|
284 | {
|
---|
285 | retVal |= ch - 'a' + 10;
|
---|
286 | }
|
---|
287 | else if (ch >= 'A' && ch <= 'Z')
|
---|
288 | {
|
---|
289 | retVal |= ch - 'A' + 10;
|
---|
290 | }
|
---|
291 | else throw new RuntimeException();
|
---|
292 | }
|
---|
293 |
|
---|
294 | return retVal;
|
---|
295 | }
|
---|
296 |
|
---|
297 | }
|
---|