Better alpha mixing, should produce correct results

This commit is contained in:
LemonBoy 2014-08-08 13:33:24 +02:00
parent 8475ff4fdb
commit dbbdf373a3

30
bar.c
View File

@ -157,18 +157,30 @@ parse_color (const char *str, char **end, const uint32_t def)
if (str[0] == '#') { if (str[0] == '#') {
errno = 0; errno = 0;
uint32_t tmp = strtoul(str + 1, end, 16); uint32_t tmp = strtoul(str + 1, end, 16);
/* Some error checking is definitely good */ /* Some error checking is definitely good */
if (errno) if (errno)
tmp = def; tmp = def;
/* Xorg uses colors with premultiplied alpha.
* Don't do anything if we didn't acquire a rgba visual. */ /* Xorg uses colors with premultiplied alpha */
if (visual != scr->root_visual) { unsigned int a = (tmp&0xff000000) >> 24;
const uint8_t a = ((tmp>>24)&255); unsigned int r = (tmp&0x00ff0000) >> 16;
const uint32_t t1 = (tmp&0xff00ff) * (0x100-a); unsigned int g = (tmp&0x0000ff00) >> 8;
const uint32_t t2 = (tmp&0x00ff00) * (0x100-a); unsigned int b = (tmp&0x000000ff);
tmp = (a<<24)|(t1&0xff00ff)|(t2&0x00ff00);
} if (a) {
return tmp; r = (r * 255) / a;
g = (g * 255) / a;
b = (b * 255) / a;
/* Clamp on overflow */
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;
} else
r = g = b = 0;
return a << 24 | r << 16 | g << 8 | b;
} }
/* Actual color name, resolve it */ /* Actual color name, resolve it */