[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.
This commit is contained in:
Thomas Wood 2009-10-07 12:27:38 +01:00
parent 13ac1fe75b
commit c7d50083ec
2 changed files with 50 additions and 0 deletions

View 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;
}
}
}

View 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