Code

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