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.
26 #include "globalconf.h"
27 #include "common/xutil.h"
29 /* 0xFFFF / 0xFF == 0x101 (257) */
30 #define RGB_8TO16(i) (((i) & 0xff) * 0x101)
31 #define RGB_16TO8(i) (((i) & 0xffff) / 0x101)
33 /** Parse an hexadecimal color string to its component.
34 * \param colstr The color string.
35 * \param len The color string length.
36 * \param red A pointer to the red color to fill.
37 * \param green A pointer to the green color to fill.
38 * \param blue A pointer to the blue color to fill.
39 * \return True if everything alright.
42 color_parse(const char *colstr
, ssize_t len
,
43 uint8_t *red
, uint8_t *green
, uint8_t *blue
)
48 colnum
= strtoul(colstr
+ 1, &p
, 16);
49 if(len
== 9 && (p
- colstr
) == 9)
51 /* We ignore the alpha component */
56 if(len
!= 7 || colstr
[0] != '#' || (p
- colstr
) != 7)
58 warn("awesome: error, invalid color '%s'", colstr
);
62 *red
= (colnum
>> 16) & 0xff;
63 *green
= (colnum
>> 8) & 0xff;
64 *blue
= colnum
& 0xff;
69 /** Send a request to initialize a X color.
70 * If you are only interested in the rgba values and don't need the color's
71 * pixel value, you should use color_init_unchecked() instead.
72 * \param color color_t struct to store color into.
73 * \param colstr Color specification.
74 * \param len The length of colstr (which still MUST be NULL terminated).
75 * \return request informations.
78 color_init_unchecked(color_t
*color
, const char *colstr
, ssize_t len
)
80 color_init_request_t req
;
81 uint8_t red
, green
, blue
;
93 /* The color is given in RGB value */
94 if(!color_parse(colstr
, len
, &red
, &green
, &blue
))
96 warn("awesome: error, invalid color '%s'", colstr
);
101 req
.cookie_hexa
= xcb_alloc_color_unchecked(globalconf
.connection
,
102 globalconf
.default_cmap
,
107 req
.has_error
= false;
113 /** Initialize a X color.
114 * \param req color_init request.
115 * \return True if color allocation was successful.
118 color_init_reply(color_init_request_t req
)
123 xcb_alloc_color_reply_t
*hexa_color
;
125 if((hexa_color
= xcb_alloc_color_reply(globalconf
.connection
,
126 req
.cookie_hexa
, NULL
)))
128 req
.color
->pixel
= hexa_color
->pixel
;
129 req
.color
->red
= hexa_color
->red
;
130 req
.color
->green
= hexa_color
->green
;
131 req
.color
->blue
= hexa_color
->blue
;
132 req
.color
->initialized
= true;
133 p_delete(&hexa_color
);
137 warn("awesome: error, cannot allocate color '%s'", req
.colstr
);
141 /** Push a color as a string onto the stack
142 * \param L The Lua VM state.
143 * \param c The color to push.
144 * \return The number of elements pushed on stack.
147 luaA_pushcolor(lua_State
*L
, const color_t c
)
149 uint8_t r
= RGB_16TO8(c
.red
);
150 uint8_t g
= RGB_16TO8(c
.green
);
151 uint8_t b
= RGB_16TO8(c
.blue
);
154 int len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x", r
, g
, b
);
155 lua_pushlstring(L
, s
, len
);
159 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80