From 2bf56f481f338aa5eaba5d524680e0ee785b2b22 Mon Sep 17 00:00:00 2001 From: Randy McShandy Date: Sat, 7 Oct 2023 19:56:51 -0500 Subject: [PATCH] Added a palette system --- project.vim | 12 +++------ source/config.h | 7 +++--- source/rendering.c | 61 ++++++++++++++++++++++++++++++++++++++++++++-- source/structs.h | 35 +++++++++++++++----------- tasks.md | 8 ++++-- 5 files changed, 94 insertions(+), 29 deletions(-) diff --git a/project.vim b/project.vim index 8ec80d4..f9e6391 100644 --- a/project.vim +++ b/project.vim @@ -14,14 +14,10 @@ augroup remember_folds augroup END " start up a faux-IDE view -command! Ide call s:ide() -function! s:ide() - execute("sp") - execute("\J") - execute("resize 10") - execute("term") - execute("NERDTreeToggle") -endfunction +execute("sp") +execute("resize 10") +execute("term") +execute("NERDTreeToggle") let $src=prj_base.'/source' let $bin=prj_base.'/bin' diff --git a/source/config.h b/source/config.h index 1c51135..f15e5e2 100644 --- a/source/config.h +++ b/source/config.h @@ -21,8 +21,8 @@ Window_Layout config_layout = { // satisfying rel.[xy]*2 == .[wh] centers axis in parent container .hsl_square.rel = (SDL_FRect){.x = 0.05, .y = 0.05, .w = 0.50, .h = 0.50}, .hue_slider.rel = (SDL_FRect){.x = 0.70, .y = 0.05, .w = 0.08, .h = 0.50}, - .final_sample.rel = (SDL_FRect){.x = 0.05, .y = 0.65, .w = 0.20, .h = 0.20}, - .info_container.rel = (SDL_FRect){.x = 0.05, .y = 0.65, .w = 0.90, .h = 0.30}, + .final_sample.rel = (SDL_FRect){.x = 0.05, .y = 0.60, .w = 0.20, .h = 0.20}, + .info_container.rel = (SDL_FRect){.x = 0.05, .y = 0.60, .w = 0.90, .h = 0.40}, .info_boxes.rel = (SDL_FRect){.x = 0.25, .y = 0.00, .w = 0.75, .h = 1.00}, .rgb_info.rel = (SDL_FRect){.x = 0.00, .y = 0.00, .w = 1.00, .h = 0.50}, .red_component.body.rel = (SDL_FRect){.x = 0.00, .y = 0.00, .w = 0.30, .h = 1.00}, @@ -31,7 +31,8 @@ Window_Layout config_layout = { .hsl_info.rel = (SDL_FRect){.x = 0.50, .y = 0.00, .w = 1.00, .h = 0.50}, .hue_component.body.rel = (SDL_FRect){.x = 0.00, .y = 0.00, .w = 0.30, .h = 1.00}, .sat_component.body.rel = (SDL_FRect){.x = 0.00, .y = 0.40, .w = 0.30, .h = 1.00}, - .lum_component.body.rel = (SDL_FRect){.x = 0.00, .y = 0.80, .w = 0.30, .h = 1.00} + .lum_component.body.rel = (SDL_FRect){.x = 0.00, .y = 0.80, .w = 0.30, .h = 1.00}, + .palette.rel = (SDL_FRect){.x = 0.00, .y = 0.60, .w = 1.00, .h = 0.30} }; #endif //__PICKER_CONFIG__ diff --git a/source/rendering.c b/source/rendering.c index 691786b..498762d 100644 --- a/source/rendering.c +++ b/source/rendering.c @@ -15,6 +15,7 @@ SDL_Color green = {0, 255, 0, 255}; SDL_Color blue = {0, 0, 255, 255}; SDL_Color black = {0, 0, 0, 255}; SDL_Color white = {255, 255, 255, 255}; +HSL_Color hsl_white = {0, 0, 100}; SDL_Color magenta = {255, 0, 255, 255}; sdl_group mgr; @@ -35,6 +36,7 @@ int32_t refresh_layout(Runtime_Info* runtime) render_container(&runtime->layout.window, &runtime->layout.info_container); render_container(&runtime->layout.window, &runtime->layout.final_sample); render_container(&runtime->layout.info_container.real, &runtime->layout.info_boxes); + render_container(&runtime->layout.info_container.real, &runtime->layout.palette); render_container(&runtime->layout.info_boxes.real, &runtime->layout.rgb_info); render_container(&runtime->layout.rgb_info.real, &runtime->layout.red_component.body); render_container(&runtime->layout.rgb_info.real, &runtime->layout.green_component.body); @@ -86,6 +88,11 @@ int32_t init_renderer(Runtime_Info* runtime) init_text_container(&runtime->layout.sat_component, 64); init_text_container(&runtime->layout.lum_component, 64); + for (int p = 0; p < PALETTE_SIZE; p++) + { + runtime->layout.palette_color[p] = hsl_white; + } + refresh_layout(runtime); return 0; @@ -152,8 +159,27 @@ int32_t mouse_click_in_container(Runtime_Info* runtime, SDL_FRect* container) return 0; } -// Rename this eventually -// It's actually HSL +int32_t render_palette(Runtime_Info* runtime, SDL_FRect* container) +{ + for (int p = 0; p < PALETTE_SIZE; p++) + { + SDL_Color item_color = hsl_to_rgb(runtime->layout.palette_color[p]); + SDL_FRect item = { + .x = container->x + (p * ((float)container->w / (float)PALETTE_SIZE)), .y = container->y, + .w = ((float)container->w / PALETTE_SIZE) * 0.90, .h = container->h}; + + SDL_SetRenderDrawColor(mgr.rend, unroll_sdl_color(item_color)); + if (p == runtime->active_palette) + { + SDL_SetRenderDrawColor(mgr.rend, unroll_sdl_color(item_color)); + item.h *= 1.2; + } + SDL_RenderFillRectF(mgr.rend, &item); + } + + return 0; +} + int32_t render_hsl_square(Runtime_Info* runtime, SDL_FRect* container) { NULL_CHECK(runtime); @@ -354,6 +380,7 @@ int32_t render_layout(Runtime_Info* runtime, Window_Layout* layout) render_vertical_hue_spectrum(runtime, &layout->hue_slider.real); render_hsl_square(runtime, &layout->hsl_square.real); render_info_boxes(runtime, &layout->info_boxes.real); + render_palette(runtime, &layout->palette.real); return 0; } @@ -422,6 +449,7 @@ int32_t check_inputs(Runtime_Info* runtime) } if (mgr.event.type == SDL_KEYUP) { + int p = runtime->active_palette; switch(mgr.event.key.keysym.sym) { case SDLK_q: @@ -431,7 +459,36 @@ int32_t check_inputs(Runtime_Info* runtime) case SDLK_LSHIFT: move_speed = 1; break; + + case SDLK_0: + case SDLK_1: + case SDLK_2: + case SDLK_3: + case SDLK_4: + case SDLK_5: + case SDLK_6: + case SDLK_7: + case SDLK_8: + case SDLK_9: + { + int new_palette = mgr.event.key.keysym.sym - 48 - 1; + if (new_palette < 0) + new_palette = PALETTE_SIZE-1; + + if (new_palette <= PALETTE_SIZE) + { + runtime->active_palette = new_palette; + runtime->active_hsl = runtime->layout.palette_color[new_palette]; + runtime->active_rgb = hsl_to_rgb(runtime->active_hsl); + } + } break; + + case SDLK_RETURN: + case SDLK_KP_ENTER: + runtime->layout.palette_color[p] = runtime->active_hsl; + break; } + } } diff --git a/source/structs.h b/source/structs.h index 1cbf73d..92053ec 100644 --- a/source/structs.h +++ b/source/structs.h @@ -6,6 +6,8 @@ #define Relative_Rect SDL_FRect #include +#define PALETTE_SIZE 10 + // A placeable rect, with relative components to its parent, // and its finalized real placement rectangle typedef struct @@ -54,20 +56,23 @@ typedef struct { // Tabbing these to visually indicate layout SDL_FRect window; - Layout_Rect hsl_square; // big clicky draggy square - Layout_Rect hue_slider; // HSV slider bar - Layout_Rect info_container; - Layout_Rect final_sample; // small square showing full selected color - Layout_Rect info_boxes; - Layout_Rect rgb_info; - Text_Container red_component; - Text_Container green_component; - Text_Container blue_component; - Layout_Rect hsl_info; - Text_Container hue_component; - Text_Container sat_component; - Text_Container lum_component; - + Layout_Rect hsl_square; // big clicky draggy square + Layout_Rect hue_slider; // HSV slider bar + Layout_Rect info_container; + Layout_Rect palette; + Layout_Rect final_sample; // small square showing full selected color + Layout_Rect info_boxes; + Layout_Rect rgb_info; + Text_Container red_component; + Text_Container green_component; + Text_Container blue_component; + Layout_Rect hsl_info; + Text_Container hue_component; + Text_Container sat_component; + Text_Container lum_component; + + Layout_Rect palette_item[PALETTE_SIZE]; + HSL_Color palette_color[PALETTE_SIZE]; } Window_Layout; typedef struct @@ -85,6 +90,8 @@ typedef struct SDL_Point mouse_click_pos; int mouse_click; + int active_palette; + } Runtime_Info; #endif // STRUCTS__ diff --git a/tasks.md b/tasks.md index 507fd00..babe5cc 100644 --- a/tasks.md +++ b/tasks.md @@ -1,17 +1,21 @@ ## To Do -- Mode-based keybinds - UI library > Move UI stuff into a standalone library some day, because it's kinda cool +- Color export + > Send active color somewhere like the clipboard. X usually uses xclip, so it'll need pipes. ## Doing ## Done -- Color export - Config header - Cleanup * [x] Text rendering * [x] Layout rendering * [x] Old gameframe cruft needs to go +- Mouse support + > Add some simple mouse support for hue spectrum and S/L box +- Palette + > Add a palette for saving and recalling colors -- 2.49.0