4 #include "game/level/boxes.h"
5 #include "system/stacktrace.h"
6 #include "system/line_stream.h"
7 #include "system/log.h"
8 #include "game/camera.h"
9 #include "color_picker.h"
11 #include "undo_history.h"
13 #define COLOR_SLIDER_HEIGHT 60.0f
14 #define COLOR_PICKER_WIDTH 300.0f
15 #define COLOR_PICKER_HEIGHT (COLOR_SLIDER_HEIGHT * COLOR_SLIDER_N)
16 #define COLOR_PICKER_REFERENCE 1920.0f
17 #define COLOR_PICKER_HW_RATIO (COLOR_PICKER_HEIGHT/ COLOR_PICKER_WIDTH)
18 #define COLOR_PICKER_WR_RATIO (COLOR_PICKER_WIDTH / COLOR_PICKER_REFERENCE)
20 const char *slider_labels[COLOR_SLIDER_N] = {
26 ColorPicker create_color_picker_from_rgba(Color color)
28 Color color_hsla = rgba_to_hsla(color);
29 ColorPicker color_picker = {
31 {0, color_hsla.r, 360.0f},
32 {0, color_hsla.g, 1.0f},
33 {0, color_hsla.b, 1.0f}
39 int color_picker_read_from_line_stream(ColorPicker *color_picker,
40 LineStream *line_stream)
43 const char *line = line_stream_next(line_stream);
48 if (sscanf(line, "%6s", color) == EOF) {
49 log_fail("Could not read color\n");
52 *color_picker = create_color_picker_from_rgba(hexstr(color));
57 int color_picker_render(const ColorPicker *color_picker,
60 trace_assert(color_picker);
63 const Rect viewport = camera_view_port_screen(camera);
64 const Rect boundary = rect(
66 viewport.w * COLOR_PICKER_WR_RATIO,
67 viewport.w * COLOR_PICKER_WR_RATIO * COLOR_PICKER_HW_RATIO);
69 const float color_slider_height =
70 boundary.h / (COLOR_SLIDER_N + 1.0f);
72 if (camera_fill_rect_screen(
74 rect(boundary.x, boundary.y,
75 boundary.w, color_slider_height),
76 color_picker_rgba(color_picker)) < 0) {
80 for (ColorPickerSlider index = 0; index < COLOR_SLIDER_N; ++index) {
81 const Rect slider_rect =
83 boundary.y + color_slider_height * (float) (index + 1),
84 boundary.w, color_slider_height);
85 const float font_scale = boundary.w / COLOR_PICKER_WIDTH;
86 const Vec2f label_size = vec(2.5f * font_scale, 2.5f * font_scale);
89 &color_picker->sliders[index],
95 camera_render_text_screen(
100 vec(slider_rect.x + boundary.w,
101 slider_rect.y + color_slider_height * 0.5f - label_size.y * (float) FONT_CHAR_HEIGHT * 0.5f));
107 // TODO(#932): the `selected` event propagation control is cumbersome
108 int color_picker_event(ColorPicker *color_picker,
109 const SDL_Event *event,
110 const Camera *camera,
113 trace_assert(color_picker);
115 trace_assert(camera);
119 const Rect viewport = camera_view_port_screen(camera);
120 const Rect boundary = rect(
122 viewport.w * COLOR_PICKER_WR_RATIO,
123 viewport.w * COLOR_PICKER_WR_RATIO * COLOR_PICKER_HW_RATIO);
125 const float color_slider_height =
126 boundary.h / (COLOR_SLIDER_N + 1.0f);
128 for (ColorPickerSlider index = 0;
129 !selected && index < COLOR_SLIDER_N;
132 &color_picker->sliders[index],
135 boundary.y + color_slider_height * (float) (index + 1),
136 boundary.w, color_slider_height),
143 *selected_out = selected;
149 Color color_picker_rgba(const ColorPicker *color_picker)
152 color_picker->sliders[COLOR_SLIDER_HUE].value,
153 color_picker->sliders[COLOR_SLIDER_SAT].value,
154 color_picker->sliders[COLOR_SLIDER_LIT].value,