Code

gboolean -> bool conversion commit 1. Modifies code to do with getting the undo...
[inkscape.git] / src / svg / svg-color.cpp
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)
197     return sp_svg_read_color(str, NULL, dfl);
200 static guint32
201 internal_sp_svg_read_color(gchar const *str, gchar const **end_ptr, guint32 def)
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         bool 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             s += 1;
278         } else {
279             hasd = true;
280         }
281         while(*s && g_ascii_isspace(*s)) s += 1;
282         if (*s != ')') {
283             return def;
284         }
285         ++s;
286         if (hasp && hasd) return def;
287         if (hasp) {
288             val = (guint) floor(CLAMP(r, 0.0, 100.0) * 2.559999) << 24;
289             val |= ((guint) floor(CLAMP(g, 0.0, 100.0) * 2.559999) << 16);
290             val |= ((guint) floor(CLAMP(b, 0.0, 100.0) * 2.559999) << 8);
291         } else {
292             val = (guint) CLAMP(r, 0, 255) << 24;
293             val |= ((guint) CLAMP(g, 0, 255) << 16);
294             val |= ((guint) CLAMP(b, 0, 255) << 8);
295         }
296         if (end_ptr) {
297             *end_ptr = s;
298         }
299         return val;
300     } else {
301         gint i;
302         if (!colors) {
303             colors = sp_svg_create_color_hash();
304         }
305         gchar c[32];
306         for (i = 0; i < 31; i++) {
307             if (str[i] == ';' || g_ascii_isspace(str[i])) {
308                 c[i] = '\0';
309                 break;
310             }
311             c[i] = g_ascii_tolower(str[i]);
312             if (!str[i]) break;
313         }
314         c[31] = '\0';
316         gpointer const rgb_ptr = g_hash_table_lookup(colors, c);
317         if (rgb_ptr) {
318             val = *(static_cast<unsigned long *>(rgb_ptr));
319         } else {
320             return def;
321         }
322         if (end_ptr) {
323             *end_ptr = str + i;
324         }
325     }
327     return (val << 8);
330 guint32
331 sp_svg_read_color(gchar const *str, gchar const **end_ptr, guint32 dfl)
333     /* I've been rather hurried in editing the above to add support for end_ptr, so I'm adding
334      * this check wrapper. */
335     gchar const *end = str;
336     guint32 const ret = internal_sp_svg_read_color(str, &end, dfl);
337     assert(ret == dfl && end == str
338            || (((ret & 0xff) == 0)
339                && str < end));
340     if (str < end) {
341         gchar *buf = (gchar *) g_malloc(end + 1 - str);
342         memcpy(buf, str, end - str);
343         buf[end - str] = '\0';
344         gchar const *buf_end = buf;
345         guint32 const check = internal_sp_svg_read_color(buf, &buf_end, 1);
346         assert(check == ret
347                && buf_end - buf == end - str);
348         g_free(buf);
350         if ( end_ptr ) {
351             *end_ptr = end;
352         }
353     }
354     return ret;
358 /**
359  * Converts an RGB colour expressed in form 0x00rrggbb to a CSS/SVG representation of that colour.
360  * The result is valid even in SVG Tiny or non-SVG CSS.
361  */
362 static void
363 rgb24_to_css(char *const buf, unsigned const rgb24)
365     assert(rgb24 < (1u << 24));
367     /* SVG 1.1 Full allows additional colour names not supported by SVG Tiny, but we don't bother
368      * with them: it's good for these colours to be copyable to non-SVG CSS stylesheets and for
369      * documents to be more viewable in SVG Tiny/Basic viewers; and some of the SVG Full names are
370      * less meaningful than hex equivalents anyway.  And it's easier for a person to map from the
371      * restricted set because the only component values are {00,80,ff}, other than silver 0xc0c0c0.
372      */
374     char const *src = NULL;
375     switch (rgb24) {
376         /* Extracted mechanically from the table at
377          * http://www.w3.org/TR/REC-html40/types.html#h-6.5 .*/
378         case 0x000000: src = "black"; break;            
379         case 0xc0c0c0: src = "silver"; break;           
380         case 0x808080: src = "gray"; break;             
381         case 0xffffff: src = "white"; break;            
382         case 0x800000: src = "maroon"; break;           
383         case 0xff0000: src = "red"; break;              
384         case 0x800080: src = "purple"; break;           
385         case 0xff00ff: src = "fuchsia"; break;          
386         case 0x008000: src = "green"; break; 
387         case 0x00ff00: src = "lime"; break;  
388         case 0x808000: src = "olive"; break; 
389         case 0xffff00: src = "yellow"; break;
390         case 0x000080: src = "navy"; break;  
391         case 0x0000ff: src = "blue"; break;  
392         case 0x008080: src = "teal"; break;  
393         case 0x00ffff: src = "aqua"; break;  
395         default: {
396             if ((rgb24 & 0xf0f0f) * 0x11 == rgb24) {
397                 /* Can use the shorter three-digit form #rgb instead of #rrggbb. */
398                 sprintf(buf, "#%x%x%x",
399                         (rgb24 >> 16) & 0xf,
400                         (rgb24 >> 8) & 0xf,
401                         rgb24 & 0xf);
402             } else {
403                 sprintf(buf, "#%06x", rgb24);
404             }
405             break;
406         }
407     }
408     if (src) {
409         strcpy(buf, src);
410     }
412     assert(sp_svg_read_color(buf, 0xff) == (rgb24 << 8));
415 /**
416  * Converts an RGBA32 colour to a CSS/SVG representation of the RGB portion of that colour.  The
417  * result is valid even in SVG Tiny or non-SVG CSS.
418  *
419  * \param rgba32 Colour expressed in form 0xrrggbbaa.
420  * \pre buflen \>= 8.
421  */
422 void
423 sp_svg_write_color(gchar *buf, unsigned const buflen, guint32 const rgba32)
425     g_assert(8 <= buflen);
427     unsigned const rgb24 = rgba32 >> 8;
428     rgb24_to_css(buf, rgb24);
431 static GHashTable *
432 sp_svg_create_color_hash()
434     GHashTable *colors = g_hash_table_new(g_str_hash, g_str_equal);
436     for (unsigned i = 0 ; i < G_N_ELEMENTS(sp_svg_color_named) ; i++) {
437         g_hash_table_insert(colors,
438                             (gpointer)(sp_svg_color_named[i].name),
439                             (gpointer)(&sp_svg_color_named[i].rgb));
440     }
442     return colors;
446 bool sp_svg_read_icc_color( gchar const *str, gchar const **end_ptr, SVGICCColor* dest )
448     bool good = true;
450     if ( end_ptr ) {
451         *end_ptr = str;
452     }
453     if ( dest ) {
454         dest->colorProfile.clear();
455         dest->colors.clear();
456     }
458     if ( !str ) {
459         // invalid input
460         good = false;
461     } else {
462         while ( g_ascii_isspace(*str) ) {
463             str++;
464         }
466         good = strneq( str, "icc-color(", 10 );
468         if ( good ) {
469             str += 10;
470             while ( g_ascii_isspace(*str) ) {
471                 str++;
472             }
474             if ( !g_ascii_isalpha(*str)
475                  && ( !(0x080 & *str) ) ) {
476                 // Name must start with a certain type of character
477                 good = false;
478             } else {
479                 while ( g_ascii_isdigit(*str) || g_ascii_islower(*str) || (*str == '-') ) {
480                     if ( dest ) {
481                         dest->colorProfile += *str;
482                     }
483                     str++;
484                 }
485                 while ( g_ascii_isspace(*str) || *str == ',' ) {
486                     str++;
487                 }
488             }
489         }
491         if ( good ) {
492             while ( *str && *str != ')' ) {
493                 if ( g_ascii_isdigit(*str) || *str == '.' || *str == '-' || *str == '+') {
494                     gchar* endPtr = 0;
495                     gdouble dbl = g_ascii_strtod( str, &endPtr );
496                     if ( !errno ) {
497                         if ( dest ) {
498                             dest->colors.push_back( dbl );
499                         }
500                         str = endPtr;
501                     } else {
502                         good = false;
503                         break;
504                     }
506                     while ( g_ascii_isspace(*str) || *str == ',' ) {
507                         str++;
508                     }
509                 } else {
510                     break;
511                 }
512             }
513         }
515         // We need to have ended on a closing parenthesis
516         if ( good ) {
517             while ( g_ascii_isspace(*str) ) {
518                 str++;
519             }
520             good &= *str == ')';
521         }
522     }
524     if ( good ) {
525         if ( end_ptr ) {
526             *end_ptr = str;
527         }
528     } else {
529         if ( dest ) {
530             dest->colorProfile.clear();
531             dest->colors.clear();
532         }
533     }
535     return good;
538 /*
539   Local Variables:
540   mode:c++
541   c-file-style:"stroustrup"
542   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
543   indent-tabs-mode:nil
544   fill-column:99
545   End:
546 */
547 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :