aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
index 2425576..29fdd65 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -32,3 +32,49 @@ parse_int(char *string, char *arg)
}
return (int)val;
}
+
+bool
+parse_color (char *hex, Color *c, int a)
+{
+ if ((strlen(hex) != 7) && (strlen(hex) != 9))
+ return false;
+
+ int len;
+ if (strlen(hex) == 9) {
+ len = 4;
+ } else {
+ len = 3;
+ }
+
+ hex++;
+ int colors[4];
+ for (int i = 0; i < len; hex += 2, i++) {
+ char color[3];
+ strlcpy(color, hex, 3);
+
+ char *endptr;
+ errno = 0;
+ long val = strtol(color, &endptr, 16);
+ if (errno != 0) {
+ perror("strtol");
+ exit(-2);
+ }
+ if (endptr == color) {
+ fprintf(stderr, "No valid hex color found\n");
+ exit(-2);
+ }
+
+ colors[i] = (int)val;
+ }
+
+ c->r = colors[0];
+ c->g = colors[1];
+ c->b = colors[2];
+ if (len == 4) {
+ c->a = colors[3];
+ } else {
+ c->a = a;
+ }
+
+ return true;
+}