From: pjrm Date: Mon, 13 Mar 2006 05:47:27 +0000 (+0000) Subject: (sp_svg_write_color): Use CSS/XHTML/SVG Basic named colours (the 16 colours white... X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=4ae1ca116f0a51d4bad97c782e986f15b172e130;p=inkscape.git (sp_svg_write_color): Use CSS/XHTML/SVG Basic named colours (the 16 colours white, red, lime, ..., but not the X11 colours) when possible; else use three hex difits (#ccc); else use existing six hex digits. (sp_svg_write_color): Change return type from int (never used by existing callers) to void. --- diff --git a/ChangeLog b/ChangeLog index 114d694bb..5c7519736 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2006-03-13 Peter Moulder + + * src/svg/svg-color.cpp (sp_svg_write_color): More readable colour + names when possible: e.g. `red', `#cfc'. + 2006-03-10 Jon A. Cruz * src/interface.cpp: diff --git a/src/svg/svg-color.cpp b/src/svg/svg-color.cpp index 89c273fe5..b81cb2660 100644 --- a/src/svg/svg-color.cpp +++ b/src/svg/svg-color.cpp @@ -18,12 +18,15 @@ #endif #include "svg-color.h" +#include #include #include #include #include #include +#include // sprintf #include "strneq.h" +using std::sprintf; struct SPSVGColor { unsigned long rgb; @@ -301,13 +304,76 @@ sp_svg_read_color(gchar const *str, guint32 def) } /** + * Converts an RGB colour expressed in form 0x00rrggbb to a CSS/SVG representation of that colour. + * The result is valid even in SVG Tiny or non-SVG CSS. + */ +static void +rgb24_to_css(char *const buf, unsigned const rgb24) +{ + assert(rgb24 < (1u << 24)); + + /* SVG 1.1 Full allows additional colour names not supported by SVG Tiny, but we don't bother + * with them: it's good for these colours to be copyable to non-SVG CSS stylesheets and for + * documents to be more viewable in SVG Tiny/Basic viewers; and some of the SVG Full names are + * less meaningful than hex equivalents anyway. And it's easier for a person to map from the + * restricted set because the only component values are {00,80,ff}, other than silver 0xc0c0c0. + */ + + char const *src = NULL; + switch (rgb24) { + /* Extracted mechanically from the table at + * http://www.w3.org/TR/REC-html40/types.html#h-6.5 .*/ + case 0x000000: src = "black"; break; + case 0xc0c0c0: src = "silver"; break; + case 0x808080: src = "gray"; break; + case 0xffffff: src = "white"; break; + case 0x800000: src = "maroon"; break; + case 0xff0000: src = "red"; break; + case 0x800080: src = "purple"; break; + case 0xff00ff: src = "fuchsia"; break; + case 0x008000: src = "green"; break; + case 0x00ff00: src = "lime"; break; + case 0x808000: src = "olive"; break; + case 0xffff00: src = "yellow"; break; + case 0x000080: src = "navy"; break; + case 0x0000ff: src = "blue"; break; + case 0x008080: src = "teal"; break; + case 0x00ffff: src = "aqua"; break; + + default: { + if ((rgb24 & 0xf0f0f) * 0x11 == rgb24) { + /* Can use the shorter three-digit form #rgb instead of #rrggbb. */ + sprintf(buf, "#%x%x%x", + (rgb24 >> 16) & 0xf, + (rgb24 >> 8) & 0xf, + rgb24 & 0xf); + } else { + sprintf(buf, "#%06x", rgb24); + } + break; + } + } + if (src) { + strcpy(buf, src); + } + + assert(sp_svg_read_color(buf, 0xff) == (rgb24 << 8)); +} + +/** + * Converts an RGBA32 colour to a CSS/SVG representation of the RGB portion of that colour. The + * result is valid even in SVG Tiny or non-SVG CSS. + * + * \param rgba32 Colour expressed in form 0xrrggbbaa. * \pre buflen \>= 8. */ -gint -sp_svg_write_color(gchar *buf, unsigned const buflen, guint32 const color) +void +sp_svg_write_color(gchar *buf, unsigned const buflen, guint32 const rgba32) { g_assert(8 <= buflen); - return g_snprintf(buf, buflen, "#%06x", color >> 8); + + unsigned const rgb24 = rgba32 >> 8; + rgb24_to_css(buf, rgb24); } static GHashTable * diff --git a/src/svg/svg-color.h b/src/svg/svg-color.h index 09cac4f2e..beab1a50c 100644 --- a/src/svg/svg-color.h +++ b/src/svg/svg-color.h @@ -4,7 +4,7 @@ #include unsigned int sp_svg_read_color(gchar const *str, unsigned int dfl); -int sp_svg_write_color(char *buf, unsigned buflen, unsigned int color); +void sp_svg_write_color(char *buf, unsigned int buflen, unsigned int rgba32); #endif /* !SVG_SVG_COLOR_H_SEEN */