Don't parse named colors. Expand #rgb format.

This commit is contained in:
LemonBoy 2015-10-24 14:04:37 +02:00
parent 1585d7257d
commit 38422c5014
2 changed files with 49 additions and 51 deletions

View File

@ -47,7 +47,7 @@ Sets the underline width in pixels. The default is 1.
=item B<-B> I<color>
Set the background color of the bar. I<color> might be either in hex format (#aarrggbb) or in the symbolic name format (eg. white, indianred, darkgray). If no compositor such as compton or xcompmgr is running the alpha channel is silently ignored.
Set the background color of the bar. I<color> must be specified in the hex format (#aarrggbb, #rrggbb, #rgb). If no compositor such as compton or xcompmgr is running the alpha channel is silently ignored.
=item B<-F> I<color>

View File

@ -233,9 +233,7 @@ draw_char (monitor_t *mon, font_t *cur_font, int x, int align, uint16_t ch)
rgba_t
parse_color (const char *str, char **end, const rgba_t def)
{
xcb_alloc_named_color_reply_t *nc_reply;
int string_len;
rgba_t ret;
char *ep;
if (!str)
@ -250,7 +248,14 @@ parse_color (const char *str, char **end, const rgba_t def)
}
// Hex representation
if (str[0] == '#') {
if (str[0] != '#') {
if (end)
*end = (char *)str;
fprintf(stderr, "Invalid color specified\n");
return def;
}
errno = 0;
rgba_t tmp = (rgba_t)(uint32_t)strtoul(str + 1, &ep, 16);
@ -265,9 +270,24 @@ parse_color (const char *str, char **end, const rgba_t def)
string_len = ep - (str + 1);
switch (string_len) {
case 3:
// Expand the #rgb format into #rrggbb (aa is set to 0xff)
tmp.v = (tmp.v & 0xf00) * 0x1100
| (tmp.v & 0x0f0) * 0x0110
| (tmp.v & 0x00f) * 0x0011;
case 6:
// If the code is in #rrggbb form then assume it's opaque
if (string_len <= 6)
tmp.a = 255;
break;
case 7:
case 8:
// Colors in #aarrggbb format, those need no adjustments
break;
default:
fprintf(stderr, "Invalid color specified\n");
return def;
}
// Premultiply the alpha in
if (tmp.a) {
@ -283,28 +303,6 @@ parse_color (const char *str, char **end, const rgba_t def)
return (rgba_t)0U;
}
// Actual color name, resolve it
for (string_len = 0; isalpha(str[string_len]); string_len++)
;
nc_reply = xcb_alloc_named_color_reply(c, xcb_alloc_named_color(c, colormap, string_len, str), NULL);
if (!nc_reply)
fprintf(stderr, "Could not allocate the color \"%.*s\"\n", string_len, str);
ret = nc_reply?
(rgba_t)nc_reply->pixel:
def;
free(nc_reply);
if (end)
*end = (char *)str + string_len;
return ret;
}
void
set_attribute (const char modifier, const char attribute)
{
@ -1229,8 +1227,8 @@ main (int argc, char **argv)
xconn();
// B/W combo
dbgc = bgc = parse_color("black", NULL, (rgba_t)scr->black_pixel);
dfgc = fgc = parse_color("white", NULL, (rgba_t)scr->white_pixel);
dbgc = bgc = (rgba_t)scr->black_pixel;
dfgc = fgc = (rgba_t)scr->white_pixel;
ugc = fgc;