summaryrefslogtreecommitdiff
path: root/src/til.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2023-11-02 23:15:01 -0700
committerVito Caputo <vcaputo@pengaru.com>2023-11-02 23:15:01 -0700
commitd9f0a3edace7eac5679a5bb33f18ff176cf7f877 (patch)
tree646e9f32768f36dbac1abfd013b50c07286ef7c5 /src/til.c
parent5758164a4fd4a65c148aee72159c7cff0010764f (diff)
til,checkers: move checkers_rgb_to_uint32() to libtil
Becomes til_rgb_to_uint32(). Nothing functionally changed, just making this generally available for anything wanting to parse an rgb hex string into a uint32 packed pixel in a setup_func.
Diffstat (limited to 'src/til.c')
-rw-r--r--src/til.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/til.c b/src/til.c
index 89e0046..2936fce 100644
--- a/src/til.c
+++ b/src/til.c
@@ -694,3 +694,56 @@ int til_value_to_pos(const char **options, const char *value, unsigned *res_pos)
return -ENOENT;
}
+
+
+/* Helper for turning a hex string rgb color into an uint32 */
+int til_rgb_to_uint32(const char *str, uint32_t *res)
+{
+ uint32_t color = 0;
+
+ assert(str);
+ assert(res);
+
+ /* this isn't html, but accept #rrggbb syntax */
+ if (*str == '#')
+ str++;
+ else if (str[0] == '0' && str[1] == 'x') /* and 0xrrggbb */
+ str += 2;
+
+ if (strlen(str) != 6)
+ return -EINVAL;
+
+ /* TODO: maybe support alternatively including alpha? e.g. #aarrggbb? */
+ for (int i = 0; i < 6;) {
+ uint8_t c = 0;
+
+ color <<= 8;
+
+ for (int j = 0; j < 2; str++, j++, i++) {
+ c <<= 4;
+
+ switch (*str) {
+ case '0'...'9':
+ c |= (*str) - '0';
+ break;
+
+ case 'a'...'f':
+ c |= (*str) - 'a' + 10;
+ break;
+
+ case 'A'...'F':
+ c |= (*str) - 'A' + 10;
+ break;
+
+ default:
+ return -EINVAL;
+ }
+ }
+
+ color |= c;
+ }
+
+ *res = color;
+
+ return 0;
+}
© All Rights Reserved