From e86db85cd2f0bbdbb65924a8f9ae2575e1212d14 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Mon, 22 Nov 2010 15:02:47 +0000 Subject: [PATCH] color: Support the CSS hsl() notation Since we support the rgb() and rgba() notations we might as well also support the hsl() one. --- clutter/clutter-color.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++ tests/conform/test-color.c | 14 ++++++++++ 2 files changed, 84 insertions(+) diff --git a/clutter/clutter-color.c b/clutter/clutter-color.c index 2d20734..ced3db7 100644 --- a/clutter/clutter-color.c +++ b/clutter/clutter-color.c @@ -534,6 +534,69 @@ parse_rgba (ClutterColor *color, return TRUE; } +static gboolean +parse_hsl (ClutterColor *color, + gchar *str) +{ + gdouble number; + gdouble h, l, s; + + skip_whitespace (&str); + + if (*str != '(') + return FALSE; + + str += 1; + + /* hue */ + skip_whitespace (&str); + /* we don't do any angle normalization here because + * clutter_color_from_hls() will do it for us + */ + number = g_ascii_strtod (str, &str); + skip_whitespace (&str); + if (*str != ',') + return FALSE; + + h = number; + + str += 1; + + /* saturation */ + skip_whitespace (&str); + number = g_ascii_strtod (str, &str); + skip_whitespace (&str); + if (*str != '%') + return FALSE; + + str += 1; + + s = CLAMP (number / 100.0, 0.0, 1.0); + skip_whitespace (&str); + if (*str != ',') + return FALSE; + + str += 1; + + /* luminance */ + skip_whitespace (&str); + number = g_ascii_strtod (str, &str); + skip_whitespace (&str); + if (*str != '%') + return FALSE; + + str += 1; + + l = CLAMP (number / 100.0, 0.0, 1.0); + skip_whitespace (&str); + if (*str != ')') + return FALSE; + + clutter_color_from_hls (color, h, l, s); + + return TRUE; +} + /** * clutter_color_from_string: * @color: (out caller-allocates): return location for a #ClutterColor @@ -595,6 +658,13 @@ clutter_color_from_string (ClutterColor *color, return res; } + if (strncmp (str, "hsl", 3) == 0) + { + gchar *s = (gchar *) str; + + return parse_hsl (color, s + 3); + } + /* if the string contains a color encoded using the hexadecimal * notations (#rrggbbaa or #rgba) we attempt a rough pass at * parsing the color ourselves, as we need the alpha channel that diff --git a/tests/conform/test-color.c b/tests/conform/test-color.c index e9681e1..a0eab95 100644 --- a/tests/conform/test-color.c +++ b/tests/conform/test-color.c @@ -191,6 +191,20 @@ test_color_from_string (TestConformSimpleFixture *fixture, g_assert_cmpint (color.green, ==, 0); g_assert_cmpint (color.blue, ==, 255); g_assert_cmpint (color.alpha, ==, 255); + + g_assert (clutter_color_from_string (&color, "hsl( 0, 100%, 50% )")); + if (g_test_verbose ()) + { + g_print ("color = { %x, %x, %x, %x }, expected = { 255, 0, 0, 255 }\n", + color.red, + color.green, + color.blue, + color.alpha); + } + g_assert_cmpint (color.red, ==, 255); + g_assert_cmpint (color.green, ==, 0); + g_assert_cmpint (color.blue, ==, 0); + g_assert_cmpint (color.alpha, ==, 255); } void -- 2.7.4