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"
45 /* Default width/height */
46 #define DEFAULT_SIZE 100
48 /* Default ring width */
49 #define DEFAULT_RING_WIDTH 10
59 /* Private part of the GtkHSV structure */
66 /* Size and ring width */
70 /* Window for capturing events */
79 guint focus_on_ring : 1;
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];
119 G_DEFINE_TYPE (GtkHSV, gtk_hsv, GTK_TYPE_WIDGET)
121 /* Class initialization function for the HSV color selector */
123 gtk_hsv_class_init (GtkHSVClass *class)
125 GObjectClass *gobject_class;
126 GtkObjectClass *object_class;
127 GtkWidgetClass *widget_class;
128 GtkHSVClass *hsv_class;
129 GtkBindingSet *binding_set;
131 gobject_class = (GObjectClass *) class;
132 object_class = (GtkObjectClass *) class;
133 widget_class = (GtkWidgetClass *) class;
134 hsv_class = GTK_HSV_CLASS (class);
136 object_class->destroy = gtk_hsv_destroy;
138 widget_class->map = gtk_hsv_map;
139 widget_class->unmap = gtk_hsv_unmap;
140 widget_class->realize = gtk_hsv_realize;
141 widget_class->unrealize = gtk_hsv_unrealize;
142 widget_class->size_request = gtk_hsv_size_request;
143 widget_class->size_allocate = gtk_hsv_size_allocate;
144 widget_class->button_press_event = gtk_hsv_button_press;
145 widget_class->button_release_event = gtk_hsv_button_release;
146 widget_class->motion_notify_event = gtk_hsv_motion;
147 widget_class->expose_event = gtk_hsv_expose;
148 widget_class->focus = gtk_hsv_focus;
149 widget_class->grab_broken_event = gtk_hsv_grab_broken;
151 hsv_class->move = gtk_hsv_move;
153 hsv_signals[CHANGED] =
154 g_signal_new (I_("changed"),
155 G_OBJECT_CLASS_TYPE (object_class),
157 G_STRUCT_OFFSET (GtkHSVClass, changed),
159 _gtk_marshal_VOID__VOID,
163 g_signal_new (I_("move"),
164 G_OBJECT_CLASS_TYPE (object_class),
165 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
166 G_STRUCT_OFFSET (GtkHSVClass, move),
168 _gtk_marshal_VOID__ENUM,
170 GTK_TYPE_DIRECTION_TYPE);
172 binding_set = gtk_binding_set_by_class (class);
174 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
176 G_TYPE_ENUM, GTK_DIR_UP);
177 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
179 G_TYPE_ENUM, GTK_DIR_UP);
181 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
183 G_TYPE_ENUM, GTK_DIR_DOWN);
184 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
186 G_TYPE_ENUM, GTK_DIR_DOWN);
189 gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
191 G_TYPE_ENUM, GTK_DIR_RIGHT);
192 gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
194 G_TYPE_ENUM, GTK_DIR_RIGHT);
196 gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
198 G_TYPE_ENUM, GTK_DIR_LEFT);
199 gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
201 G_TYPE_ENUM, GTK_DIR_LEFT);
203 g_type_class_add_private (gobject_class, sizeof (HSVPrivate));
206 /* Object initialization function for the HSV color selector */
208 gtk_hsv_init (GtkHSV *hsv)
212 priv = G_TYPE_INSTANCE_GET_PRIVATE (hsv, GTK_TYPE_HSV, HSVPrivate);
216 GTK_WIDGET_SET_FLAGS (hsv, GTK_NO_WINDOW);
217 GTK_WIDGET_SET_FLAGS (hsv, GTK_CAN_FOCUS);
223 priv->size = DEFAULT_SIZE;
224 priv->ring_width = DEFAULT_RING_WIDTH;
227 /* Destroy handler for the HSV color selector */
229 gtk_hsv_destroy (GtkObject *object)
231 GTK_OBJECT_CLASS (gtk_hsv_parent_class)->destroy (object);
234 /* Default signal handlers */
237 /* Map handler for the HSV color selector */
240 gtk_hsv_map (GtkWidget *widget)
245 hsv = GTK_HSV (widget);
248 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->map (widget);
250 gdk_window_show (priv->window);
253 /* Unmap handler for the HSV color selector */
256 gtk_hsv_unmap (GtkWidget *widget)
261 hsv = GTK_HSV (widget);
264 gdk_window_hide (priv->window);
266 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unmap (widget);
269 /* Realize handler for the HSV color selector */
271 gtk_hsv_realize (GtkWidget *widget)
277 GdkWindow *parent_window;
279 hsv = GTK_HSV (widget);
282 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
286 attr.window_type = GDK_WINDOW_CHILD;
287 attr.x = widget->allocation.x;
288 attr.y = widget->allocation.y;
289 attr.width = widget->allocation.width;
290 attr.height = widget->allocation.height;
291 attr.wclass = GDK_INPUT_ONLY;
292 attr.event_mask = gtk_widget_get_events (widget);
293 attr.event_mask |= (GDK_KEY_PRESS_MASK
294 | GDK_BUTTON_PRESS_MASK
295 | GDK_BUTTON_RELEASE_MASK
296 | GDK_POINTER_MOTION_MASK
297 | GDK_ENTER_NOTIFY_MASK
298 | GDK_LEAVE_NOTIFY_MASK);
300 attr_mask = GDK_WA_X | GDK_WA_Y;
302 parent_window = gtk_widget_get_parent_window (widget);
304 widget->window = parent_window;
305 g_object_ref (widget->window);
307 priv->window = gdk_window_new (parent_window, &attr, attr_mask);
308 gdk_window_set_user_data (priv->window, hsv);
310 widget->style = gtk_style_attach (widget->style, widget->window);
314 priv->gc = gdk_gc_new (parent_window);
317 /* Unrealize handler for the HSV color selector */
319 gtk_hsv_unrealize (GtkWidget *widget)
324 hsv = GTK_HSV (widget);
327 gdk_window_set_user_data (priv->window, NULL);
328 gdk_window_destroy (priv->window);
331 g_object_unref (priv->gc);
334 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unrealize (widget);
337 /* Size_request handler for the HSV color selector */
339 gtk_hsv_size_request (GtkWidget *widget,
340 GtkRequisition *requisition)
342 GtkHSV *hsv = GTK_HSV (widget);
343 HSVPrivate *priv = hsv->priv;
347 gtk_widget_style_get (widget,
348 "focus-line-width", &focus_width,
349 "focus-padding", &focus_pad,
352 requisition->width = priv->size + 2 * (focus_width + focus_pad);
353 requisition->height = priv->size + 2 * (focus_width + focus_pad);
356 /* Size_allocate handler for the HSV color selector */
358 gtk_hsv_size_allocate (GtkWidget *widget,
359 GtkAllocation *allocation)
364 hsv = GTK_HSV (widget);
367 widget->allocation = *allocation;
369 if (GTK_WIDGET_REALIZED (widget))
370 gdk_window_move_resize (priv->window,
378 /* Utility functions */
380 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
382 /* Converts from HSV to RGB */
384 hsv_to_rgb (gdouble *h,
388 gdouble hue, saturation, value;
407 p = value * (1.0 - saturation);
408 q = value * (1.0 - saturation * f);
409 t = value * (1.0 - saturation * (1.0 - f));
450 g_assert_not_reached ();
455 /* Converts from RGB to HSV */
457 rgb_to_hsv (gdouble *r,
461 gdouble red, green, blue;
500 s = (max - min) / max;
511 h = (green - blue) / delta;
512 else if (green == max)
513 h = 2 + (blue - red) / delta;
514 else if (blue == max)
515 h = 4 + (red - green) / delta;
530 /* Computes the vertices of the saturation/value triangle */
532 compute_triangle (GtkHSV *hsv,
543 gdouble inner, outer;
548 center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
549 center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
550 outer = priv->size / 2.0;
551 inner = outer - priv->ring_width;
552 angle = priv->h * 2.0 * G_PI;
554 *hx = floor (center_x + cos (angle) * inner + 0.5);
555 *hy = floor (center_y - sin (angle) * inner + 0.5);
556 *sx = floor (center_x + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
557 *sy = floor (center_y - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
558 *vx = floor (center_x + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
559 *vy = floor (center_y - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
562 /* Computes whether a point is inside the hue ring */
564 is_in_ring (GtkHSV *hsv,
569 gdouble dx, dy, dist;
572 gdouble inner, outer;
576 center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
577 center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
578 outer = priv->size / 2.0;
579 inner = outer - priv->ring_width;
583 dist = dx * dx + dy * dy;
585 return (dist >= inner * inner && dist <= outer * outer);
588 /* Computes a saturation/value pair based on the mouse coordinates */
590 compute_sv (GtkHSV *hsv,
596 int ihx, ihy, isx, isy, ivx, ivy;
597 double hx, hy, sx, sy, vx, vy;
601 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
602 center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
603 center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
613 if (vx * (x - sx) + vy * (y - sy) < 0.0)
616 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
617 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
624 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
627 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
628 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
635 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
638 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
639 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
648 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
649 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
661 if (fabs (hy - vy) < fabs (hx - vx))
662 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
664 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
674 /* Computes whether a point is inside the saturation/value triangle */
676 is_in_triangle (GtkHSV *hsv,
680 int hx, hy, sx, sy, vx, vy;
683 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
685 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
687 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
688 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
690 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
693 /* Computes a value based on the mouse coordinates */
695 compute_v (GtkHSV *hsv,
704 center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
705 center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
709 angle = atan2 (dy, dx);
713 return angle / (2.0 * G_PI);
719 set_cross_grab (GtkHSV *hsv,
727 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
729 gdk_pointer_grab (priv->window, FALSE,
730 (GDK_POINTER_MOTION_MASK
731 | GDK_POINTER_MOTION_HINT_MASK
732 | GDK_BUTTON_RELEASE_MASK),
736 gdk_cursor_unref (cursor);
740 gtk_hsv_grab_broken (GtkWidget *widget,
741 GdkEventGrabBroken *event)
743 GtkHSV *hsv = GTK_HSV (widget);
748 priv->mode = DRAG_NONE;
753 /* Button_press_event handler for the HSV color selector */
755 gtk_hsv_button_press (GtkWidget *widget,
756 GdkEventButton *event)
762 hsv = GTK_HSV (widget);
765 if (priv->mode != DRAG_NONE || event->button != 1)
771 if (is_in_ring (hsv, x, y))
774 set_cross_grab (hsv, event->time);
776 gtk_hsv_set_color (hsv,
777 compute_v (hsv, x, y),
781 gtk_widget_grab_focus (widget);
782 priv->focus_on_ring = TRUE;
787 if (is_in_triangle (hsv, x, y))
791 priv->mode = DRAG_SV;
792 set_cross_grab (hsv, event->time);
794 compute_sv (hsv, x, y, &s, &v);
795 gtk_hsv_set_color (hsv, priv->h, s, v);
797 gtk_widget_grab_focus (widget);
798 priv->focus_on_ring = FALSE;
806 /* Button_release_event handler for the HSV color selector */
808 gtk_hsv_button_release (GtkWidget *widget,
809 GdkEventButton *event)
816 hsv = GTK_HSV (widget);
819 if (priv->mode == DRAG_NONE || event->button != 1)
822 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
823 * can see that this is the final color state.
827 priv->mode = DRAG_NONE;
833 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
834 else if (mode == DRAG_SV) {
837 compute_sv (hsv, x, y, &s, &v);
838 gtk_hsv_set_color (hsv, priv->h, s, v);
840 g_assert_not_reached ();
842 gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
847 /* Motion_notify_event handler for the HSV color selector */
849 gtk_hsv_motion (GtkWidget *widget,
850 GdkEventMotion *event)
855 GdkModifierType mods;
857 hsv = GTK_HSV (widget);
860 if (priv->mode == DRAG_NONE)
863 gdk_event_request_motions (event);
868 if (priv->mode == DRAG_H)
870 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
873 else if (priv->mode == DRAG_SV)
877 compute_sv (hsv, x, y, &s, &v);
878 gtk_hsv_set_color (hsv, priv->h, s, v);
882 g_assert_not_reached ();
889 /* Paints the hue ring */
891 paint_ring (GtkHSV *hsv,
898 GtkWidget *widget = GTK_WIDGET (hsv);
901 gdouble dx, dy, dist;
904 gdouble inner, outer;
909 cairo_surface_t *source;
915 gtk_widget_style_get (widget,
916 "focus-line-width", &focus_width,
917 "focus-padding", &focus_pad,
922 center_x = widget->allocation.width / 2.0;
923 center_y = widget->allocation.height / 2.0;
925 outer = priv->size / 2.0;
926 inner = outer - priv->ring_width;
928 /* Create an image initialized with the ring colors */
930 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
931 buf = g_new (guint32, height * stride / 4);
933 for (yy = 0; yy < height; yy++)
935 p = buf + yy * width;
937 dy = -(yy + y - center_y);
939 for (xx = 0; xx < width; xx++)
941 dx = xx + x - center_x;
943 dist = dx * dx + dy * dy;
944 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
950 angle = atan2 (dy, dx);
954 hue = angle / (2.0 * G_PI);
959 hsv_to_rgb (&r, &g, &b);
961 *p++ = (((int)floor (r * 255 + 0.5) << 16) |
962 ((int)floor (g * 255 + 0.5) << 8) |
963 (int)floor (b * 255 + 0.5));
967 source = cairo_image_surface_create_for_data ((unsigned char *)buf,
969 width, height, stride);
971 /* Now draw the value marker onto the source image, so that it
972 * will get properly clipped at the edges of the ring
974 source_cr = cairo_create (source);
979 hsv_to_rgb (&r, &g, &b);
981 if (INTENSITY (r, g, b) > 0.5)
982 cairo_set_source_rgb (source_cr, 0., 0., 0.);
984 cairo_set_source_rgb (source_cr, 1., 1., 1.);
986 cairo_move_to (source_cr, -x + center_x, - y + center_y);
987 cairo_line_to (source_cr,
988 -x + center_x + cos (priv->h * 2.0 * G_PI) * priv->size / 2,
989 -y + center_y - sin (priv->h * 2.0 * G_PI) * priv->size / 2);
990 cairo_stroke (source_cr);
991 cairo_destroy (source_cr);
993 /* Draw the ring using the source image */
997 cairo_set_source_surface (cr, source, x, y);
998 cairo_surface_destroy (source);
1000 cairo_set_line_width (cr, priv->ring_width);
1001 cairo_new_path (cr);
1004 priv->size / 2. - priv->ring_width / 2.,
1013 /* Converts an HSV triplet to an integer RGB triplet */
1015 get_color (gdouble h,
1022 hsv_to_rgb (&h, &s, &v);
1024 *r = floor (h * 255 + 0.5);
1025 *g = floor (s * 255 + 0.5);
1026 *b = floor (v * 255 + 0.5);
1029 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1031 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
1032 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
1035 /* Number of pixels we extend out from the edges when creating
1036 * color source to avoid artifacts
1040 /* Paints the HSV triangle */
1042 paint_triangle (GtkHSV *hsv,
1049 GtkWidget *widget = GTK_WIDGET (hsv);
1051 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1052 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1053 gint x2, y2, r2, g2, b2; /* Second vertex */
1054 gint x3, y3, r3, g3, b3; /* Third vertex */
1056 guint32 *buf, *p, c;
1057 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1059 gint x_interp, y_interp;
1060 gint x_start, x_end;
1061 cairo_surface_t *source;
1068 /* Compute triangle's vertices */
1070 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1074 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1078 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1082 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1111 /* Shade the triangle */
1113 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
1114 buf = g_new (guint32, height * stride / 4);
1116 for (yy = 0; yy < height; yy++)
1118 p = buf + yy * width;
1120 if (yy + y >= y1 - PAD && yy + y < y3 + PAD) {
1121 y_interp = CLAMP (yy + y, y1, y3);
1125 xl = LERP (x1, x2, y1, y2, y_interp);
1127 rl = LERP (r1, r2, y1, y2, y_interp);
1128 gl = LERP (g1, g2, y1, y2, y_interp);
1129 bl = LERP (b1, b2, y1, y2, y_interp);
1133 xl = LERP (x2, x3, y2, y3, y_interp);
1135 rl = LERP (r2, r3, y2, y3, y_interp);
1136 gl = LERP (g2, g3, y2, y3, y_interp);
1137 bl = LERP (b2, b3, y2, y3, y_interp);
1140 xr = LERP (x1, x3, y1, y3, y_interp);
1142 rr = LERP (r1, r3, y1, y3, y_interp);
1143 gr = LERP (g1, g3, y1, y3, y_interp);
1144 br = LERP (b1, b3, y1, y3, y_interp);
1154 x_start = MAX (xl - PAD, x);
1155 x_end = MIN (xr + PAD, x + width);
1156 x_start = MIN (x_start, x_end);
1158 c = (rl << 16) | (gl << 8) | bl;
1160 for (xx = x; xx < x_start; xx++)
1163 for (; xx < x_end; xx++)
1165 x_interp = CLAMP (xx, xl, xr);
1167 *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
1168 (LERP (gl, gr, xl, xr, x_interp) << 8) |
1169 LERP (bl, br, xl, xr, x_interp));
1172 c = (rr << 16) | (gr << 8) | br;
1174 for (; xx < x + width; xx++)
1179 source = cairo_image_surface_create_for_data ((unsigned char *)buf,
1181 width, height, stride);
1183 /* Draw a triangle with the image as a source */
1185 cairo_set_source_surface (cr, source, x, y);
1186 cairo_surface_destroy (source);
1188 cairo_move_to (cr, x1, y1);
1189 cairo_line_to (cr, x2, y2);
1190 cairo_line_to (cr, x3, y3);
1191 cairo_close_path (cr);
1196 /* Draw value marker */
1198 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1199 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1204 hsv_to_rgb (&r, &g, &b);
1206 if (INTENSITY (r, g, b) > 0.5)
1208 detail = "colorwheel_light";
1209 cairo_set_source_rgb (cr, 0., 0., 0.);
1213 detail = "colorwheel_dark";
1214 cairo_set_source_rgb (cr, 1., 1., 1.);
1218 #define FOCUS_RADIUS 6
1220 cairo_new_path (cr);
1221 cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
1224 /* Draw focus outline */
1226 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1227 !priv->focus_on_ring)
1232 gtk_widget_style_get (widget,
1233 "focus-line-width", &focus_width,
1234 "focus-padding", &focus_pad,
1237 gtk_paint_focus (widget->style, widget->window,
1238 GTK_WIDGET_STATE (widget),
1239 NULL, widget, detail,
1240 widget->allocation.x + xx - FOCUS_RADIUS - focus_width - focus_pad,
1241 widget->allocation.y + yy - FOCUS_RADIUS - focus_width - focus_pad,
1242 2 * (FOCUS_RADIUS + focus_width + focus_pad),
1243 2 * (FOCUS_RADIUS + focus_width + focus_pad));
1248 /* Paints the contents of the HSV color selector */
1257 paint_ring (hsv, cr, x, y, width, height);
1258 paint_triangle (hsv, cr, x, y, width, height);
1261 /* Expose_event handler for the HSV color selector */
1263 gtk_hsv_expose (GtkWidget *widget,
1264 GdkEventExpose *event)
1268 GdkRectangle rect, dest;
1271 hsv = GTK_HSV (widget);
1274 if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
1277 rect.x = widget->allocation.x;
1278 rect.y = widget->allocation.y;
1279 rect.width = widget->allocation.width;
1280 rect.height = widget->allocation.height;
1282 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1285 cr = gdk_cairo_create (widget->window);
1287 cairo_translate (cr, widget->allocation.x, widget->allocation.y);
1289 dest.x - widget->allocation.x,
1290 dest.y - widget->allocation.y,
1291 dest.width, dest.height);
1294 if (GTK_WIDGET_HAS_FOCUS (hsv) && priv->focus_on_ring)
1295 gtk_paint_focus (widget->style, widget->window,
1296 GTK_WIDGET_STATE (widget),
1297 &event->area, widget, NULL,
1298 widget->allocation.x,
1299 widget->allocation.y,
1300 widget->allocation.width,
1301 widget->allocation.height);
1307 gtk_hsv_focus (GtkWidget *widget,
1308 GtkDirectionType dir)
1313 hsv = GTK_HSV (widget);
1316 if (!GTK_WIDGET_HAS_FOCUS (hsv))
1318 if (dir == GTK_DIR_TAB_BACKWARD)
1319 priv->focus_on_ring = FALSE;
1321 priv->focus_on_ring = TRUE;
1323 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1330 if (priv->focus_on_ring)
1333 priv->focus_on_ring = TRUE;
1337 if (priv->focus_on_ring)
1338 priv->focus_on_ring = FALSE;
1344 case GTK_DIR_TAB_BACKWARD:
1345 if (priv->focus_on_ring)
1348 priv->focus_on_ring = TRUE;
1352 case GTK_DIR_TAB_FORWARD:
1353 if (priv->focus_on_ring)
1354 priv->focus_on_ring = FALSE;
1360 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1368 * Creates a new HSV color selector.
1370 * Return value: A newly-created HSV color selector.
1377 return g_object_new (GTK_TYPE_HSV, NULL);
1381 * gtk_hsv_set_color:
1382 * @hsv: An HSV color selector
1387 * Sets the current color in an HSV color selector.
1388 * Color component values must be in the [0.0, 1.0] range.
1393 gtk_hsv_set_color (GtkHSV *hsv,
1400 g_return_if_fail (GTK_IS_HSV (hsv));
1401 g_return_if_fail (h >= 0.0 && h <= 1.0);
1402 g_return_if_fail (s >= 0.0 && s <= 1.0);
1403 g_return_if_fail (v >= 0.0 && v <= 1.0);
1411 g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1413 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1417 * gtk_hsv_get_color:
1418 * @hsv: An HSV color selector
1419 * @h: Return value for the hue
1420 * @s: Return value for the saturation
1421 * @v: Return value for the value
1423 * Queries the current color in an HSV color selector.
1424 * Returned values will be in the [0.0, 1.0] range.
1429 gtk_hsv_get_color (GtkHSV *hsv,
1436 g_return_if_fail (GTK_IS_HSV (hsv));
1451 * gtk_hsv_set_metrics:
1452 * @hsv: An HSV color selector
1453 * @size: Diameter for the hue ring
1454 * @ring_width: Width of the hue ring
1456 * Sets the size and ring width of an HSV color selector.
1461 gtk_hsv_set_metrics (GtkHSV *hsv,
1468 g_return_if_fail (GTK_IS_HSV (hsv));
1469 g_return_if_fail (size > 0);
1470 g_return_if_fail (ring_width > 0);
1471 g_return_if_fail (2 * ring_width + 1 <= size);
1475 same_size = (priv->size == size);
1478 priv->ring_width = ring_width;
1481 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1483 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1487 * gtk_hsv_get_metrics:
1488 * @hsv: An HSV color selector
1489 * @size: Return value for the diameter of the hue ring
1490 * @ring_width: Return value for the width of the hue ring
1492 * Queries the size and ring width of an HSV color selector.
1497 gtk_hsv_get_metrics (GtkHSV *hsv,
1503 g_return_if_fail (GTK_IS_HSV (hsv));
1511 *ring_width = priv->ring_width;
1515 * gtk_hsv_is_adjusting:
1518 * An HSV color selector can be said to be adjusting if multiple rapid
1519 * changes are being made to its value, for example, when the user is
1520 * adjusting the value with the mouse. This function queries whether
1521 * the HSV color selector is being adjusted or not.
1523 * Return value: %TRUE if clients can ignore changes to the color value,
1524 * since they may be transitory, or %FALSE if they should consider
1525 * the color value status to be final.
1530 gtk_hsv_is_adjusting (GtkHSV *hsv)
1534 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1538 return priv->mode != DRAG_NONE;
1546 * @r: Return value for the red component
1547 * @g: Return value for the green component
1548 * @b: Return value for the blue component
1550 * Converts a color from HSV space to RGB.
1551 * Input values must be in the [0.0, 1.0] range;
1552 * output values will be in the same range.
1557 gtk_hsv_to_rgb (gdouble h,
1564 g_return_if_fail (h >= 0.0 && h <= 1.0);
1565 g_return_if_fail (s >= 0.0 && s <= 1.0);
1566 g_return_if_fail (v >= 0.0 && v <= 1.0);
1568 hsv_to_rgb (&h, &s, &v);
1585 * @h: Return value for the hue component
1586 * @s: Return value for the saturation component
1587 * @v: Return value for the value component
1589 * Converts a color from RGB space to HSV.
1590 * Input values must be in the [0.0, 1.0] range;
1591 * output values will be in the same range.
1596 gtk_rgb_to_hsv (gdouble r,
1603 g_return_if_fail (r >= 0.0 && r <= 1.0);
1604 g_return_if_fail (g >= 0.0 && g <= 1.0);
1605 g_return_if_fail (b >= 0.0 && b <= 1.0);
1607 rgb_to_hsv (&r, &g, &b);
1620 gtk_hsv_move (GtkHSV *hsv,
1621 GtkDirectionType dir)
1624 gdouble hue, sat, val;
1625 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1626 gint x, y; /* position in triangle */
1634 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1636 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1637 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1639 #define HUE_DELTA 0.002
1643 if (priv->focus_on_ring)
1648 compute_sv (hsv, x, y, &sat, &val);
1653 if (priv->focus_on_ring)
1658 compute_sv (hsv, x, y, &sat, &val);
1663 if (priv->focus_on_ring)
1668 compute_sv (hsv, x, y, &sat, &val);
1673 if (priv->focus_on_ring)
1679 compute_sv (hsv, x, y, &sat, &val);
1684 /* we don't care about the tab directions */
1694 gtk_hsv_set_color (hsv, hue, sat, val);
1697 #define __GTK_HSV_C__
1698 #include "gtkaliasdef.c"