From d9f0a3edace7eac5679a5bb33f18ff176cf7f877 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Thu, 2 Nov 2023 23:15:01 -0700 Subject: 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. --- src/modules/checkers/checkers.c | 53 ++--------------------------------------- src/til.c | 53 +++++++++++++++++++++++++++++++++++++++++ src/til.h | 3 +++ 3 files changed, 58 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/modules/checkers/checkers.c b/src/modules/checkers/checkers.c index 14d6d5c..998c5bf 100644 --- a/src/modules/checkers/checkers.c +++ b/src/modules/checkers/checkers.c @@ -511,55 +511,6 @@ static char * checkers_random_color(unsigned seed) } -/* TODO: migrate to libtil */ -static int checkers_rgb_to_uint32(const char *in, uint32_t *out) -{ - uint32_t color = 0; - - /* this isn't html, but accept #rrggbb syntax */ - if (*in == '#') - in++; - else if (in[0] == '0' && in[1] == 'x') /* and 0xrrggbb */ - in += 2; - - if (strlen(in) != 6) - return -EINVAL; - - for (int i = 0; i < 6;) { - uint8_t c = 0; - - color <<= 8; - - for (int j = 0; j < 2; in++, j++, i++) { - c <<= 4; - - switch (*in) { - case '0'...'9': - c |= (*in) - '0'; - break; - - case 'a'...'f': - c |= (*in) - 'a' + 10; - break; - - case 'A'...'F': - c |= (*in) - 'A' + 10; - break; - - default: - return -EINVAL; - } - } - - color |= c; - } - - *out = color; - - return 0; -} - - static void checkers_setup_free(til_setup_t *setup) { checkers_setup_t *s = (checkers_setup_t *)setup; @@ -879,7 +830,7 @@ static int checkers_setup(const til_settings_t *settings, til_setting_t **res_se if (r < 0) return til_setup_free_with_failed_setting_ret_err(&setup->til_setup, fill, res_setting, -EINVAL); - r = checkers_rgb_to_uint32(fill_color->value, &setup->fill_color); + r = til_rgb_to_uint32(fill_color->value, &setup->fill_color); if (r < 0) return til_setup_free_with_failed_setting_ret_err(&setup->til_setup, fill_color, res_setting, -EINVAL); @@ -888,7 +839,7 @@ static int checkers_setup(const til_settings_t *settings, til_setting_t **res_se return til_setup_free_with_failed_setting_ret_err(&setup->til_setup, clear, res_setting, -EINVAL); if (setup->clear != CHECKERS_CLEAR_CLEAR) { - r = checkers_rgb_to_uint32(clear_color->value, &setup->clear_color); + r = til_rgb_to_uint32(clear_color->value, &setup->clear_color); if (r < 0) return til_setup_free_with_failed_setting_ret_err(&setup->til_setup, clear_color, res_setting, -EINVAL); } 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; +} diff --git a/src/til.h b/src/til.h index 9049a0b..3126ced 100644 --- a/src/til.h +++ b/src/til.h @@ -5,6 +5,8 @@ #include "til_module_context.h" #include "til_setup.h" +#include + /* til_fragmenter_t produces fragments from an input fragment, num being the desired fragment for the current call. * return value of 1 means a fragment has been produced, 0 means num is beyond the end of fragments. */ typedef int (*til_fragmenter_t)(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment); @@ -59,5 +61,6 @@ int til_fragmenter_slice_per_cpu(til_module_context_t *context, const til_fb_fra int til_fragmenter_slice_per_cpu_x16(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment); int til_fragmenter_tile64(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment); int til_value_to_pos(const char **options, const char *value, unsigned *res_pos); +int til_rgb_to_uint32(const char *str, uint32_t *res); #endif -- cgit v1.2.3