From 5c6e7a308fdb8648dd9d009139836ad2b8b32829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Almeida?= Date: Sun, 18 Oct 2020 16:52:21 -0300 Subject: [PATCH 1/2] ink-color-wheel: Select color just by clicking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To select a color (in both ring and triangle), one needs to click and drag. Enable selection just by clicking as well. Signed-off-by: André Almeida --- src/ui/widget/ink-color-wheel.cpp | 48 ++++++++++++++++++++----------- src/ui/widget/ink-color-wheel.h | 3 ++ 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/ui/widget/ink-color-wheel.cpp b/src/ui/widget/ink-color-wheel.cpp index b33771e8ad..1840f619a7 100644 --- a/src/ui/widget/ink-color-wheel.cpp +++ b/src/ui/widget/ink-color-wheel.cpp @@ -572,6 +572,32 @@ ColorWheel::on_focus(Gtk::DirectionType direction) return keep_focus; } +void +ColorWheel::update_triangle_color(const double x, const double y) +{ + set_from_xy(x, y); + _signal_color_changed.emit(); + queue_draw(); +} + +void +ColorWheel::update_ring_color(const double x, const double y) +{ + Gtk::Allocation allocation = get_allocation(); + const int width = allocation.get_width(); + const int height = allocation.get_height(); + double cx = width / 2.0; + double cy = height / 2.0; + double angle = -atan2(y - cy, x - cx); + + if (angle < 0) + angle += 2.0 * M_PI; + _hue = angle / (2.0 * M_PI); + + queue_draw(); + _signal_color_changed.emit(); +} + bool ColorWheel::on_button_press_event(GdkEventButton* event) { @@ -583,6 +609,7 @@ ColorWheel::on_button_press_event(GdkEventButton* event) _mode = DRAG_H; grab_focus(); _focus_on_ring = true; + update_ring_color(x, y); return true; } @@ -590,6 +617,7 @@ ColorWheel::on_button_press_event(GdkEventButton* event) _mode = DRAG_SV; grab_focus(); _focus_on_ring = false; + update_triangle_color(x, y); return true; } @@ -603,34 +631,20 @@ ColorWheel::on_button_release_event(GdkEventButton* event) return true; } + bool ColorWheel::on_motion_notify_event(GdkEventMotion* event) { double x = event->x; double y = event->y; - Gtk::Allocation allocation = get_allocation(); - const int width = allocation.get_width(); - const int height = allocation.get_height(); - double cx = width/2.0; - double cy = height/2.0; - if (_mode == DRAG_H) { - - double angle = -atan2(y-cy, x-cx); - if (angle < 0) angle += 2.0 * M_PI; - _hue = angle / (2.0 * M_PI); - - queue_draw(); - _signal_color_changed.emit(); + update_ring_color(x, y); return true; } if (_mode == DRAG_SV) { - - set_from_xy(x, y); - _signal_color_changed.emit(); - queue_draw(); + update_triangle_color(x, y); return true; } diff --git a/src/ui/widget/ink-color-wheel.h b/src/ui/widget/ink-color-wheel.h index c6333b7024..fab1cdde6b 100644 --- a/src/ui/widget/ink-color-wheel.h +++ b/src/ui/widget/ink-color-wheel.h @@ -40,6 +40,9 @@ private: bool is_in_ring( const double& x, const double& y); bool is_in_triangle(const double& x, const double& y); + void update_triangle_color(const double x, const double y); + void update_ring_color(const double x, const double y); + enum DragMode { DRAG_NONE, DRAG_H, -- GitLab From ca7b054091a1f4ab3e5e4037b570e12e5f6408c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Almeida?= Date: Sun, 18 Oct 2020 16:57:30 -0300 Subject: [PATCH 2/2] ink-color-wheel: Clarify DRAG_* enums MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DRAG_H is for the hue ring, and DRAG_SV is for the saturation triangle. Use a Enum Class to clarify the meaning of them. Signed-off-by: André Almeida --- src/ui/widget/ink-color-wheel.cpp | 12 ++++++------ src/ui/widget/ink-color-wheel.h | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ui/widget/ink-color-wheel.cpp b/src/ui/widget/ink-color-wheel.cpp index 1840f619a7..447cf4253e 100644 --- a/src/ui/widget/ink-color-wheel.cpp +++ b/src/ui/widget/ink-color-wheel.cpp @@ -107,7 +107,7 @@ ColorWheel::ColorWheel() , _saturation(1.0) , _value(1.0) , _ring_width(0.2) - , _mode(DRAG_NONE) + , _mode(DragMode::NONE) , _focus_on_ring(true) { set_name("ColorWheel"); @@ -606,7 +606,7 @@ ColorWheel::on_button_press_event(GdkEventButton* event) double y = event->y; if (is_in_ring(x, y) ) { - _mode = DRAG_H; + _mode = DragMode::HUE; grab_focus(); _focus_on_ring = true; update_ring_color(x, y); @@ -614,7 +614,7 @@ ColorWheel::on_button_press_event(GdkEventButton* event) } if (is_in_triangle(x, y)) { - _mode = DRAG_SV; + _mode = DragMode::SATURATION_VALUE; grab_focus(); _focus_on_ring = false; update_triangle_color(x, y); @@ -627,7 +627,7 @@ ColorWheel::on_button_press_event(GdkEventButton* event) bool ColorWheel::on_button_release_event(GdkEventButton* event) { - _mode = DRAG_NONE; + _mode = DragMode::NONE; return true; } @@ -638,12 +638,12 @@ ColorWheel::on_motion_notify_event(GdkEventMotion* event) double x = event->x; double y = event->y; - if (_mode == DRAG_H) { + if (_mode == DragMode::HUE) { update_ring_color(x, y); return true; } - if (_mode == DRAG_SV) { + if (_mode == DragMode::SATURATION_VALUE) { update_triangle_color(x, y); return true; } diff --git a/src/ui/widget/ink-color-wheel.h b/src/ui/widget/ink-color-wheel.h index fab1cdde6b..353b506208 100644 --- a/src/ui/widget/ink-color-wheel.h +++ b/src/ui/widget/ink-color-wheel.h @@ -27,7 +27,7 @@ public: void set_rgb(const double& r, const double& g, const double& b, bool override_hue = true); void get_rgb(double& r, double& g, double& b); guint32 get_rgb(); - bool is_adjusting() {return _mode != DRAG_NONE;} + bool is_adjusting() {return _mode != DragMode::NONE;} protected: bool on_draw(const::Cairo::RefPtr<::Cairo::Context>& cr) override; @@ -43,10 +43,10 @@ private: void update_triangle_color(const double x, const double y); void update_ring_color(const double x, const double y); - enum DragMode { - DRAG_NONE, - DRAG_H, - DRAG_SV + enum class DragMode { + NONE, + HUE, + SATURATION_VALUE }; double _hue; // Range [0,1) -- GitLab