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 "svg-icc-color.h"
22 #include <cassert>
23 #include <math.h>
24 #include <glib/gmem.h>
25 #include <glib/gmessages.h>
26 #include <glib/gstrfuncs.h>
27 #include <glib/ghash.h>
28 #include <glib/gutils.h>
29 #include <cstdio> // sprintf
30 #include <errno.h>
31 #include "strneq.h"
32 using std::sprintf;
34 struct SPSVGColor {
35 unsigned long rgb;
36 char const *name;
37 };
39 /*
40 * These are the colors defined in the SVG standard
41 */
42 static SPSVGColor const sp_svg_color_named[] = {
43 { 0xF0F8FF, "aliceblue" },
44 { 0xFAEBD7, "antiquewhite" },
45 { 0x00FFFF, "aqua" },
46 { 0x7FFFD4, "aquamarine" },
47 { 0xF0FFFF, "azure" },
48 { 0xF5F5DC, "beige" },
49 { 0xFFE4C4, "bisque" },
50 { 0x000000, "black" },
51 { 0xFFEBCD, "blanchedalmond" },
52 { 0x0000FF, "blue" },
53 { 0x8A2BE2, "blueviolet" },
54 { 0xA52A2A, "brown" },
55 { 0xDEB887, "burlywood" },
56 { 0x5F9EA0, "cadetblue" },
57 { 0x7FFF00, "chartreuse" },
58 { 0xD2691E, "chocolate" },
59 { 0xFF7F50, "coral" },
60 { 0x6495ED, "cornflowerblue" },
61 { 0xFFF8DC, "cornsilk" },
62 { 0xDC143C, "crimson" },
63 { 0x00FFFF, "cyan" },
64 { 0x00008B, "darkblue" },
65 { 0x008B8B, "darkcyan" },
66 { 0xB8860B, "darkgoldenrod" },
67 { 0xA9A9A9, "darkgray" },
68 { 0x006400, "darkgreen" },
69 { 0xA9A9A9, "darkgrey" },
70 { 0xBDB76B, "darkkhaki" },
71 { 0x8B008B, "darkmagenta" },
72 { 0x556B2F, "darkolivegreen" },
73 { 0xFF8C00, "darkorange" },
74 { 0x9932CC, "darkorchid" },
75 { 0x8B0000, "darkred" },
76 { 0xE9967A, "darksalmon" },
77 { 0x8FBC8F, "darkseagreen" },
78 { 0x483D8B, "darkslateblue" },
79 { 0x2F4F4F, "darkslategray" },
80 { 0x2F4F4F, "darkslategrey" },
81 { 0x00CED1, "darkturquoise" },
82 { 0x9400D3, "darkviolet" },
83 { 0xFF1493, "deeppink" },
84 { 0x00BFFF, "deepskyblue" },
85 { 0x696969, "dimgray" },
86 { 0x696969, "dimgrey" },
87 { 0x1E90FF, "dodgerblue" },
88 { 0xB22222, "firebrick" },
89 { 0xFFFAF0, "floralwhite" },
90 { 0x228B22, "forestgreen" },
91 { 0xFF00FF, "fuchsia" },
92 { 0xDCDCDC, "gainsboro" },
93 { 0xF8F8FF, "ghostwhite" },
94 { 0xFFD700, "gold" },
95 { 0xDAA520, "goldenrod" },
96 { 0x808080, "gray" },
97 { 0x808080, "grey" },
98 { 0x008000, "green" },
99 { 0xADFF2F, "greenyellow" },
100 { 0xF0FFF0, "honeydew" },
101 { 0xFF69B4, "hotpink" },
102 { 0xCD5C5C, "indianred" },
103 { 0x4B0082, "indigo" },
104 { 0xFFFFF0, "ivory" },
105 { 0xF0E68C, "khaki" },
106 { 0xE6E6FA, "lavender" },
107 { 0xFFF0F5, "lavenderblush" },
108 { 0x7CFC00, "lawngreen" },
109 { 0xFFFACD, "lemonchiffon" },
110 { 0xADD8E6, "lightblue" },
111 { 0xF08080, "lightcoral" },
112 { 0xE0FFFF, "lightcyan" },
113 { 0xFAFAD2, "lightgoldenrodyellow" },
114 { 0xD3D3D3, "lightgray" },
115 { 0x90EE90, "lightgreen" },
116 { 0xD3D3D3, "lightgrey" },
117 { 0xFFB6C1, "lightpink" },
118 { 0xFFA07A, "lightsalmon" },
119 { 0x20B2AA, "lightseagreen" },
120 { 0x87CEFA, "lightskyblue" },
121 { 0x778899, "lightslategray" },
122 { 0x778899, "lightslategrey" },
123 { 0xB0C4DE, "lightsteelblue" },
124 { 0xFFFFE0, "lightyellow" },
125 { 0x00FF00, "lime" },
126 { 0x32CD32, "limegreen" },
127 { 0xFAF0E6, "linen" },
128 { 0xFF00FF, "magenta" },
129 { 0x800000, "maroon" },
130 { 0x66CDAA, "mediumaquamarine" },
131 { 0x0000CD, "mediumblue" },
132 { 0xBA55D3, "mediumorchid" },
133 { 0x9370DB, "mediumpurple" },
134 { 0x3CB371, "mediumseagreen" },
135 { 0x7B68EE, "mediumslateblue" },
136 { 0x00FA9A, "mediumspringgreen" },
137 { 0x48D1CC, "mediumturquoise" },
138 { 0xC71585, "mediumvioletred" },
139 { 0x191970, "midnightblue" },
140 { 0xF5FFFA, "mintcream" },
141 { 0xFFE4E1, "mistyrose" },
142 { 0xFFE4B5, "moccasin" },
143 { 0xFFDEAD, "navajowhite" },
144 { 0x000080, "navy" },
145 { 0xFDF5E6, "oldlace" },
146 { 0x808000, "olive" },
147 { 0x6B8E23, "olivedrab" },
148 { 0xFFA500, "orange" },
149 { 0xFF4500, "orangered" },
150 { 0xDA70D6, "orchid" },
151 { 0xEEE8AA, "palegoldenrod" },
152 { 0x98FB98, "palegreen" },
153 { 0xAFEEEE, "paleturquoise" },
154 { 0xDB7093, "palevioletred" },
155 { 0xFFEFD5, "papayawhip" },
156 { 0xFFDAB9, "peachpuff" },
157 { 0xCD853F, "peru" },
158 { 0xFFC0CB, "pink" },
159 { 0xDDA0DD, "plum" },
160 { 0xB0E0E6, "powderblue" },
161 { 0x800080, "purple" },
162 { 0xFF0000, "red" },
163 { 0xBC8F8F, "rosybrown" },
164 { 0x4169E1, "royalblue" },
165 { 0x8B4513, "saddlebrown" },
166 { 0xFA8072, "salmon" },
167 { 0xF4A460, "sandybrown" },
168 { 0x2E8B57, "seagreen" },
169 { 0xFFF5EE, "seashell" },
170 { 0xA0522D, "sienna" },
171 { 0xC0C0C0, "silver" },
172 { 0x87CEEB, "skyblue" },
173 { 0x6A5ACD, "slateblue" },
174 { 0x708090, "slategray" },
175 { 0x708090, "slategrey" },
176 { 0xFFFAFA, "snow" },
177 { 0x00FF7F, "springgreen" },
178 { 0x4682B4, "steelblue" },
179 { 0xD2B48C, "tan" },
180 { 0x008080, "teal" },
181 { 0xD8BFD8, "thistle" },
182 { 0xFF6347, "tomato" },
183 { 0x40E0D0, "turquoise" },
184 { 0xEE82EE, "violet" },
185 { 0xF5DEB3, "wheat" },
186 { 0xFFFFFF, "white" },
187 { 0xF5F5F5, "whitesmoke" },
188 { 0xFFFF00, "yellow" },
189 { 0x9ACD32, "yellowgreen" }
190 };
192 static GHashTable *sp_svg_create_color_hash();
194 guint32
195 sp_svg_read_color(gchar const *str, guint32 const dfl)
196 {
197 return sp_svg_read_color(str, NULL, dfl);
198 }
200 static guint32
201 internal_sp_svg_read_color(gchar const *str, gchar const **end_ptr, guint32 def)
202 {
203 static GHashTable *colors = NULL;
204 guint32 val = 0;
206 if (str == NULL) return def;
207 while ((*str <= ' ') && *str) str++;
208 if (!*str) return def;
210 if (str[0] == '#') {
211 gint i;
212 for (i = 1; str[i]; i++) {
213 int hexval;
214 if (str[i] >= '0' && str[i] <= '9')
215 hexval = str[i] - '0';
216 else if (str[i] >= 'A' && str[i] <= 'F')
217 hexval = str[i] - 'A' + 10;
218 else if (str[i] >= 'a' && str[i] <= 'f')
219 hexval = str[i] - 'a' + 10;
220 else
221 break;
222 val = (val << 4) + hexval;
223 }
224 /* handle #rgb case */
225 if (i == 1 + 3) {
226 val = ((val & 0xf00) << 8) |
227 ((val & 0x0f0) << 4) |
228 (val & 0x00f);
229 val |= val << 4;
230 } else if (i != 1 + 6) {
231 /* must be either 3 or 6 digits. */
232 return def;
233 }
234 if (end_ptr) {
235 *end_ptr = str + i;
236 }
237 } else if (strneq(str, "rgb(", 4)) {
238 gboolean hasp, hasd;
239 gchar *s, *e;
240 gdouble r, g, b;
242 s = (gchar *) str + 4;
243 hasp = FALSE;
244 hasd = FALSE;
246 r = g_ascii_strtod(s, &e);
247 if (s == e) return def;
248 s = e;
249 if (*s == '%') {
250 hasp = TRUE;
251 s += 1;
252 } else {
253 hasd = TRUE;
254 }
255 while (*s && g_ascii_isspace(*s)) s += 1;
256 if (*s != ',') return def;
257 s += 1;
258 while (*s && g_ascii_isspace(*s)) s += 1;
259 g = g_ascii_strtod(s, &e);
260 if (s == e) return def;
261 s = e;
262 if (*s == '%') {
263 hasp = TRUE;
264 s += 1;
265 } else {
266 hasd = TRUE;
267 }
268 while (*s && g_ascii_isspace(*s)) s += 1;
269 if (*s != ',') return def;
270 s += 1;
271 while (*s && g_ascii_isspace(*s)) s += 1;
272 b = g_ascii_strtod(s, &e);
273 if (s == e) return def;
274 s = e;
275 if (*s == '%') {
276 hasp = TRUE;
277 } else {
278 hasd = TRUE;
279 }
280 while(*s && g_ascii_isspace(*s)) s += 1;
281 if (*s != ')') {
282 return def;
283 }
284 ++s;
285 if (hasp && hasd) return def;
286 if (hasp) {
287 val = (guint) floor(CLAMP(r, 0.0, 100.0) * 2.559999) << 24;
288 val |= ((guint) floor(CLAMP(g, 0.0, 100.0) * 2.559999) << 16);
289 val |= ((guint) floor(CLAMP(b, 0.0, 100.0) * 2.559999) << 8);
290 } else {
291 val = (guint) CLAMP(r, 0, 255) << 24;
292 val |= ((guint) CLAMP(g, 0, 255) << 16);
293 val |= ((guint) CLAMP(b, 0, 255) << 8);
294 }
295 if (end_ptr) {
296 *end_ptr = s;
297 }
298 return val;
299 } else {
300 gint i;
301 if (!colors) {
302 colors = sp_svg_create_color_hash();
303 }
304 gchar c[32];
305 for (i = 0; i < 31; i++) {
306 if (str[i] == ';' || g_ascii_isspace(str[i])) {
307 c[i] = '\0';
308 break;
309 }
310 c[i] = g_ascii_tolower(str[i]);
311 if (!str[i]) break;
312 }
313 c[31] = '\0';
315 gpointer const rgb_ptr = g_hash_table_lookup(colors, c);
316 if (rgb_ptr) {
317 val = *(static_cast<unsigned long *>(rgb_ptr));
318 } else {
319 return def;
320 }
321 if (end_ptr) {
322 *end_ptr = str + i;
323 }
324 }
326 return (val << 8);
327 }
329 guint32
330 sp_svg_read_color(gchar const *str, gchar const **end_ptr, guint32 dfl)
331 {
332 /* I've been rather hurried in editing the above to add support for end_ptr, so I'm adding
333 * this check wrapper. */
334 gchar const *end = str;
335 guint32 const ret = internal_sp_svg_read_color(str, &end, dfl);
336 assert(ret == dfl && end == str
337 || (((ret & 0xff) == 0)
338 && str < end));
339 if (str < end) {
340 gchar *buf = (gchar *) g_malloc(end + 1 - str);
341 memcpy(buf, str, end - str);
342 buf[end - str] = '\0';
343 gchar const *buf_end = buf;
344 guint32 const check = internal_sp_svg_read_color(buf, &buf_end, 1);
345 assert(check == ret
346 && buf_end - buf == end - str);
347 g_free(buf);
349 if ( end_ptr ) {
350 *end_ptr = end;
351 }
352 }
353 return ret;
354 }
357 /**
358 * Converts an RGB colour expressed in form 0x00rrggbb to a CSS/SVG representation of that colour.
359 * The result is valid even in SVG Tiny or non-SVG CSS.
360 */
361 static void
362 rgb24_to_css(char *const buf, unsigned const rgb24)
363 {
364 assert(rgb24 < (1u << 24));
366 /* SVG 1.1 Full allows additional colour names not supported by SVG Tiny, but we don't bother
367 * with them: it's good for these colours to be copyable to non-SVG CSS stylesheets and for
368 * documents to be more viewable in SVG Tiny/Basic viewers; and some of the SVG Full names are
369 * less meaningful than hex equivalents anyway. And it's easier for a person to map from the
370 * restricted set because the only component values are {00,80,ff}, other than silver 0xc0c0c0.
371 */
373 char const *src = NULL;
374 switch (rgb24) {
375 /* Extracted mechanically from the table at
376 * http://www.w3.org/TR/REC-html40/types.html#h-6.5 .*/
377 case 0x000000: src = "black"; break;
378 case 0xc0c0c0: src = "silver"; break;
379 case 0x808080: src = "gray"; break;
380 case 0xffffff: src = "white"; break;
381 case 0x800000: src = "maroon"; break;
382 case 0xff0000: src = "red"; break;
383 case 0x800080: src = "purple"; break;
384 case 0xff00ff: src = "fuchsia"; break;
385 case 0x008000: src = "green"; break;
386 case 0x00ff00: src = "lime"; break;
387 case 0x808000: src = "olive"; break;
388 case 0xffff00: src = "yellow"; break;
389 case 0x000080: src = "navy"; break;
390 case 0x0000ff: src = "blue"; break;
391 case 0x008080: src = "teal"; break;
392 case 0x00ffff: src = "aqua"; break;
394 default: {
395 if ((rgb24 & 0xf0f0f) * 0x11 == rgb24) {
396 /* Can use the shorter three-digit form #rgb instead of #rrggbb. */
397 sprintf(buf, "#%x%x%x",
398 (rgb24 >> 16) & 0xf,
399 (rgb24 >> 8) & 0xf,
400 rgb24 & 0xf);
401 } else {
402 sprintf(buf, "#%06x", rgb24);
403 }
404 break;
405 }
406 }
407 if (src) {
408 strcpy(buf, src);
409 }
411 assert(sp_svg_read_color(buf, 0xff) == (rgb24 << 8));
412 }
414 /**
415 * Converts an RGBA32 colour to a CSS/SVG representation of the RGB portion of that colour. The
416 * result is valid even in SVG Tiny or non-SVG CSS.
417 *
418 * \param rgba32 Colour expressed in form 0xrrggbbaa.
419 * \pre buflen \>= 8.
420 */
421 void
422 sp_svg_write_color(gchar *buf, unsigned const buflen, guint32 const rgba32)
423 {
424 g_assert(8 <= buflen);
426 unsigned const rgb24 = rgba32 >> 8;
427 rgb24_to_css(buf, rgb24);
428 }
430 static GHashTable *
431 sp_svg_create_color_hash()
432 {
433 GHashTable *colors = g_hash_table_new(g_str_hash, g_str_equal);
435 for (unsigned i = 0 ; i < G_N_ELEMENTS(sp_svg_color_named) ; i++) {
436 g_hash_table_insert(colors,
437 (gpointer)(sp_svg_color_named[i].name),
438 (gpointer)(&sp_svg_color_named[i].rgb));
439 }
441 return colors;
442 }
445 bool sp_svg_read_icc_color( gchar const *str, gchar const **end_ptr, SVGICCColor* dest )
446 {
447 bool good = true;
449 if ( end_ptr ) {
450 *end_ptr = str;
451 }
452 if ( dest ) {
453 dest->colorProfile.clear();
454 dest->colors.clear();
455 }
457 if ( !str ) {
458 // invalid input
459 good = false;
460 } else {
461 while ( g_ascii_isspace(*str) ) {
462 str++;
463 }
465 good = strneq( str, "icc-color(", 10 );
467 if ( good ) {
468 str += 10;
469 while ( g_ascii_isspace(*str) ) {
470 str++;
471 }
473 if ( !g_ascii_isalpha(*str)
474 && ( !(0x080 & *str) ) ) {
475 // Name must start with a certain type of character
476 good = false;
477 } else {
478 while ( g_ascii_isdigit(*str) || g_ascii_islower(*str) || (*str == '-') ) {
479 if ( dest ) {
480 dest->colorProfile += *str;
481 }
482 str++;
483 }
484 while ( g_ascii_isspace(*str) || *str == ',' ) {
485 str++;
486 }
487 }
488 }
490 if ( good ) {
491 while ( *str && *str != ')' ) {
492 if ( g_ascii_isdigit(*str) || *str == '.' || *str == '-' || *str == '+') {
493 gchar* endPtr = 0;
494 gdouble dbl = g_ascii_strtod( str, &endPtr );
495 if ( !errno ) {
496 if ( dest ) {
497 dest->colors.push_back( dbl );
498 }
499 str = endPtr;
500 } else {
501 good = false;
502 break;
503 }
505 while ( g_ascii_isspace(*str) || *str == ',' ) {
506 str++;
507 }
508 } else {
509 break;
510 }
511 }
512 }
514 // We need to have ended on a closing parenthesis
515 if ( good ) {
516 while ( g_ascii_isspace(*str) ) {
517 str++;
518 }
519 good &= *str == ')';
520 }
521 }
523 if ( good ) {
524 if ( end_ptr ) {
525 *end_ptr = str;
526 }
527 } else {
528 if ( dest ) {
529 dest->colorProfile.clear();
530 dest->colors.clear();
531 }
532 }
534 return good;
535 }
537 /*
538 Local Variables:
539 mode:c++
540 c-file-style:"stroustrup"
541 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
542 indent-tabs-mode:nil
543 fill-column:99
544 End:
545 */
546 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :