]> git.sur5r.net Git - i3/i3/blob - libi3/get_colorpixel.c
i3bar: fix flickering shortened status bar on other output(s)
[i3/i3] / libi3 / get_colorpixel.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #include <stdlib.h>
9 #include <stdint.h>
10
11 #include "libi3.h"
12
13 /*
14  * Returns the colorpixel to use for the given hex color (think of HTML). Only
15  * works for true-color (vast majority of cases) at the moment, avoiding a
16  * roundtrip to X11.
17  *
18  * The hex_color has to start with #, for example #FF00FF.
19  *
20  * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
21  * This has to be done by the caller.
22  *
23  * NOTE that this function may in the future rely on a global xcb_connection_t
24  * variable called 'conn' to be present.
25  *
26  */
27 uint32_t get_colorpixel(const char *hex) {
28     char strgroups[3][3] = {{hex[1], hex[2], '\0'},
29                             {hex[3], hex[4], '\0'},
30                             {hex[5], hex[6], '\0'}};
31     uint8_t r = strtol(strgroups[0], NULL, 16);
32     uint8_t g = strtol(strgroups[1], NULL, 16);
33     uint8_t b = strtol(strgroups[2], NULL, 16);
34
35     /* We set the first 8 bits high to have 100% opacity in case of a 32 bit
36      * color depth visual. */
37     return (0xFF << 24) | (r << 16 | g << 8 | b);
38 }