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, see <http://www.gnu.org/licenses/>.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #define GDK_DISABLE_DEPRECATION_WARNINGS
38 #include "gtkbindings.h"
39 #include "gtkmarshalers.h"
40 #include "gtkstylecontext.h"
41 #include "gtktypebuiltins.h"
47 * @Short_description: A 'color wheel' widget
49 * @See_also: #GtkColorSelection, #GtkColorSelectionDialog
51 * #GtkHSV is the 'color wheel' part of a complete color selector widget.
52 * It allows to select a color by determining its HSV components in an
53 * intuitive way. Moving the selection around the outer ring changes the hue,
54 * and moving the selection point inside the inner triangle changes value and
57 * #GtkHSV has been deprecated together with #GtkColorSelection, where
62 /* Default width/height */
63 #define DEFAULT_SIZE 100
65 /* Default ring width */
66 #define DEFAULT_RING_WIDTH 10
76 /* Private part of the GtkHSV structure */
84 /* Size and ring width */
88 /* Window for capturing events */
94 guint focus_on_ring : 1;
106 static void gtk_hsv_destroy (GtkWidget *widget);
107 static void gtk_hsv_realize (GtkWidget *widget);
108 static void gtk_hsv_unrealize (GtkWidget *widget);
109 static void gtk_hsv_get_preferred_width (GtkWidget *widget,
112 static void gtk_hsv_get_preferred_height (GtkWidget *widget,
115 static void gtk_hsv_size_allocate (GtkWidget *widget,
116 GtkAllocation *allocation);
117 static gboolean gtk_hsv_button_press (GtkWidget *widget,
118 GdkEventButton *event);
119 static gboolean gtk_hsv_button_release (GtkWidget *widget,
120 GdkEventButton *event);
121 static gboolean gtk_hsv_motion (GtkWidget *widget,
122 GdkEventMotion *event);
123 static gboolean gtk_hsv_draw (GtkWidget *widget,
125 static gboolean gtk_hsv_grab_broken (GtkWidget *widget,
126 GdkEventGrabBroken *event);
127 static gboolean gtk_hsv_focus (GtkWidget *widget,
128 GtkDirectionType direction);
129 static void gtk_hsv_move (GtkHSV *hsv,
130 GtkDirectionType dir);
132 static guint hsv_signals[LAST_SIGNAL];
134 G_DEFINE_TYPE (GtkHSV, gtk_hsv, GTK_TYPE_WIDGET)
136 /* Class initialization function for the HSV color selector */
138 gtk_hsv_class_init (GtkHSVClass *class)
140 GObjectClass *gobject_class;
141 GtkWidgetClass *widget_class;
142 GtkHSVClass *hsv_class;
143 GtkBindingSet *binding_set;
145 gobject_class = (GObjectClass *) class;
146 widget_class = (GtkWidgetClass *) class;
147 hsv_class = GTK_HSV_CLASS (class);
149 widget_class->destroy = gtk_hsv_destroy;
150 widget_class->realize = gtk_hsv_realize;
151 widget_class->unrealize = gtk_hsv_unrealize;
152 widget_class->get_preferred_width = gtk_hsv_get_preferred_width;
153 widget_class->get_preferred_height = gtk_hsv_get_preferred_height;
154 widget_class->size_allocate = gtk_hsv_size_allocate;
155 widget_class->button_press_event = gtk_hsv_button_press;
156 widget_class->button_release_event = gtk_hsv_button_release;
157 widget_class->motion_notify_event = gtk_hsv_motion;
158 widget_class->draw = gtk_hsv_draw;
159 widget_class->focus = gtk_hsv_focus;
160 widget_class->grab_broken_event = gtk_hsv_grab_broken;
162 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_COLOR_CHOOSER);
164 hsv_class->move = gtk_hsv_move;
166 hsv_signals[CHANGED] =
167 g_signal_new (I_("changed"),
168 G_OBJECT_CLASS_TYPE (gobject_class),
170 G_STRUCT_OFFSET (GtkHSVClass, changed),
172 _gtk_marshal_VOID__VOID,
176 g_signal_new (I_("move"),
177 G_OBJECT_CLASS_TYPE (gobject_class),
178 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
179 G_STRUCT_OFFSET (GtkHSVClass, move),
181 _gtk_marshal_VOID__ENUM,
183 GTK_TYPE_DIRECTION_TYPE);
185 binding_set = gtk_binding_set_by_class (class);
187 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
189 G_TYPE_ENUM, GTK_DIR_UP);
190 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
192 G_TYPE_ENUM, GTK_DIR_UP);
193 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
195 G_TYPE_ENUM, GTK_DIR_DOWN);
196 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
198 G_TYPE_ENUM, GTK_DIR_DOWN);
199 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Right, 0,
201 G_TYPE_ENUM, GTK_DIR_RIGHT);
202 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Right, 0,
204 G_TYPE_ENUM, GTK_DIR_RIGHT);
205 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Left, 0,
207 G_TYPE_ENUM, GTK_DIR_LEFT);
208 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Left, 0,
210 G_TYPE_ENUM, GTK_DIR_LEFT);
212 g_type_class_add_private (gobject_class, sizeof (GtkHSVPrivate));
216 gtk_hsv_init (GtkHSV *hsv)
220 priv = G_TYPE_INSTANCE_GET_PRIVATE (hsv, GTK_TYPE_HSV, GtkHSVPrivate);
224 gtk_widget_set_has_window (GTK_WIDGET (hsv), FALSE);
225 gtk_widget_set_can_focus (GTK_WIDGET (hsv), TRUE);
231 priv->size = DEFAULT_SIZE;
232 priv->ring_width = DEFAULT_RING_WIDTH;
236 gtk_hsv_destroy (GtkWidget *widget)
238 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->destroy (widget);
242 gtk_hsv_realize (GtkWidget *widget)
244 GtkHSV *hsv = GTK_HSV (widget);
245 GtkHSVPrivate *priv = hsv->priv;
246 GtkAllocation allocation;
247 GdkWindow *parent_window;
251 gtk_widget_set_realized (widget, TRUE);
253 gtk_widget_get_allocation (widget, &allocation);
255 attr.window_type = GDK_WINDOW_CHILD;
256 attr.x = allocation.x;
257 attr.y = allocation.y;
258 attr.width = allocation.width;
259 attr.height = allocation.height;
260 attr.wclass = GDK_INPUT_ONLY;
261 attr.event_mask = gtk_widget_get_events (widget);
262 attr.event_mask |= (GDK_KEY_PRESS_MASK
263 | GDK_BUTTON_PRESS_MASK
264 | GDK_BUTTON_RELEASE_MASK
265 | GDK_POINTER_MOTION_MASK
266 | GDK_ENTER_NOTIFY_MASK
267 | GDK_LEAVE_NOTIFY_MASK);
268 attr_mask = GDK_WA_X | GDK_WA_Y;
270 parent_window = gtk_widget_get_parent_window (widget);
271 gtk_widget_set_window (widget, parent_window);
272 g_object_ref (parent_window);
274 priv->window = gdk_window_new (parent_window, &attr, attr_mask);
275 gdk_window_set_user_data (priv->window, hsv);
276 gdk_window_show (priv->window);
280 gtk_hsv_unrealize (GtkWidget *widget)
282 GtkHSV *hsv = GTK_HSV (widget);
283 GtkHSVPrivate *priv = hsv->priv;
285 gdk_window_set_user_data (priv->window, NULL);
286 gdk_window_destroy (priv->window);
289 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unrealize (widget);
293 gtk_hsv_get_preferred_width (GtkWidget *widget,
297 GtkHSV *hsv = GTK_HSV (widget);
298 GtkHSVPrivate *priv = hsv->priv;
302 gtk_widget_style_get (widget,
303 "focus-line-width", &focus_width,
304 "focus-padding", &focus_pad,
307 *minimum = priv->size + 2 * (focus_width + focus_pad);
308 *natural = priv->size + 2 * (focus_width + focus_pad);
312 gtk_hsv_get_preferred_height (GtkWidget *widget,
316 GtkHSV *hsv = GTK_HSV (widget);
317 GtkHSVPrivate *priv = hsv->priv;
321 gtk_widget_style_get (widget,
322 "focus-line-width", &focus_width,
323 "focus-padding", &focus_pad,
326 *minimum = priv->size + 2 * (focus_width + focus_pad);
327 *natural = priv->size + 2 * (focus_width + focus_pad);
331 gtk_hsv_size_allocate (GtkWidget *widget,
332 GtkAllocation *allocation)
334 GtkHSV *hsv = GTK_HSV (widget);
335 GtkHSVPrivate *priv = hsv->priv;
337 gtk_widget_set_allocation (widget, allocation);
339 if (gtk_widget_get_realized (widget))
340 gdk_window_move_resize (priv->window,
348 /* Utility functions */
350 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
352 /* Converts from HSV to RGB */
354 hsv_to_rgb (gdouble *h,
358 gdouble hue, saturation, value;
377 p = value * (1.0 - saturation);
378 q = value * (1.0 - saturation * f);
379 t = value * (1.0 - saturation * (1.0 - f));
420 g_assert_not_reached ();
425 /* Computes the vertices of the saturation/value triangle */
427 compute_triangle (GtkHSV *hsv,
435 GtkHSVPrivate *priv = hsv->priv;
436 GtkWidget *widget = GTK_WIDGET (hsv);
439 gdouble inner, outer;
442 center_x = gtk_widget_get_allocated_width (widget) / 2.0;
443 center_y = gtk_widget_get_allocated_height (widget) / 2.0;
444 outer = priv->size / 2.0;
445 inner = outer - priv->ring_width;
446 angle = priv->h * 2.0 * G_PI;
448 *hx = floor (center_x + cos (angle) * inner + 0.5);
449 *hy = floor (center_y - sin (angle) * inner + 0.5);
450 *sx = floor (center_x + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
451 *sy = floor (center_y - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
452 *vx = floor (center_x + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
453 *vy = floor (center_y - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
456 /* Computes whether a point is inside the hue ring */
458 is_in_ring (GtkHSV *hsv,
462 GtkHSVPrivate *priv = hsv->priv;
463 GtkWidget *widget = GTK_WIDGET (hsv);
464 gdouble dx, dy, dist;
467 gdouble inner, outer;
469 center_x = gtk_widget_get_allocated_width (widget) / 2.0;
470 center_y = gtk_widget_get_allocated_height (widget) / 2.0;
471 outer = priv->size / 2.0;
472 inner = outer - priv->ring_width;
476 dist = dx * dx + dy * dy;
478 return (dist >= inner * inner && dist <= outer * outer);
481 /* Computes a saturation/value pair based on the mouse coordinates */
483 compute_sv (GtkHSV *hsv,
489 GtkWidget *widget = GTK_WIDGET (hsv);
490 int ihx, ihy, isx, isy, ivx, ivy;
491 double hx, hy, sx, sy, vx, vy;
495 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
496 center_x = gtk_widget_get_allocated_width (widget) / 2.0;
497 center_y = gtk_widget_get_allocated_height (widget) / 2.0;
507 if (vx * (x - sx) + vy * (y - sy) < 0.0)
510 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
511 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
518 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
521 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
522 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
529 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
532 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
533 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
542 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
543 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
555 if (fabs (hy - vy) < fabs (hx - vx))
556 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
558 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
568 /* Computes whether a point is inside the saturation/value triangle */
570 is_in_triangle (GtkHSV *hsv,
574 int hx, hy, sx, sy, vx, vy;
577 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
579 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
581 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
582 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
584 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
587 /* Computes a value based on the mouse coordinates */
589 compute_v (GtkHSV *hsv,
593 GtkWidget *widget = GTK_WIDGET (hsv);
599 center_x = gtk_widget_get_allocated_width (widget) / 2.0;
600 center_y = gtk_widget_get_allocated_height (widget) / 2.0;
604 angle = atan2 (dy, dx);
608 return angle / (2.0 * G_PI);
614 set_cross_grab (GtkHSV *hsv,
618 GtkHSVPrivate *priv = hsv->priv;
621 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
623 gdk_device_grab (device,
627 GDK_POINTER_MOTION_MASK
628 | GDK_POINTER_MOTION_HINT_MASK
629 | GDK_BUTTON_RELEASE_MASK,
632 g_object_unref (cursor);
636 gtk_hsv_grab_broken (GtkWidget *widget,
637 GdkEventGrabBroken *event)
639 GtkHSV *hsv = GTK_HSV (widget);
640 GtkHSVPrivate *priv = hsv->priv;
642 priv->mode = DRAG_NONE;
648 gtk_hsv_button_press (GtkWidget *widget,
649 GdkEventButton *event)
651 GtkHSV *hsv = GTK_HSV (widget);
652 GtkHSVPrivate *priv = hsv->priv;
655 if (priv->mode != DRAG_NONE || event->button != GDK_BUTTON_PRIMARY)
661 if (is_in_ring (hsv, x, y))
664 set_cross_grab (hsv, gdk_event_get_device ((GdkEvent *) event), event->time);
666 gtk_hsv_set_color (hsv,
667 compute_v (hsv, x, y),
671 gtk_widget_grab_focus (widget);
672 priv->focus_on_ring = TRUE;
677 if (is_in_triangle (hsv, x, y))
681 priv->mode = DRAG_SV;
682 set_cross_grab (hsv, gdk_event_get_device ((GdkEvent *) event), event->time);
684 compute_sv (hsv, x, y, &s, &v);
685 gtk_hsv_set_color (hsv, priv->h, s, v);
687 gtk_widget_grab_focus (widget);
688 priv->focus_on_ring = FALSE;
697 gtk_hsv_button_release (GtkWidget *widget,
698 GdkEventButton *event)
700 GtkHSV *hsv = GTK_HSV (widget);
701 GtkHSVPrivate *priv = hsv->priv;
705 if (priv->mode == DRAG_NONE || event->button != GDK_BUTTON_PRIMARY)
708 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
709 * can see that this is the final color state.
712 priv->mode = DRAG_NONE;
719 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
721 else if (mode == DRAG_SV)
725 compute_sv (hsv, x, y, &s, &v);
726 gtk_hsv_set_color (hsv, priv->h, s, v);
730 g_assert_not_reached ();
733 gdk_device_ungrab (gdk_event_get_device ((GdkEvent *) event), event->time);
739 gtk_hsv_motion (GtkWidget *widget,
740 GdkEventMotion *event)
742 GtkHSV *hsv = GTK_HSV (widget);
743 GtkHSVPrivate *priv = hsv->priv;
746 if (priv->mode == DRAG_NONE)
749 gdk_event_request_motions (event);
753 if (priv->mode == DRAG_H)
755 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
758 else if (priv->mode == DRAG_SV)
762 compute_sv (hsv, x, y, &s, &v);
763 gtk_hsv_set_color (hsv, priv->h, s, v);
767 g_assert_not_reached ();
775 /* Paints the hue ring */
777 paint_ring (GtkHSV *hsv,
780 GtkHSVPrivate *priv = hsv->priv;
781 GtkWidget *widget = GTK_WIDGET (hsv);
782 int xx, yy, width, height;
783 gdouble dx, dy, dist;
786 gdouble inner, outer;
791 cairo_surface_t *source;
795 width = gtk_widget_get_allocated_width (widget);
796 height = gtk_widget_get_allocated_height (widget);
798 center_x = width / 2.0;
799 center_y = height / 2.0;
801 outer = priv->size / 2.0;
802 inner = outer - priv->ring_width;
804 /* Create an image initialized with the ring colors */
806 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
807 buf = g_new (guint32, height * stride / 4);
809 for (yy = 0; yy < height; yy++)
811 p = buf + yy * width;
813 dy = -(yy - center_y);
815 for (xx = 0; xx < width; xx++)
819 dist = dx * dx + dy * dy;
820 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
826 angle = atan2 (dy, dx);
830 hue = angle / (2.0 * G_PI);
835 hsv_to_rgb (&r, &g, &b);
837 *p++ = (((int)floor (r * 255 + 0.5) << 16) |
838 ((int)floor (g * 255 + 0.5) << 8) |
839 (int)floor (b * 255 + 0.5));
843 source = cairo_image_surface_create_for_data ((unsigned char *)buf,
845 width, height, stride);
847 /* Now draw the value marker onto the source image, so that it
848 * will get properly clipped at the edges of the ring
850 source_cr = cairo_create (source);
855 hsv_to_rgb (&r, &g, &b);
857 if (INTENSITY (r, g, b) > 0.5)
858 cairo_set_source_rgb (source_cr, 0., 0., 0.);
860 cairo_set_source_rgb (source_cr, 1., 1., 1.);
862 cairo_move_to (source_cr, center_x, center_y);
863 cairo_line_to (source_cr,
864 center_x + cos (priv->h * 2.0 * G_PI) * priv->size / 2,
865 center_y - sin (priv->h * 2.0 * G_PI) * priv->size / 2);
866 cairo_stroke (source_cr);
867 cairo_destroy (source_cr);
869 /* Draw the ring using the source image */
873 cairo_set_source_surface (cr, source, 0, 0);
874 cairo_surface_destroy (source);
876 cairo_set_line_width (cr, priv->ring_width);
880 priv->size / 2. - priv->ring_width / 2.,
889 /* Converts an HSV triplet to an integer RGB triplet */
891 get_color (gdouble h,
898 hsv_to_rgb (&h, &s, &v);
900 *r = floor (h * 255 + 0.5);
901 *g = floor (s * 255 + 0.5);
902 *b = floor (v * 255 + 0.5);
905 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
907 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
908 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
911 /* Number of pixels we extend out from the edges when creating
912 * color source to avoid artifacts
916 /* Paints the HSV triangle */
918 paint_triangle (GtkHSV *hsv,
922 GtkHSVPrivate *priv = hsv->priv;
923 GtkWidget *widget = GTK_WIDGET (hsv);
924 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
925 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
926 gint x2, y2, r2, g2, b2; /* Second vertex */
927 gint x3, y3, r3, g3, b3; /* Third vertex */
930 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
932 gint x_interp, y_interp;
934 cairo_surface_t *source;
938 GtkStyleContext *context;
941 width = gtk_widget_get_allocated_width (widget);
942 height = gtk_widget_get_allocated_height (widget);
943 /* Compute triangle's vertices */
945 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
949 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
953 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
957 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
986 /* Shade the triangle */
988 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
989 buf = g_new (guint32, height * stride / 4);
991 for (yy = 0; yy < height; yy++)
993 p = buf + yy * width;
995 if (yy >= y1 - PAD && yy < y3 + PAD) {
996 y_interp = CLAMP (yy, y1, y3);
1000 xl = LERP (x1, x2, y1, y2, y_interp);
1002 rl = LERP (r1, r2, y1, y2, y_interp);
1003 gl = LERP (g1, g2, y1, y2, y_interp);
1004 bl = LERP (b1, b2, y1, y2, y_interp);
1008 xl = LERP (x2, x3, y2, y3, y_interp);
1010 rl = LERP (r2, r3, y2, y3, y_interp);
1011 gl = LERP (g2, g3, y2, y3, y_interp);
1012 bl = LERP (b2, b3, y2, y3, y_interp);
1015 xr = LERP (x1, x3, y1, y3, y_interp);
1017 rr = LERP (r1, r3, y1, y3, y_interp);
1018 gr = LERP (g1, g3, y1, y3, y_interp);
1019 br = LERP (b1, b3, y1, y3, y_interp);
1029 x_start = MAX (xl - PAD, 0);
1030 x_end = MIN (xr + PAD, width);
1031 x_start = MIN (x_start, x_end);
1033 c = (rl << 16) | (gl << 8) | bl;
1035 for (xx = 0; xx < x_start; xx++)
1038 for (; xx < x_end; xx++)
1040 x_interp = CLAMP (xx, xl, xr);
1042 *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
1043 (LERP (gl, gr, xl, xr, x_interp) << 8) |
1044 LERP (bl, br, xl, xr, x_interp));
1047 c = (rr << 16) | (gr << 8) | br;
1049 for (; xx < width; xx++)
1054 source = cairo_image_surface_create_for_data ((unsigned char *)buf,
1056 width, height, stride);
1058 /* Draw a triangle with the image as a source */
1060 cairo_set_source_surface (cr, source, 0, 0);
1061 cairo_surface_destroy (source);
1063 cairo_move_to (cr, x1, y1);
1064 cairo_line_to (cr, x2, y2);
1065 cairo_line_to (cr, x3, y3);
1066 cairo_close_path (cr);
1071 /* Draw value marker */
1073 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1074 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1079 hsv_to_rgb (&r, &g, &b);
1081 context = gtk_widget_get_style_context (widget);
1083 gtk_style_context_save (context);
1085 if (INTENSITY (r, g, b) > 0.5)
1087 gtk_style_context_add_class (context, "light-area-focus");
1088 cairo_set_source_rgb (cr, 0., 0., 0.);
1092 gtk_style_context_add_class (context, "dark-area-focus");
1093 cairo_set_source_rgb (cr, 1., 1., 1.);
1097 #define FOCUS_RADIUS 6
1099 cairo_new_path (cr);
1100 cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
1103 /* Draw focus outline */
1105 if (draw_focus && !priv->focus_on_ring)
1110 gtk_widget_style_get (widget,
1111 "focus-line-width", &focus_width,
1112 "focus-padding", &focus_pad,
1115 gtk_render_focus (context, cr,
1116 xx - FOCUS_RADIUS - focus_width - focus_pad,
1117 yy - FOCUS_RADIUS - focus_width - focus_pad,
1118 2 * (FOCUS_RADIUS + focus_width + focus_pad),
1119 2 * (FOCUS_RADIUS + focus_width + focus_pad));
1122 gtk_style_context_restore (context);
1125 /* Paints the contents of the HSV color selector */
1127 gtk_hsv_draw (GtkWidget *widget,
1130 GtkHSV *hsv = GTK_HSV (widget);
1131 GtkHSVPrivate *priv = hsv->priv;
1132 gboolean draw_focus;
1134 draw_focus = gtk_widget_has_visible_focus (widget);
1136 paint_ring (hsv, cr);
1137 paint_triangle (hsv, cr, draw_focus);
1140 if (draw_focus && priv->focus_on_ring)
1142 GtkStyleContext *context;
1144 context = gtk_widget_get_style_context (widget);
1146 gtk_render_focus (context, cr, 0, 0,
1147 gtk_widget_get_allocated_width (widget),
1148 gtk_widget_get_allocated_height (widget));
1155 gtk_hsv_focus (GtkWidget *widget,
1156 GtkDirectionType dir)
1158 GtkHSV *hsv = GTK_HSV (widget);
1159 GtkHSVPrivate *priv = hsv->priv;
1161 if (!gtk_widget_has_focus (widget))
1163 if (dir == GTK_DIR_TAB_BACKWARD)
1164 priv->focus_on_ring = FALSE;
1166 priv->focus_on_ring = TRUE;
1168 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1175 if (priv->focus_on_ring)
1178 priv->focus_on_ring = TRUE;
1182 if (priv->focus_on_ring)
1183 priv->focus_on_ring = FALSE;
1189 case GTK_DIR_TAB_BACKWARD:
1190 if (priv->focus_on_ring)
1193 priv->focus_on_ring = TRUE;
1197 case GTK_DIR_TAB_FORWARD:
1198 if (priv->focus_on_ring)
1199 priv->focus_on_ring = FALSE;
1205 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1213 * Creates a new HSV color selector.
1215 * Return value: A newly-created HSV color selector.
1222 return g_object_new (GTK_TYPE_HSV, NULL);
1226 * gtk_hsv_set_color:
1227 * @hsv: An HSV color selector
1232 * Sets the current color in an HSV color selector.
1233 * Color component values must be in the [0.0, 1.0] range.
1238 gtk_hsv_set_color (GtkHSV *hsv,
1243 GtkHSVPrivate *priv;
1245 g_return_if_fail (GTK_IS_HSV (hsv));
1246 g_return_if_fail (h >= 0.0 && h <= 1.0);
1247 g_return_if_fail (s >= 0.0 && s <= 1.0);
1248 g_return_if_fail (v >= 0.0 && v <= 1.0);
1256 g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1258 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1262 * gtk_hsv_get_color:
1263 * @hsv: An HSV color selector
1264 * @h: (out): Return value for the hue
1265 * @s: (out): Return value for the saturation
1266 * @v: (out): Return value for the value
1268 * Queries the current color in an HSV color selector.
1269 * Returned values will be in the [0.0, 1.0] range.
1274 gtk_hsv_get_color (GtkHSV *hsv,
1279 GtkHSVPrivate *priv;
1281 g_return_if_fail (GTK_IS_HSV (hsv));
1296 * gtk_hsv_set_metrics:
1297 * @hsv: An HSV color selector
1298 * @size: Diameter for the hue ring
1299 * @ring_width: Width of the hue ring
1301 * Sets the size and ring width of an HSV color selector.
1306 gtk_hsv_set_metrics (GtkHSV *hsv,
1310 GtkHSVPrivate *priv;
1313 g_return_if_fail (GTK_IS_HSV (hsv));
1314 g_return_if_fail (size > 0);
1315 g_return_if_fail (ring_width > 0);
1316 g_return_if_fail (2 * ring_width + 1 <= size);
1320 same_size = (priv->size == size);
1323 priv->ring_width = ring_width;
1326 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1328 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1332 * gtk_hsv_get_metrics:
1333 * @hsv: An HSV color selector
1334 * @size: (out): Return value for the diameter of the hue ring
1335 * @ring_width: (out): Return value for the width of the hue ring
1337 * Queries the size and ring width of an HSV color selector.
1342 gtk_hsv_get_metrics (GtkHSV *hsv,
1346 GtkHSVPrivate *priv;
1348 g_return_if_fail (GTK_IS_HSV (hsv));
1356 *ring_width = priv->ring_width;
1360 * gtk_hsv_is_adjusting:
1363 * An HSV color selector can be said to be adjusting if multiple rapid
1364 * changes are being made to its value, for example, when the user is
1365 * adjusting the value with the mouse. This function queries whether
1366 * the HSV color selector is being adjusted or not.
1368 * Return value: %TRUE if clients can ignore changes to the color value,
1369 * since they may be transitory, or %FALSE if they should consider
1370 * the color value status to be final.
1375 gtk_hsv_is_adjusting (GtkHSV *hsv)
1377 GtkHSVPrivate *priv;
1379 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1383 return priv->mode != DRAG_NONE;
1387 gtk_hsv_move (GtkHSV *hsv,
1388 GtkDirectionType dir)
1390 GtkHSVPrivate *priv = hsv->priv;
1391 gdouble hue, sat, val;
1392 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1393 gint x, y; /* position in triangle */
1399 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1401 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1402 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1404 #define HUE_DELTA 0.002
1408 if (priv->focus_on_ring)
1413 compute_sv (hsv, x, y, &sat, &val);
1418 if (priv->focus_on_ring)
1423 compute_sv (hsv, x, y, &sat, &val);
1428 if (priv->focus_on_ring)
1433 compute_sv (hsv, x, y, &sat, &val);
1438 if (priv->focus_on_ring)
1444 compute_sv (hsv, x, y, &sat, &val);
1449 /* we don't care about the tab directions */
1459 gtk_hsv_set_color (hsv, hue, sat, val);