summaryrefslogtreecommitdiffstats
path: root/src/ui
diff options
context:
space:
mode:
authorTavmjong Bah <tavmjong@free.fr>2018-11-16 14:03:11 +0000
committerTavmjong Bah <tavmjong@free.fr>2018-11-16 14:03:11 +0000
commit8feb1faf32539dcb22065607b8be783c12a1a41c (patch)
tree415e47b9446ceea46bd6feab1d37d02c2e0459ba /src/ui
parentRemove Adwaita-hicolor icon themes toggles making more compat (diff)
downloadinkscape-8feb1faf32539dcb22065607b8be783c12a1a41c.tar.gz
inkscape-8feb1faf32539dcb22065607b8be783c12a1a41c.zip
Rewrite of Gimp Color Wheel in C++.
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/CMakeLists.txt2
-rw-r--r--src/ui/widget/color-wheel-selector.cpp66
-rw-r--r--src/ui/widget/color-wheel-selector.h8
-rw-r--r--src/ui/widget/ink-color-wheel.cpp675
-rw-r--r--src/ui/widget/ink-color-wheel.h86
5 files changed, 786 insertions, 51 deletions
diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt
index 47ff6884e..96658b2a1 100644
--- a/src/ui/CMakeLists.txt
+++ b/src/ui/CMakeLists.txt
@@ -170,6 +170,7 @@ set(ui_SRC
widget/frame.cpp
widget/highlight-picker.cpp
widget/imagetoggler.cpp
+ widget/ink-color-wheel.cpp
widget/ink-select-one-action.cpp
widget/ink-spinscale.cpp
widget/insertordericon.cpp
@@ -394,6 +395,7 @@ set(ui_SRC
widget/highlight-picker.h
widget/insertordericon.h
widget/imagetoggler.h
+ widget/ink-color-wheel.h
widget/ink-select-one-action.h
widget/ink-spinscale.h
widget/labelled.h
diff --git a/src/ui/widget/color-wheel-selector.cpp b/src/ui/widget/color-wheel-selector.cpp
index c0a27a552..1c2660876 100644
--- a/src/ui/widget/color-wheel-selector.cpp
+++ b/src/ui/widget/color-wheel-selector.cpp
@@ -17,7 +17,7 @@
#include "ui/dialog-events.h"
#include "ui/widget/color-scales.h"
#include "ui/widget/color-slider.h"
-#include "widgets/gimp/gimpcolorwheel.h"
+#include "ui/widget/ink-color-wheel.h"
namespace Inkscape {
namespace UI {
@@ -46,8 +46,6 @@ ColorWheelSelector::ColorWheelSelector(SelectedColor &color)
ColorWheelSelector::~ColorWheelSelector()
{
- _wheel = nullptr;
-
_color_changed_connection.disconnect();
_color_dragged_connection.disconnect();
}
@@ -57,14 +55,12 @@ void ColorWheelSelector::_initUI()
/* Create components */
gint row = 0;
- _wheel = gimp_color_wheel_new();
- gtk_widget_show(_wheel);
-
- gtk_widget_set_halign(_wheel, GTK_ALIGN_FILL);
- gtk_widget_set_valign(_wheel, GTK_ALIGN_FILL);
- gtk_widget_set_hexpand(_wheel, TRUE);
- gtk_widget_set_vexpand(_wheel, TRUE);
- gtk_grid_attach(GTK_GRID(gobj()), _wheel, 0, row, 3, 1);
+ _wheel = Gtk::manage(new Inkscape::UI::Widget::ColorWheel());
+ _wheel->set_halign(Gtk::ALIGN_FILL);
+ _wheel->set_valign(Gtk::ALIGN_FILL);
+ _wheel->set_hexpand(true);
+ _wheel->set_vexpand(true);
+ attach(*_wheel, 0, row, 3, 1);
row++;
@@ -72,15 +68,9 @@ void ColorWheelSelector::_initUI()
Gtk::Label *label = Gtk::manage(new Gtk::Label(_("_A:"), true));
label->set_halign(Gtk::ALIGN_END);
label->set_valign(Gtk::ALIGN_CENTER);
- label->show();
- #if GTK_CHECK_VERSION(3, 12, 0)
label->set_margin_start(XPAD);
label->set_margin_end(XPAD);
- #else
- label->set_margin_left(XPAD);
- label->set_margin_right(XPAD);
- #endif
label->set_margin_top(YPAD);
label->set_margin_bottom(YPAD);
label->set_halign(Gtk::ALIGN_FILL);
@@ -93,15 +83,9 @@ void ColorWheelSelector::_initUI()
/* Slider */
_slider = Gtk::manage(new Inkscape::UI::Widget::ColorSlider(_alpha_adjustment));
_slider->set_tooltip_text(_("Alpha (opacity)"));
- _slider->show();
- #if GTK_CHECK_VERSION(3, 12, 0)
_slider->set_margin_start(XPAD);
_slider->set_margin_end(XPAD);
- #else
- _slider->set_margin_left(XPAD);
- _slider->set_margin_right(XPAD);
- #endif
_slider->set_margin_top(YPAD);
_slider->set_margin_bottom(YPAD);
_slider->set_hexpand(true);
@@ -117,15 +101,9 @@ void ColorWheelSelector::_initUI()
spin_button->set_tooltip_text(_("Alpha (opacity)"));
sp_dialog_defocus_on_enter(GTK_WIDGET(spin_button->gobj()));
label->set_mnemonic_widget(*spin_button);
- spin_button->show();
- #if GTK_CHECK_VERSION(3, 12, 0)
spin_button->set_margin_start(XPAD);
spin_button->set_margin_end(XPAD);
- #else
- spin_button->set_margin_left(XPAD);
- spin_button->set_margin_right(XPAD);
- #endif
spin_button->set_margin_top(YPAD);
spin_button->set_margin_bottom(YPAD);
spin_button->set_halign(Gtk::ALIGN_CENTER);
@@ -137,8 +115,9 @@ void ColorWheelSelector::_initUI()
_slider->signal_grabbed.connect(sigc::mem_fun(*this, &ColorWheelSelector::_sliderGrabbed));
_slider->signal_released.connect(sigc::mem_fun(*this, &ColorWheelSelector::_sliderReleased));
_slider->signal_value_changed.connect(sigc::mem_fun(*this, &ColorWheelSelector::_sliderChanged));
+ _wheel->signal_color_changed().connect(sigc::mem_fun(*this, &ColorWheelSelector::_wheelChanged));
- g_signal_connect(G_OBJECT(_wheel), "changed", G_CALLBACK(_wheelChanged), this);
+ show_all();
}
void ColorWheelSelector::on_show()
@@ -192,19 +171,14 @@ void ColorWheelSelector::_sliderChanged()
_color.setAlpha(ColorScales::getScaled(_alpha_adjustment->gobj()));
}
-void ColorWheelSelector::_wheelChanged(GimpColorWheel *wheel, ColorWheelSelector *wheelSelector)
+void ColorWheelSelector::_wheelChanged()
{
- if (wheelSelector->_updating) {
+ if (_updating) {
return;
}
- gdouble h = 0;
- gdouble s = 0;
- gdouble v = 0;
- gimp_color_wheel_get_color(wheel, &h, &s, &v);
-
- float rgb[3] = { 0, 0, 0 };
- SPColor::hsv_to_rgb_floatv(rgb, h, s, v);
+ double rgb[3] = { 0, 0, 0 };
+ _wheel->get_rgb(rgb[0], rgb[1], rgb[2]);
SPColor color(rgb[0], rgb[1], rgb[2]);
@@ -212,13 +186,13 @@ void ColorWheelSelector::_wheelChanged(GimpColorWheel *wheel, ColorWheelSelector
guint32 mid = color.toRGBA32(0x7f);
guint32 end = color.toRGBA32(0xff);
- wheelSelector->_updating = true;
- wheelSelector->_slider->setColors(start, mid, end);
- wheelSelector->_color.preserveICC();
+ _updating = true;
+ _slider->setColors(start, mid, end);
+ _color.preserveICC();
- wheelSelector->_color.setHeld(gimp_color_wheel_is_adjusting(wheel));
- wheelSelector->_color.setColor(color);
- wheelSelector->_updating = false;
+ _color.setHeld(_wheel->is_adjusting());
+ _color.setColor(color);
+ _updating = false;
}
void ColorWheelSelector::_updateDisplay()
@@ -234,7 +208,7 @@ void ColorWheelSelector::_updateDisplay()
{
float hsv[3] = { 0, 0, 0 };
SPColor::rgb_to_hsv_floatv(hsv, _color.color().v.c[0], _color.color().v.c[1], _color.color().v.c[2]);
- gimp_color_wheel_set_color(GIMP_COLOR_WHEEL(_wheel), hsv[0], hsv[1], hsv[2]);
+ _wheel->set_rgb(_color.color().v.c[0], _color.color().v.c[1], _color.color().v.c[2]);
}
guint32 start = _color.color().toRGBA32(0x00);
diff --git a/src/ui/widget/color-wheel-selector.h b/src/ui/widget/color-wheel-selector.h
index a5e492f4b..59cf6b626 100644
--- a/src/ui/widget/color-wheel-selector.h
+++ b/src/ui/widget/color-wheel-selector.h
@@ -16,14 +16,12 @@
#include "ui/selected-color.h"
-typedef struct _GimpColorWheel GimpColorWheel;
-
namespace Inkscape {
namespace UI {
namespace Widget {
class ColorSlider;
-
+class ColorWheel;
class ColorWheelSelector
: public Gtk::Grid
{
@@ -43,14 +41,14 @@ protected:
void _sliderGrabbed();
void _sliderReleased();
void _sliderChanged();
- static void _wheelChanged(GimpColorWheel *wheel, ColorWheelSelector *cs);
+ void _wheelChanged();
void _updateDisplay();
SelectedColor &_color;
bool _updating;
Glib::RefPtr<Gtk::Adjustment> _alpha_adjustment;
- GtkWidget *_wheel;
+ Inkscape::UI::Widget::ColorWheel *_wheel;
Inkscape::UI::Widget::ColorSlider *_slider;
private:
diff --git a/src/ui/widget/ink-color-wheel.cpp b/src/ui/widget/ink-color-wheel.cpp
new file mode 100644
index 000000000..09688eca9
--- /dev/null
+++ b/src/ui/widget/ink-color-wheel.cpp
@@ -0,0 +1,675 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Color wheel widget. Outer ring for Hue. Inner triangle for Saturation and Value.
+ *
+ * Copyright (C) 2018 Tavmjong Bah
+ *
+ * The contents of this file may be used under the GNU General Public License Version 2 or later.
+ *
+ */
+
+#include "ink-color-wheel.h"
+
+// A point with a color value.
+class color_point {
+public:
+ color_point() : x(0), y(0), r(0), g(0), b(0) {};
+ color_point(double x, double y, double r, double g, double b) : x(x), y(y), r(r), g(g), b(b) {};
+ color_point(double x, double y, guint32 color) : x(x), y(y),
+ r(((color & 0xff0000) >> 16)/255.0),
+ g(((color & 0xff00) >> 8)/255.0),
+ b(((color & 0xff) )/255.0) {};
+ guint32 get_color() { return (int(r*255) << 16 | int(g*255) << 8 | int(b*255)); };
+ double x;
+ double y;
+ double r;
+ double g;
+ double b;
+};
+
+inline double lerp(const double& v0, const double& v1, const double& t0, const double&t1, const double& t) {
+ double s = 0;
+ if (t0 != t1) {
+ s = (t - t0)/(t1 - t0);
+ }
+ return (1.0 - s) * v0 + s * v1;
+}
+
+inline color_point lerp(const color_point& v0, const color_point& v1, const double &t0, const double &t1, const double& t) {
+ double x = lerp(v0.x, v1.x, t0, t1, t);
+ double y = lerp(v0.y, v1.y, t0, t1, t);
+ double r = lerp(v0.r, v1.r, t0, t1, t);
+ double g = lerp(v0.g, v1.g, t0, t1, t);
+ double b = lerp(v0.b, v1.b, t0, t1, t);
+ return (color_point(x, y, r, g, b));
+}
+
+inline double clamp(const double& value, const double& min, const double& max) {
+ if (value < min) return min;
+ if (value > max) return max;
+ return value;
+}
+
+// h, s, and v in range 0 to 1. Returns rgb value useful for use in Cairo.
+guint32 hsv_to_rgb(double h, double s, double v) {
+
+ if (h < 0.0 || h > 1.0 ||
+ s < 0.0 || s > 1.0 ||
+ v < 0.0 || v > 1.0) {
+ std::cerr << "ColorWheel: hsv_to_rgb: input out of bounds: (0-1)"
+ << " h: " << h << " s: " << s << " v: " << v << std::endl;
+ return 0x0;
+ }
+
+ double r = v;
+ double g = v;
+ double b = v;
+
+ if (s != 0.0) {
+ double c = s * v;
+ if (h == 1.0) h = 0.0;
+ h *= 6.0;
+
+ double f = h - (int)h;
+ double p = v * (1.0 - s);
+ double q = v * (1.0 - s * f);
+ double t = v * (1.0 - s * (1.0 - f));
+
+ switch ((int)h) {
+ case 0: r = v; g = t; b = p; break;
+ case 1: r = q; g = v; b = p; break;
+ case 2: r = p; g = v; b = t; break;
+ case 3: r = p; g = q; b = v; break;
+ case 4: r = t; g = p; b = v; break;
+ case 5: r = v; g = p; b = q; break;
+ default: g_assert_not_reached();
+ }
+ }
+ guint32 rgb = (((int)floor (r*255 + 0.5) << 16) |
+ ((int)floor (g*255 + 0.5) << 8) |
+ ((int)floor (b*255 + 0.5) ));
+ return rgb;
+}
+
+double luminance(guint32 color)
+{
+ double r(((color & 0xff0000) >> 16)/255.0);
+ double g(((color & 0xff00) >> 8)/255.0);
+ double b(((color & 0xff) )/255.0);
+ return (r * 0.2125 + g * 0.7154 + b * 0.0721);
+}
+
+namespace Inkscape {
+namespace UI {
+namespace Widget {
+
+ColorWheel::ColorWheel()
+ : _hue(0.0)
+ , _saturation(1.0)
+ , _value(1.0)
+ , _ring_width(0.2)
+ , _mode(DRAG_NONE)
+ , _focus_on_ring(true)
+{
+ set_name("ColorWheel");
+ add_events(Gdk::BUTTON_PRESS_MASK |
+ Gdk::BUTTON_RELEASE_MASK |
+ Gdk::BUTTON_MOTION_MASK |
+ Gdk::KEY_PRESS_MASK );
+ set_can_focus();
+}
+
+void
+ColorWheel::set_rgb(const double& r, const double&g, const double&b, bool override_hue)
+{
+ double Min = std::min({r, g, b});
+ double Max = std::max({r, g, b});
+ _value = Max;
+ if (Min == Max) {
+ if (override_hue) {
+ _hue = 0.0;
+ }
+ } else {
+ if (Max == r) {
+ _hue = ((g-b)/(Max-Min) )/6.0;
+ } else if (Max == g) {
+ _hue = ((b-r)/(Max-Min) + 2)/6.0;
+ } else {
+ _hue = ((r-g)/(Max-Min) + 4)/6.0;
+ }
+ if (_hue < 0.0) {
+ _hue += 1.0;
+ }
+ }
+ if (Max == 0) {
+ _saturation = 0;
+ } else {
+ _saturation = (Max - Min)/Max;
+ }
+}
+
+void
+ColorWheel::get_rgb(double& r, double& g, double& b)
+{
+ guint32 color = get_rgb();
+ r = ((color & 0xff0000) >> 16)/255.0;
+ g = ((color & 0x00ff00) >> 8)/255.0;
+ b = ((color & 0x0000ff) )/255.0;
+}
+
+guint32
+ColorWheel::get_rgb()
+{
+ return hsv_to_rgb(_hue, _saturation, _value);
+}
+
+bool
+ColorWheel::on_draw(const::Cairo::RefPtr<::Cairo::Context>& cr) {
+
+ Cairo::RefPtr<Cairo::Surface> surface = cr->get_target();
+ Cairo::RefPtr<Cairo::ImageSurface> image_surface =
+ Cairo::RefPtr<Cairo::ImageSurface>::cast_dynamic(surface);
+ if (!image_surface) {
+ std::cerr << "ColorWheel::on_draw: Failed Cast!" << std::endl;
+ return false;
+ }
+
+ Gtk::Allocation allocation = get_allocation();
+ const int width = allocation.get_width();
+ const int height = allocation.get_height();
+
+ Cairo::Format format = image_surface->get_format();
+ const int stride = Cairo::ImageSurface::format_stride_for_width(format, width);
+
+ int cx = width/2;
+ int cy = height/2;
+
+ int focus_line_width;
+ int focus_padding;
+ get_style_property("focus-line-width", focus_line_width);
+ get_style_property("focus-padding", focus_padding);
+
+ // Paint ring
+ guint32* buffer_ring = g_new (guint32, height * stride / 4);
+ double r_max = std::min( width, height)/2.0 - 2 * (focus_line_width + focus_padding);
+ double r_min = r_max * (1.0 - _ring_width);
+ double r2_max = (r_max+1) * (r_max+1); // Must expand a bit to avoid edge effects.
+ double r2_min = (r_min-1) * (r_min-1); // Must shrink a bit to avoid edge effects.
+
+ for (int i = 0; i < height; ++i) {
+ guint32* p = buffer_ring + i * width;
+ double dy = (cy - i);
+ for (int j = 0; j < width; ++j) {
+ double dx = (j - cx);
+ double r2 = dx * dx + dy * dy;
+ if (r2 < r2_min || r2 > r2_max) {
+ *p++ = 0; // Save calculation time.
+ } else {
+ double angle = atan2 (dy, dx);
+ if (angle < 0.0) {
+ angle += 2.0 * M_PI;
+ }
+ double hue = angle/(2.0 * M_PI);
+
+ *p++ = hsv_to_rgb(hue, 1.0, 1.0);
+ }
+ }
+ }
+
+ Cairo::RefPtr<::Cairo::ImageSurface> source_ring =
+ ::Cairo::ImageSurface::create((unsigned char *)buffer_ring,
+ Cairo::FORMAT_RGB24,
+ width, height, stride);
+
+ cr->set_antialias(Cairo::ANTIALIAS_SUBPIXEL);
+
+ // Paint line on ring in source (so it gets clipped by stroke).
+ double l = 0.0;
+ guint32 color_on_ring = hsv_to_rgb(_hue, 1.0, 1.0);
+ if (luminance(color_on_ring) < 0.5) l = 1.0;
+
+ Cairo::RefPtr<::Cairo::Context> cr_source_ring = ::Cairo::Context::create(source_ring);
+ cr_source_ring->set_source_rgb(l, l, l);
+
+ cr_source_ring->move_to (cx, cy);
+ cr_source_ring->line_to (cx + cos(_hue * M_PI * 2.0) * r_max+1,
+ cy - sin(_hue * M_PI * 2.0) * r_max+1);
+ cr_source_ring->stroke();
+
+ // Paint with ring surface, clipping to ring.
+ cr->save();
+ cr->set_source(source_ring, 0, 0);
+ cr->set_line_width (r_max - r_min);
+ cr->begin_new_path();
+ cr->arc(cx, cy, (r_max + r_min)/2.0, 0, 2.0 * M_PI);
+ cr->stroke();
+ cr->restore();
+
+ // Draw focus
+ if (has_focus() && _focus_on_ring) {
+ Glib::RefPtr<Gtk::StyleContext> style_context = get_style_context();
+ style_context->render_focus(cr, 0, 0, width, height);
+ }
+
+ // Paint triangle.
+ /* The triangle is painted by first finding color points on the
+ * edges of the triangle at the same y value via linearly
+ * interpolating between corner values, and then interpolating along
+ * x between the those edge points. The interpolation is in sRGB
+ * space which leads to a complicated mapping between x/y and
+ * saturation/value. This was probably done to remove the need to
+ * convert between HSV and RGB for each pixel.
+ * Black corner: v = 0, s = 1
+ * White corner: v = 1, s = 0
+ * Color corner; v = 1, s = 1
+ */
+ const int padding = 3; // Avoid edge artifacts (only needed in y).
+ double x0, y0, x1, y1, x2, y2;
+ triangle_corners(x0, y0, x1, y1, x2, y2);
+ guint32 color0 = hsv_to_rgb(_hue, 1.0, 1.0);
+ guint32 color1 = hsv_to_rgb(_hue, 1.0, 0.0);
+ guint32 color2 = hsv_to_rgb(_hue, 0.0, 1.0);
+
+ color_point p0 (x0, y0, color0);
+ color_point p1 (x1, y1, color1);
+ color_point p2 (x2, y2, color2);
+
+ // Reorder so we paint from top down.
+ if (p1.y > p2.y) {
+ std::swap(p1, p2);
+ }
+
+ if (p0.y > p2.y) {
+ std::swap(p0, p2);
+ }
+
+ if (p0.y > p1.y) {
+ std::swap(p0, p1);
+ }
+
+ guint32* buffer_triangle = g_new (guint32, height * stride / 4);
+
+ for (int y = 0; y < height; ++y) {
+ guint32* p = buffer_triangle + y * width;
+
+ if (p0.y <= y+padding && y-padding < p2.y) {
+
+ // Get values on side at position y.
+ color_point side0;
+ if (y < p1.y) {
+ side0 = lerp(p0, p1, p0.y, p1.y, y);
+ } else {
+ side0 = lerp(p1, p2, p1.y, p2.y, y);
+ }
+ color_point side1 = lerp(p0, p2, p0.y, p2.y, y);
+
+ // side0 should be on left
+ if (side0.x > side1.x) {
+ std::swap (side0, side1);
+ }
+
+ int x_start = std::max(0, int(side0.x));
+ int x_end = std::min(int(side1.x), width);
+
+ for (int x = 0; x < width; ++x) {
+ if (x < x_start) {
+ *p++ = side0.get_color();
+ } else if (x < x_end) {
+ *p++ = lerp(side0, side1, side0.x, side1.x, x).get_color();
+ } else {
+ *p++ = side1.get_color();
+ }
+ }
+ }
+ }
+
+ Cairo::RefPtr<::Cairo::ImageSurface> source_triangle =
+ ::Cairo::ImageSurface::create((unsigned char *)buffer_triangle,
+ Cairo::FORMAT_RGB24,
+ width, height, stride);
+
+ // Paint with triangle surface, clipping to triangle.
+ cr->save();
+ cr->set_source(source_triangle, 0, 0);
+ cr->move_to(p0.x, p0.y);
+ cr->line_to(p1.x, p1.y);
+ cr->line_to(p2.x, p2.y);
+ cr->close_path();
+ cr->fill();
+ cr->restore();
+
+
+ // Draw marker
+ double mx = x1 + (x2-x1) * _value + (x0-x2) * _saturation * _value;
+ double my = y1 + (y2-y1) * _value + (y0-y2) * _saturation * _value;
+
+ double a = 0.0;
+ guint32 color_at_marker = get_rgb();
+ if (luminance(color_at_marker) < 0.5) a = 1.0;
+
+ cr->set_source_rgb(a, a, a);
+ cr->begin_new_path();
+ cr->arc(mx, my, 4, 0, 2 * M_PI);
+ cr->stroke();
+
+ // Draw focus
+ if (has_focus() && !_focus_on_ring) {
+ Glib::RefPtr<Gtk::StyleContext> style_context = get_style_context();
+ style_context->render_focus(cr, mx-4, my-4, 8, 8); // This doesn't seem to work.
+ cr->set_line_width(0.5);
+ cr->set_source_rgb(1-a, 1-a, 1-a);
+ cr->begin_new_path();
+ cr->arc(mx, my, 7, 0, 2 * M_PI);
+ cr->stroke();
+ }
+
+ return true;
+}
+
+// Find triangle corners given hue and radius.
+void
+ColorWheel::triangle_corners(double &x0, double &y0,
+ double &x1, double &y1,
+ double &x2, double &y2)
+{
+ Gtk::Allocation allocation = get_allocation();
+ const int width = allocation.get_width();
+ const int height = allocation.get_height();
+
+ int cx = width/2;
+ int cy = height/2;
+
+ int focus_line_width;
+ int focus_padding;
+ get_style_property("focus-line-width", focus_line_width);
+ get_style_property("focus-padding", focus_padding);
+
+ double r_max = std::min( width, height)/2.0 - 2 * (focus_line_width + focus_padding);
+ double r_min = r_max * (1.0 - _ring_width);
+
+ double angle = _hue * 2.0 * M_PI;
+
+ x0 = cx + cos(angle) * r_min;
+ y0 = cy - sin(angle) * r_min;
+ x1 = cx + cos(angle + 2.0 * M_PI / 3.0) * r_min;
+ y1 = cy - sin(angle + 2.0 * M_PI / 3.0) * r_min;
+ x2 = cx + cos(angle + 4.0 * M_PI / 3.0) * r_min;
+ y2 = cy - sin(angle + 4.0 * M_PI / 3.0) * r_min;
+}
+
+void
+ColorWheel::set_from_xy(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 r = std::min(cx, cy) * (1 - _ring_width);
+
+ // We calculate RGB value under the cursor by rotating the cursor
+ // and triangle by the hue value and looking at position in the
+ // now right pointing triangle.
+ double angle = _hue * 2 * M_PI;
+ double Sin = sin(angle);
+ double Cos = cos(angle);
+ double xp = ((x-cx) * Cos - (y-cy) * Sin) / r;
+ double yp = ((x-cx) * Sin + (y-cy) * Cos) / r;
+
+ double xt = lerp(0.0, 1.0, -0.5, 1.0, xp);
+ xt = clamp(xt, 0, 1);
+
+ double dy = (1-xt) * cos(M_PI/6.0);
+ double yt = lerp(0.0, 1.0, -dy, dy, yp);
+ yt = clamp(yt, 0, 1);
+
+ color_point c0(0, 0, yt, yt, yt); // Grey point along base.
+ color_point c1(0, 0, hsv_to_rgb(_hue, 1, 1)); // Hue point at apex
+ color_point c = lerp(c0, c1, 0, 1, xt);
+
+ set_rgb(c.r, c.g, c.b, false); // Don't override previous hue.
+}
+
+bool
+ColorWheel::is_in_ring(const double& x, const double& y)
+{
+ Gtk::Allocation allocation = get_allocation();
+ const int width = allocation.get_width();
+ const int height = allocation.get_height();
+
+ int cx = width/2;
+ int cy = height/2;
+
+ int focus_line_width;
+ int focus_padding;
+ get_style_property("focus-line-width", focus_line_width);
+ get_style_property("focus-padding", focus_padding);
+
+ double r_max = std::min( width, height)/2.0 - 2 * (focus_line_width + focus_padding);
+ double r_min = r_max * (1.0 - _ring_width);
+ double r2_max = r_max * r_max;
+ double r2_min = r_min * r_min;
+
+ double dx = x - cx;
+ double dy = y - cy;
+ double r2 = dx * dx + dy * dy;
+
+ return (r2_min < r2 && r2 < r2_max);
+}
+
+bool
+ColorWheel::is_in_triangle(const double& x, const double& y)
+{
+ double x0, y0, x1, y1, x2, y2;
+ triangle_corners(x0, y0, x1, y1, x2, y2);
+
+ double det = (x2-x1) * (y0-y1) - (y2-y1) * (x0-x1);
+ double s = ((x -x1) * (y0-y1) - (y -y1) * (x0-x1)) / det;
+ double t = ((x2-x1) * (y -y1) - (y2-y1) * (x -x1)) / det;
+
+ return (s >= 0.0 && t >= 0.0 && s+t <= 1.0);
+}
+
+bool
+ColorWheel::on_focus(Gtk::DirectionType direction)
+{
+ // In forward direction, focus passes from no focus to ring focus to triangle focus to no focus.
+ if (!has_focus()) {
+ _focus_on_ring = (direction == Gtk::DIR_TAB_FORWARD);
+ grab_focus();
+ return true;
+ }
+
+ // Already have focus
+ bool keep_focus = false;
+
+ switch (direction) {
+ case Gtk::DIR_UP:
+ case Gtk::DIR_LEFT:
+ case Gtk::DIR_TAB_BACKWARD:
+ if (!_focus_on_ring) {
+ _focus_on_ring = true;
+ keep_focus = true;
+ }
+ break;
+
+ case Gtk::DIR_DOWN:
+ case Gtk::DIR_RIGHT:
+ case Gtk::DIR_TAB_FORWARD:
+ if (_focus_on_ring) {
+ _focus_on_ring = false;
+ keep_focus = true;
+ }
+ break;
+ }
+
+ queue_draw(); // Update focus indicators.
+
+ return keep_focus;
+}
+
+bool
+ColorWheel::on_button_press_event(GdkEventButton* event)
+{
+ // Seat is automatically grabbed.
+ double x = event->x;
+ double y = event->y;
+
+ if (is_in_ring(x, y) ) {
+ _mode = DRAG_H;
+ grab_focus();
+ _focus_on_ring = true;
+ return true;
+ }
+
+ if (is_in_triangle(x, y)) {
+ _mode = DRAG_SV;
+ grab_focus();
+ _focus_on_ring = false;
+ return true;
+ }
+
+ return false;
+}
+
+bool
+ColorWheel::on_button_release_event(GdkEventButton* event)
+{
+ _mode == DRAG_NONE;
+ 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;
+ double r = std::min(cx, cy) * (1 - _ring_width);
+
+ 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();
+ return true;
+ }
+
+ if (_mode == DRAG_SV) {
+
+ set_from_xy(x, y);
+ _signal_color_changed.emit();
+ queue_draw();
+ return true;
+ }
+
+ return false;
+}
+
+bool
+ColorWheel::on_key_press_event(GdkEventKey* key_event)
+{
+ bool consumed = false;
+
+ unsigned int key = 0;
+ gdk_keymap_translate_keyboard_state( Gdk::Display::get_default()->get_keymap(),
+ key_event->hardware_keycode,
+ (GdkModifierType)key_event->state,
+ 0, &key, nullptr, nullptr, nullptr );
+
+ double x0, y0, x1, y1, x2, y2;
+ triangle_corners(x0, y0, x1, y1, x2, y2);
+
+ // Marker position
+ double mx = x1 + (x2-x1) * _value + (x0-x2) * _saturation * _value;
+ double my = y1 + (y2-y1) * _value + (y0-y2) * _saturation * _value;
+
+
+ const double delta_hue = 2.0/360.0;
+
+ switch (key) {
+
+ case GDK_KEY_Up:
+ case GDK_KEY_KP_Up:
+ if (_focus_on_ring) {
+ _hue += delta_hue;
+ } else {
+ my -= 1.0;
+ set_from_xy(mx, my);
+ }
+ consumed = true;
+ break;
+
+ case GDK_KEY_Down:
+ case GDK_KEY_KP_Down:
+ if (_focus_on_ring) {
+ _hue -= delta_hue;
+ } else {
+ my += 1.0;
+ set_from_xy(mx, my);
+ }
+ consumed = true;
+ break;
+
+ case GDK_KEY_Left:
+ case GDK_KEY_KP_Left:
+ if (_focus_on_ring) {
+ _hue += delta_hue;
+ } else {
+ mx -= 1.0;
+ set_from_xy(mx, my);
+ }
+ consumed = true;
+ break;
+
+ case GDK_KEY_Right:
+ case GDK_KEY_KP_Right:
+ if (_focus_on_ring) {
+ _hue -= delta_hue;
+ } else {
+ mx += 1.0;
+ set_from_xy(mx, my);
+ }
+ consumed = true;
+ break;
+
+ }
+
+ if (consumed) {
+ if (_hue >= 1.0) _hue -= 1.0;
+ if (_hue < 0.0) _hue += 1.0;
+ _signal_color_changed.emit();
+ queue_draw();
+ }
+
+ return consumed;
+}
+
+sigc::signal<void>
+ColorWheel::signal_color_changed()
+{
+ return _signal_color_changed;
+}
+
+} // Namespace Inkscape
+}
+}
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/ui/widget/ink-color-wheel.h b/src/ui/widget/ink-color-wheel.h
new file mode 100644
index 000000000..c6333b702
--- /dev/null
+++ b/src/ui/widget/ink-color-wheel.h
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Color wheel widget. Outer ring for Hue. Inner triangle for Saturation and Value.
+ *
+ * Copyright (C) 2018 Tavmjong Bah
+ *
+ * The contents of this file may be used under the GNU General Public License Version 2 or later.
+ *
+ */
+
+#ifndef INK_COLORWHEEL_H
+#define INK_COLORWHEEL_H
+
+/* Rewrite of the C Gimp ColorWheel which came originally from GTK2. */
+
+#include <gtkmm.h>
+#include <iostream>
+
+namespace Inkscape {
+namespace UI {
+namespace Widget {
+
+class ColorWheel : public Gtk::DrawingArea
+{
+public:
+ ColorWheel();
+ 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;}
+
+protected:
+ bool on_draw(const::Cairo::RefPtr<::Cairo::Context>& cr) override;
+
+private:
+ void triangle_corners(double& x0, double& y0,
+ double& x1, double& y1,
+ double& x2, double& y2);
+ void set_from_xy(const double& x, const double& y);
+ bool is_in_ring( const double& x, const double& y);
+ bool is_in_triangle(const double& x, const double& y);
+
+ enum DragMode {
+ DRAG_NONE,
+ DRAG_H,
+ DRAG_SV
+ };
+
+ double _hue; // Range [0,1)
+ double _saturation;
+ double _value;
+ double _ring_width;
+ DragMode _mode;
+ bool _focus_on_ring;
+
+ // Callbacks
+ bool on_focus(Gtk::DirectionType direction) override;
+ bool on_button_press_event(GdkEventButton* event) override;
+ bool on_button_release_event(GdkEventButton* event) override;
+ bool on_motion_notify_event(GdkEventMotion* event) override;
+ bool on_key_press_event(GdkEventKey* key_event) override;
+
+ // Signals
+public:
+ sigc::signal<void> signal_color_changed();
+
+protected:
+ sigc::signal<void> _signal_color_changed;
+
+};
+
+} // Namespace Inkscape
+}
+}
+#endif // INK_COLOR_WHEEL_H
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :