gri3-wm/libi3/get_colorpixel.c

49 lines
1.3 KiB
C

/*
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
*
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "libi3.h"
/*
* Returns the colorpixel to use for the given hex color (think of HTML). Only
* works for true-color (vast majority of cases) at the moment, avoiding a
* roundtrip to X11.
*
* 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.
*
* NOTE that this function may in the future rely on a global xcb_connection_t
* variable called 'conn' to be present.
*
*/
uint32_t get_colorpixel(const char *hex) {
char alpha[2];
if (strlen(hex) == strlen("#rrggbbaa")) {
alpha[0] = hex[7];
alpha[1] = hex[8];
} else {
alpha[0] = alpha[1] = 'F';
}
char strgroups[4][3] = {
{hex[1], hex[2], '\0'},
{hex[3], hex[4], '\0'},
{hex[5], hex[6], '\0'},
{alpha[0], alpha[1], '\0'}};
uint8_t r = strtol(strgroups[0], NULL, 16);
uint8_t g = strtol(strgroups[1], NULL, 16);
uint8_t b = strtol(strgroups[2], NULL, 16);
uint8_t a = strtol(strgroups[3], NULL, 16);
return (a << 24) | (r << 16 | g << 8 | b);
}