]> git.sur5r.net Git - i3/i3/blob - libi3/get_colorpixel.c
Fixes for undefined behaviour on signed shift (#3453)
[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 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #include "libi3.h"
9
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <string.h>
13
14 #include "queue.h"
15 struct Colorpixel {
16     char hex[8];
17     uint32_t pixel;
18
19     SLIST_ENTRY(Colorpixel)
20     colorpixels;
21 };
22
23 SLIST_HEAD(colorpixel_head, Colorpixel)
24 colorpixels;
25
26 /*
27  * Returns the colorpixel to use for the given hex color (think of HTML).
28  *
29  * The hex_color has to start with #, for example #FF00FF.
30  *
31  * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
32  * This has to be done by the caller.
33  *
34  */
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);
43
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 (0xFFUL << 24) | (r << 16 | g << 8 | b);
47     }
48
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;
54     }
55
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);
60
61     xcb_alloc_color_reply_t *reply;
62
63     reply = xcb_alloc_color_reply(conn, xcb_alloc_color(conn, root_screen->default_colormap, r16, g16, b16),
64                                   NULL);
65
66     if (!reply) {
67         LOG("Could not allocate color\n");
68         exit(1);
69     }
70
71     uint32_t pixel = reply->pixel;
72     free(reply);
73
74     /* Store the result in the cache */
75     struct Colorpixel *cache_pixel = scalloc(1, sizeof(struct Colorpixel));
76
77     strncpy(cache_pixel->hex, hex, 7);
78     cache_pixel->hex[7] = '\0';
79
80     cache_pixel->pixel = pixel;
81
82     SLIST_INSERT_HEAD(&(colorpixels), cache_pixel, colorpixels);
83
84     return pixel;
85 }