2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
19 SLIST_ENTRY(Colorpixel)
23 SLIST_HEAD(colorpixel_head, Colorpixel)
27 * Returns the colorpixel to use for the given hex color (think of HTML).
29 * The hex_color has to start with #, for example #FF00FF.
31 * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
32 * This has to be done by the caller.
35 uint32_t get_colorpixel(const char *hex) {
36 char strgroups[3][3] = {
37 {hex[1], hex[2], '\0'},
38 {hex[3], hex[4], '\0'},
39 {hex[5], hex[6], '\0'}};
40 uint8_t r = strtol(strgroups[0], NULL, 16);
41 uint8_t g = strtol(strgroups[1], NULL, 16);
42 uint8_t b = strtol(strgroups[2], NULL, 16);
44 /* Shortcut: if our screen is true color, no need to do a roundtrip to X11 */
45 if (root_screen == NULL || root_screen->root_depth == 24 || root_screen->root_depth == 32) {
46 return (0xFF << 24) | (r << 16 | g << 8 | b);
49 /* Lookup this colorpixel in the cache */
50 struct Colorpixel *colorpixel;
51 SLIST_FOREACH(colorpixel, &(colorpixels), colorpixels) {
52 if (strcmp(colorpixel->hex, hex) == 0)
53 return colorpixel->pixel;
56 #define RGB_8_TO_16(i) (65535 * ((i)&0xFF) / 255)
57 int r16 = RGB_8_TO_16(r);
58 int g16 = RGB_8_TO_16(g);
59 int b16 = RGB_8_TO_16(b);
61 xcb_alloc_color_reply_t *reply;
63 reply = xcb_alloc_color_reply(conn, xcb_alloc_color(conn, root_screen->default_colormap, r16, g16, b16),
67 LOG("Could not allocate color\n");
71 uint32_t pixel = reply->pixel;
74 /* Store the result in the cache */
75 struct Colorpixel *cache_pixel = scalloc(1, sizeof(struct Colorpixel));
77 strncpy(cache_pixel->hex, hex, 7);
78 cache_pixel->hex[7] = '\0';
80 cache_pixel->pixel = pixel;
82 SLIST_INSERT_HEAD(&(colorpixels), cache_pixel, colorpixels);