color: Support the CSS hsl() notation

Since we support the rgb() and rgba() notations we might as well also
support the hsl() one.
This commit is contained in:
Emmanuele Bassi 2010-11-22 15:02:47 +00:00
parent ab6da347f6
commit e86db85cd2
2 changed files with 84 additions and 0 deletions

View File

@ -534,6 +534,69 @@ parse_rgba (ClutterColor *color,
return TRUE; 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: * clutter_color_from_string:
* @color: (out caller-allocates): return location for a #ClutterColor * @color: (out caller-allocates): return location for a #ClutterColor
@ -595,6 +658,13 @@ clutter_color_from_string (ClutterColor *color,
return res; 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 /* if the string contains a color encoded using the hexadecimal
* notations (#rrggbbaa or #rgba) we attempt a rough pass at * notations (#rrggbbaa or #rgba) we attempt a rough pass at
* parsing the color ourselves, as we need the alpha channel that * parsing the color ourselves, as we need the alpha channel that

View File

@ -191,6 +191,20 @@ test_color_from_string (TestConformSimpleFixture *fixture,
g_assert_cmpint (color.green, ==, 0); g_assert_cmpint (color.green, ==, 0);
g_assert_cmpint (color.blue, ==, 255); g_assert_cmpint (color.blue, ==, 255);
g_assert_cmpint (color.alpha, ==, 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 void