Commit: 42b243f3a4e198c7f175494aba255dcb532ee4a8
Parent: 052c2355a309ed9b16e1a5f98adfeb2ec757129b
Author: Randy Palamar
Date: Mon, 29 Jul 2024 07:25:03 -0600
replace input hex parsing with parse_hex_u32
Diffstat:
3 files changed, 28 insertions(+), 32 deletions(-)
diff --git a/colourpicker.c b/colourpicker.c
@@ -244,11 +244,8 @@ parse_and_store_text_input(ColourPickerCtx *ctx)
v4 new_colour = {0};
enum colour_mode new_mode = CM_LAST;
if (ctx->is.idx == INPUT_HEX) {
- u32 ri, gi, bi, ai;
- sscanf(ctx->is.buf, "%02x%02x%02x%02x", &ri, &gi, &bi, &ai);
- new_colour = (v4){.rv = ColorNormalize((Color){.r = ri, .g = gi,
- .b = bi, .a = ai})};
- new_mode = CM_RGB;
+ new_colour = normalize_colour(parse_hex_u32(ctx->is.buf));
+ new_mode = CM_RGB;
} else {
new_mode = ctx->colour_mode;
new_colour = ctx->colour;
@@ -522,7 +519,6 @@ do_status_bar(ColourPickerCtx *ctx, Rect r, v2 relative_origin)
Rect mode_r;
get_slider_subrects(r, 0, 0, &mode_r);
-
char *mode_txt;
switch (ctx->colour_mode) {
case CM_RGB: mode_txt = "RGB"; break;
@@ -701,7 +697,7 @@ do_colour_stack(ColourPickerCtx *ctx, Rect sa)
static f32 param = 0.0;
param = move_towards_f32(param, push_collides? 1 : 0, 8 * ctx->dt);
- v2 tri_size = {.x = 0.25 * r.size.w, .y = 0.5 * r.size.h};
+ v2 tri_size = {.x = 0.25 * r.size.w, .y = 0.5 * r.size.h};
v2 tri_scale = {.x = 1 - 0.5 * param, .y = 1 + 0.3 * param};
v2 tri_mid = {.x = r.pos.x + 0.5 * r.size.w, .y = r.pos.y - 0.3 * r.size.h * param};
draw_cardinal_triangle(tri_mid, tri_size, tri_scale, NORTH, ctx->fg);
diff --git a/main.c b/main.c
@@ -83,30 +83,6 @@ parse_f32(char *s)
return CLAMP01(res);
}
-static u32
-parse_u32(char *s)
-{
- u32 res = 0;
-
- /* NOTE: skip over '0x' or '0X' */
- if (*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))
- s += 2;
-
- for (; *s; s++) {
- res <<= 4;
- if (ISDIGIT(*s)) {
- res |= *s - '0';
- } else if (ISHEX(*s)) {
- /* NOTE: convert to lowercase first then convert to value */
- *s |= 0x20;
- res |= *s - 0x57;
- } else {
- /* NOTE: do nothing (treat invalid value as 0) */
- }
- }
- return res;
-}
-
int
main(i32 argc, char *argv[])
{
@@ -157,7 +133,7 @@ main(i32 argc, char *argv[])
switch (argv[i][1]) {
case 'h':
- rgb = normalize_colour(parse_u32(argv[i + 1]));
+ rgb = normalize_colour(parse_hex_u32(argv[i + 1]));
ctx.colour = rgb_to_hsv(rgb);
break;
case 'r': rgb.r = parse_f32(argv[i + 1]); break;
diff --git a/util.c b/util.c
@@ -280,4 +280,28 @@ pack_rl_colour(Color colour)
return colour.r << 24 | colour.g << 16 | colour.b << 8 | colour.a << 0;
}
+static u32
+parse_hex_u32(char *s)
+{
+ u32 res = 0;
+
+ /* NOTE: skip over '0x' or '0X' */
+ if (*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))
+ s += 2;
+
+ for (; *s; s++) {
+ res <<= 4;
+ if (ISDIGIT(*s)) {
+ res |= *s - '0';
+ } else if (ISHEX(*s)) {
+ /* NOTE: convert to lowercase first then convert to value */
+ *s |= 0x20;
+ res |= *s - 0x57;
+ } else {
+ /* NOTE: do nothing (treat invalid value as 0) */
+ }
+ }
+ return res;
+}
+
#endif /* _UTIL_C_ */