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/.
37 #include "gdk/gdkkeysyms.h"
40 #include "gtkbindings.h"
41 #include "gtkmarshalers.h"
44 /* Default width/height */
45 #define DEFAULT_SIZE 100
47 /* Default ring width */
48 #define DEFAULT_RING_WIDTH 10
58 /* Private part of the GtkHSV structure */
66 /* Size and ring width */
70 /* Window for capturing events */
79 guint focus_on_ring : 1;
91 static void gtk_hsv_destroy (GtkObject *object);
92 static void gtk_hsv_map (GtkWidget *widget);
93 static void gtk_hsv_unmap (GtkWidget *widget);
94 static void gtk_hsv_realize (GtkWidget *widget);
95 static void gtk_hsv_unrealize (GtkWidget *widget);
96 static void gtk_hsv_size_request (GtkWidget *widget,
97 GtkRequisition *requisition);
98 static void gtk_hsv_size_allocate (GtkWidget *widget,
99 GtkAllocation *allocation);
100 static gint gtk_hsv_button_press (GtkWidget *widget,
101 GdkEventButton *event);
102 static gint gtk_hsv_button_release (GtkWidget *widget,
103 GdkEventButton *event);
104 static gint gtk_hsv_motion (GtkWidget *widget,
105 GdkEventMotion *event);
106 static gint gtk_hsv_expose (GtkWidget *widget,
107 GdkEventExpose *event);
108 static gboolean gtk_hsv_grab_broken (GtkWidget *widget,
109 GdkEventGrabBroken *event);
110 static gboolean gtk_hsv_focus (GtkWidget *widget,
111 GtkDirectionType direction);
112 static void gtk_hsv_move (GtkHSV *hsv,
113 GtkDirectionType dir);
115 static guint hsv_signals[LAST_SIGNAL];
117 G_DEFINE_TYPE (GtkHSV, gtk_hsv, GTK_TYPE_WIDGET)
119 /* Class initialization function for the HSV color selector */
121 gtk_hsv_class_init (GtkHSVClass *class)
123 GObjectClass *gobject_class;
124 GtkObjectClass *object_class;
125 GtkWidgetClass *widget_class;
126 GtkHSVClass *hsv_class;
127 GtkBindingSet *binding_set;
129 gobject_class = (GObjectClass *) class;
130 object_class = (GtkObjectClass *) class;
131 widget_class = (GtkWidgetClass *) class;
132 hsv_class = GTK_HSV_CLASS (class);
134 object_class->destroy = gtk_hsv_destroy;
136 widget_class->map = gtk_hsv_map;
137 widget_class->unmap = gtk_hsv_unmap;
138 widget_class->realize = gtk_hsv_realize;
139 widget_class->unrealize = gtk_hsv_unrealize;
140 widget_class->size_request = gtk_hsv_size_request;
141 widget_class->size_allocate = gtk_hsv_size_allocate;
142 widget_class->button_press_event = gtk_hsv_button_press;
143 widget_class->button_release_event = gtk_hsv_button_release;
144 widget_class->motion_notify_event = gtk_hsv_motion;
145 widget_class->expose_event = gtk_hsv_expose;
146 widget_class->focus = gtk_hsv_focus;
147 widget_class->grab_broken_event = gtk_hsv_grab_broken;
149 hsv_class->move = gtk_hsv_move;
151 hsv_signals[CHANGED] =
152 g_signal_new (I_("changed"),
153 G_OBJECT_CLASS_TYPE (object_class),
155 G_STRUCT_OFFSET (GtkHSVClass, changed),
157 _gtk_marshal_VOID__VOID,
161 g_signal_new (I_("move"),
162 G_OBJECT_CLASS_TYPE (object_class),
163 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
164 G_STRUCT_OFFSET (GtkHSVClass, move),
166 _gtk_marshal_VOID__ENUM,
168 GTK_TYPE_DIRECTION_TYPE);
170 binding_set = gtk_binding_set_by_class (class);
172 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
174 G_TYPE_ENUM, GTK_DIR_UP);
175 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
177 G_TYPE_ENUM, GTK_DIR_UP);
179 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
181 G_TYPE_ENUM, GTK_DIR_DOWN);
182 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
184 G_TYPE_ENUM, GTK_DIR_DOWN);
187 gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
189 G_TYPE_ENUM, GTK_DIR_RIGHT);
190 gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
192 G_TYPE_ENUM, GTK_DIR_RIGHT);
194 gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
196 G_TYPE_ENUM, GTK_DIR_LEFT);
197 gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
199 G_TYPE_ENUM, GTK_DIR_LEFT);
201 g_type_class_add_private (gobject_class, sizeof (GtkHSVPriv));
204 /* Object initialization function for the HSV color selector */
206 gtk_hsv_init (GtkHSV *hsv)
210 priv = G_TYPE_INSTANCE_GET_PRIVATE (hsv, GTK_TYPE_HSV, GtkHSVPriv);
214 gtk_widget_set_has_window (GTK_WIDGET (hsv), FALSE);
215 gtk_widget_set_can_focus (GTK_WIDGET (hsv), TRUE);
221 priv->size = DEFAULT_SIZE;
222 priv->ring_width = DEFAULT_RING_WIDTH;
225 /* Destroy handler for the HSV color selector */
227 gtk_hsv_destroy (GtkObject *object)
229 GTK_OBJECT_CLASS (gtk_hsv_parent_class)->destroy (object);
232 /* Default signal handlers */
235 /* Map handler for the HSV color selector */
238 gtk_hsv_map (GtkWidget *widget)
240 GtkHSV *hsv = GTK_HSV (widget);
241 GtkHSVPriv *priv = hsv->priv;
243 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->map (widget);
245 gdk_window_show (priv->window);
248 /* Unmap handler for the HSV color selector */
251 gtk_hsv_unmap (GtkWidget *widget)
253 GtkHSV *hsv = GTK_HSV (widget);
254 GtkHSVPriv *priv = hsv->priv;
256 gdk_window_hide (priv->window);
258 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unmap (widget);
261 /* Realize handler for the HSV color selector */
263 gtk_hsv_realize (GtkWidget *widget)
265 GtkHSV *hsv = GTK_HSV (widget);
266 GtkHSVPriv *priv = hsv->priv;
267 GdkWindow *parent_window;
271 gtk_widget_set_realized (widget, TRUE);
275 attr.window_type = GDK_WINDOW_CHILD;
276 attr.x = widget->allocation.x;
277 attr.y = widget->allocation.y;
278 attr.width = widget->allocation.width;
279 attr.height = widget->allocation.height;
280 attr.wclass = GDK_INPUT_ONLY;
281 attr.event_mask = gtk_widget_get_events (widget);
282 attr.event_mask |= (GDK_KEY_PRESS_MASK
283 | GDK_BUTTON_PRESS_MASK
284 | GDK_BUTTON_RELEASE_MASK
285 | GDK_POINTER_MOTION_MASK
286 | GDK_ENTER_NOTIFY_MASK
287 | GDK_LEAVE_NOTIFY_MASK);
289 attr_mask = GDK_WA_X | GDK_WA_Y;
291 parent_window = gtk_widget_get_parent_window (widget);
293 widget->window = parent_window;
294 g_object_ref (widget->window);
296 priv->window = gdk_window_new (parent_window, &attr, attr_mask);
297 gdk_window_set_user_data (priv->window, hsv);
299 widget->style = gtk_style_attach (widget->style, widget->window);
303 priv->gc = gdk_gc_new (parent_window);
306 /* Unrealize handler for the HSV color selector */
308 gtk_hsv_unrealize (GtkWidget *widget)
310 GtkHSV *hsv = GTK_HSV (widget);
311 GtkHSVPriv *priv = hsv->priv;
313 gdk_window_set_user_data (priv->window, NULL);
314 gdk_window_destroy (priv->window);
317 g_object_unref (priv->gc);
320 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unrealize (widget);
323 /* Size_request handler for the HSV color selector */
325 gtk_hsv_size_request (GtkWidget *widget,
326 GtkRequisition *requisition)
328 GtkHSV *hsv = GTK_HSV (widget);
329 GtkHSVPriv *priv = hsv->priv;
333 gtk_widget_style_get (widget,
334 "focus-line-width", &focus_width,
335 "focus-padding", &focus_pad,
338 requisition->width = priv->size + 2 * (focus_width + focus_pad);
339 requisition->height = priv->size + 2 * (focus_width + focus_pad);
342 /* Size_allocate handler for the HSV color selector */
344 gtk_hsv_size_allocate (GtkWidget *widget,
345 GtkAllocation *allocation)
347 GtkHSV *hsv = GTK_HSV (widget);
348 GtkHSVPriv *priv = hsv->priv;
350 widget->allocation = *allocation;
352 if (gtk_widget_get_realized (widget))
353 gdk_window_move_resize (priv->window,
361 /* Utility functions */
363 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
365 /* Converts from HSV to RGB */
367 hsv_to_rgb (gdouble *h,
371 gdouble hue, saturation, value;
390 p = value * (1.0 - saturation);
391 q = value * (1.0 - saturation * f);
392 t = value * (1.0 - saturation * (1.0 - f));
433 g_assert_not_reached ();
438 /* Converts from RGB to HSV */
440 rgb_to_hsv (gdouble *r,
444 gdouble red, green, blue;
483 s = (max - min) / max;
494 h = (green - blue) / delta;
495 else if (green == max)
496 h = 2 + (blue - red) / delta;
497 else if (blue == max)
498 h = 4 + (red - green) / delta;
513 /* Computes the vertices of the saturation/value triangle */
515 compute_triangle (GtkHSV *hsv,
523 GtkHSVPriv *priv = hsv->priv;
526 gdouble inner, outer;
529 center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
530 center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
531 outer = priv->size / 2.0;
532 inner = outer - priv->ring_width;
533 angle = priv->h * 2.0 * G_PI;
535 *hx = floor (center_x + cos (angle) * inner + 0.5);
536 *hy = floor (center_y - sin (angle) * inner + 0.5);
537 *sx = floor (center_x + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
538 *sy = floor (center_y - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
539 *vx = floor (center_x + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
540 *vy = floor (center_y - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
543 /* Computes whether a point is inside the hue ring */
545 is_in_ring (GtkHSV *hsv,
549 GtkHSVPriv *priv = hsv->priv;
550 gdouble dx, dy, dist;
553 gdouble inner, outer;
555 center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
556 center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
557 outer = priv->size / 2.0;
558 inner = outer - priv->ring_width;
562 dist = dx * dx + dy * dy;
564 return (dist >= inner * inner && dist <= outer * outer);
567 /* Computes a saturation/value pair based on the mouse coordinates */
569 compute_sv (GtkHSV *hsv,
575 int ihx, ihy, isx, isy, ivx, ivy;
576 double hx, hy, sx, sy, vx, vy;
580 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
581 center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
582 center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
592 if (vx * (x - sx) + vy * (y - sy) < 0.0)
595 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
596 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
603 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
606 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
607 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
614 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
617 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
618 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
627 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
628 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
640 if (fabs (hy - vy) < fabs (hx - vx))
641 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
643 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
653 /* Computes whether a point is inside the saturation/value triangle */
655 is_in_triangle (GtkHSV *hsv,
659 int hx, hy, sx, sy, vx, vy;
662 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
664 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
666 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
667 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
669 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
672 /* Computes a value based on the mouse coordinates */
674 compute_v (GtkHSV *hsv,
683 center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
684 center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
688 angle = atan2 (dy, dx);
692 return angle / (2.0 * G_PI);
698 set_cross_grab (GtkHSV *hsv,
701 GtkHSVPriv *priv = hsv->priv;
704 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
706 gdk_pointer_grab (priv->window, FALSE,
707 (GDK_POINTER_MOTION_MASK
708 | GDK_POINTER_MOTION_HINT_MASK
709 | GDK_BUTTON_RELEASE_MASK),
713 gdk_cursor_unref (cursor);
717 gtk_hsv_grab_broken (GtkWidget *widget,
718 GdkEventGrabBroken *event)
720 GtkHSV *hsv = GTK_HSV (widget);
721 GtkHSVPriv *priv = hsv->priv;
723 priv->mode = DRAG_NONE;
728 /* Button_press_event handler for the HSV color selector */
730 gtk_hsv_button_press (GtkWidget *widget,
731 GdkEventButton *event)
733 GtkHSV *hsv = GTK_HSV (widget);
734 GtkHSVPriv *priv = hsv->priv;
737 if (priv->mode != DRAG_NONE || event->button != 1)
743 if (is_in_ring (hsv, x, y))
746 set_cross_grab (hsv, event->time);
748 gtk_hsv_set_color (hsv,
749 compute_v (hsv, x, y),
753 gtk_widget_grab_focus (widget);
754 priv->focus_on_ring = TRUE;
759 if (is_in_triangle (hsv, x, y))
763 priv->mode = DRAG_SV;
764 set_cross_grab (hsv, event->time);
766 compute_sv (hsv, x, y, &s, &v);
767 gtk_hsv_set_color (hsv, priv->h, s, v);
769 gtk_widget_grab_focus (widget);
770 priv->focus_on_ring = FALSE;
778 /* Button_release_event handler for the HSV color selector */
780 gtk_hsv_button_release (GtkWidget *widget,
781 GdkEventButton *event)
783 GtkHSV *hsv = GTK_HSV (widget);
784 GtkHSVPriv *priv = hsv->priv;
788 if (priv->mode == DRAG_NONE || event->button != 1)
791 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
792 * can see that this is the final color state.
796 priv->mode = DRAG_NONE;
802 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
803 else if (mode == DRAG_SV) {
806 compute_sv (hsv, x, y, &s, &v);
807 gtk_hsv_set_color (hsv, priv->h, s, v);
809 g_assert_not_reached ();
811 gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
816 /* Motion_notify_event handler for the HSV color selector */
818 gtk_hsv_motion (GtkWidget *widget,
819 GdkEventMotion *event)
821 GtkHSV *hsv = GTK_HSV (widget);
822 GtkHSVPriv *priv = hsv->priv;
823 GdkModifierType mods;
826 if (priv->mode == DRAG_NONE)
829 gdk_event_request_motions (event);
834 if (priv->mode == DRAG_H)
836 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
839 else if (priv->mode == DRAG_SV)
843 compute_sv (hsv, x, y, &s, &v);
844 gtk_hsv_set_color (hsv, priv->h, s, v);
848 g_assert_not_reached ();
855 /* Paints the hue ring */
857 paint_ring (GtkHSV *hsv,
864 GtkHSVPriv *priv = hsv->priv;
865 GtkWidget *widget = GTK_WIDGET (hsv);
867 gdouble dx, dy, dist;
870 gdouble inner, outer;
875 cairo_surface_t *source;
881 gtk_widget_style_get (widget,
882 "focus-line-width", &focus_width,
883 "focus-padding", &focus_pad,
888 center_x = widget->allocation.width / 2.0;
889 center_y = widget->allocation.height / 2.0;
891 outer = priv->size / 2.0;
892 inner = outer - priv->ring_width;
894 /* Create an image initialized with the ring colors */
896 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
897 buf = g_new (guint32, height * stride / 4);
899 for (yy = 0; yy < height; yy++)
901 p = buf + yy * width;
903 dy = -(yy + y - center_y);
905 for (xx = 0; xx < width; xx++)
907 dx = xx + x - center_x;
909 dist = dx * dx + dy * dy;
910 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
916 angle = atan2 (dy, dx);
920 hue = angle / (2.0 * G_PI);
925 hsv_to_rgb (&r, &g, &b);
927 *p++ = (((int)floor (r * 255 + 0.5) << 16) |
928 ((int)floor (g * 255 + 0.5) << 8) |
929 (int)floor (b * 255 + 0.5));
933 source = cairo_image_surface_create_for_data ((unsigned char *)buf,
935 width, height, stride);
937 /* Now draw the value marker onto the source image, so that it
938 * will get properly clipped at the edges of the ring
940 source_cr = cairo_create (source);
945 hsv_to_rgb (&r, &g, &b);
947 if (INTENSITY (r, g, b) > 0.5)
948 cairo_set_source_rgb (source_cr, 0., 0., 0.);
950 cairo_set_source_rgb (source_cr, 1., 1., 1.);
952 cairo_move_to (source_cr, -x + center_x, - y + center_y);
953 cairo_line_to (source_cr,
954 -x + center_x + cos (priv->h * 2.0 * G_PI) * priv->size / 2,
955 -y + center_y - sin (priv->h * 2.0 * G_PI) * priv->size / 2);
956 cairo_stroke (source_cr);
957 cairo_destroy (source_cr);
959 /* Draw the ring using the source image */
963 cairo_set_source_surface (cr, source, x, y);
964 cairo_surface_destroy (source);
966 cairo_set_line_width (cr, priv->ring_width);
970 priv->size / 2. - priv->ring_width / 2.,
979 /* Converts an HSV triplet to an integer RGB triplet */
981 get_color (gdouble h,
988 hsv_to_rgb (&h, &s, &v);
990 *r = floor (h * 255 + 0.5);
991 *g = floor (s * 255 + 0.5);
992 *b = floor (v * 255 + 0.5);
995 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
997 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
998 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
1001 /* Number of pixels we extend out from the edges when creating
1002 * color source to avoid artifacts
1006 /* Paints the HSV triangle */
1008 paint_triangle (GtkHSV *hsv,
1015 GtkHSVPriv *priv = hsv->priv;
1016 GtkWidget *widget = GTK_WIDGET (hsv);
1017 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1018 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1019 gint x2, y2, r2, g2, b2; /* Second vertex */
1020 gint x3, y3, r3, g3, b3; /* Third vertex */
1022 guint32 *buf, *p, c;
1023 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1025 gint x_interp, y_interp;
1026 gint x_start, x_end;
1027 cairo_surface_t *source;
1034 /* Compute triangle's vertices */
1036 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1040 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1044 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1048 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1077 /* Shade the triangle */
1079 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
1080 buf = g_new (guint32, height * stride / 4);
1082 for (yy = 0; yy < height; yy++)
1084 p = buf + yy * width;
1086 if (yy + y >= y1 - PAD && yy + y < y3 + PAD) {
1087 y_interp = CLAMP (yy + y, y1, y3);
1091 xl = LERP (x1, x2, y1, y2, y_interp);
1093 rl = LERP (r1, r2, y1, y2, y_interp);
1094 gl = LERP (g1, g2, y1, y2, y_interp);
1095 bl = LERP (b1, b2, y1, y2, y_interp);
1099 xl = LERP (x2, x3, y2, y3, y_interp);
1101 rl = LERP (r2, r3, y2, y3, y_interp);
1102 gl = LERP (g2, g3, y2, y3, y_interp);
1103 bl = LERP (b2, b3, y2, y3, y_interp);
1106 xr = LERP (x1, x3, y1, y3, y_interp);
1108 rr = LERP (r1, r3, y1, y3, y_interp);
1109 gr = LERP (g1, g3, y1, y3, y_interp);
1110 br = LERP (b1, b3, y1, y3, y_interp);
1120 x_start = MAX (xl - PAD, x);
1121 x_end = MIN (xr + PAD, x + width);
1122 x_start = MIN (x_start, x_end);
1124 c = (rl << 16) | (gl << 8) | bl;
1126 for (xx = x; xx < x_start; xx++)
1129 for (; xx < x_end; xx++)
1131 x_interp = CLAMP (xx, xl, xr);
1133 *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
1134 (LERP (gl, gr, xl, xr, x_interp) << 8) |
1135 LERP (bl, br, xl, xr, x_interp));
1138 c = (rr << 16) | (gr << 8) | br;
1140 for (; xx < x + width; xx++)
1145 source = cairo_image_surface_create_for_data ((unsigned char *)buf,
1147 width, height, stride);
1149 /* Draw a triangle with the image as a source */
1151 cairo_set_source_surface (cr, source, x, y);
1152 cairo_surface_destroy (source);
1154 cairo_move_to (cr, x1, y1);
1155 cairo_line_to (cr, x2, y2);
1156 cairo_line_to (cr, x3, y3);
1157 cairo_close_path (cr);
1162 /* Draw value marker */
1164 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1165 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1170 hsv_to_rgb (&r, &g, &b);
1172 if (INTENSITY (r, g, b) > 0.5)
1174 detail = "colorwheel_light";
1175 cairo_set_source_rgb (cr, 0., 0., 0.);
1179 detail = "colorwheel_dark";
1180 cairo_set_source_rgb (cr, 1., 1., 1.);
1184 #define FOCUS_RADIUS 6
1186 cairo_new_path (cr);
1187 cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
1190 /* Draw focus outline */
1192 if (gtk_widget_has_focus (widget) &&
1193 !priv->focus_on_ring)
1198 gtk_widget_style_get (widget,
1199 "focus-line-width", &focus_width,
1200 "focus-padding", &focus_pad,
1203 gtk_paint_focus (widget->style, widget->window,
1204 gtk_widget_get_state (widget),
1205 NULL, widget, detail,
1206 widget->allocation.x + xx - FOCUS_RADIUS - focus_width - focus_pad,
1207 widget->allocation.y + yy - FOCUS_RADIUS - focus_width - focus_pad,
1208 2 * (FOCUS_RADIUS + focus_width + focus_pad),
1209 2 * (FOCUS_RADIUS + focus_width + focus_pad));
1214 /* Paints the contents of the HSV color selector */
1223 paint_ring (hsv, cr, x, y, width, height);
1224 paint_triangle (hsv, cr, x, y, width, height);
1227 /* Expose_event handler for the HSV color selector */
1229 gtk_hsv_expose (GtkWidget *widget,
1230 GdkEventExpose *event)
1232 GtkHSV *hsv = GTK_HSV (widget);
1233 GtkHSVPriv *priv = hsv->priv;
1234 GdkRectangle rect, dest;
1237 if (!(event->window == widget->window && gtk_widget_is_drawable (widget)))
1240 rect.x = widget->allocation.x;
1241 rect.y = widget->allocation.y;
1242 rect.width = widget->allocation.width;
1243 rect.height = widget->allocation.height;
1245 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1248 cr = gdk_cairo_create (widget->window);
1250 cairo_translate (cr, widget->allocation.x, widget->allocation.y);
1252 dest.x - widget->allocation.x,
1253 dest.y - widget->allocation.y,
1254 dest.width, dest.height);
1257 if (gtk_widget_has_focus (widget) && priv->focus_on_ring)
1258 gtk_paint_focus (widget->style, widget->window,
1259 gtk_widget_get_state (widget),
1260 &event->area, widget, NULL,
1261 widget->allocation.x,
1262 widget->allocation.y,
1263 widget->allocation.width,
1264 widget->allocation.height);
1270 gtk_hsv_focus (GtkWidget *widget,
1271 GtkDirectionType dir)
1273 GtkHSV *hsv = GTK_HSV (widget);
1274 GtkHSVPriv *priv = hsv->priv;
1276 if (!gtk_widget_has_focus (widget))
1278 if (dir == GTK_DIR_TAB_BACKWARD)
1279 priv->focus_on_ring = FALSE;
1281 priv->focus_on_ring = TRUE;
1283 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1290 if (priv->focus_on_ring)
1293 priv->focus_on_ring = TRUE;
1297 if (priv->focus_on_ring)
1298 priv->focus_on_ring = FALSE;
1304 case GTK_DIR_TAB_BACKWARD:
1305 if (priv->focus_on_ring)
1308 priv->focus_on_ring = TRUE;
1312 case GTK_DIR_TAB_FORWARD:
1313 if (priv->focus_on_ring)
1314 priv->focus_on_ring = FALSE;
1320 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1328 * Creates a new HSV color selector.
1330 * Return value: A newly-created HSV color selector.
1337 return g_object_new (GTK_TYPE_HSV, NULL);
1341 * gtk_hsv_set_color:
1342 * @hsv: An HSV color selector
1347 * Sets the current color in an HSV color selector.
1348 * Color component values must be in the [0.0, 1.0] range.
1353 gtk_hsv_set_color (GtkHSV *hsv,
1360 g_return_if_fail (GTK_IS_HSV (hsv));
1361 g_return_if_fail (h >= 0.0 && h <= 1.0);
1362 g_return_if_fail (s >= 0.0 && s <= 1.0);
1363 g_return_if_fail (v >= 0.0 && v <= 1.0);
1371 g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1373 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1377 * gtk_hsv_get_color:
1378 * @hsv: An HSV color selector
1379 * @h: Return value for the hue
1380 * @s: Return value for the saturation
1381 * @v: Return value for the value
1383 * Queries the current color in an HSV color selector.
1384 * Returned values will be in the [0.0, 1.0] range.
1389 gtk_hsv_get_color (GtkHSV *hsv,
1394 GtkHSVPriv *priv = hsv->priv;
1396 g_return_if_fail (GTK_IS_HSV (hsv));
1411 * gtk_hsv_set_metrics:
1412 * @hsv: An HSV color selector
1413 * @size: Diameter for the hue ring
1414 * @ring_width: Width of the hue ring
1416 * Sets the size and ring width of an HSV color selector.
1421 gtk_hsv_set_metrics (GtkHSV *hsv,
1425 GtkHSVPriv *priv = hsv->priv;
1428 g_return_if_fail (GTK_IS_HSV (hsv));
1429 g_return_if_fail (size > 0);
1430 g_return_if_fail (ring_width > 0);
1431 g_return_if_fail (2 * ring_width + 1 <= size);
1435 same_size = (priv->size == size);
1438 priv->ring_width = ring_width;
1441 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1443 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1447 * gtk_hsv_get_metrics:
1448 * @hsv: An HSV color selector
1449 * @size: Return value for the diameter of the hue ring
1450 * @ring_width: Return value for the width of the hue ring
1452 * Queries the size and ring width of an HSV color selector.
1457 gtk_hsv_get_metrics (GtkHSV *hsv,
1461 GtkHSVPriv *priv = hsv->priv;
1463 g_return_if_fail (GTK_IS_HSV (hsv));
1471 *ring_width = priv->ring_width;
1475 * gtk_hsv_is_adjusting:
1478 * An HSV color selector can be said to be adjusting if multiple rapid
1479 * changes are being made to its value, for example, when the user is
1480 * adjusting the value with the mouse. This function queries whether
1481 * the HSV color selector is being adjusted or not.
1483 * Return value: %TRUE if clients can ignore changes to the color value,
1484 * since they may be transitory, or %FALSE if they should consider
1485 * the color value status to be final.
1490 gtk_hsv_is_adjusting (GtkHSV *hsv)
1492 GtkHSVPriv *priv = hsv->priv;
1494 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1498 return priv->mode != DRAG_NONE;
1506 * @r: Return value for the red component
1507 * @g: Return value for the green component
1508 * @b: Return value for the blue component
1510 * Converts a color from HSV space to RGB.
1511 * Input values must be in the [0.0, 1.0] range;
1512 * output values will be in the same range.
1517 gtk_hsv_to_rgb (gdouble h,
1524 g_return_if_fail (h >= 0.0 && h <= 1.0);
1525 g_return_if_fail (s >= 0.0 && s <= 1.0);
1526 g_return_if_fail (v >= 0.0 && v <= 1.0);
1528 hsv_to_rgb (&h, &s, &v);
1545 * @h: Return value for the hue component
1546 * @s: Return value for the saturation component
1547 * @v: Return value for the value component
1549 * Converts a color from RGB space to HSV.
1550 * Input values must be in the [0.0, 1.0] range;
1551 * output values will be in the same range.
1556 gtk_rgb_to_hsv (gdouble r,
1563 g_return_if_fail (r >= 0.0 && r <= 1.0);
1564 g_return_if_fail (g >= 0.0 && g <= 1.0);
1565 g_return_if_fail (b >= 0.0 && b <= 1.0);
1567 rgb_to_hsv (&r, &g, &b);
1580 gtk_hsv_move (GtkHSV *hsv,
1581 GtkDirectionType dir)
1583 GtkHSVPriv *priv = hsv->priv;
1584 gdouble hue, sat, val;
1585 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1586 gint x, y; /* position in triangle */
1592 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1594 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1595 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1597 #define HUE_DELTA 0.002
1601 if (priv->focus_on_ring)
1606 compute_sv (hsv, x, y, &sat, &val);
1611 if (priv->focus_on_ring)
1616 compute_sv (hsv, x, y, &sat, &val);
1621 if (priv->focus_on_ring)
1626 compute_sv (hsv, x, y, &sat, &val);
1631 if (priv->focus_on_ring)
1637 compute_sv (hsv, x, y, &sat, &val);
1642 /* we don't care about the tab directions */
1652 gtk_hsv_set_color (hsv, hue, sat, val);