Accept colors with only 1 digit per channel in utils_parse_color()
AKA short HTML color notation.
This commit is contained in:
parent
11a2f0ee91
commit
ebde42617a
27
src/utils.c
27
src/utils.c
@ -978,11 +978,13 @@ static gboolean read_hex(const gchar *s, guint len, gint *h)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Converts a string containing a hex colour ("0x00ff00") into an integer.
|
/* Converts a string containing an hex or HTML RGB color with 1 or 2 digits per
|
||||||
|
* channel (0x00ff11, 0x0f1, #00ff11, #0f1) to an integer.
|
||||||
* Returns an integer color in the format BBGGRR or -1 on failure. */
|
* Returns an integer color in the format BBGGRR or -1 on failure. */
|
||||||
gint utils_parse_color(const gchar *source)
|
gint utils_parse_color(const gchar *source)
|
||||||
{
|
{
|
||||||
gint red, green, blue;
|
gint r, g, b;
|
||||||
|
guint len;
|
||||||
|
|
||||||
g_return_val_if_fail(source != NULL, -1);
|
g_return_val_if_fail(source != NULL, -1);
|
||||||
|
|
||||||
@ -993,15 +995,24 @@ gint utils_parse_color(const gchar *source)
|
|||||||
else
|
else
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (strlen(source) != 6)
|
len = strlen(source);
|
||||||
|
if (len % 3 || len < 3 || len > 6)
|
||||||
|
return -1;
|
||||||
|
len /= 3;
|
||||||
|
|
||||||
|
if (! read_hex(source, len, &r) ||
|
||||||
|
! read_hex(source + len, len, &g) ||
|
||||||
|
! read_hex(source + len * 2, len, &b))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (! read_hex(source, 2, &red) ||
|
if (len < 2)
|
||||||
! read_hex(source + 2, 2, &green) ||
|
{
|
||||||
! read_hex(source + 4, 2, &blue))
|
r |= r << 4;
|
||||||
return -1;
|
g |= g << 4;
|
||||||
|
b |= b << 4;
|
||||||
|
}
|
||||||
|
|
||||||
return (red | (green << 8) | (blue << 16));
|
return (r | (g << 8) | (b << 16));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user