Refactor xsetcolorname()

I mainly improved the slightly off algorithm used to load colours in the 256-colour-space and
removed unnecessary local values (r,g,b,colour).
"colour" is not necessary as a punchbag for XftColorAlloc[Value,Name], as they don't mess with
the result-adress until they are absolutely sure everything worked out[0].

Being at it, I changed the error-returns for AllocValue to dies (just like in xloadcols()), as
a failure is most likely an OOM-situation you better catch early.
In case of an invalid name everything stays the same.

[0]: http://www.opensource.apple.com/source/X11libs/X11libs-40/libXft/libXft-2.1.13/src/xftcolor.c

Signed-off-by: Christoph Lohmann <20h@r-36.net>
dev
FRIGN 2014-05-25 12:13:22 +02:00 committed by Christoph Lohmann
parent ba36d1394b
commit a32c5f5726
1 changed files with 14 additions and 18 deletions

32
st.c
View File

@ -2757,32 +2757,28 @@ xloadcols(void) {
int int
xsetcolorname(int x, const char *name) { xsetcolorname(int x, const char *name) {
XRenderColor color = { .alpha = 0xffff }; XRenderColor color = { .alpha = 0xffff };
Colour colour;
if(!BETWEEN(x, 0, LEN(colorname))) if(!BETWEEN(x, 0, LEN(colorname)))
return -1; return -1;
if(!name) { if(!name) {
if(BETWEEN(x, 16, 16 + 215)) { if(BETWEEN(x, 16, 6*6*6+16)) { /* 256 colour */
int r = (x - 16) / 36, g = ((x - 16) % 36) / 6, b = (x - 16) % 6; color.red = sixd_to_16bit( ((x-16)/36)%6 );
color.red = sixd_to_16bit(r); color.green = sixd_to_16bit( ((x-16)/6) %6 );
color.green = sixd_to_16bit(g); color.blue = sixd_to_16bit( ((x-16)/1) %6 );
color.blue = sixd_to_16bit(b); if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &dc.col[x]))
if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &colour)) die("Could not allocate color %d\n", x);
return 0; /* something went wrong */
dc.col[x] = colour;
return 1; return 1;
} else if(BETWEEN(x, 16 + 216, 255)) { } else if(BETWEEN(x, 6*6*6+16, 255)) { /* grayscale */
color.red = color.green = color.blue = 0x0808 + 0x0a0a * (x - (16 + 216)); color.red = color.green = color.blue = 0x0808 + 0x0a0a * (x-(6*6*6+16));
if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &colour)) if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &dc.col[x]))
return 0; /* something went wrong */ die("Could not allocate color %d\n", x);
dc.col[x] = colour;
return 1; return 1;
} else { } else { /* system colours */
name = colorname[x]; name = colorname[x];
} }
} }
if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, &colour)) if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, &dc.col[x]))
return 0; return 0; /* invalid name */
dc.col[x] = colour;
return 1; return 1;
} }