#include "color.h"
#include "undo_history.h"
-#define COLOR_SLIDER_HEIGHT 50.0f
-#define COLOR_SLIDER_WIDTH 300.0f
-#define COLOR_SLIDER_PADDING_BOTTOM 10.0f
+#define COLOR_SLIDER_HEIGHT 60.0f
+#define COLOR_PICKER_WIDTH 300.0f
+#define COLOR_PICKER_HEIGHT (COLOR_SLIDER_HEIGHT * COLOR_SLIDER_N)
+#define COLOR_PICKER_REFERENCE 1920.0f
+#define COLOR_PICKER_HW_RATIO (COLOR_PICKER_HEIGHT/ COLOR_PICKER_WIDTH)
+#define COLOR_PICKER_WR_RATIO (COLOR_PICKER_WIDTH / COLOR_PICKER_REFERENCE)
const char *slider_labels[COLOR_SLIDER_N] = {
"Hue",
"Lightness"
};
-LayerPtr color_picker_as_layer(ColorPicker *color_picker)
-{
- LayerPtr layer = {
- .ptr = color_picker,
- .type = LAYER_COLOR_PICKER
- };
- return layer;
-}
-
ColorPicker create_color_picker_from_rgba(Color color)
{
Color color_hsla = rgba_to_hsla(color);
{0, color_hsla.r, 360.0f},
{0, color_hsla.g, 1.0f},
{0, color_hsla.b, 1.0f}
- },
- .color = color
+ }
};
return color_picker;
}
}
int color_picker_render(const ColorPicker *color_picker,
- Camera *camera)
+ const Camera *camera)
{
trace_assert(color_picker);
trace_assert(camera);
+ const Rect viewport = camera_view_port_screen(camera);
+ const Rect boundary = rect(
+ 0.0f, 0.0f,
+ viewport.w * COLOR_PICKER_WR_RATIO,
+ viewport.w * COLOR_PICKER_WR_RATIO * COLOR_PICKER_HW_RATIO);
+
+ const float color_slider_height =
+ boundary.h / (COLOR_SLIDER_N + 1.0f);
+
if (camera_fill_rect_screen(
camera,
- rect(0.0f, 0.0f, COLOR_SLIDER_WIDTH, COLOR_SLIDER_HEIGHT),
+ rect(boundary.x, boundary.y,
+ boundary.w, color_slider_height),
color_picker_rgba(color_picker)) < 0) {
return -1;
}
for (ColorPickerSlider index = 0; index < COLOR_SLIDER_N; ++index) {
const Rect slider_rect =
- rect(0.0f, (COLOR_SLIDER_HEIGHT + COLOR_SLIDER_PADDING_BOTTOM) * (float) (index + 1),
- COLOR_SLIDER_WIDTH, COLOR_SLIDER_HEIGHT);
- const Point label_size = vec(2.5f, 2.5f);
+ rect(boundary.x,
+ boundary.y + color_slider_height * (float) (index + 1),
+ boundary.w, color_slider_height);
+ const float font_scale = boundary.w / COLOR_PICKER_WIDTH;
+ const Vec2f label_size = vec(2.5f * font_scale, 2.5f * font_scale);
if (slider_render(
&color_picker->sliders[index],
slider_labels[index],
label_size,
COLOR_BLACK,
- vec(slider_rect.x + COLOR_SLIDER_WIDTH,
- slider_rect.y + COLOR_SLIDER_HEIGHT * 0.5f - label_size.y * (float) FONT_CHAR_HEIGHT * 0.5f)) < 0) {
+ vec(slider_rect.x + boundary.w,
+ slider_rect.y + color_slider_height * 0.5f - label_size.y * (float) FONT_CHAR_HEIGHT * 0.5f)) < 0) {
return -1;
}
}
return 0;
}
-static void color_picker_revert(void *layer, Context context)
-{
- trace_assert(layer);
- ColorPicker *color_picker = layer;
-
- log_info("color_picker_revert\n");
-
- *color_picker = create_color_picker_from_rgba(*((Color *)context.data));
-}
-
// TODO(#932): the `selected` event propagation control is cumbersome
int color_picker_event(ColorPicker *color_picker,
const SDL_Event *event,
- int *selected_out,
- UndoHistory *undo_history)
+ const Camera *camera,
+ int *selected_out)
{
trace_assert(color_picker);
trace_assert(event);
+ trace_assert(camera);
int selected = 0;
+ const Rect viewport = camera_view_port_screen(camera);
+ const Rect boundary = rect(
+ 0.0f, 0.0f,
+ viewport.w * COLOR_PICKER_WR_RATIO,
+ viewport.w * COLOR_PICKER_WR_RATIO * COLOR_PICKER_HW_RATIO);
+
+ const float color_slider_height =
+ boundary.h / (COLOR_SLIDER_N + 1.0f);
+
for (ColorPickerSlider index = 0;
!selected && index < COLOR_SLIDER_N;
++index) {
if (slider_event(
&color_picker->sliders[index],
event,
- rect(0.0f, (COLOR_SLIDER_HEIGHT + COLOR_SLIDER_PADDING_BOTTOM) * (float) (index + 1),
- COLOR_SLIDER_WIDTH, COLOR_SLIDER_HEIGHT),
+ rect(boundary.x,
+ boundary.y + color_slider_height * (float) (index + 1),
+ boundary.w, color_slider_height),
&selected) < 0) {
return -1;
}
-
- if (selected && !color_picker->sliders[index].drag) {
- if (undo_history) {
- trace_assert(sizeof(Color) <= CONTEXT_SIZE);
- Action action = {
- .layer = color_picker,
- .revert = color_picker_revert,
- };
- memcpy(action.context.data, &color_picker->color, sizeof(Color));
- undo_history_push(undo_history, action);
- }
-
- color_picker->color = color_picker_rgba(color_picker);
- }
}
if (selected_out) {