+ /* Shortcut: if our screen is true color, no need to do a roundtrip to X11 */
+ if (root_screen == NULL || root_screen->root_depth == 24 || root_screen->root_depth == 32) {
+ return (0xFFUL << 24) | (r << 16 | g << 8 | b);
+ }
+
+ /* Lookup this colorpixel in the cache */
+ struct Colorpixel *colorpixel;
+ SLIST_FOREACH(colorpixel, &(colorpixels), colorpixels) {
+ if (strcmp(colorpixel->hex, hex) == 0)
+ return colorpixel->pixel;
+ }
+
+#define RGB_8_TO_16(i) (65535 * ((i)&0xFF) / 255)
+ int r16 = RGB_8_TO_16(r);
+ int g16 = RGB_8_TO_16(g);
+ int b16 = RGB_8_TO_16(b);
+
+ xcb_alloc_color_reply_t *reply;
+
+ reply = xcb_alloc_color_reply(conn, xcb_alloc_color(conn, root_screen->default_colormap, r16, g16, b16),
+ NULL);
+
+ if (!reply) {
+ LOG("Could not allocate color\n");
+ exit(1);
+ }
+
+ uint32_t pixel = reply->pixel;
+ free(reply);
+
+ /* Store the result in the cache */
+ struct Colorpixel *cache_pixel = scalloc(1, sizeof(struct Colorpixel));
+
+ strncpy(cache_pixel->hex, hex, 7);
+ cache_pixel->hex[7] = '\0';
+
+ cache_pixel->pixel = pixel;
+
+ SLIST_INSERT_HEAD(&(colorpixels), cache_pixel, colorpixels);
+
+ return pixel;