Support colors in #rrggbb form

This commit is contained in:
LemonBoy 2015-02-14 00:32:16 +01:00
parent 2778c81699
commit 8351859593
1 changed files with 13 additions and 2 deletions

15
bar.c
View File

@ -122,7 +122,7 @@ fill_gradient (xcb_drawable_t d, int x, int y, int width, int height, rgba_t sta
.r = rr, .r = rr,
.g = gg, .g = gg,
.b = bb, .b = bb,
.a = 0xff .a = 255,
}; };
xcb_change_gc(c, gc[GC_DRAW], XCB_GC_FOREGROUND, (const uint32_t []){ step.v }); xcb_change_gc(c, gc[GC_DRAW], XCB_GC_FOREGROUND, (const uint32_t []){ step.v });
@ -231,6 +231,7 @@ parse_color (const char *str, char **end, const rgba_t def)
xcb_alloc_named_color_reply_t *nc_reply; xcb_alloc_named_color_reply_t *nc_reply;
int string_len; int string_len;
rgba_t ret; rgba_t ret;
char *ep;
if (!str) if (!str)
return def; return def;
@ -239,13 +240,17 @@ parse_color (const char *str, char **end, const rgba_t def)
if (str[0] == '-') { if (str[0] == '-') {
if (end) if (end)
*end = (char *)str + 1; *end = (char *)str + 1;
return def; return def;
} }
// Hex representation // Hex representation
if (str[0] == '#') { if (str[0] == '#') {
errno = 0; errno = 0;
rgba_t tmp = (rgba_t)(uint32_t)strtoul(str + 1, end, 16); rgba_t tmp = (rgba_t)(uint32_t)strtoul(str + 1, &ep, 16);
if (end)
*end = ep;
// Some error checking is definitely good // Some error checking is definitely good
if (errno) { if (errno) {
@ -253,6 +258,12 @@ parse_color (const char *str, char **end, const rgba_t def)
return def; return def;
} }
string_len = ep - (str + 1);
// If the code is in #rrggbb form then assume it's opaque
if (string_len <= 6)
tmp.a = 255;
// Premultiply the alpha in // Premultiply the alpha in
if (tmp.a) { if (tmp.a) {
// The components are clamped automagically as the rgba_t is made of uint8_t // The components are clamped automagically as the rgba_t is made of uint8_t