color: Validate hex formats

Right now, we pass through to Pango unrecognized hexadecimal formats
when parsing colors from strings. Since we parse all possible formats
ourselves, we can do validation ourselves as well, and avoid the Pango
path.
This commit is contained in:
Emmanuele Bassi 2011-11-10 14:13:45 +00:00
parent 889a1f44f4
commit 0550a8b99d

View File

@ -720,17 +720,16 @@ clutter_color_from_string (ClutterColor *color,
* parsing the color ourselves, as we need the alpha channel that * parsing the color ourselves, as we need the alpha channel that
* Pango can't retrieve. * Pango can't retrieve.
*/ */
if (str[0] == '#') if (str[0] == '#' && str[1] != '\0')
{ {
gsize length = strlen (str + 1);
gint32 result; gint32 result;
if (sscanf (str + 1, "%x", &result)) if (sscanf (str + 1, "%x", &result) == 1)
{ {
gsize length = strlen (str);
switch (length) switch (length)
{ {
case 9: /* rrggbbaa */ case 8: /* rrggbbaa */
color->red = (result >> 24) & 0xff; color->red = (result >> 24) & 0xff;
color->green = (result >> 16) & 0xff; color->green = (result >> 16) & 0xff;
color->blue = (result >> 8) & 0xff; color->blue = (result >> 8) & 0xff;
@ -739,7 +738,7 @@ clutter_color_from_string (ClutterColor *color,
return TRUE; return TRUE;
case 7: /* #rrggbb */ case 6: /* #rrggbb */
color->red = (result >> 16) & 0xff; color->red = (result >> 16) & 0xff;
color->green = (result >> 8) & 0xff; color->green = (result >> 8) & 0xff;
color->blue = result & 0xff; color->blue = result & 0xff;
@ -748,7 +747,7 @@ clutter_color_from_string (ClutterColor *color,
return TRUE; return TRUE;
case 5: /* #rgba */ case 4: /* #rgba */
color->red = ((result >> 12) & 0xf); color->red = ((result >> 12) & 0xf);
color->green = ((result >> 8) & 0xf); color->green = ((result >> 8) & 0xf);
color->blue = ((result >> 4) & 0xf); color->blue = ((result >> 4) & 0xf);
@ -761,7 +760,7 @@ clutter_color_from_string (ClutterColor *color,
return TRUE; return TRUE;
case 4: /* #rgb */ case 3: /* #rgb */
color->red = ((result >> 8) & 0xf); color->red = ((result >> 8) & 0xf);
color->green = ((result >> 4) & 0xf); color->green = ((result >> 4) & 0xf);
color->blue = result & 0xf; color->blue = result & 0xf;
@ -775,13 +774,18 @@ clutter_color_from_string (ClutterColor *color,
return TRUE; return TRUE;
default: default:
/* pass through to Pango */ return FALSE;
break;
} }
} }
} }
/* Fall back to pango for named colors */ /* fall back to pango for X11-style named colors; see:
*
* http://en.wikipedia.org/wiki/X11_color_names
*
* for a list. at some point we might even ship with our own list generated
* from X11/rgb.txt, like we generate the key symbols.
*/
if (pango_color_parse (&pango_color, str)) if (pango_color_parse (&pango_color, str))
{ {
color->red = pango_color.red; color->red = pango_color.red;