From bd13a4ddc486586bc1051080cbe64c2d90ee853e Mon Sep 17 00:00:00 2001 From: Robert Staudinger Date: Mon, 1 Jun 2009 18:54:46 +0200 Subject: [PATCH] [color] Add support for the "#rgba" color format clutter_color_from_string() only supported the "#rrggbbaa" format with alpha channel, this patch adds support for "#rgba". Colors in "#rrggbb" format were parsed manually, this is now left to the pango color parsing fallback, since that's handling it just fine. Signed-off-by: Emmanuele Bassi --- clutter/clutter-color.c | 58 ++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/clutter/clutter-color.c b/clutter/clutter-color.c index 3d40ea5..0ff170e 100644 --- a/clutter/clutter-color.c +++ b/clutter/clutter-color.c @@ -398,7 +398,7 @@ clutter_color_from_string (ClutterColor *color, g_return_val_if_fail (str != NULL, FALSE); /* if the string contains a color encoded using the hexadecimal - * notations (#rrggbbaa or #rrggbb) 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 * Pango can't retrieve. */ @@ -407,36 +407,34 @@ clutter_color_from_string (ClutterColor *color, gint32 result; if (sscanf (str + 1, "%x", &result)) - { - if (strlen (str) == 9) - { + { + if (strlen (str) == 9) + { /* #rrggbbaa */ - color->red = (result >> 24) & 0xff; - color->green = (result >> 16) & 0xff; - color->blue = (result >> 8) & 0xff; - - color->alpha = result & 0xff; - - 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; - - return TRUE; - } - } - - /* XXX - should we return FALSE here? it's not like - * Pango is endowed with mystical parsing powers and - * will be able to do better than the code above. - * still, it doesn't hurt - */ + color->red = (result >> 24) & 0xff; + color->green = (result >> 16) & 0xff; + color->blue = (result >> 8) & 0xff; + + color->alpha = result & 0xff; + + return TRUE; + } + else if (strlen (str) == 5) + { + /* #rgba */ + color->red = ((result >> 12) & 0xf); + color->green = ((result >> 8) & 0xf); + color->blue = ((result >> 4) & 0xf); + color->alpha = 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 = (color->alpha << 4) | color->alpha; + + return TRUE; + } + } } /* Fall back to pango for named colors */ -- 2.7.4