summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 8c6c581)
raw | patch | inline | side by side (parent: 8c6c581)
author | pjrm <pjrm@users.sourceforge.net> | |
Mon, 13 Mar 2006 05:47:27 +0000 (05:47 +0000) | ||
committer | pjrm <pjrm@users.sourceforge.net> | |
Mon, 13 Mar 2006 05:47:27 +0000 (05:47 +0000) |
(sp_svg_write_color): Change return type from int (never used by existing callers) to void.
ChangeLog | patch | blob | history | |
src/svg/svg-color.cpp | patch | blob | history | |
src/svg/svg-color.h | patch | blob | history |
diff --git a/ChangeLog b/ChangeLog
index 114d694bb4389fea912fdf4e15d65d492bd5f9c5..5c7519736f95c6dbbdaefb976b72c4b61f8cf7e7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
+2006-03-13 Peter Moulder <pmoulder@mail.csse.monash.edu.au>
+
+ * 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 <jon@joncruz.org>
* src/interface.cpp:
diff --git a/src/svg/svg-color.cpp b/src/svg/svg-color.cpp
index 89c273fe57e7dee37b06e4aa5f66ce6772129b52..b81cb2660c78e307937a4b6555f022c0f8d2d0c3 100644 (file)
--- a/src/svg/svg-color.cpp
+++ b/src/svg/svg-color.cpp
#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;
}
/**
+ * 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 09cac4f2e586e6aad5eae3413cf722704c141bf4..beab1a50cb6a3288b1a415abdaf87e29841fd1f1 100644 (file)
--- a/src/svg/svg-color.h
+++ b/src/svg/svg-color.h
#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 */