colourpicker

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

Commit: 10b950996020ca14cc7c7f29353c789f16d30f04
Parent: acb7f1e748158f833cfbf89f76d72f54d0e969fc
Author: Randy Palamar
Date:   Sat, 27 Jul 2024 08:47:14 -0600

store colour t and lerp instead

Diffstat:
Mcolourpicker.c | 37++++++++++++++-----------------------
Mmain.c | 9++-------
Mutil.c | 5+++--
3 files changed, 19 insertions(+), 32 deletions(-)

diff --git a/colourpicker.c b/colourpicker.c @@ -25,24 +25,13 @@ move_towards_f32(f32 current, f32 target, f32 delta) } static v4 -move_towards_v4(v4 current, v4 target, v4 delta) +lerp_v4(v4 a, v4 b, f32 t) { return (v4){ - .x = move_towards_f32(current.x, target.x, delta.x), - .y = move_towards_f32(current.y, target.y, delta.y), - .z = move_towards_f32(current.z, target.z, delta.z), - .w = move_towards_f32(current.w, target.w, delta.w), - }; -} - -static v4 -scaled_sub_v4(v4 a, v4 b, f32 scale) -{ - return (v4){ - .x = scale * (a.x - b.x), - .y = scale * (a.y - b.y), - .z = scale * (a.z - b.z), - .w = scale * (a.w - b.w), + .x = a.x + t * (b.x - a.x), + .y = a.y + t * (b.y - a.y), + .z = a.z + t * (b.z - a.z), + .w = a.w + t * (b.w - a.w), }; } @@ -176,7 +165,7 @@ do_slider(ColourPickerCtx *ctx, Rect r, i32 label_idx) if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) current = (ctx->mouse_pos.x - sr.pos.x) / sr.size.w; current += wheel / 255; - CLAMP(current, 0.0, 1.0); + CLAMP01(current); ctx->colour.E[held_idx] = current; switch (ctx->mode) { case CPM_HSV: ctx->flags |= CPF_REFILL_TEXTURE; break; @@ -498,28 +487,30 @@ do_colour_selector(ColourPickerCtx *ctx, Rect r) v4 fg = normalize_colour(pack_rl_colour(ctx->fg)); f32 scale = 5; - v4 delta = scaled_sub_v4(fg, ctx->hover_colour, scale * ctx->dt); char *labels[2] = {"Revert", "Apply"}; i32 pressed_idx = -1; for (i32 i = 0; i < ARRAY_COUNT(cs); i++) { if (CheckCollisionPointRec(ctx->mouse_pos.rv, cs[i].rr)) { - ctx->selection_colours[i] = move_towards_v4(ctx->selection_colours[i], - ctx->hover_colour, delta); + ctx->selection_hover_t[i] += scale * ctx->dt; + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) pressed_idx = i; } else { - ctx->selection_colours[i] = move_towards_v4(ctx->selection_colours[i], - fg, delta); + ctx->selection_hover_t[i] -= scale * ctx->dt; } + CLAMP01(ctx->selection_hover_t[i]); + + v4 colour = lerp_v4(fg, ctx->hover_colour, ctx->selection_hover_t[i]); + v2 fpos = center_align_text_in_rect(cs[i], labels[i], ctx->font, ctx->font_size); v2 pos = fpos; pos.x += 2; pos.y += 2.5; DrawTextEx(ctx->font, labels[i], pos.rv, ctx->font_size, 0, Fade(BLACK, 0.8)); DrawTextEx(ctx->font, labels[i], fpos.rv, ctx->font_size, 0, - colour_from_normalized(ctx->selection_colours[i])); + colour_from_normalized(colour)); } DrawRectangleRoundedLinesEx(r.rr, STACK_ROUNDNESS, 0, 12, ctx->bg); diff --git a/main.c b/main.c @@ -80,7 +80,7 @@ static f32 parse_f32(char *s) { f32 res = atof(s); - return CLAMP(res, 0, 1); + return CLAMP01(res); } static u32 @@ -138,13 +138,8 @@ main(i32 argc, char *argv[]) }, }, }; - /* TODO: store fg/bg as v4? */ - u32 fg_packed_rgba = ctx.fg.r << 24 | ctx.fg.g << 16 | ctx.fg.b << 8 | ctx.fg.a << 0; - v4 fg = normalize_colour(fg_packed_rgba); - ctx.previous_colour = hsv_to_rgb(ctx.colour); - ctx.selection_colours[0] = fg; - ctx.selection_colours[1] = fg; + ctx.previous_colour = hsv_to_rgb(ctx.colour); { v4 rgb = hsv_to_rgb(ctx.colour); diff --git a/util.c b/util.c @@ -77,8 +77,8 @@ typedef struct { uv2 window_size; Color bg, fg; - v4 selection_colours[2]; - v4 hover_colour; + f32 selection_hover_t[2]; + v4 hover_colour; v2 mouse_pos; @@ -93,6 +93,7 @@ typedef struct { #define ARRAY_COUNT(a) (sizeof(a) / sizeof(*a)) #define ABS(x) ((x) < 0 ? (-x) : (x)) #define CLAMP(x, a, b) ((x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)) +#define CLAMP01(a) CLAMP(a, 0, 1) #define ISDIGIT(a) ((a) >= '0' && (a) <= '9') #define ISHEX(a) (ISDIGIT(a) || ((a) >= 'a' && (a) <= 'f') || ((a) >= 'A' && (a) <= 'F'))