globalconf: remove config.h include
[awesome.git] / color.c
blob364ceb12dc6875974fc4bf0f934f801a6913eb7b
1 /*
2 * color.c - color functions
4 * Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
5 * Copyright © 2009 Uli Schlachter <psychon@znc.in>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <ctype.h>
25 #include "color.h"
26 #include "globalconf.h"
27 #include "common/xutil.h"
29 #define RGB_8TO16(i) (0xffff * ((i) & 0xff) / 0xff)
30 #define RGB_16TO8(i) (0xff * ((i) & 0xffff) / 0xffff)
32 /** Parse an hexadecimal color string to its component.
33 * \param colstr The color string.
34 * \param len The color string length.
35 * \param red A pointer to the red color to fill.
36 * \param green A pointer to the green color to fill.
37 * \param blue A pointer to the blue color to fill.
38 * \param alpha A pointer to the alpha color to fill.
39 * \return True if everything alright.
41 static bool
42 color_parse(const char *colstr, ssize_t len,
43 uint8_t *red, uint8_t *green, uint8_t *blue, uint8_t *alpha)
45 unsigned long colnum;
46 char *p;
48 if(len == 7)
50 colnum = strtoul(colstr + 1, &p, 16);
51 if(p - colstr != 7)
52 goto invalid;
53 *alpha = 0xff;
55 /* we have alpha */
56 else if(len == 9)
58 colnum = strtoul(colstr + 1, &p, 16);
59 if(p - colstr != 9)
60 goto invalid;
61 *alpha = colnum & 0xff;
62 colnum >>= 8;
64 else
66 invalid:
67 warn("awesome: error, invalid color '%s'", colstr);
68 return false;
71 *red = (colnum >> 16) & 0xff;
72 *green = (colnum >> 8) & 0xff;
73 *blue = colnum & 0xff;
75 return true;
78 /** Send a request to initialize a color.
79 * If you are only interested in the color's pixel value or need both, the pixel
80 * value and the rgba components, use xcolor_init_unchecked() and/or
81 * xcolor_to_color() instead.
82 * \param color color_t struct to store color into.
83 * \param colstr Color specification.
84 * \param len The length of colstr (which still MUST be NULL terminated).
85 * \return request informations.
87 color_init_cookie_t
88 color_init_unchecked(color_t *color, const char *colstr, ssize_t len)
90 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
91 color_init_cookie_t req;
93 p_clear(&req, 1);
95 if(!len)
97 req.has_error = true;
98 return req;
101 /* The color is given in RGB value */
102 if(colstr[0] == '#')
104 if(!color_parse(colstr, len, &color->red, &color->green, &color->blue, &color->alpha))
106 req.has_error = true;
107 return req;
110 color->initialized = true;
112 /* This means everything is done and _reply() will just return true */
113 req.color = NULL;
115 else
117 req.color = color;
118 req.colstr = colstr;
119 req.cookie = xcb_alloc_named_color_unchecked(globalconf.connection,
120 s->default_colormap, len,
121 colstr);
124 req.has_error = false;
126 return req;
129 /** Initialize a color.
130 * \param req color_init request.
131 * \return True if color allocation was successful.
133 bool
134 color_init_reply(color_init_cookie_t req)
136 if(req.has_error)
137 return false;
139 if(req.color == NULL)
140 return true;
142 xcb_alloc_named_color_reply_t *named_color;
144 if((named_color = xcb_alloc_named_color_reply(globalconf.connection,
145 req.cookie, NULL)))
147 req.color->red = named_color->visual_red;
148 req.color->green = named_color->visual_green;
149 req.color->blue = named_color->visual_blue;
150 req.color->alpha = 0xff;
151 req.color->initialized = true;
152 p_delete(&named_color);
153 return true;
156 warn("awesome: error, cannot allocate color '%s'", req.colstr);
157 return false;
160 /** Send a request to initialize a X color.
161 * If you are only interested in the rgba values and don't need the color's
162 * pixel value, you should use color_init_unchecked() instead.
163 * \param color xcolor_t struct to store color into.
164 * \param colstr Color specification.
165 * \param len The length of colstr (which still MUST be NULL terminated).
166 * \return request informations.
168 xcolor_init_request_t
169 xcolor_init_unchecked(xcolor_t *color, const char *colstr, ssize_t len)
171 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
172 xcolor_init_request_t req;
173 uint8_t red, green, blue, alpha;
175 p_clear(&req, 1);
177 if(!len)
179 req.has_error = true;
180 return req;
183 req.alpha = 0xffff;
184 req.color = color;
186 /* The color is given in RGB value */
187 if(colstr[0] == '#')
189 if(!color_parse(colstr, len, &red, &green, &blue, &alpha))
191 warn("awesome: error, invalid color '%s'", colstr);
192 req.has_error = true;
193 return req;
196 req.alpha = RGB_8TO16(alpha);
198 req.is_hexa = true;
199 req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection,
200 s->default_colormap,
201 RGB_8TO16(red),
202 RGB_8TO16(green),
203 RGB_8TO16(blue));
205 else
207 req.is_hexa = false;
208 req.cookie_named = xcb_alloc_named_color_unchecked(globalconf.connection,
209 s->default_colormap, len,
210 colstr);
213 req.has_error = false;
214 req.colstr = colstr;
216 return req;
219 /** Initialize a X color.
220 * \param req xcolor_init request.
221 * \return True if color allocation was successful.
223 bool
224 xcolor_init_reply(xcolor_init_request_t req)
226 if(req.has_error)
227 return false;
229 if(req.is_hexa)
231 xcb_alloc_color_reply_t *hexa_color;
233 if((hexa_color = xcb_alloc_color_reply(globalconf.connection,
234 req.cookie_hexa, NULL)))
236 req.color->pixel = hexa_color->pixel;
237 req.color->red = hexa_color->red;
238 req.color->green = hexa_color->green;
239 req.color->blue = hexa_color->blue;
240 req.color->alpha = req.alpha;
241 req.color->initialized = true;
242 p_delete(&hexa_color);
243 return true;
246 else
248 xcb_alloc_named_color_reply_t *named_color;
250 if((named_color = xcb_alloc_named_color_reply(globalconf.connection,
251 req.cookie_named, NULL)))
253 req.color->pixel = named_color->pixel;
254 req.color->red = named_color->visual_red;
255 req.color->green = named_color->visual_green;
256 req.color->blue = named_color->visual_blue;
257 req.color->alpha = req.alpha;
258 req.color->initialized = true;
259 p_delete(&named_color);
260 return true;
264 warn("awesome: error, cannot allocate color '%s'", req.colstr);
265 return false;
268 /** Convert a xcolor struct to a color one.
269 * \param xcol The X color.
270 * \param col The color.
271 * \return True if everything has been converted.
273 bool
274 xcolor_to_color(const xcolor_t *xcol, color_t *col)
276 if (!xcol->initialized)
278 col->initialized = false;
279 return false;
282 col->initialized = true;
283 col->red = RGB_16TO8(xcol->red);
284 col->green = RGB_16TO8(xcol->green);
285 col->blue = RGB_16TO8(xcol->blue);
286 col->alpha = RGB_16TO8(xcol->alpha);
288 return true;
291 /** Push a color as a string onto the stack
292 * \param L The Lua VM state.
293 * \param c The color to push.
294 * \return The number of elements pushed on stack.
297 luaA_pushxcolor(lua_State *L, const xcolor_t c)
299 uint8_t r = (unsigned) c.red * 0xff / 0xffff;
300 uint8_t g = (unsigned) c.green * 0xff / 0xffff;
301 uint8_t b = (unsigned) c.blue * 0xff / 0xffff;
302 uint8_t a = (unsigned) c.alpha * 0xff / 0xffff;
303 char s[10];
304 int len;
305 /* do not print alpha if it's full */
306 if(a == 0xff)
307 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
308 else
309 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
310 lua_pushlstring(L, s, len);
311 return 1;
314 /** Push a color as a string onto the stack
315 * \param L The Lua VM state.
316 * \param c The color to push.
317 * \return The number of elements pushed on stack.
320 luaA_pushcolor(lua_State *L, const color_t *c)
322 uint8_t r = c->red;
323 uint8_t g = c->green;
324 uint8_t b = c->blue;
325 uint8_t a = c->alpha;
326 char s[10];
327 int len;
328 /* do not print alpha if it's full */
329 if(a == 0xff)
330 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
331 else
332 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
333 lua_pushlstring(L, s, len);
334 return 1;
337 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80