Remove named colors

The only string format for colors that we now support are #rrggbb and #rrggbbaa.
All other strings will cause errors.

Thanks to this, color_init_cookie_t can be removed. There won't be a request to
the X server for transforming named colors any more and so there won't be a
cookie. This means that color_init_reply() has to be removed, too.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2010-09-10 11:27:40 +02:00
parent 97f66c57b5
commit cf679c2db3
5 changed files with 41 additions and 155 deletions

150
color.c
View file

@ -82,79 +82,20 @@ color_parse(const char *colstr, ssize_t len,
* \param color color_t struct to store color into.
* \param colstr Color specification.
* \param len The length of colstr (which still MUST be NULL terminated).
* \return request informations.
*/
color_init_cookie_t
color_init_unchecked(color_t *color, const char *colstr, ssize_t len)
{
color_init_cookie_t req;
p_clear(&req, 1);
if(!len)
{
req.has_error = true;
return req;
}
/* The color is given in RGB value */
if(colstr[0] == '#')
{
if(!color_parse(colstr, len, &color->red, &color->green, &color->blue, &color->alpha))
{
req.has_error = true;
return req;
}
color->initialized = true;
/* This means everything is done and _reply() will just return true */
req.color = NULL;
}
else
{
req.color = color;
req.colstr = colstr;
req.cookie = xcb_alloc_named_color_unchecked(globalconf.connection,
globalconf.screen->default_colormap,
len,
colstr);
}
req.has_error = false;
return req;
}
/** Initialize a color.
* \param req color_init request.
* \return True if color allocation was successful.
*/
bool
color_init_reply(color_init_cookie_t req)
color_init_unchecked(color_t *color, const char *colstr, ssize_t len)
{
if(req.has_error)
if(!len)
return false;
if(req.color == NULL)
return true;
/* The color is given in RGB value */
if(!color_parse(colstr, len, &color->red, &color->green, &color->blue, &color->alpha))
return false;
xcb_alloc_named_color_reply_t *named_color;
if((named_color = xcb_alloc_named_color_reply(globalconf.connection,
req.cookie, NULL)))
{
req.color->red = named_color->visual_red;
req.color->green = named_color->visual_green;
req.color->blue = named_color->visual_blue;
req.color->alpha = 0xff;
req.color->initialized = true;
p_delete(&named_color);
return true;
}
warn("awesome: error, cannot allocate color '%s'", req.colstr);
return false;
color->initialized = true;
return true;
}
/** Send a request to initialize a X color.
@ -183,32 +124,21 @@ xcolor_init_unchecked(xcolor_t *color, const char *colstr, ssize_t len)
req.color = color;
/* The color is given in RGB value */
if(colstr[0] == '#')
if(!color_parse(colstr, len, &red, &green, &blue, &alpha))
{
if(!color_parse(colstr, len, &red, &green, &blue, &alpha))
{
warn("awesome: error, invalid color '%s'", colstr);
req.has_error = true;
return req;
}
req.alpha = RGB_8TO16(alpha);
req.is_hexa = true;
req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection,
globalconf.screen->default_colormap,
RGB_8TO16(red),
RGB_8TO16(green),
RGB_8TO16(blue));
}
else
{
req.is_hexa = false;
req.cookie_named = xcb_alloc_named_color_unchecked(globalconf.connection,
globalconf.screen->default_colormap, len,
colstr);
warn("awesome: error, invalid color '%s'", colstr);
req.has_error = true;
return req;
}
req.alpha = RGB_8TO16(alpha);
req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection,
globalconf.screen->default_colormap,
RGB_8TO16(red),
RGB_8TO16(green),
RGB_8TO16(blue));
req.has_error = false;
req.colstr = colstr;
@ -225,39 +155,19 @@ xcolor_init_reply(xcolor_init_request_t req)
if(req.has_error)
return false;
if(req.is_hexa)
{
xcb_alloc_color_reply_t *hexa_color;
xcb_alloc_color_reply_t *hexa_color;
if((hexa_color = xcb_alloc_color_reply(globalconf.connection,
req.cookie_hexa, NULL)))
{
req.color->pixel = hexa_color->pixel;
req.color->red = hexa_color->red;
req.color->green = hexa_color->green;
req.color->blue = hexa_color->blue;
req.color->alpha = req.alpha;
req.color->initialized = true;
p_delete(&hexa_color);
return true;
}
}
else
if((hexa_color = xcb_alloc_color_reply(globalconf.connection,
req.cookie_hexa, NULL)))
{
xcb_alloc_named_color_reply_t *named_color;
if((named_color = xcb_alloc_named_color_reply(globalconf.connection,
req.cookie_named, NULL)))
{
req.color->pixel = named_color->pixel;
req.color->red = named_color->visual_red;
req.color->green = named_color->visual_green;
req.color->blue = named_color->visual_blue;
req.color->alpha = req.alpha;
req.color->initialized = true;
p_delete(&named_color);
return true;
}
req.color->pixel = hexa_color->pixel;
req.color->red = hexa_color->red;
req.color->green = hexa_color->green;
req.color->blue = hexa_color->blue;
req.color->alpha = req.alpha;
req.color->initialized = true;
p_delete(&hexa_color);
return true;
}
warn("awesome: error, cannot allocate color '%s'", req.colstr);

20
color.h
View file

@ -36,14 +36,6 @@ typedef struct
bool initialized;
} color_t;
typedef struct
{
color_t *color;
const char *colstr;
xcb_alloc_named_color_cookie_t cookie;
bool has_error;
} color_init_cookie_t;
typedef struct
{
uint32_t pixel;
@ -56,20 +48,14 @@ typedef struct
typedef struct
{
union
{
xcb_alloc_color_cookie_t cookie_hexa;
xcb_alloc_named_color_cookie_t cookie_named;
};
xcb_alloc_color_cookie_t cookie_hexa;
uint16_t alpha;
xcolor_t *color;
bool is_hexa, has_error;
bool has_error;
const char *colstr;
} xcolor_init_request_t;
color_init_cookie_t color_init_unchecked(color_t *, const char *, ssize_t);
bool color_init_reply(color_init_cookie_t);
bool color_init_unchecked(color_t *, const char *, ssize_t);
xcolor_init_request_t xcolor_init_unchecked(xcolor_t *, const char *, ssize_t);
bool xcolor_init_reply(xcolor_init_request_t);

View file

@ -468,19 +468,17 @@ luaA_image_draw_pixel(lua_State *L)
{
size_t len;
color_t color;
color_init_cookie_t cookie;
image_t *image = luaA_checkudata(L, 1, &image_class);
int x = luaL_checkint(L, 2);
int y = luaL_checkint(L, 3);
const char *buf = luaL_checklstring(L, 4, &len);
cookie = color_init_unchecked(&color, buf, len);
imlib_context_set_image(image->image);
color_init_reply(cookie);
if((x > imlib_image_get_width()) || (y > imlib_image_get_height()))
return 0;
color_init_unchecked(&color, buf, len);
imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
imlib_image_draw_pixel(x, y, 1);
image->isupdated = false;
@ -502,7 +500,6 @@ luaA_image_draw_line(lua_State *L)
{
size_t len;
color_t color;
color_init_cookie_t cookie;
image_t *image = luaA_checkudata(L, 1, &image_class);
int x1 = luaL_checkint(L, 2);
int y1 = luaL_checkint(L, 3);
@ -510,10 +507,9 @@ luaA_image_draw_line(lua_State *L)
int y2 = luaL_checkint(L, 5);
const char *buf = luaL_checklstring(L, 6, &len);
cookie = color_init_unchecked(&color, buf, len);
imlib_context_set_image(image->image);
color_init_reply(cookie);
color_init_unchecked(&color, buf, len);
imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
imlib_image_draw_line(x1, y1, x2, y2, 0);
image->isupdated = false;
@ -536,7 +532,6 @@ luaA_image_draw_rectangle(lua_State *L)
{
size_t len;
color_t color;
color_init_cookie_t cookie;
image_t *image = luaA_checkudata(L, 1, &image_class);
int x = luaL_checkint(L, 2);
int y = luaL_checkint(L, 3);
@ -545,10 +540,9 @@ luaA_image_draw_rectangle(lua_State *L)
int fill = luaA_checkboolean(L, 6);
const char *buf = luaL_checklstring(L, 7, &len);
cookie = color_init_unchecked(&color, buf, len);
imlib_context_set_image(image->image);
color_init_reply(cookie);
color_init_unchecked(&color, buf, len);
imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
if(!fill)
imlib_image_draw_rectangle(x, y, width, height);
@ -583,7 +577,6 @@ luaA_table_to_color_range(lua_State *L, int ud)
{
color_t color;
color_init_cookie_t cookie = color_init_unchecked(&color, colstr, len);
/* get value with colstr as key in table */
lua_pushvalue(L, -1);
@ -594,8 +587,7 @@ luaA_table_to_color_range(lua_State *L, int ud)
/* remove distance */
lua_pop(L, 1);
color_init_reply(cookie);
color_init_unchecked(&color, colstr, len);
imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
imlib_add_color_to_color_range(distance);
@ -661,7 +653,6 @@ luaA_image_draw_circle(lua_State *L)
{
size_t len;
color_t color;
color_init_cookie_t cookie;
image_t *image = luaA_checkudata(L, 1, &image_class);
int x = luaL_checkint(L, 2);
int y = luaL_checkint(L, 3);
@ -670,10 +661,9 @@ luaA_image_draw_circle(lua_State *L)
int fill = luaA_checkboolean(L, 6);
const char *buf = luaL_checklstring(L, 7, &len);
cookie = color_init_unchecked(&color, buf, len);
imlib_context_set_image(image->image);
color_init_reply(cookie);
color_init_unchecked(&color, buf, len);
imlib_context_set_color(color.red, color.green, color.blue, color.alpha);
if(!fill)
imlib_image_draw_ellipse(x, y, ah, av);

View file

@ -133,7 +133,7 @@ luaA_imagebox_newindex(lua_State *L, const char *prop)
if(lua_isnil(L, 3))
p_clear(&d->bg, 1);
else if((buf = luaL_checklstring(L, 3, &len)))
color_init_reply(color_init_unchecked(&d->bg, buf, len));
color_init_unchecked(&d->bg, buf, len);
}
else if(a_strcmp(prop, "resize") == 0)
d->resize = luaA_checkboolean(L, 3);

View file

@ -347,7 +347,7 @@ luaA_textbox_newindex(lua_State *L, const char *prop)
if(lua_isnil(L, 3))
p_clear(&d->bg, 1);
else if((buf = luaL_checklstring(L, 3, &len)))
color_init_reply(color_init_unchecked(&d->bg, buf, len));
color_init_unchecked(&d->bg, buf, len);
}
else if(a_strcmp(prop, "align") == 0)
{
@ -362,7 +362,7 @@ luaA_textbox_newindex(lua_State *L, const char *prop)
else if(a_strcmp(prop, "border_color") == 0)
{
if((buf = luaL_checklstring(L, 3, &len)))
color_init_reply(color_init_unchecked(&d->border.color, buf, len));
color_init_unchecked(&d->border.color, buf, len);
}
else if(a_strcmp(prop, "border_width") == 0)
d->border.width = luaL_checknumber(L, 3);