1 /* HSV color selector for GTK+
3 * Copyright (C) 1999 The Free Software Foundation
5 * Authors: Simon Budig <Simon.Budig@unix-ag.org> (original code)
6 * Federico Mena-Quintero <federico@gimp.org> (cleanup for GTK+)
7 * Jonathan Blandford <jrb@redhat.com> (cleanup for GTK+)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
27 * file for a list of people on the GTK+ Team. See the ChangeLog
28 * files for a list of changes. These files are distributed with
29 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34 #define GDK_DISABLE_DEPRECATION_WARNINGS
40 #include "gtkbindings.h"
41 #include "gtkmarshalers.h"
42 #include "gtktypebuiltins.h"
48 * @Short_description: A 'color wheel' widget
50 * @See_also: #GtkColorSelection, #GtkColorSelectionDialog
52 * #GtkHSV is the 'color wheel' part of a complete color selector widget.
53 * It allows to select a color by determining its HSV components in an
54 * intuitive way. Moving the selection around the outer ring changes the hue,
55 * and moving the selection point inside the inner triangle changes value and
58 * #GtkHSV has been deprecated together with #GtkColorSelection, where
63 /* Default width/height */
64 #define DEFAULT_SIZE 100
66 /* Default ring width */
67 #define DEFAULT_RING_WIDTH 10
77 /* Private part of the GtkHSV structure */
85 /* Size and ring width */
89 /* Window for capturing events */
95 guint focus_on_ring : 1;
107 static void gtk_hsv_destroy (GtkWidget *widget);
108 static void gtk_hsv_realize (GtkWidget *widget);
109 static void gtk_hsv_unrealize (GtkWidget *widget);
110 static void gtk_hsv_get_preferred_width (GtkWidget *widget,
113 static void gtk_hsv_get_preferred_height (GtkWidget *widget,
116 static void gtk_hsv_size_allocate (GtkWidget *widget,
117 GtkAllocation *allocation);
118 static gboolean gtk_hsv_button_press (GtkWidget *widget,
119 GdkEventButton *event);
120 static gboolean gtk_hsv_button_release (GtkWidget *widget,
121 GdkEventButton *event);
122 static gboolean gtk_hsv_motion (GtkWidget *widget,
123 GdkEventMotion *event);
124 static gboolean gtk_hsv_draw (GtkWidget *widget,
126 static gboolean gtk_hsv_grab_broken (GtkWidget *widget,
127 GdkEventGrabBroken *event);
128 static gboolean gtk_hsv_focus (GtkWidget *widget,
129 GtkDirectionType direction);
130 static void gtk_hsv_move (GtkHSV *hsv,
131 GtkDirectionType dir);
133 static guint hsv_signals[LAST_SIGNAL];
135 G_DEFINE_TYPE (GtkHSV, gtk_hsv, GTK_TYPE_WIDGET)
137 /* Class initialization function for the HSV color selector */
139 gtk_hsv_class_init (GtkHSVClass *class)
141 GObjectClass *gobject_class;
142 GtkWidgetClass *widget_class;
143 GtkHSVClass *hsv_class;
144 GtkBindingSet *binding_set;
146 gobject_class = (GObjectClass *) class;
147 widget_class = (GtkWidgetClass *) class;
148 hsv_class = GTK_HSV_CLASS (class);
150 widget_class->destroy = gtk_hsv_destroy;
151 widget_class->realize = gtk_hsv_realize;
152 widget_class->unrealize = gtk_hsv_unrealize;
153 widget_class->get_preferred_width = gtk_hsv_get_preferred_width;
154 widget_class->get_preferred_height = gtk_hsv_get_preferred_height;
155 widget_class->size_allocate = gtk_hsv_size_allocate;
156 widget_class->button_press_event = gtk_hsv_button_press;
157 widget_class->button_release_event = gtk_hsv_button_release;
158 widget_class->motion_notify_event = gtk_hsv_motion;
159 widget_class->draw = gtk_hsv_draw;
160 widget_class->focus = gtk_hsv_focus;
161 widget_class->grab_broken_event = gtk_hsv_grab_broken;
163 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_COLOR_CHOOSER);
165 hsv_class->move = gtk_hsv_move;
167 hsv_signals[CHANGED] =
168 g_signal_new (I_("changed"),
169 G_OBJECT_CLASS_TYPE (gobject_class),
171 G_STRUCT_OFFSET (GtkHSVClass, changed),
173 _gtk_marshal_VOID__VOID,
177 g_signal_new (I_("move"),
178 G_OBJECT_CLASS_TYPE (gobject_class),
179 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
180 G_STRUCT_OFFSET (GtkHSVClass, move),
182 _gtk_marshal_VOID__ENUM,
184 GTK_TYPE_DIRECTION_TYPE);
186 binding_set = gtk_binding_set_by_class (class);
188 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
190 G_TYPE_ENUM, GTK_DIR_UP);
191 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
193 G_TYPE_ENUM, GTK_DIR_UP);
194 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
196 G_TYPE_ENUM, GTK_DIR_DOWN);
197 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
199 G_TYPE_ENUM, GTK_DIR_DOWN);
200 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Right, 0,
202 G_TYPE_ENUM, GTK_DIR_RIGHT);
203 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Right, 0,
205 G_TYPE_ENUM, GTK_DIR_RIGHT);
206 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Left, 0,
208 G_TYPE_ENUM, GTK_DIR_LEFT);
209 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Left, 0,
211 G_TYPE_ENUM, GTK_DIR_LEFT);
213 g_type_class_add_private (gobject_class, sizeof (GtkHSVPrivate));
217 gtk_hsv_init (GtkHSV *hsv)
221 priv = G_TYPE_INSTANCE_GET_PRIVATE (hsv, GTK_TYPE_HSV, GtkHSVPrivate);
225 gtk_widget_set_has_window (GTK_WIDGET (hsv), FALSE);
226 gtk_widget_set_can_focus (GTK_WIDGET (hsv), TRUE);
232 priv->size = DEFAULT_SIZE;
233 priv->ring_width = DEFAULT_RING_WIDTH;
237 gtk_hsv_destroy (GtkWidget *widget)
239 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->destroy (widget);
243 gtk_hsv_realize (GtkWidget *widget)
245 GtkHSV *hsv = GTK_HSV (widget);
246 GtkHSVPrivate *priv = hsv->priv;
247 GtkAllocation allocation;
248 GdkWindow *parent_window;
252 gtk_widget_set_realized (widget, TRUE);
254 gtk_widget_get_allocation (widget, &allocation);
256 attr.window_type = GDK_WINDOW_CHILD;
257 attr.x = allocation.x;
258 attr.y = allocation.y;
259 attr.width = allocation.width;
260 attr.height = allocation.height;
261 attr.wclass = GDK_INPUT_ONLY;
262 attr.event_mask = gtk_widget_get_events (widget);
263 attr.event_mask |= (GDK_KEY_PRESS_MASK
264 | GDK_BUTTON_PRESS_MASK
265 | GDK_BUTTON_RELEASE_MASK
266 | GDK_POINTER_MOTION_MASK
267 | GDK_ENTER_NOTIFY_MASK
268 | GDK_LEAVE_NOTIFY_MASK);
269 attr_mask = GDK_WA_X | GDK_WA_Y;
271 parent_window = gtk_widget_get_parent_window (widget);
272 gtk_widget_set_window (widget, parent_window);
273 g_object_ref (parent_window);
275 priv->window = gdk_window_new (parent_window, &attr, attr_mask);
276 gdk_window_set_user_data (priv->window, hsv);
277 gdk_window_show (priv->window);
281 gtk_hsv_unrealize (GtkWidget *widget)
283 GtkHSV *hsv = GTK_HSV (widget);
284 GtkHSVPrivate *priv = hsv->priv;
286 gdk_window_set_user_data (priv->window, NULL);
287 gdk_window_destroy (priv->window);
290 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unrealize (widget);
294 gtk_hsv_get_preferred_width (GtkWidget *widget,
298 GtkHSV *hsv = GTK_HSV (widget);
299 GtkHSVPrivate *priv = hsv->priv;
303 gtk_widget_style_get (widget,
304 "focus-line-width", &focus_width,
305 "focus-padding", &focus_pad,
308 *minimum = priv->size + 2 * (focus_width + focus_pad);
309 *natural = priv->size + 2 * (focus_width + focus_pad);
313 gtk_hsv_get_preferred_height (GtkWidget *widget,
317 GtkHSV *hsv = GTK_HSV (widget);
318 GtkHSVPrivate *priv = hsv->priv;
322 gtk_widget_style_get (widget,
323 "focus-line-width", &focus_width,
324 "focus-padding", &focus_pad,
327 *minimum = priv->size + 2 * (focus_width + focus_pad);
328 *natural = priv->size + 2 * (focus_width + focus_pad);
332 gtk_hsv_size_allocate (GtkWidget *widget,
333 GtkAllocation *allocation)
335 GtkHSV *hsv = GTK_HSV (widget);
336 GtkHSVPrivate *priv = hsv->priv;
338 gtk_widget_set_allocation (widget, allocation);
340 if (gtk_widget_get_realized (widget))
341 gdk_window_move_resize (priv->window,
349 /* Utility functions */
351 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
353 /* Converts from HSV to RGB */
355 hsv_to_rgb (gdouble *h,
359 gdouble hue, saturation, value;
378 p = value * (1.0 - saturation);
379 q = value * (1.0 - saturation * f);
380 t = value * (1.0 - saturation * (1.0 - f));
421 g_assert_not_reached ();
426 /* Converts from RGB to HSV */
428 rgb_to_hsv (gdouble *r,
432 gdouble red, green, blue;
471 s = (max - min) / max;
482 h = (green - blue) / delta;
483 else if (green == max)
484 h = 2 + (blue - red) / delta;
485 else if (blue == max)
486 h = 4 + (red - green) / delta;
501 /* Computes the vertices of the saturation/value triangle */
503 compute_triangle (GtkHSV *hsv,
511 GtkHSVPrivate *priv = hsv->priv;
512 GtkWidget *widget = GTK_WIDGET (hsv);
515 gdouble inner, outer;
518 center_x = gtk_widget_get_allocated_width (widget) / 2.0;
519 center_y = gtk_widget_get_allocated_height (widget) / 2.0;
520 outer = priv->size / 2.0;
521 inner = outer - priv->ring_width;
522 angle = priv->h * 2.0 * G_PI;
524 *hx = floor (center_x + cos (angle) * inner + 0.5);
525 *hy = floor (center_y - sin (angle) * inner + 0.5);
526 *sx = floor (center_x + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
527 *sy = floor (center_y - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
528 *vx = floor (center_x + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
529 *vy = floor (center_y - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
532 /* Computes whether a point is inside the hue ring */
534 is_in_ring (GtkHSV *hsv,
538 GtkHSVPrivate *priv = hsv->priv;
539 GtkWidget *widget = GTK_WIDGET (hsv);
540 gdouble dx, dy, dist;
543 gdouble inner, outer;
545 center_x = gtk_widget_get_allocated_width (widget) / 2.0;
546 center_y = gtk_widget_get_allocated_height (widget) / 2.0;
547 outer = priv->size / 2.0;
548 inner = outer - priv->ring_width;
552 dist = dx * dx + dy * dy;
554 return (dist >= inner * inner && dist <= outer * outer);
557 /* Computes a saturation/value pair based on the mouse coordinates */
559 compute_sv (GtkHSV *hsv,
565 GtkWidget *widget = GTK_WIDGET (hsv);
566 int ihx, ihy, isx, isy, ivx, ivy;
567 double hx, hy, sx, sy, vx, vy;
571 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
572 center_x = gtk_widget_get_allocated_width (widget) / 2.0;
573 center_y = gtk_widget_get_allocated_height (widget) / 2.0;
583 if (vx * (x - sx) + vy * (y - sy) < 0.0)
586 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
587 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
594 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
597 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
598 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
605 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
608 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
609 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
618 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
619 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
631 if (fabs (hy - vy) < fabs (hx - vx))
632 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
634 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
644 /* Computes whether a point is inside the saturation/value triangle */
646 is_in_triangle (GtkHSV *hsv,
650 int hx, hy, sx, sy, vx, vy;
653 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
655 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
657 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
658 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
660 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
663 /* Computes a value based on the mouse coordinates */
665 compute_v (GtkHSV *hsv,
669 GtkWidget *widget = GTK_WIDGET (hsv);
675 center_x = gtk_widget_get_allocated_width (widget) / 2.0;
676 center_y = gtk_widget_get_allocated_height (widget) / 2.0;
680 angle = atan2 (dy, dx);
684 return angle / (2.0 * G_PI);
690 set_cross_grab (GtkHSV *hsv,
694 GtkHSVPrivate *priv = hsv->priv;
697 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
699 gdk_device_grab (device,
703 GDK_POINTER_MOTION_MASK
704 | GDK_POINTER_MOTION_HINT_MASK
705 | GDK_BUTTON_RELEASE_MASK,
708 g_object_unref (cursor);
712 gtk_hsv_grab_broken (GtkWidget *widget,
713 GdkEventGrabBroken *event)
715 GtkHSV *hsv = GTK_HSV (widget);
716 GtkHSVPrivate *priv = hsv->priv;
718 priv->mode = DRAG_NONE;
724 gtk_hsv_button_press (GtkWidget *widget,
725 GdkEventButton *event)
727 GtkHSV *hsv = GTK_HSV (widget);
728 GtkHSVPrivate *priv = hsv->priv;
731 if (priv->mode != DRAG_NONE || event->button != GDK_BUTTON_PRIMARY)
737 if (is_in_ring (hsv, x, y))
740 set_cross_grab (hsv, gdk_event_get_device ((GdkEvent *) event), event->time);
742 gtk_hsv_set_color (hsv,
743 compute_v (hsv, x, y),
747 gtk_widget_grab_focus (widget);
748 priv->focus_on_ring = TRUE;
753 if (is_in_triangle (hsv, x, y))
757 priv->mode = DRAG_SV;
758 set_cross_grab (hsv, gdk_event_get_device ((GdkEvent *) event), event->time);
760 compute_sv (hsv, x, y, &s, &v);
761 gtk_hsv_set_color (hsv, priv->h, s, v);
763 gtk_widget_grab_focus (widget);
764 priv->focus_on_ring = FALSE;
773 gtk_hsv_button_release (GtkWidget *widget,
774 GdkEventButton *event)
776 GtkHSV *hsv = GTK_HSV (widget);
777 GtkHSVPrivate *priv = hsv->priv;
781 if (priv->mode == DRAG_NONE || event->button != GDK_BUTTON_PRIMARY)
784 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
785 * can see that this is the final color state.
788 priv->mode = DRAG_NONE;
795 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
797 else if (mode == DRAG_SV)
801 compute_sv (hsv, x, y, &s, &v);
802 gtk_hsv_set_color (hsv, priv->h, s, v);
806 g_assert_not_reached ();
809 gdk_device_ungrab (gdk_event_get_device ((GdkEvent *) event), event->time);
815 gtk_hsv_motion (GtkWidget *widget,
816 GdkEventMotion *event)
818 GtkHSV *hsv = GTK_HSV (widget);
819 GtkHSVPrivate *priv = hsv->priv;
822 if (priv->mode == DRAG_NONE)
825 gdk_event_request_motions (event);
829 if (priv->mode == DRAG_H)
831 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
834 else if (priv->mode == DRAG_SV)
838 compute_sv (hsv, x, y, &s, &v);
839 gtk_hsv_set_color (hsv, priv->h, s, v);
843 g_assert_not_reached ();
851 /* Paints the hue ring */
853 paint_ring (GtkHSV *hsv,
856 GtkHSVPrivate *priv = hsv->priv;
857 GtkWidget *widget = GTK_WIDGET (hsv);
858 int xx, yy, width, height;
859 gdouble dx, dy, dist;
862 gdouble inner, outer;
867 cairo_surface_t *source;
871 width = gtk_widget_get_allocated_width (widget);
872 height = gtk_widget_get_allocated_height (widget);
874 center_x = width / 2.0;
875 center_y = height / 2.0;
877 outer = priv->size / 2.0;
878 inner = outer - priv->ring_width;
880 /* Create an image initialized with the ring colors */
882 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
883 buf = g_new (guint32, height * stride / 4);
885 for (yy = 0; yy < height; yy++)
887 p = buf + yy * width;
889 dy = -(yy - center_y);
891 for (xx = 0; xx < width; xx++)
895 dist = dx * dx + dy * dy;
896 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
902 angle = atan2 (dy, dx);
906 hue = angle / (2.0 * G_PI);
911 hsv_to_rgb (&r, &g, &b);
913 *p++ = (((int)floor (r * 255 + 0.5) << 16) |
914 ((int)floor (g * 255 + 0.5) << 8) |
915 (int)floor (b * 255 + 0.5));
919 source = cairo_image_surface_create_for_data ((unsigned char *)buf,
921 width, height, stride);
923 /* Now draw the value marker onto the source image, so that it
924 * will get properly clipped at the edges of the ring
926 source_cr = cairo_create (source);
931 hsv_to_rgb (&r, &g, &b);
933 if (INTENSITY (r, g, b) > 0.5)
934 cairo_set_source_rgb (source_cr, 0., 0., 0.);
936 cairo_set_source_rgb (source_cr, 1., 1., 1.);
938 cairo_move_to (source_cr, center_x, center_y);
939 cairo_line_to (source_cr,
940 center_x + cos (priv->h * 2.0 * G_PI) * priv->size / 2,
941 center_y - sin (priv->h * 2.0 * G_PI) * priv->size / 2);
942 cairo_stroke (source_cr);
943 cairo_destroy (source_cr);
945 /* Draw the ring using the source image */
949 cairo_set_source_surface (cr, source, 0, 0);
950 cairo_surface_destroy (source);
952 cairo_set_line_width (cr, priv->ring_width);
956 priv->size / 2. - priv->ring_width / 2.,
965 /* Converts an HSV triplet to an integer RGB triplet */
967 get_color (gdouble h,
974 hsv_to_rgb (&h, &s, &v);
976 *r = floor (h * 255 + 0.5);
977 *g = floor (s * 255 + 0.5);
978 *b = floor (v * 255 + 0.5);
981 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
983 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
984 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
987 /* Number of pixels we extend out from the edges when creating
988 * color source to avoid artifacts
992 /* Paints the HSV triangle */
994 paint_triangle (GtkHSV *hsv,
998 GtkHSVPrivate *priv = hsv->priv;
999 GtkWidget *widget = GTK_WIDGET (hsv);
1000 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1001 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1002 gint x2, y2, r2, g2, b2; /* Second vertex */
1003 gint x3, y3, r3, g3, b3; /* Third vertex */
1005 guint32 *buf, *p, c;
1006 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1008 gint x_interp, y_interp;
1009 gint x_start, x_end;
1010 cairo_surface_t *source;
1014 GtkStyleContext *context;
1017 width = gtk_widget_get_allocated_width (widget);
1018 height = gtk_widget_get_allocated_height (widget);
1019 /* Compute triangle's vertices */
1021 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1025 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1029 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1033 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1062 /* Shade the triangle */
1064 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
1065 buf = g_new (guint32, height * stride / 4);
1067 for (yy = 0; yy < height; yy++)
1069 p = buf + yy * width;
1071 if (yy >= y1 - PAD && yy < y3 + PAD) {
1072 y_interp = CLAMP (yy, y1, y3);
1076 xl = LERP (x1, x2, y1, y2, y_interp);
1078 rl = LERP (r1, r2, y1, y2, y_interp);
1079 gl = LERP (g1, g2, y1, y2, y_interp);
1080 bl = LERP (b1, b2, y1, y2, y_interp);
1084 xl = LERP (x2, x3, y2, y3, y_interp);
1086 rl = LERP (r2, r3, y2, y3, y_interp);
1087 gl = LERP (g2, g3, y2, y3, y_interp);
1088 bl = LERP (b2, b3, y2, y3, y_interp);
1091 xr = LERP (x1, x3, y1, y3, y_interp);
1093 rr = LERP (r1, r3, y1, y3, y_interp);
1094 gr = LERP (g1, g3, y1, y3, y_interp);
1095 br = LERP (b1, b3, y1, y3, y_interp);
1105 x_start = MAX (xl - PAD, 0);
1106 x_end = MIN (xr + PAD, width);
1107 x_start = MIN (x_start, x_end);
1109 c = (rl << 16) | (gl << 8) | bl;
1111 for (xx = 0; xx < x_start; xx++)
1114 for (; xx < x_end; xx++)
1116 x_interp = CLAMP (xx, xl, xr);
1118 *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
1119 (LERP (gl, gr, xl, xr, x_interp) << 8) |
1120 LERP (bl, br, xl, xr, x_interp));
1123 c = (rr << 16) | (gr << 8) | br;
1125 for (; xx < width; xx++)
1130 source = cairo_image_surface_create_for_data ((unsigned char *)buf,
1132 width, height, stride);
1134 /* Draw a triangle with the image as a source */
1136 cairo_set_source_surface (cr, source, 0, 0);
1137 cairo_surface_destroy (source);
1139 cairo_move_to (cr, x1, y1);
1140 cairo_line_to (cr, x2, y2);
1141 cairo_line_to (cr, x3, y3);
1142 cairo_close_path (cr);
1147 /* Draw value marker */
1149 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1150 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1155 hsv_to_rgb (&r, &g, &b);
1157 context = gtk_widget_get_style_context (widget);
1159 gtk_style_context_save (context);
1161 if (INTENSITY (r, g, b) > 0.5)
1163 gtk_style_context_add_class (context, "light-area-focus");
1164 cairo_set_source_rgb (cr, 0., 0., 0.);
1168 gtk_style_context_add_class (context, "dark-area-focus");
1169 cairo_set_source_rgb (cr, 1., 1., 1.);
1173 #define FOCUS_RADIUS 6
1175 cairo_new_path (cr);
1176 cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
1179 /* Draw focus outline */
1181 if (draw_focus && !priv->focus_on_ring)
1186 gtk_widget_style_get (widget,
1187 "focus-line-width", &focus_width,
1188 "focus-padding", &focus_pad,
1191 gtk_render_focus (context, cr,
1192 xx - FOCUS_RADIUS - focus_width - focus_pad,
1193 yy - FOCUS_RADIUS - focus_width - focus_pad,
1194 2 * (FOCUS_RADIUS + focus_width + focus_pad),
1195 2 * (FOCUS_RADIUS + focus_width + focus_pad));
1198 gtk_style_context_restore (context);
1201 /* Paints the contents of the HSV color selector */
1203 gtk_hsv_draw (GtkWidget *widget,
1206 GtkHSV *hsv = GTK_HSV (widget);
1207 GtkHSVPrivate *priv = hsv->priv;
1208 gboolean draw_focus;
1210 draw_focus = gtk_widget_has_visible_focus (widget);
1212 paint_ring (hsv, cr);
1213 paint_triangle (hsv, cr, draw_focus);
1216 if (draw_focus && priv->focus_on_ring)
1218 GtkStyleContext *context;
1220 context = gtk_widget_get_style_context (widget);
1222 gtk_render_focus (context, cr, 0, 0,
1223 gtk_widget_get_allocated_width (widget),
1224 gtk_widget_get_allocated_height (widget));
1231 gtk_hsv_focus (GtkWidget *widget,
1232 GtkDirectionType dir)
1234 GtkHSV *hsv = GTK_HSV (widget);
1235 GtkHSVPrivate *priv = hsv->priv;
1237 if (!gtk_widget_has_focus (widget))
1239 if (dir == GTK_DIR_TAB_BACKWARD)
1240 priv->focus_on_ring = FALSE;
1242 priv->focus_on_ring = TRUE;
1244 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1251 if (priv->focus_on_ring)
1254 priv->focus_on_ring = TRUE;
1258 if (priv->focus_on_ring)
1259 priv->focus_on_ring = FALSE;
1265 case GTK_DIR_TAB_BACKWARD:
1266 if (priv->focus_on_ring)
1269 priv->focus_on_ring = TRUE;
1273 case GTK_DIR_TAB_FORWARD:
1274 if (priv->focus_on_ring)
1275 priv->focus_on_ring = FALSE;
1281 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1289 * Creates a new HSV color selector.
1291 * Return value: A newly-created HSV color selector.
1298 return g_object_new (GTK_TYPE_HSV, NULL);
1302 * gtk_hsv_set_color:
1303 * @hsv: An HSV color selector
1308 * Sets the current color in an HSV color selector.
1309 * Color component values must be in the [0.0, 1.0] range.
1314 gtk_hsv_set_color (GtkHSV *hsv,
1319 GtkHSVPrivate *priv;
1321 g_return_if_fail (GTK_IS_HSV (hsv));
1322 g_return_if_fail (h >= 0.0 && h <= 1.0);
1323 g_return_if_fail (s >= 0.0 && s <= 1.0);
1324 g_return_if_fail (v >= 0.0 && v <= 1.0);
1332 g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1334 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1338 * gtk_hsv_get_color:
1339 * @hsv: An HSV color selector
1340 * @h: (out): Return value for the hue
1341 * @s: (out): Return value for the saturation
1342 * @v: (out): Return value for the value
1344 * Queries the current color in an HSV color selector.
1345 * Returned values will be in the [0.0, 1.0] range.
1350 gtk_hsv_get_color (GtkHSV *hsv,
1355 GtkHSVPrivate *priv;
1357 g_return_if_fail (GTK_IS_HSV (hsv));
1372 * gtk_hsv_set_metrics:
1373 * @hsv: An HSV color selector
1374 * @size: Diameter for the hue ring
1375 * @ring_width: Width of the hue ring
1377 * Sets the size and ring width of an HSV color selector.
1382 gtk_hsv_set_metrics (GtkHSV *hsv,
1386 GtkHSVPrivate *priv;
1389 g_return_if_fail (GTK_IS_HSV (hsv));
1390 g_return_if_fail (size > 0);
1391 g_return_if_fail (ring_width > 0);
1392 g_return_if_fail (2 * ring_width + 1 <= size);
1396 same_size = (priv->size == size);
1399 priv->ring_width = ring_width;
1402 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1404 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1408 * gtk_hsv_get_metrics:
1409 * @hsv: An HSV color selector
1410 * @size: (out): Return value for the diameter of the hue ring
1411 * @ring_width: (out): Return value for the width of the hue ring
1413 * Queries the size and ring width of an HSV color selector.
1418 gtk_hsv_get_metrics (GtkHSV *hsv,
1422 GtkHSVPrivate *priv;
1424 g_return_if_fail (GTK_IS_HSV (hsv));
1432 *ring_width = priv->ring_width;
1436 * gtk_hsv_is_adjusting:
1439 * An HSV color selector can be said to be adjusting if multiple rapid
1440 * changes are being made to its value, for example, when the user is
1441 * adjusting the value with the mouse. This function queries whether
1442 * the HSV color selector is being adjusted or not.
1444 * Return value: %TRUE if clients can ignore changes to the color value,
1445 * since they may be transitory, or %FALSE if they should consider
1446 * the color value status to be final.
1451 gtk_hsv_is_adjusting (GtkHSV *hsv)
1453 GtkHSVPrivate *priv;
1455 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1459 return priv->mode != DRAG_NONE;
1463 gtk_hsv_move (GtkHSV *hsv,
1464 GtkDirectionType dir)
1466 GtkHSVPrivate *priv = hsv->priv;
1467 gdouble hue, sat, val;
1468 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1469 gint x, y; /* position in triangle */
1475 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1477 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1478 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1480 #define HUE_DELTA 0.002
1484 if (priv->focus_on_ring)
1489 compute_sv (hsv, x, y, &sat, &val);
1494 if (priv->focus_on_ring)
1499 compute_sv (hsv, x, y, &sat, &val);
1504 if (priv->focus_on_ring)
1509 compute_sv (hsv, x, y, &sat, &val);
1514 if (priv->focus_on_ring)
1520 compute_sv (hsv, x, y, &sat, &val);
1525 /* we don't care about the tab directions */
1535 gtk_hsv_set_color (hsv, hue, sat, val);