colourpicker

Simple Colour Picker written in C
git clone anongit@rnpnr.xyz:colourpicker.git
Log | Files | Refs | Feed | Submodules | README | LICENSE

Commit: ae1ae3723092a19514d797b2eaffff77f7c104e7
Parent: 9040ae9f6f9e3fe4f726d006a02a22f8a4277054
Author: Randy Palamar
Date:   Wed,  9 Oct 2024 20:05:12 -0600

remove atof

Diffstat:
Mcolourpicker.c | 3++-
Mmain.c | 8++++----
Mutil.c | 39++++++++++++++++++++++++++++++++++++---
3 files changed, 42 insertions(+), 8 deletions(-)

diff --git a/colourpicker.c b/colourpicker.c @@ -271,6 +271,7 @@ get_slider_subrects(Rect r, Rect *label, Rect *slider, Rect *value) static void parse_and_store_text_input(ColourPickerCtx *ctx) { + s8 input = {.len = ctx->is.buf_len, .data = ctx->is.buf}; v4 new_colour = {0}; enum colour_mode new_mode = CM_LAST; if (ctx->is.idx == -1) { @@ -281,7 +282,7 @@ parse_and_store_text_input(ColourPickerCtx *ctx) } else { new_mode = ctx->colour_mode; new_colour = ctx->colour; - f32 fv = parse_f32(ctx->is.buf); + f32 fv = parse_f64(input); CLAMP01(fv); switch(ctx->is.idx) { case INPUT_R: new_colour.r = fv; break; diff --git a/main.c b/main.c @@ -133,10 +133,10 @@ main(i32 argc, char *argv[]) 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]); CLAMP01(rgb.r); break; - case 'g': rgb.g = parse_f32(argv[i + 1]); CLAMP01(rgb.g); break; - case 'b': rgb.b = parse_f32(argv[i + 1]); CLAMP01(rgb.b); break; - case 'a': rgb.a = parse_f32(argv[i + 1]); CLAMP01(rgb.a); break; + case 'r': rgb.r = parse_f64(cstr_to_s8(argv[i + 1])); CLAMP01(rgb.r); break; + case 'g': rgb.g = parse_f64(cstr_to_s8(argv[i + 1])); CLAMP01(rgb.g); break; + case 'b': rgb.b = parse_f64(cstr_to_s8(argv[i + 1])); CLAMP01(rgb.b); break; + case 'a': rgb.a = parse_f64(cstr_to_s8(argv[i + 1])); CLAMP01(rgb.a); break; default: usage(argv[0]); break; } i++; diff --git a/util.c b/util.c @@ -364,10 +364,43 @@ parse_hex_u32(char *s) return res; } -static f32 -parse_f32(char *s) +static f64 +parse_f64(s8 s) { - return atof(s); + f64 integral = 0, fractional = 0, sign = 1; + + if (s.len && *s.data == '-') { + sign *= -1; + s.data++; + s.len--; + } + + while (s.len && ISDIGIT(*s.data)) { + integral *= 10; + integral += *s.data - '0'; + s.data++; + s.len--; + } + + if (s.len && *s.data == '.') { s.data++; s.len--; } + + while (s.len) { + ASSERT(s.data[s.len - 1] != '.'); + fractional *= 0.1f; + fractional += (s.data[--s.len] - '0') * 0.1f; + } + + f64 result = sign * (integral + fractional); + + return result; +} + +static s8 +cstr_to_s8(char *s) +{ + s8 result = {.data = s}; + if (s) while (*s) { result.len++; s++; } + return result; } #endif /* _UTIL_C_ */