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.
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkbindings.h"
31 #include "gtkcontainer.h"
32 #include "gtkmarshalers.h"
37 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
38 * file for a list of people on the GTK+ Team. See the ChangeLog
39 * files for a list of changes. These files are distributed with
40 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
43 /* Default width/height */
44 #define DEFAULT_SIZE 100
46 /* Default ring width */
47 #define DEFAULT_RING_WIDTH 10
57 /* Private part of the GtkHSV structure */
64 /* Size and ring width */
68 /* Window for capturing events */
77 guint focus_on_ring : 1;
91 static void gtk_hsv_class_init (GtkHSVClass *class);
92 static void gtk_hsv_init (GtkHSV *hsv);
93 static void gtk_hsv_destroy (GtkObject *object);
94 static void gtk_hsv_map (GtkWidget *widget);
95 static void gtk_hsv_unmap (GtkWidget *widget);
96 static void gtk_hsv_realize (GtkWidget *widget);
97 static void gtk_hsv_unrealize (GtkWidget *widget);
98 static void gtk_hsv_size_request (GtkWidget *widget,
99 GtkRequisition *requisition);
100 static void gtk_hsv_size_allocate (GtkWidget *widget,
101 GtkAllocation *allocation);
102 static gint gtk_hsv_button_press (GtkWidget *widget,
103 GdkEventButton *event);
104 static gint gtk_hsv_button_release (GtkWidget *widget,
105 GdkEventButton *event);
106 static gint gtk_hsv_motion (GtkWidget *widget,
107 GdkEventMotion *event);
108 static gint gtk_hsv_expose (GtkWidget *widget,
109 GdkEventExpose *event);
110 static gboolean gtk_hsv_grab_broken (GtkWidget *widget,
111 GdkEventGrabBroken *event);
112 static gboolean gtk_hsv_focus (GtkWidget *widget,
113 GtkDirectionType direction);
114 static void gtk_hsv_move (GtkHSV *hsv,
115 GtkDirectionType dir);
117 static guint hsv_signals[LAST_SIGNAL];
118 static GtkWidgetClass *parent_class;
125 * Registers the &GtkHSV class if necessary, and returns the type ID associated
128 * Return value: The type ID of the &GtkHSV class.
131 gtk_hsv_get_type (void)
133 static GType hsv_type = 0;
136 static const GTypeInfo hsv_info = {
137 sizeof (GtkHSVClass),
138 NULL, /* base_init */
139 NULL, /* base_finalize */
140 (GClassInitFunc) gtk_hsv_class_init,
141 NULL, /* class_finalize */
142 NULL, /* class_data */
145 (GInstanceInitFunc) gtk_hsv_init,
148 hsv_type = g_type_register_static (GTK_TYPE_WIDGET, I_("GtkHSV"),
155 /* Class initialization function for the HSV color selector */
157 gtk_hsv_class_init (GtkHSVClass *class)
159 GtkObjectClass *object_class;
160 GtkWidgetClass *widget_class;
161 GtkHSVClass *hsv_class;
162 GtkBindingSet *binding_set;
164 object_class = (GtkObjectClass *) class;
165 widget_class = (GtkWidgetClass *) class;
166 hsv_class = GTK_HSV_CLASS (class);
168 parent_class = g_type_class_peek_parent (class);
170 object_class->destroy = gtk_hsv_destroy;
172 widget_class->map = gtk_hsv_map;
173 widget_class->unmap = gtk_hsv_unmap;
174 widget_class->realize = gtk_hsv_realize;
175 widget_class->unrealize = gtk_hsv_unrealize;
176 widget_class->size_request = gtk_hsv_size_request;
177 widget_class->size_allocate = gtk_hsv_size_allocate;
178 widget_class->button_press_event = gtk_hsv_button_press;
179 widget_class->button_release_event = gtk_hsv_button_release;
180 widget_class->motion_notify_event = gtk_hsv_motion;
181 widget_class->expose_event = gtk_hsv_expose;
182 widget_class->focus = gtk_hsv_focus;
183 widget_class->grab_broken_event = gtk_hsv_grab_broken;
185 hsv_class->move = gtk_hsv_move;
187 hsv_signals[CHANGED] =
188 g_signal_new (I_("changed"),
189 G_OBJECT_CLASS_TYPE (object_class),
191 G_STRUCT_OFFSET (GtkHSVClass, changed),
193 _gtk_marshal_VOID__VOID,
197 g_signal_new (I_("move"),
198 G_OBJECT_CLASS_TYPE (object_class),
199 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
200 G_STRUCT_OFFSET (GtkHSVClass, move),
202 _gtk_marshal_VOID__ENUM,
204 GTK_TYPE_DIRECTION_TYPE);
206 binding_set = gtk_binding_set_by_class (class);
208 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
210 G_TYPE_ENUM, GTK_DIR_UP);
211 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
213 G_TYPE_ENUM, GTK_DIR_UP);
215 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
217 G_TYPE_ENUM, GTK_DIR_DOWN);
218 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
220 G_TYPE_ENUM, GTK_DIR_DOWN);
223 gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
225 G_TYPE_ENUM, GTK_DIR_RIGHT);
226 gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
228 G_TYPE_ENUM, GTK_DIR_RIGHT);
230 gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
232 G_TYPE_ENUM, GTK_DIR_LEFT);
233 gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
235 G_TYPE_ENUM, GTK_DIR_LEFT);
238 /* Object initialization function for the HSV color selector */
240 gtk_hsv_init (GtkHSV *hsv)
244 priv = g_new0 (HSVPrivate, 1);
247 GTK_WIDGET_SET_FLAGS (hsv, GTK_NO_WINDOW);
248 GTK_WIDGET_SET_FLAGS (hsv, GTK_CAN_FOCUS);
254 priv->size = DEFAULT_SIZE;
255 priv->ring_width = DEFAULT_RING_WIDTH;
258 /* Destroy handler for the HSV color selector */
260 gtk_hsv_destroy (GtkObject *object)
264 g_return_if_fail (GTK_IS_HSV (object));
266 hsv = GTK_HSV (object);
274 GTK_OBJECT_CLASS (parent_class)->destroy (object);
277 /* Default signal handlers */
280 /* Map handler for the HSV color selector */
283 gtk_hsv_map (GtkWidget *widget)
288 hsv = GTK_HSV (widget);
291 GTK_WIDGET_CLASS (parent_class)->map (widget);
293 gdk_window_show (priv->window);
296 /* Unmap handler for the HSV color selector */
299 gtk_hsv_unmap (GtkWidget *widget)
304 hsv = GTK_HSV (widget);
307 gdk_window_hide (priv->window);
309 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
312 /* Realize handler for the HSV color selector */
314 gtk_hsv_realize (GtkWidget *widget)
320 GdkWindow *parent_window;
322 hsv = GTK_HSV (widget);
325 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
329 attr.window_type = GDK_WINDOW_CHILD;
330 attr.x = widget->allocation.x;
331 attr.y = widget->allocation.y;
332 attr.width = widget->allocation.width;
333 attr.height = widget->allocation.height;
334 attr.wclass = GDK_INPUT_ONLY;
335 attr.event_mask = gtk_widget_get_events (widget);
336 attr.event_mask |= (GDK_KEY_PRESS_MASK
337 | GDK_BUTTON_PRESS_MASK
338 | GDK_BUTTON_RELEASE_MASK
339 | GDK_POINTER_MOTION_MASK
340 | GDK_ENTER_NOTIFY_MASK
341 | GDK_LEAVE_NOTIFY_MASK);
343 attr_mask = GDK_WA_X | GDK_WA_Y;
345 parent_window = gtk_widget_get_parent_window (widget);
347 widget->window = parent_window;
348 g_object_ref (widget->window);
350 priv->window = gdk_window_new (parent_window, &attr, attr_mask);
351 gdk_window_set_user_data (priv->window, hsv);
353 widget->style = gtk_style_attach (widget->style, widget->window);
357 priv->gc = gdk_gc_new (parent_window);
360 /* Unrealize handler for the HSV color selector */
362 gtk_hsv_unrealize (GtkWidget *widget)
367 hsv = GTK_HSV (widget);
370 gdk_window_set_user_data (priv->window, NULL);
371 gdk_window_destroy (priv->window);
374 g_object_unref (priv->gc);
377 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
378 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
381 /* Size_request handler for the HSV color selector */
383 gtk_hsv_size_request (GtkWidget *widget,
384 GtkRequisition *requisition)
386 GtkHSV *hsv = GTK_HSV (widget);
387 HSVPrivate *priv = hsv->priv;
391 gtk_widget_style_get (widget,
392 "focus-line-width", &focus_width,
393 "focus-padding", &focus_pad,
396 requisition->width = priv->size + 2 * (focus_width + focus_pad);
397 requisition->height = priv->size + 2 * (focus_width + focus_pad);
400 /* Size_allocate handler for the HSV color selector */
402 gtk_hsv_size_allocate (GtkWidget *widget,
403 GtkAllocation *allocation)
408 hsv = GTK_HSV (widget);
411 widget->allocation = *allocation;
413 if (GTK_WIDGET_REALIZED (widget))
414 gdk_window_move_resize (priv->window,
422 /* Utility functions */
424 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
426 /* Converts from HSV to RGB */
428 hsv_to_rgb (gdouble *h,
432 gdouble hue, saturation, value;
451 p = value * (1.0 - saturation);
452 q = value * (1.0 - saturation * f);
453 t = value * (1.0 - saturation * (1.0 - f));
494 g_assert_not_reached ();
499 /* Converts from RGB to HSV */
501 rgb_to_hsv (gdouble *r,
505 gdouble red, green, blue;
544 s = (max - min) / max;
555 h = (green - blue) / delta;
556 else if (green == max)
557 h = 2 + (blue - red) / delta;
558 else if (blue == max)
559 h = 4 + (red - green) / delta;
574 /* Computes the vertices of the saturation/value triangle */
576 compute_triangle (GtkHSV *hsv,
586 gdouble inner, outer;
591 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
592 outer = priv->size / 2.0;
593 inner = outer - priv->ring_width;
594 angle = priv->h * 2.0 * G_PI;
596 *hx = floor (center + cos (angle) * inner + 0.5);
597 *hy = floor (center - sin (angle) * inner + 0.5);
598 *sx = floor (center + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
599 *sy = floor (center - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
600 *vx = floor (center + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
601 *vy = floor (center - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
604 /* Computes whether a point is inside the hue ring */
606 is_in_ring (GtkHSV *hsv,
611 gdouble dx, dy, dist;
612 gdouble center, inner, outer;
616 center = priv->size / 2.0;
617 outer = priv->size / 2.0;
618 inner = outer - priv->ring_width;
622 dist = dx * dx + dy * dy;
624 return (dist >= inner * inner && dist <= outer * outer);
627 /* Computes a saturation/value pair based on the mouse coordinates */
629 compute_sv (GtkHSV *hsv,
636 int ihx, ihy, isx, isy, ivx, ivy;
637 double hx, hy, sx, sy, vx, vy;
642 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
643 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
653 if (vx * (x - sx) + vy * (y - sy) < 0.0)
656 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
657 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
664 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
667 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
668 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
675 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
678 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
679 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
688 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
689 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
701 if (fabs (hy - vy) < fabs (hx - vx))
702 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
704 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
714 /* Computes whether a point is inside the saturation/value triangle */
716 is_in_triangle (GtkHSV *hsv,
720 int hx, hy, sx, sy, vx, vy;
723 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
725 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
727 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
728 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
730 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
733 /* Computes a value based on the mouse coordinates */
735 compute_v (GtkHSV *hsv,
746 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
750 angle = atan2 (dy, dx);
754 return angle / (2.0 * G_PI);
760 set_cross_grab (GtkHSV *hsv,
768 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
770 gdk_pointer_grab (priv->window, FALSE,
771 (GDK_POINTER_MOTION_MASK
772 | GDK_POINTER_MOTION_HINT_MASK
773 | GDK_BUTTON_RELEASE_MASK),
777 gdk_cursor_unref (cursor);
781 gtk_hsv_grab_broken (GtkWidget *widget,
782 GdkEventGrabBroken *event)
784 GtkHSV *hsv = GTK_HSV (widget);
789 priv->mode = DRAG_NONE;
794 /* Button_press_event handler for the HSV color selector */
796 gtk_hsv_button_press (GtkWidget *widget,
797 GdkEventButton *event)
803 hsv = GTK_HSV (widget);
806 if (priv->mode != DRAG_NONE || event->button != 1)
812 if (is_in_ring (hsv, x, y))
815 set_cross_grab (hsv, event->time);
817 gtk_hsv_set_color (hsv,
818 compute_v (hsv, x, y),
822 gtk_widget_grab_focus (widget);
823 priv->focus_on_ring = TRUE;
828 if (is_in_triangle (hsv, x, y))
832 priv->mode = DRAG_SV;
833 set_cross_grab (hsv, event->time);
835 compute_sv (hsv, x, y, &s, &v);
836 gtk_hsv_set_color (hsv, priv->h, s, v);
838 gtk_widget_grab_focus (widget);
839 priv->focus_on_ring = FALSE;
847 /* Button_release_event handler for the HSV color selector */
849 gtk_hsv_button_release (GtkWidget *widget,
850 GdkEventButton *event)
857 hsv = GTK_HSV (widget);
860 if (priv->mode == DRAG_NONE || event->button != 1)
863 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
864 * can see that this is the final color state.
868 priv->mode = DRAG_NONE;
874 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
875 else if (mode == DRAG_SV) {
878 compute_sv (hsv, x, y, &s, &v);
879 gtk_hsv_set_color (hsv, priv->h, s, v);
881 g_assert_not_reached ();
883 gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
888 /* Motion_notify_event handler for the HSV color selector */
890 gtk_hsv_motion (GtkWidget *widget,
891 GdkEventMotion *event)
897 GdkModifierType mods;
899 hsv = GTK_HSV (widget);
902 if (priv->mode == DRAG_NONE)
907 gdk_window_get_pointer (priv->window, &ix, &iy, &mods);
917 if (priv->mode == DRAG_H)
919 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
922 else if (priv->mode == DRAG_SV)
926 compute_sv (hsv, x, y, &s, &v);
927 gtk_hsv_set_color (hsv, priv->h, s, v);
931 g_assert_not_reached ();
938 /* Paints the hue ring */
940 paint_ring (GtkHSV *hsv,
947 GtkWidget *widget = GTK_WIDGET (hsv);
950 gdouble dx, dy, dist;
952 gdouble inner, outer;
957 cairo_surface_t *source;
962 gtk_widget_style_get (widget,
963 "focus-line-width", &focus_width,
964 "focus-padding", &focus_pad,
969 center = widget->requisition.width / 2.0;
971 outer = priv->size / 2.0;
972 inner = outer - priv->ring_width;
974 /* Create an image initialized with the ring colors */
976 buf = g_new (guint32, width * height);
978 for (yy = 0; yy < height; yy++)
980 p = buf + yy * width;
982 dy = -(yy + y - center);
984 for (xx = 0; xx < width; xx++)
986 dx = xx + x - center;
988 dist = dx * dx + dy * dy;
989 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
995 angle = atan2 (dy, dx);
999 hue = angle / (2.0 * G_PI);
1004 hsv_to_rgb (&r, &g, &b);
1006 *p++ = (((int)floor (r * 255 + 0.5) << 16) |
1007 ((int)floor (g * 255 + 0.5) << 8) |
1008 (int)floor (b * 255 + 0.5));
1012 source = cairo_image_surface_create_for_data ((char *)buf,
1014 width, height, 4 * width);
1016 /* Now draw the value marker onto the source image, so that it
1017 * will get properly clipped at the edges of the ring
1019 source_cr = cairo_create (source);
1024 hsv_to_rgb (&r, &g, &b);
1026 if (INTENSITY (r, g, b) > 0.5)
1027 cairo_set_source_rgb (source_cr, 0., 0., 0.);
1029 cairo_set_source_rgb (source_cr, 1., 1., 1.);
1031 cairo_move_to (source_cr, -x + center, - y + center);
1032 cairo_line_to (source_cr,
1033 -x + center + cos (priv->h * 2.0 * G_PI) * center,
1034 -y + center - sin (priv->h * 2.0 * G_PI) * center);
1035 cairo_stroke (source_cr);
1036 cairo_destroy (source_cr);
1038 /* Draw the ring using the source image */
1042 cairo_set_source_surface (cr, source, x, y);
1043 cairo_surface_destroy (source);
1045 cairo_set_line_width (cr, priv->ring_width);
1046 cairo_new_path (cr);
1049 priv->size / 2. - priv->ring_width / 2.,
1058 /* Converts an HSV triplet to an integer RGB triplet */
1060 get_color (gdouble h,
1067 hsv_to_rgb (&h, &s, &v);
1069 *r = floor (h * 255 + 0.5);
1070 *g = floor (s * 255 + 0.5);
1071 *b = floor (v * 255 + 0.5);
1074 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1076 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
1077 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
1080 /* Number of pixels we extend out from the edges when creating
1081 * color source to avoid artifacts
1085 /* Paints the HSV triangle */
1087 paint_triangle (GtkHSV *hsv,
1094 GtkWidget *widget = GTK_WIDGET (hsv);
1096 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1097 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1098 gint x2, y2, r2, g2, b2; /* Second vertex */
1099 gint x3, y3, r3, g3, b3; /* Third vertex */
1101 guint32 *buf, *p, c;
1102 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1104 gint x_interp, y_interp;
1105 gint x_start, x_end;
1106 cairo_surface_t *source;
1112 /* Compute triangle's vertices */
1114 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1118 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1122 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1126 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1155 /* Shade the triangle */
1157 buf = g_new (guint32, width * height);
1159 for (yy = 0; yy < height; yy++)
1161 p = buf + yy * width;
1163 if (yy + y >= y1 - PAD && yy + y < y3 + PAD) {
1164 y_interp = CLAMP (yy + y, y1, y3);
1168 xl = LERP (x1, x2, y1, y2, y_interp);
1170 rl = LERP (r1, r2, y1, y2, y_interp);
1171 gl = LERP (g1, g2, y1, y2, y_interp);
1172 bl = LERP (b1, b2, y1, y2, y_interp);
1176 xl = LERP (x2, x3, y2, y3, y_interp);
1178 rl = LERP (r2, r3, y2, y3, y_interp);
1179 gl = LERP (g2, g3, y2, y3, y_interp);
1180 bl = LERP (b2, b3, y2, y3, y_interp);
1183 xr = LERP (x1, x3, y1, y3, y_interp);
1185 rr = LERP (r1, r3, y1, y3, y_interp);
1186 gr = LERP (g1, g3, y1, y3, y_interp);
1187 br = LERP (b1, b3, y1, y3, y_interp);
1197 x_start = MAX (xl - PAD, x);
1198 x_end = MIN (xr + PAD, x + width);
1199 x_start = MIN (x_start, x_end);
1201 c = (rl << 16) | (gl << 8) | bl;
1203 for (xx = x; xx < x_start; xx++)
1206 for (; xx < x_end; xx++)
1208 x_interp = CLAMP (xx, xl, xr);
1210 *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
1211 (LERP (gl, gr, xl, xr, x_interp) << 8) |
1212 LERP (bl, br, xl, xr, x_interp));
1215 c = (rr << 16) | (gr << 8) | br;
1217 for (; xx < x + width; xx++)
1222 source = cairo_image_surface_create_for_data ((char *)buf,
1224 width, height, 4 * width);
1226 /* Draw a triangle with the image as a source */
1228 cairo_set_source_surface (cr, source, x, y);
1229 cairo_surface_destroy (source);
1231 cairo_move_to (cr, x1, y1);
1232 cairo_line_to (cr, x2, y2);
1233 cairo_line_to (cr, x3, y3);
1234 cairo_close_path (cr);
1239 /* Draw value marker */
1241 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1242 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1247 hsv_to_rgb (&r, &g, &b);
1249 if (INTENSITY (r, g, b) > 0.5)
1251 detail = "colorwheel_light";
1252 cairo_set_source_rgb (cr, 0., 0., 0.);
1256 detail = "colorwheel_dark";
1257 cairo_set_source_rgb (cr, 1., 1., 1.);
1261 #define FOCUS_RADIUS 6
1263 cairo_new_path (cr);
1264 cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
1267 /* Draw focus outline */
1269 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1270 !priv->focus_on_ring)
1275 gtk_widget_style_get (widget,
1276 "focus-line-width", &focus_width,
1277 "focus-padding", &focus_pad,
1280 gtk_paint_focus (widget->style, widget->window,
1281 GTK_WIDGET_STATE (widget),
1282 NULL, widget, detail,
1283 widget->allocation.x + xx - FOCUS_RADIUS - focus_width - focus_pad,
1284 widget->allocation.y + yy - FOCUS_RADIUS - focus_width - focus_pad,
1285 2 * (FOCUS_RADIUS + focus_width + focus_pad),
1286 2 * (FOCUS_RADIUS + focus_width + focus_pad));
1291 /* Paints the contents of the HSV color selector */
1300 paint_ring (hsv, cr, x, y, width, height);
1301 paint_triangle (hsv, cr, x, y, width, height);
1304 /* Expose_event handler for the HSV color selector */
1306 gtk_hsv_expose (GtkWidget *widget,
1307 GdkEventExpose *event)
1311 GdkRectangle rect, dest;
1314 hsv = GTK_HSV (widget);
1317 if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
1320 rect.x = widget->allocation.x;
1321 rect.y = widget->allocation.y;
1322 rect.width = widget->allocation.width;
1323 rect.height = widget->allocation.height;
1325 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1328 cr = gdk_cairo_create (widget->window);
1330 cairo_translate (cr, widget->allocation.x, widget->allocation.y);
1332 dest.x - widget->allocation.x,
1333 dest.y - widget->allocation.y,
1334 dest.width, dest.height);
1337 if (GTK_WIDGET_HAS_FOCUS (hsv) && priv->focus_on_ring)
1338 gtk_paint_focus (widget->style, widget->window,
1339 GTK_WIDGET_STATE (widget),
1340 &event->area, widget, NULL,
1341 widget->allocation.x,
1342 widget->allocation.y,
1343 widget->allocation.width,
1344 widget->allocation.height);
1350 gtk_hsv_focus (GtkWidget *widget,
1351 GtkDirectionType dir)
1356 hsv = GTK_HSV (widget);
1359 if (!GTK_WIDGET_HAS_FOCUS (hsv))
1361 if (dir == GTK_DIR_TAB_BACKWARD)
1362 priv->focus_on_ring = FALSE;
1364 priv->focus_on_ring = TRUE;
1366 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1373 if (priv->focus_on_ring)
1376 priv->focus_on_ring = TRUE;
1380 if (priv->focus_on_ring)
1381 priv->focus_on_ring = FALSE;
1387 case GTK_DIR_TAB_BACKWARD:
1388 if (priv->focus_on_ring)
1391 priv->focus_on_ring = TRUE;
1395 case GTK_DIR_TAB_FORWARD:
1396 if (priv->focus_on_ring)
1397 priv->focus_on_ring = FALSE;
1403 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1412 * Creates a new HSV color selector.
1414 * Return value: A newly-created HSV color selector.
1419 return g_object_new (GTK_TYPE_HSV, NULL);
1423 * gtk_hsv_set_color:
1424 * @hsv: An HSV color selector.
1429 * Sets the current color in an HSV color selector. Color component values must
1430 * be in the [0.0, 1.0] range.
1433 gtk_hsv_set_color (GtkHSV *hsv,
1440 g_return_if_fail (hsv != NULL);
1441 g_return_if_fail (GTK_IS_HSV (hsv));
1442 g_return_if_fail (h >= 0.0 && h <= 1.0);
1443 g_return_if_fail (s >= 0.0 && s <= 1.0);
1444 g_return_if_fail (v >= 0.0 && v <= 1.0);
1452 g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1454 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1458 * gtk_hsv_get_color:
1459 * @hsv: An HSV color selector.
1460 * @h: Return value for the hue.
1461 * @s: Return value for the saturation.
1462 * @v: Return value for the value.
1464 * Queries the current color in an HSV color selector. Returned values will be
1465 * in the [0.0, 1.0] range.
1468 gtk_hsv_get_color (GtkHSV *hsv, double *h, double *s, double *v)
1472 g_return_if_fail (GTK_IS_HSV (hsv));
1487 * gtk_hsv_set_metrics:
1488 * @hsv: An HSV color selector.
1489 * @size: Diameter for the hue ring.
1490 * @ring_width: Width of the hue ring.
1492 * Sets the size and ring width of an HSV color selector.
1495 gtk_hsv_set_metrics (GtkHSV *hsv,
1502 g_return_if_fail (GTK_IS_HSV (hsv));
1503 g_return_if_fail (size > 0);
1504 g_return_if_fail (ring_width > 0);
1505 g_return_if_fail (2 * ring_width + 1 <= size);
1509 same_size = (priv->size == size);
1512 priv->ring_width = ring_width;
1515 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1517 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1521 * gtk_hsv_get_metrics:
1522 * @hsv: An HSV color selector.
1523 * @size: Return value for the diameter of the hue ring.
1524 * @ring_width: Return value for the width of the hue ring.
1526 * Queries the size and ring width of an HSV color selector.
1529 gtk_hsv_get_metrics (GtkHSV *hsv,
1535 g_return_if_fail (GTK_IS_HSV (hsv));
1543 *ring_width = priv->ring_width;
1547 * gtk_hsv_is_adjusting:
1550 * An HSV color selector can be said to be adjusting if multiple rapid changes
1551 * are being made to its value, for example, when the user is adjusting the
1552 * value with the mouse. This function queries whether the HSV color selector
1553 * is being adjusted or not.
1555 * Return value: TRUE if clients can ignore changes to the color value, since
1556 * they may be transitory, or FALSE if they should consider the color value
1557 * status to be final.
1560 gtk_hsv_is_adjusting (GtkHSV *hsv)
1564 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1568 return priv->mode != DRAG_NONE;
1576 * @r: Return value for the red component.
1577 * @g: Return value for the green component.
1578 * @b: Return value for the blue component.
1580 * Converts a color from HSV space to RGB. Input values must be in the
1581 * [0.0, 1.0] range; output values will be in the same range.
1584 gtk_hsv_to_rgb (gdouble h,
1591 g_return_if_fail (h >= 0.0 && h <= 1.0);
1592 g_return_if_fail (s >= 0.0 && s <= 1.0);
1593 g_return_if_fail (v >= 0.0 && v <= 1.0);
1595 hsv_to_rgb (&h, &s, &v);
1612 * @h: Return value for the hue component.
1613 * @s: Return value for the saturation component.
1614 * @v: Return value for the value component.
1616 * Converts a color from RGB space to HSV. Input values must be in the
1617 * [0.0, 1.0] range; output values will be in the same range.
1620 gtk_rgb_to_hsv (gdouble r,
1627 g_return_if_fail (r >= 0.0 && r <= 1.0);
1628 g_return_if_fail (g >= 0.0 && g <= 1.0);
1629 g_return_if_fail (b >= 0.0 && b <= 1.0);
1631 rgb_to_hsv (&r, &g, &b);
1644 gtk_hsv_move (GtkHSV *hsv,
1645 GtkDirectionType dir)
1648 gdouble hue, sat, val;
1649 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1650 gint x, y; /* position in triangle */
1658 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1660 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1661 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1663 #define HUE_DELTA 0.002
1667 if (priv->focus_on_ring)
1672 compute_sv (hsv, x, y, &sat, &val);
1677 if (priv->focus_on_ring)
1682 compute_sv (hsv, x, y, &sat, &val);
1687 if (priv->focus_on_ring)
1692 compute_sv (hsv, x, y, &sat, &val);
1697 if (priv->focus_on_ring)
1703 compute_sv (hsv, x, y, &sat, &val);
1708 /* we don't care about the tab directions */
1718 gtk_hsv_set_color (hsv, hue, sat, val);
1721 #define __GTK_HSV_C__
1722 #include "gtkaliasdef.c"