89c273fe57e7dee37b06e4aa5f66ce6772129b52
1 #define __SP_SVG_COLOR_C__
3 /**
4 * \file
5 * Reading \& writing of SVG/CSS colors.
6 */
7 /*
8 * Authors:
9 * Lauris Kaplinski <lauris@kaplinski.com>
10 *
11 * Copyright (C) 1999-2002 Lauris Kaplinski
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
20 #include "svg-color.h"
21 #include <math.h>
22 #include <glib/gmessages.h>
23 #include <glib/gstrfuncs.h>
24 #include <glib/ghash.h>
25 #include <glib/gutils.h>
26 #include "strneq.h"
28 struct SPSVGColor {
29 unsigned long rgb;
30 char const *name;
31 };
33 /*
34 * These are the colors defined in the SVG standard
35 */
36 static SPSVGColor const sp_svg_color_named[] = {
37 { 0xF0F8FF, "aliceblue" },
38 { 0xFAEBD7, "antiquewhite" },
39 { 0x00FFFF, "aqua" },
40 { 0x7FFFD4, "aquamarine" },
41 { 0xF0FFFF, "azure" },
42 { 0xF5F5DC, "beige" },
43 { 0xFFE4C4, "bisque" },
44 { 0x000000, "black" },
45 { 0xFFEBCD, "blanchedalmond" },
46 { 0x0000FF, "blue" },
47 { 0x8A2BE2, "blueviolet" },
48 { 0xA52A2A, "brown" },
49 { 0xDEB887, "burlywood" },
50 { 0x5F9EA0, "cadetblue" },
51 { 0x7FFF00, "chartreuse" },
52 { 0xD2691E, "chocolate" },
53 { 0xFF7F50, "coral" },
54 { 0x6495ED, "cornflowerblue" },
55 { 0xFFF8DC, "cornsilk" },
56 { 0xDC143C, "crimson" },
57 { 0x00FFFF, "cyan" },
58 { 0x00008B, "darkblue" },
59 { 0x008B8B, "darkcyan" },
60 { 0xB8860B, "darkgoldenrod" },
61 { 0xA9A9A9, "darkgray" },
62 { 0x006400, "darkgreen" },
63 { 0xA9A9A9, "darkgrey" },
64 { 0xBDB76B, "darkkhaki" },
65 { 0x8B008B, "darkmagenta" },
66 { 0x556B2F, "darkolivegreen" },
67 { 0xFF8C00, "darkorange" },
68 { 0x9932CC, "darkorchid" },
69 { 0x8B0000, "darkred" },
70 { 0xE9967A, "darksalmon" },
71 { 0x8FBC8F, "darkseagreen" },
72 { 0x483D8B, "darkslateblue" },
73 { 0x2F4F4F, "darkslategray" },
74 { 0x2F4F4F, "darkslategrey" },
75 { 0x00CED1, "darkturquoise" },
76 { 0x9400D3, "darkviolet" },
77 { 0xFF1493, "deeppink" },
78 { 0x00BFFF, "deepskyblue" },
79 { 0x696969, "dimgray" },
80 { 0x696969, "dimgrey" },
81 { 0x1E90FF, "dodgerblue" },
82 { 0xB22222, "firebrick" },
83 { 0xFFFAF0, "floralwhite" },
84 { 0x228B22, "forestgreen" },
85 { 0xFF00FF, "fuchsia" },
86 { 0xDCDCDC, "gainsboro" },
87 { 0xF8F8FF, "ghostwhite" },
88 { 0xFFD700, "gold" },
89 { 0xDAA520, "goldenrod" },
90 { 0x808080, "gray" },
91 { 0x808080, "grey" },
92 { 0x008000, "green" },
93 { 0xADFF2F, "greenyellow" },
94 { 0xF0FFF0, "honeydew" },
95 { 0xFF69B4, "hotpink" },
96 { 0xCD5C5C, "indianred" },
97 { 0x4B0082, "indigo" },
98 { 0xFFFFF0, "ivory" },
99 { 0xF0E68C, "khaki" },
100 { 0xE6E6FA, "lavender" },
101 { 0xFFF0F5, "lavenderblush" },
102 { 0x7CFC00, "lawngreen" },
103 { 0xFFFACD, "lemonchiffon" },
104 { 0xADD8E6, "lightblue" },
105 { 0xF08080, "lightcoral" },
106 { 0xE0FFFF, "lightcyan" },
107 { 0xFAFAD2, "lightgoldenrodyellow" },
108 { 0xD3D3D3, "lightgray" },
109 { 0x90EE90, "lightgreen" },
110 { 0xD3D3D3, "lightgrey" },
111 { 0xFFB6C1, "lightpink" },
112 { 0xFFA07A, "lightsalmon" },
113 { 0x20B2AA, "lightseagreen" },
114 { 0x87CEFA, "lightskyblue" },
115 { 0x778899, "lightslategray" },
116 { 0x778899, "lightslategrey" },
117 { 0xB0C4DE, "lightsteelblue" },
118 { 0xFFFFE0, "lightyellow" },
119 { 0x00FF00, "lime" },
120 { 0x32CD32, "limegreen" },
121 { 0xFAF0E6, "linen" },
122 { 0xFF00FF, "magenta" },
123 { 0x800000, "maroon" },
124 { 0x66CDAA, "mediumaquamarine" },
125 { 0x0000CD, "mediumblue" },
126 { 0xBA55D3, "mediumorchid" },
127 { 0x9370DB, "mediumpurple" },
128 { 0x3CB371, "mediumseagreen" },
129 { 0x7B68EE, "mediumslateblue" },
130 { 0x00FA9A, "mediumspringgreen" },
131 { 0x48D1CC, "mediumturquoise" },
132 { 0xC71585, "mediumvioletred" },
133 { 0x191970, "midnightblue" },
134 { 0xF5FFFA, "mintcream" },
135 { 0xFFE4E1, "mistyrose" },
136 { 0xFFE4B5, "moccasin" },
137 { 0xFFDEAD, "navajowhite" },
138 { 0x000080, "navy" },
139 { 0xFDF5E6, "oldlace" },
140 { 0x808000, "olive" },
141 { 0x6B8E23, "olivedrab" },
142 { 0xFFA500, "orange" },
143 { 0xFF4500, "orangered" },
144 { 0xDA70D6, "orchid" },
145 { 0xEEE8AA, "palegoldenrod" },
146 { 0x98FB98, "palegreen" },
147 { 0xAFEEEE, "paleturquoise" },
148 { 0xDB7093, "palevioletred" },
149 { 0xFFEFD5, "papayawhip" },
150 { 0xFFDAB9, "peachpuff" },
151 { 0xCD853F, "peru" },
152 { 0xFFC0CB, "pink" },
153 { 0xDDA0DD, "plum" },
154 { 0xB0E0E6, "powderblue" },
155 { 0x800080, "purple" },
156 { 0xFF0000, "red" },
157 { 0xBC8F8F, "rosybrown" },
158 { 0x4169E1, "royalblue" },
159 { 0x8B4513, "saddlebrown" },
160 { 0xFA8072, "salmon" },
161 { 0xF4A460, "sandybrown" },
162 { 0x2E8B57, "seagreen" },
163 { 0xFFF5EE, "seashell" },
164 { 0xA0522D, "sienna" },
165 { 0xC0C0C0, "silver" },
166 { 0x87CEEB, "skyblue" },
167 { 0x6A5ACD, "slateblue" },
168 { 0x708090, "slategray" },
169 { 0x708090, "slategrey" },
170 { 0xFFFAFA, "snow" },
171 { 0x00FF7F, "springgreen" },
172 { 0x4682B4, "steelblue" },
173 { 0xD2B48C, "tan" },
174 { 0x008080, "teal" },
175 { 0xD8BFD8, "thistle" },
176 { 0xFF6347, "tomato" },
177 { 0x40E0D0, "turquoise" },
178 { 0xEE82EE, "violet" },
179 { 0xF5DEB3, "wheat" },
180 { 0xFFFFFF, "white" },
181 { 0xF5F5F5, "whitesmoke" },
182 { 0xFFFF00, "yellow" },
183 { 0x9ACD32, "yellowgreen" }
184 };
186 static GHashTable *sp_svg_create_color_hash();
188 guint32
189 sp_svg_read_color(gchar const *str, guint32 def)
190 {
191 static GHashTable *colors = NULL;
192 gchar c[32];
193 guint32 val = 0;
195 if (str == NULL) return def;
196 while ((*str <= ' ') && *str) str++;
197 if (!*str) return def;
199 if (str[0] == '#') {
200 gint i;
201 for (i = 1; str[i]; i++) {
202 int hexval;
203 if (str[i] >= '0' && str[i] <= '9')
204 hexval = str[i] - '0';
205 else if (str[i] >= 'A' && str[i] <= 'F')
206 hexval = str[i] - 'A' + 10;
207 else if (str[i] >= 'a' && str[i] <= 'f')
208 hexval = str[i] - 'a' + 10;
209 else
210 break;
211 val = (val << 4) + hexval;
212 }
213 /* handle #rgb case */
214 if (i == 1 + 3) {
215 val = ((val & 0xf00) << 8) |
216 ((val & 0x0f0) << 4) |
217 (val & 0x00f);
218 val |= val << 4;
219 } else if (i != 1 + 6) {
220 /* must be either 3 or 6 digits. */
221 return def;
222 }
223 } else if (strneq(str, "rgb(", 4)) {
224 gboolean hasp, hasd;
225 gchar *s, *e;
226 gdouble r, g, b;
228 s = (gchar *) str + 4;
229 hasp = FALSE;
230 hasd = FALSE;
232 r = g_ascii_strtod(s, &e);
233 if (s == e) return def;
234 s = e;
235 if (*s == '%') {
236 hasp = TRUE;
237 s += 1;
238 } else {
239 hasd = TRUE;
240 }
241 while (*s && g_ascii_isspace(*s)) s += 1;
242 if (*s != ',') return def;
243 s += 1;
244 while (*s && g_ascii_isspace(*s)) s += 1;
245 g = g_ascii_strtod(s, &e);
246 if (s == e) return def;
247 s = e;
248 if (*s == '%') {
249 hasp = TRUE;
250 s += 1;
251 } else {
252 hasd = TRUE;
253 }
254 while (*s && g_ascii_isspace(*s)) s += 1;
255 if (*s != ',') return def;
256 s += 1;
257 while (*s && g_ascii_isspace(*s)) s += 1;
258 b = g_ascii_strtod(s, &e);
259 if (s == e) return def;
260 s = e;
261 if (*s == '%') {
262 hasp = TRUE;
263 } else {
264 hasd = TRUE;
265 }
266 if (hasp && hasd) return def;
267 if (hasp) {
268 val = (guint) floor(CLAMP(r, 0.0, 100.0) * 2.559999) << 24;
269 val |= ((guint) floor(CLAMP(g, 0.0, 100.0) * 2.559999) << 16);
270 val |= ((guint) floor(CLAMP(b, 0.0, 100.0) * 2.559999) << 8);
271 } else {
272 val = (guint) CLAMP(r, 0, 255) << 24;
273 val |= ((guint) CLAMP(g, 0, 255) << 16);
274 val |= ((guint) CLAMP(b, 0, 255) << 8);
275 }
276 return val;
277 } else {
278 gint i;
279 if (!colors) {
280 colors = sp_svg_create_color_hash();
281 }
282 for (i = 0; i < 31; i++) {
283 if (str[i] == ';') {
284 c[i] = '\0';
285 break;
286 }
287 c[i] = g_ascii_tolower(str[i]);
288 if (!str[i]) break;
289 }
290 c[31] = '\0';
292 gpointer const rgb_ptr = g_hash_table_lookup(colors, c);
293 if (rgb_ptr) {
294 val = *(static_cast<unsigned long *>(rgb_ptr));
295 } else {
296 return def;
297 }
298 }
300 return (val << 8);
301 }
303 /**
304 * \pre buflen \>= 8.
305 */
306 gint
307 sp_svg_write_color(gchar *buf, unsigned const buflen, guint32 const color)
308 {
309 g_assert(8 <= buflen);
310 return g_snprintf(buf, buflen, "#%06x", color >> 8);
311 }
313 static GHashTable *
314 sp_svg_create_color_hash()
315 {
316 GHashTable *colors = g_hash_table_new(g_str_hash, g_str_equal);
318 for (unsigned i = 0 ; i < G_N_ELEMENTS(sp_svg_color_named) ; i++) {
319 g_hash_table_insert(colors,
320 (gpointer)(sp_svg_color_named[i].name),
321 (gpointer)(&sp_svg_color_named[i].rgb));
322 }
324 return colors;
325 }
327 /*
328 Local Variables:
329 mode:c++
330 c-file-style:"stroustrup"
331 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
332 indent-tabs-mode:nil
333 fill-column:99
334 End:
335 */
336 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :