From c7d50083ec1d1209dd8653bc30ba4f3bec09e976 Mon Sep 17 00:00:00 2001 From: Thomas Wood Date: Wed, 7 Oct 2009 12:27:38 +0100 Subject: [PATCH] [color] allow alpha to be omitted when converting to color from string Parse #rgb and #rrggbb in addition to forms with the alpha channel specified. This allows conversion of colour strings from documents such as CSS where the alpha channel is not specified when using '#' notation. This patch also adds the relevant conformance test. --- clutter/clutter-color.c | 22 ++++++++++++++++++++++ tests/conform/test-color.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/clutter/clutter-color.c b/clutter/clutter-color.c index 221f9f4..e81e911 100644 --- a/clutter/clutter-color.c +++ b/clutter/clutter-color.c @@ -432,6 +432,28 @@ clutter_color_from_string (ClutterColor *color, return TRUE; } + else if (strlen (str) == 7) + { + /* #rrggbb */ + color->red = (result >> 16) & 0xff; + color->green = (result >> 8) & 0xff; + color->blue = result & 0xff; + + color->alpha = 0xff; + } + else if (strlen (str) == 4) + { + /* #rgb */ + color->red = ((result >> 8) & 0xf); + color->green = ((result >> 4) & 0xf); + color->blue = result & 0xf; + + color->red = (color->red << 4) | color->red; + color->green = (color->green << 4) | color->green; + color->blue = (color->blue << 4) | color->blue; + + color->alpha = 0xff; + } } } diff --git a/tests/conform/test-color.c b/tests/conform/test-color.c index 685ec70..a19323b 100644 --- a/tests/conform/test-color.c +++ b/tests/conform/test-color.c @@ -119,6 +119,34 @@ test_color_from_string (TestConformSimpleFixture *fixture, g_assert (color.green == 0); g_assert (color.blue == 0xff); g_assert (color.alpha == 0xff); + + clutter_color_from_string (&color, "#abc"); + if (g_test_verbose ()) + { + g_print ("color = { %x, %x, %x, %x }, expected = { 0xaa, 0xbb, 0xcc, 0xff }\n", + color.red, + color.green, + color.blue, + color.alpha); + } + g_assert (color.red == 0xaa); + g_assert (color.green == 0xbb); + g_assert (color.blue == 0xcc); + g_assert (color.alpha == 0xff); + + clutter_color_from_string (&color, "#123abc"); + if (g_test_verbose ()) + { + g_print ("color = { %x, %x, %x, %x }, expected = { 0x12, 0x3a, 0xbc, 0xff }\n", + color.red, + color.green, + color.blue, + color.alpha); + } + g_assert (color.red == 0x12); + g_assert (color.green == 0x3a); + g_assert (color.blue == 0xbc); + g_assert (color.alpha == 0xff); } void -- 2.7.4