[color] allow alpha to be omitted when converting to color from string
authorThomas Wood <thomas.wood@intel.com>
Wed, 7 Oct 2009 11:27:38 +0000 (12:27 +0100)
committerThomas Wood <thomas.wood@intel.com>
Wed, 7 Oct 2009 11:56:01 +0000 (12:56 +0100)
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
tests/conform/test-color.c

index 221f9f4..e81e911 100644 (file)
@@ -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;
+            }
         }
     }
   
index 685ec70..a19323b 100644 (file)
@@ -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