summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBart Massey <bart@cs.pdx.edu>2007-12-07 00:22:51 -0800
committerBart Massey <bart@cs.pdx.edu>2007-12-07 00:22:51 -0800
commit5cf63c52ef7759b73eab3762a79333f92ef077b1 (patch)
tree7f3ab10baa72d9d2089a1c2c7b8507d1209e1d7d
parenta036a2b7569c20d674a313c7d0797ff879464c34 (diff)
xcb_aux_parse_color()
-rw-r--r--aux/xcb_aux.c47
-rw-r--r--aux/xcb_aux.h3
2 files changed, 50 insertions, 0 deletions
diff --git a/aux/xcb_aux.c b/aux/xcb_aux.c
index 457b96d..011d9d1 100644
--- a/aux/xcb_aux.c
+++ b/aux/xcb_aux.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <xcb/xcb.h>
#include "xcb_aux.h"
@@ -255,3 +256,49 @@ xcb_aux_change_keyboard_control (xcb_connection_t *c,
pack_list(mask, (const uint32_t *)params, value_list);
return xcb_change_keyboard_control( c, mask, value_list );
}
+
+/* Color related functions */
+
+/* Return true if the given color name can be translated locally,
+ in which case load the components. Otherwise, a lookup_color request
+ will be needed, so return false. */
+int
+xcb_aux_parse_color(char *color_name,
+ uint16_t *red, uint16_t *green, uint16_t *blue)
+{
+ int n, r, g, b, i;
+ if (!color_name || *color_name != '#')
+ return 0;
+ /*
+ * Excitingly weird RGB parsing code from Xlib.
+ */
+ n = strlen (color_name);
+ color_name++;
+ n--;
+ if (n != 3 && n != 6 && n != 9 && n != 12)
+ return 0;
+ n /= 3;
+ g = b = 0;
+ do {
+ r = g;
+ g = b;
+ b = 0;
+ for (i = n; --i >= 0; ) {
+ char c = *color_name++;
+ b <<= 4;
+ if (c >= '0' && c <= '9')
+ b |= c - '0';
+ else if (c >= 'A' && c <= 'F')
+ b |= c - ('A' - 10);
+ else if (c >= 'a' && c <= 'f')
+ b |= c - ('a' - 10);
+ else return 0;
+ }
+ } while (*color_name != '\0');
+ n <<= 2;
+ n = 16 - n;
+ *red = r << n;
+ *green = g << n;
+ *blue = b << n;
+ return 1;
+}
diff --git a/aux/xcb_aux.h b/aux/xcb_aux.h
index 70d1ff8..6d4d851 100644
--- a/aux/xcb_aux.h
+++ b/aux/xcb_aux.h
@@ -169,6 +169,9 @@ xcb_aux_change_keyboard_control (xcb_connection_t *c,
uint32_t mask,
const xcb_params_keyboard_t *params);
+int
+xcb_aux_parse_color(char *color_name,
+ uint16_t *red, uint16_t *green, uint16_t *blue);
#ifdef __cplusplus
}