4 #include "game/level/boxes.h"
5 #include "system/stacktrace.h"
6 #include "system/log.h"
7 #include "game/camera.h"
8 #include "color_picker.h"
10 #include "undo_history.h"
12 #define COLOR_SLIDER_HEIGHT 60.0f
13 #define COLOR_PICKER_WIDTH 300.0f
14 #define COLOR_PICKER_HEIGHT (COLOR_SLIDER_HEIGHT * COLOR_SLIDER_N)
15 #define COLOR_PICKER_REFERENCE 1920.0f
16 #define COLOR_PICKER_HW_RATIO (COLOR_PICKER_HEIGHT/ COLOR_PICKER_WIDTH)
17 #define COLOR_PICKER_WR_RATIO (COLOR_PICKER_WIDTH / COLOR_PICKER_REFERENCE)
19 const char *slider_labels[COLOR_SLIDER_N] = {
25 ColorPicker create_color_picker_from_rgba(Color color)
27 Color color_hsla = rgba_to_hsla(color);
28 ColorPicker color_picker = {
30 {0, color_hsla.r, 360.0f},
31 {0, color_hsla.g, 1.0f},
32 {0, color_hsla.b, 1.0f}
38 int color_picker_render(const ColorPicker *color_picker,
41 trace_assert(color_picker);
44 const Rect viewport = camera_view_port_screen(camera);
45 const Rect boundary = rect(
47 viewport.w * COLOR_PICKER_WR_RATIO,
48 viewport.w * COLOR_PICKER_WR_RATIO * COLOR_PICKER_HW_RATIO);
50 const float color_slider_height =
51 boundary.h / (COLOR_SLIDER_N + 1.0f);
53 if (camera_fill_rect_screen(
55 rect(boundary.x, boundary.y,
56 boundary.w, color_slider_height),
57 color_picker_rgba(color_picker)) < 0) {
61 for (ColorPickerSlider index = 0; index < COLOR_SLIDER_N; ++index) {
62 const Rect slider_rect =
64 boundary.y + color_slider_height * (float) (index + 1),
65 boundary.w, color_slider_height);
66 const float font_scale = boundary.w / COLOR_PICKER_WIDTH;
67 const Vec2f label_size = vec(2.5f * font_scale, 2.5f * font_scale);
70 &color_picker->sliders[index],
76 camera_render_text_screen(
81 vec(slider_rect.x + boundary.w,
82 slider_rect.y + color_slider_height * 0.5f - label_size.y * (float) FONT_CHAR_HEIGHT * 0.5f));
88 // TODO(#932): the `selected` event propagation control is cumbersome
89 int color_picker_event(ColorPicker *color_picker,
90 const SDL_Event *event,
94 trace_assert(color_picker);
100 const Rect viewport = camera_view_port_screen(camera);
101 const Rect boundary = rect(
103 viewport.w * COLOR_PICKER_WR_RATIO,
104 viewport.w * COLOR_PICKER_WR_RATIO * COLOR_PICKER_HW_RATIO);
106 const float color_slider_height =
107 boundary.h / (COLOR_SLIDER_N + 1.0f);
109 for (ColorPickerSlider index = 0;
110 !selected && index < COLOR_SLIDER_N;
113 &color_picker->sliders[index],
116 boundary.y + color_slider_height * (float) (index + 1),
117 boundary.w, color_slider_height),
124 *selected_out = selected;
130 Color color_picker_rgba(const ColorPicker *color_picker)
133 color_picker->sliders[COLOR_SLIDER_HUE].value,
134 color_picker->sliders[COLOR_SLIDER_SAT].value,
135 color_picker->sliders[COLOR_SLIDER_LIT].value,