2011-10-23 18:38:21 +02:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2015-04-04 02:17:56 +02:00
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
2011-10-23 18:38:21 +02:00
|
|
|
*
|
|
|
|
*/
|
2016-10-11 09:13:35 +02:00
|
|
|
#include "libi3.h"
|
|
|
|
|
2011-10-23 18:38:21 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2015-10-07 11:03:18 +02:00
|
|
|
#include <string.h>
|
2011-10-23 18:38:21 +02:00
|
|
|
|
2015-12-28 12:58:32 +01:00
|
|
|
#include "queue.h"
|
|
|
|
struct Colorpixel {
|
2016-04-13 17:53:15 +02:00
|
|
|
char hex[8];
|
2015-12-28 12:58:32 +01:00
|
|
|
uint32_t pixel;
|
|
|
|
|
|
|
|
SLIST_ENTRY(Colorpixel)
|
|
|
|
colorpixels;
|
|
|
|
};
|
|
|
|
|
|
|
|
SLIST_HEAD(colorpixel_head, Colorpixel)
|
|
|
|
colorpixels;
|
|
|
|
|
2011-10-23 18:38:21 +02:00
|
|
|
/*
|
2015-12-28 12:58:32 +01:00
|
|
|
* Returns the colorpixel to use for the given hex color (think of HTML).
|
2011-10-23 18:38:21 +02:00
|
|
|
*
|
|
|
|
* The hex_color has to start with #, for example #FF00FF.
|
|
|
|
*
|
|
|
|
* NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
|
|
|
|
* This has to be done by the caller.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
uint32_t get_colorpixel(const char *hex) {
|
2015-10-10 21:27:23 +02:00
|
|
|
char strgroups[3][3] = {
|
2015-10-07 11:03:18 +02:00
|
|
|
{hex[1], hex[2], '\0'},
|
|
|
|
{hex[3], hex[4], '\0'},
|
2015-10-10 21:27:23 +02:00
|
|
|
{hex[5], hex[6], '\0'}};
|
2011-10-23 18:38:21 +02:00
|
|
|
uint8_t r = strtol(strgroups[0], NULL, 16);
|
|
|
|
uint8_t g = strtol(strgroups[1], NULL, 16);
|
|
|
|
uint8_t b = strtol(strgroups[2], NULL, 16);
|
|
|
|
|
2015-12-28 12:58:32 +01:00
|
|
|
/* Shortcut: if our screen is true color, no need to do a roundtrip to X11 */
|
2016-01-03 01:11:55 +01:00
|
|
|
if (root_screen == NULL || root_screen->root_depth == 24 || root_screen->root_depth == 32) {
|
2018-10-13 20:04:40 +02:00
|
|
|
return (0xFFUL << 24) | (r << 16 | g << 8 | b);
|
2015-12-28 12:58:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2018-08-22 13:51:17 +02:00
|
|
|
reply = xcb_alloc_color_reply(conn, xcb_alloc_color(conn, root_screen->default_colormap, r16, g16, b16),
|
2015-12-28 12:58:32 +01:00
|
|
|
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));
|
2016-04-15 15:24:03 +02:00
|
|
|
|
|
|
|
strncpy(cache_pixel->hex, hex, 7);
|
|
|
|
cache_pixel->hex[7] = '\0';
|
|
|
|
|
2015-12-28 12:58:32 +01:00
|
|
|
cache_pixel->pixel = pixel;
|
|
|
|
|
|
|
|
SLIST_INSERT_HEAD(&(colorpixels), cache_pixel, colorpixels);
|
|
|
|
|
|
|
|
return pixel;
|
2011-10-23 18:38:21 +02:00
|
|
|
}
|