Don't use luaL_error in luaA_object_emit_signal (FS#713)
[awesome.git] / color.c
blob952c9b7ac80f23e0bfaa9d43e6743bac52e8bcd8
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 * \return True if everything alright.
40 static bool
41 color_parse(const char *colstr, ssize_t len,
42 uint8_t *red, uint8_t *green, uint8_t *blue)
44 unsigned long colnum;
45 char *p;
47 colnum = strtoul(colstr + 1, &p, 16);
48 if(len != 7 || (p - colstr) != 7 || colstr[0] != '#')
50 warn("awesome: error, invalid color '%s'", colstr);
51 return false;
54 *red = (colnum >> 16) & 0xff;
55 *green = (colnum >> 8) & 0xff;
56 *blue = colnum & 0xff;
58 return true;
61 /** Send a request to initialize a X color.
62 * If you are only interested in the rgba values and don't need the color's
63 * pixel value, you should use color_init_unchecked() instead.
64 * \param color color_t struct to store color into.
65 * \param colstr Color specification.
66 * \param len The length of colstr (which still MUST be NULL terminated).
67 * \return request informations.
69 color_init_request_t
70 color_init_unchecked(color_t *color, const char *colstr, ssize_t len)
72 color_init_request_t req;
73 uint8_t red, green, blue;
75 p_clear(&req, 1);
77 if(!len)
79 req.has_error = true;
80 return req;
83 req.color = color;
85 /* The color is given in RGB value */
86 if(!color_parse(colstr, len, &red, &green, &blue))
88 warn("awesome: error, invalid color '%s'", colstr);
89 req.has_error = true;
90 return req;
93 req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection,
94 globalconf.default_cmap,
95 RGB_8TO16(red),
96 RGB_8TO16(green),
97 RGB_8TO16(blue));
99 req.has_error = false;
100 req.colstr = colstr;
102 return req;
105 /** Initialize a X color.
106 * \param req color_init request.
107 * \return True if color allocation was successful.
109 bool
110 color_init_reply(color_init_request_t req)
112 if(req.has_error)
113 return false;
115 xcb_alloc_color_reply_t *hexa_color;
117 if((hexa_color = xcb_alloc_color_reply(globalconf.connection,
118 req.cookie_hexa, NULL)))
120 req.color->pixel = hexa_color->pixel;
121 req.color->red = hexa_color->red;
122 req.color->green = hexa_color->green;
123 req.color->blue = hexa_color->blue;
124 req.color->initialized = true;
125 p_delete(&hexa_color);
126 return true;
129 warn("awesome: error, cannot allocate color '%s'", req.colstr);
130 return false;
133 /** Push a color as a string onto the stack
134 * \param L The Lua VM state.
135 * \param c The color to push.
136 * \return The number of elements pushed on stack.
139 luaA_pushcolor(lua_State *L, const color_t c)
141 uint8_t r = (unsigned) c.red * 0xff / 0xffff;
142 uint8_t g = (unsigned) c.green * 0xff / 0xffff;
143 uint8_t b = (unsigned) c.blue * 0xff / 0xffff;
144 char s[10];
145 int len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
146 lua_pushlstring(L, s, len);
147 return 1;
150 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80