summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPeter Moulder <peter.moulder@monash.edu>2006-03-13 05:47:27 +0000
committerpjrm <pjrm@users.sourceforge.net>2006-03-13 05:47:27 +0000
commit839a12841e75c19066c69a942694314220de951a (patch)
treea096e7d0d37ad130584a8e8efd6a59deb976b2a5 /src
parentsvg-color-test.h: New unit test file. (diff)
downloadinkscape-839a12841e75c19066c69a942694314220de951a.tar.gz
inkscape-839a12841e75c19066c69a942694314220de951a.zip
(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. (bzr r236)
Diffstat (limited to 'src')
-rw-r--r--src/svg/svg-color.cpp72
-rw-r--r--src/svg/svg-color.h2
2 files changed, 70 insertions, 4 deletions
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 <cassert>
#include <math.h>
#include <glib/gmessages.h>
#include <glib/gstrfuncs.h>
#include <glib/ghash.h>
#include <glib/gutils.h>
+#include <cstdio> // 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 <glib/gtypes.h>
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 */