tag: Improve tag property::index support (FS#1229)
[awesome.git] / color.c
blobb9b37f8f40a2bad381f3158057a8605a9a6e96e2
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 "color.h"
24 #include "globalconf.h"
26 #include <ctype.h>
28 /* 0xFFFF / 0xFF == 0x101 (257) */
29 #define RGB_8TO16(i) (((i) & 0xff) * 0x101)
30 #define RGB_16TO8(i) (((i) & 0xffff) / 0x101)
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 == 9 && (p - colstr) == 9)
50 /* We ignore the alpha component */
51 colnum >>= 8;
52 len -= 2;
53 p -= 2;
55 if(len != 7 || colstr[0] != '#' || (p - colstr) != 7)
57 warn("awesome: error, invalid color '%s'", colstr);
58 return false;
61 *red = (colnum >> 16) & 0xff;
62 *green = (colnum >> 8) & 0xff;
63 *blue = colnum & 0xff;
65 return true;
68 /** Send a request to initialize a X color.
69 * If you are only interested in the rgba values and don't need the color's
70 * pixel value, you should use color_init_unchecked() instead.
71 * \param color color_t struct to store color into.
72 * \param colstr Color specification.
73 * \param len The length of colstr (which still MUST be NULL terminated).
74 * \return request informations.
76 color_init_request_t
77 color_init_unchecked(color_t *color, const char *colstr, ssize_t len)
79 color_init_request_t req;
80 uint8_t red, green, blue;
82 p_clear(&req, 1);
84 if(!len)
86 req.has_error = true;
87 return req;
90 req.color = color;
92 /* The color is given in RGB value */
93 if(!color_parse(colstr, len, &red, &green, &blue))
95 warn("awesome: error, invalid color '%s'", colstr);
96 req.has_error = true;
97 return req;
100 req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection,
101 globalconf.default_cmap,
102 RGB_8TO16(red),
103 RGB_8TO16(green),
104 RGB_8TO16(blue));
106 req.has_error = false;
107 req.colstr = colstr;
109 return req;
112 /** Initialize a X color.
113 * \param req color_init request.
114 * \return True if color allocation was successful.
116 bool
117 color_init_reply(color_init_request_t req)
119 if(req.has_error)
120 return false;
122 xcb_alloc_color_reply_t *hexa_color;
124 if((hexa_color = xcb_alloc_color_reply(globalconf.connection,
125 req.cookie_hexa, NULL)))
127 req.color->pixel = hexa_color->pixel;
128 req.color->red = hexa_color->red;
129 req.color->green = hexa_color->green;
130 req.color->blue = hexa_color->blue;
131 req.color->initialized = true;
132 p_delete(&hexa_color);
133 return true;
136 warn("awesome: error, cannot allocate color '%s'", req.colstr);
137 return false;
140 /** Push a color as a string onto the stack
141 * \param L The Lua VM state.
142 * \param c The color to push.
143 * \return The number of elements pushed on stack.
146 luaA_pushcolor(lua_State *L, const color_t c)
148 uint8_t r = RGB_16TO8(c.red);
149 uint8_t g = RGB_16TO8(c.green);
150 uint8_t b = RGB_16TO8(c.blue);
152 char s[10];
153 int len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
154 lua_pushlstring(L, s, len);
155 return 1;
158 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80