From 922137f64ae3f923cad45c1fa4af9502ed84e1a9 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Mon, 10 Nov 2014 23:04:48 +0100 Subject: Move parse_color to utils.c --- src/utils.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'src/utils.c') 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; +} -- cgit v1.2.3-54-g00ecf