color: Support the CSS hsl() notation
authorEmmanuele Bassi <ebassi@linux.intel.com>
Mon, 22 Nov 2010 15:02:47 +0000 (15:02 +0000)
committerEmmanuele Bassi <ebassi@linux.intel.com>
Mon, 22 Nov 2010 15:02:47 +0000 (15:02 +0000)
Since we support the rgb() and rgba() notations we might as well also
support the hsl() one.

clutter/clutter-color.c
tests/conform/test-color.c

index 2d20734..ced3db7 100644 (file)
@@ -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
index e9681e1..a0eab95 100644 (file)
@@ -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