Code

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