xcolor: Rename to color
[awesome.git] / color.c
blobfb7bce6cd7ca371f53f63475289a27f46cecbf01
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 X color.
79 * If you are only interested in the rgba values and don't need the color's
80 * pixel value, you should use color_init_unchecked() instead.
81 * \param color color_t struct to store color into.
82 * \param colstr Color specification.
83 * \param len The length of colstr (which still MUST be NULL terminated).
84 * \return request informations.
86 color_init_request_t
87 color_init_unchecked(color_t *color, const char *colstr, ssize_t len)
89 color_init_request_t req;
90 uint8_t red, green, blue, alpha;
92 p_clear(&req, 1);
94 if(!len)
96 req.has_error = true;
97 return req;
100 req.alpha = 0xffff;
101 req.color = color;
103 /* The color is given in RGB value */
104 if(!color_parse(colstr, len, &red, &green, &blue, &alpha))
106 warn("awesome: error, invalid color '%s'", colstr);
107 req.has_error = true;
108 return req;
111 req.alpha = RGB_8TO16(alpha);
113 req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection,
114 globalconf.default_cmap,
115 RGB_8TO16(red),
116 RGB_8TO16(green),
117 RGB_8TO16(blue));
119 req.has_error = false;
120 req.colstr = colstr;
122 return req;
125 /** Initialize a X color.
126 * \param req color_init request.
127 * \return True if color allocation was successful.
129 bool
130 color_init_reply(color_init_request_t req)
132 if(req.has_error)
133 return false;
135 xcb_alloc_color_reply_t *hexa_color;
137 if((hexa_color = xcb_alloc_color_reply(globalconf.connection,
138 req.cookie_hexa, NULL)))
140 req.color->pixel = hexa_color->pixel;
141 req.color->red = hexa_color->red;
142 req.color->green = hexa_color->green;
143 req.color->blue = hexa_color->blue;
144 req.color->alpha = req.alpha;
145 req.color->initialized = true;
146 p_delete(&hexa_color);
147 return true;
150 warn("awesome: error, cannot allocate color '%s'", req.colstr);
151 return false;
154 /** Push a color as a string onto the stack
155 * \param L The Lua VM state.
156 * \param c The color to push.
157 * \return The number of elements pushed on stack.
160 luaA_pushcolor(lua_State *L, const color_t c)
162 uint8_t r = (unsigned) c.red * 0xff / 0xffff;
163 uint8_t g = (unsigned) c.green * 0xff / 0xffff;
164 uint8_t b = (unsigned) c.blue * 0xff / 0xffff;
165 uint8_t a = (unsigned) c.alpha * 0xff / 0xffff;
166 char s[10];
167 int len;
168 /* do not print alpha if it's full */
169 if(a == 0xff)
170 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
171 else
172 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
173 lua_pushlstring(L, s, len);
174 return 1;
177 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80