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.
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkbindings.h"
32 #include "gtkcontainer.h"
33 #include "gtkmarshalers.h"
36 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
37 * file for a list of people on the GTK+ Team. See the ChangeLog
38 * files for a list of changes. These files are distributed with
39 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
42 /* Default width/height */
43 #define DEFAULT_SIZE 100
45 /* Default ring width */
46 #define DEFAULT_RING_WIDTH 10
56 /* Private part of the GtkHSV structure */
63 /* Size and ring width */
67 /* Window for capturing events */
76 guint focus_on_ring : 1;
90 static void gtk_hsv_class_init (GtkHSVClass *class);
91 static void gtk_hsv_init (GtkHSV *hsv);
92 static void gtk_hsv_destroy (GtkObject *object);
93 static void gtk_hsv_map (GtkWidget *widget);
94 static void gtk_hsv_unmap (GtkWidget *widget);
95 static void gtk_hsv_realize (GtkWidget *widget);
96 static void gtk_hsv_unrealize (GtkWidget *widget);
97 static void gtk_hsv_size_request (GtkWidget *widget,
98 GtkRequisition *requisition);
99 static void gtk_hsv_size_allocate (GtkWidget *widget,
100 GtkAllocation *allocation);
101 static gint gtk_hsv_button_press (GtkWidget *widget,
102 GdkEventButton *event);
103 static gint gtk_hsv_button_release (GtkWidget *widget,
104 GdkEventButton *event);
105 static gint gtk_hsv_motion (GtkWidget *widget,
106 GdkEventMotion *event);
107 static gint gtk_hsv_expose (GtkWidget *widget,
108 GdkEventExpose *event);
109 static gboolean gtk_hsv_focus (GtkWidget *widget,
110 GtkDirectionType direction);
111 static void gtk_hsv_move (GtkHSV *hsv,
112 GtkDirectionType dir);
114 static guint hsv_signals[LAST_SIGNAL];
115 static GtkWidgetClass *parent_class;
122 * Registers the &GtkHSV class if necessary, and returns the type ID associated
125 * Return value: The type ID of the &GtkHSV class.
128 gtk_hsv_get_type (void)
130 static GType hsv_type = 0;
133 static const GTypeInfo hsv_info = {
134 sizeof (GtkHSVClass),
135 NULL, /* base_init */
136 NULL, /* base_finalize */
137 (GClassInitFunc) gtk_hsv_class_init,
138 NULL, /* class_finalize */
139 NULL, /* class_data */
142 (GInstanceInitFunc) gtk_hsv_init,
145 hsv_type = g_type_register_static (GTK_TYPE_WIDGET, "GtkHSV",
152 /* Class initialization function for the HSV color selector */
154 gtk_hsv_class_init (GtkHSVClass *class)
156 GtkObjectClass *object_class;
157 GtkWidgetClass *widget_class;
158 GtkHSVClass *hsv_class;
159 GtkBindingSet *binding_set;
161 object_class = (GtkObjectClass *) class;
162 widget_class = (GtkWidgetClass *) class;
163 hsv_class = GTK_HSV_CLASS (class);
165 parent_class = g_type_class_peek_parent (class);
167 object_class->destroy = gtk_hsv_destroy;
169 widget_class->map = gtk_hsv_map;
170 widget_class->unmap = gtk_hsv_unmap;
171 widget_class->realize = gtk_hsv_realize;
172 widget_class->unrealize = gtk_hsv_unrealize;
173 widget_class->size_request = gtk_hsv_size_request;
174 widget_class->size_allocate = gtk_hsv_size_allocate;
175 widget_class->button_press_event = gtk_hsv_button_press;
176 widget_class->button_release_event = gtk_hsv_button_release;
177 widget_class->motion_notify_event = gtk_hsv_motion;
178 widget_class->expose_event = gtk_hsv_expose;
179 widget_class->focus = gtk_hsv_focus;
181 hsv_class->move = gtk_hsv_move;
183 hsv_signals[CHANGED] =
184 g_signal_new ("changed",
185 G_OBJECT_CLASS_TYPE (object_class),
187 G_STRUCT_OFFSET (GtkHSVClass, changed),
189 _gtk_marshal_VOID__VOID,
193 g_signal_new ("move",
194 G_OBJECT_CLASS_TYPE (object_class),
195 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
196 G_STRUCT_OFFSET (GtkHSVClass, move),
198 _gtk_marshal_VOID__ENUM,
200 GTK_TYPE_DIRECTION_TYPE);
202 binding_set = gtk_binding_set_by_class (class);
204 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
206 G_TYPE_ENUM, GTK_DIR_UP);
207 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
209 G_TYPE_ENUM, GTK_DIR_UP);
211 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
213 G_TYPE_ENUM, GTK_DIR_DOWN);
214 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
216 G_TYPE_ENUM, GTK_DIR_DOWN);
219 gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
221 G_TYPE_ENUM, GTK_DIR_RIGHT);
222 gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
224 G_TYPE_ENUM, GTK_DIR_RIGHT);
226 gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
228 G_TYPE_ENUM, GTK_DIR_LEFT);
229 gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
231 G_TYPE_ENUM, GTK_DIR_LEFT);
234 /* Object initialization function for the HSV color selector */
236 gtk_hsv_init (GtkHSV *hsv)
240 priv = g_new0 (HSVPrivate, 1);
243 GTK_WIDGET_SET_FLAGS (hsv, GTK_NO_WINDOW);
244 GTK_WIDGET_SET_FLAGS (hsv, GTK_CAN_FOCUS);
250 priv->size = DEFAULT_SIZE;
251 priv->ring_width = DEFAULT_RING_WIDTH;
254 /* Destroy handler for the HSV color selector */
256 gtk_hsv_destroy (GtkObject *object)
260 g_return_if_fail (GTK_IS_HSV (object));
262 hsv = GTK_HSV (object);
270 GTK_OBJECT_CLASS (parent_class)->destroy (object);
273 /* Default signal handlers */
276 /* Map handler for the HSV color selector */
279 gtk_hsv_map (GtkWidget *widget)
284 hsv = GTK_HSV (widget);
287 GTK_WIDGET_CLASS (parent_class)->map (widget);
289 gdk_window_show (priv->window);
292 /* Unmap handler for the HSV color selector */
295 gtk_hsv_unmap (GtkWidget *widget)
300 hsv = GTK_HSV (widget);
303 gdk_window_hide (priv->window);
305 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
308 /* Realize handler for the HSV color selector */
310 gtk_hsv_realize (GtkWidget *widget)
316 GdkWindow *parent_window;
318 hsv = GTK_HSV (widget);
321 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
325 attr.window_type = GDK_WINDOW_CHILD;
326 attr.x = widget->allocation.x;
327 attr.y = widget->allocation.y;
328 attr.width = widget->allocation.width;
329 attr.height = widget->allocation.height;
330 attr.wclass = GDK_INPUT_ONLY;
331 attr.event_mask = gtk_widget_get_events (widget);
332 attr.event_mask |= (GDK_KEY_PRESS_MASK
333 | GDK_BUTTON_PRESS_MASK
334 | GDK_BUTTON_RELEASE_MASK
335 | GDK_POINTER_MOTION_MASK
336 | GDK_ENTER_NOTIFY_MASK
337 | GDK_LEAVE_NOTIFY_MASK);
339 attr_mask = GDK_WA_X | GDK_WA_Y;
341 parent_window = gtk_widget_get_parent_window (widget);
343 widget->window = parent_window;
344 g_object_ref (widget->window);
346 priv->window = gdk_window_new (parent_window, &attr, attr_mask);
347 gdk_window_set_user_data (priv->window, hsv);
349 widget->style = gtk_style_attach (widget->style, widget->window);
353 priv->gc = gdk_gc_new (parent_window);
356 /* Unrealize handler for the HSV color selector */
358 gtk_hsv_unrealize (GtkWidget *widget)
363 hsv = GTK_HSV (widget);
366 gdk_window_set_user_data (priv->window, NULL);
367 gdk_window_destroy (priv->window);
370 g_object_unref (priv->gc);
373 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
374 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
377 /* Size_request handler for the HSV color selector */
379 gtk_hsv_size_request (GtkWidget *widget,
380 GtkRequisition *requisition)
382 GtkHSV *hsv = GTK_HSV (widget);
383 HSVPrivate *priv = hsv->priv;
387 gtk_widget_style_get (widget,
388 "focus-line-width", &focus_width,
389 "focus-padding", &focus_pad,
392 requisition->width = priv->size + 2 * (focus_width + focus_pad);
393 requisition->height = priv->size + 2 * (focus_width + focus_pad);
396 /* Size_allocate handler for the HSV color selector */
398 gtk_hsv_size_allocate (GtkWidget *widget,
399 GtkAllocation *allocation)
404 hsv = GTK_HSV (widget);
407 widget->allocation = *allocation;
409 if (GTK_WIDGET_REALIZED (widget))
410 gdk_window_move_resize (priv->window,
418 /* Utility functions */
420 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
422 /* Converts from HSV to RGB */
424 hsv_to_rgb (gdouble *h,
428 gdouble hue, saturation, value;
447 p = value * (1.0 - saturation);
448 q = value * (1.0 - saturation * f);
449 t = value * (1.0 - saturation * (1.0 - f));
490 g_assert_not_reached ();
495 /* Converts from RGB to HSV */
497 rgb_to_hsv (gdouble *r,
501 gdouble red, green, blue;
540 s = (max - min) / max;
551 h = (green - blue) / delta;
552 else if (green == max)
553 h = 2 + (blue - red) / delta;
554 else if (blue == max)
555 h = 4 + (red - green) / delta;
570 /* Computes the vertices of the saturation/value triangle */
572 compute_triangle (GtkHSV *hsv,
582 gdouble inner, outer;
587 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
588 outer = priv->size / 2.0;
589 inner = outer - priv->ring_width;
590 angle = priv->h * 2.0 * G_PI;
592 *hx = floor (center + cos (angle) * inner + 0.5);
593 *hy = floor (center - sin (angle) * inner + 0.5);
594 *sx = floor (center + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
595 *sy = floor (center - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
596 *vx = floor (center + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
597 *vy = floor (center - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
600 /* Computes whether a point is inside the hue ring */
602 is_in_ring (GtkHSV *hsv,
607 gdouble dx, dy, dist;
608 gdouble center, inner, outer;
612 center = priv->size / 2.0;
613 outer = priv->size / 2.0;
614 inner = outer - priv->ring_width;
618 dist = dx * dx + dy * dy;
620 return (dist >= inner * inner && dist <= outer * outer);
623 /* Computes a saturation/value pair based on the mouse coordinates */
625 compute_sv (GtkHSV *hsv,
632 int ihx, ihy, isx, isy, ivx, ivy;
633 double hx, hy, sx, sy, vx, vy;
638 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
639 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
649 if (vx * (x - sx) + vy * (y - sy) < 0.0)
652 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
653 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
660 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
663 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
664 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
671 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
674 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
675 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
684 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
685 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
697 if (fabs (hy - vy) < fabs (hx - vx))
698 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
700 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
710 /* Computes whether a point is inside the saturation/value triangle */
712 is_in_triangle (GtkHSV *hsv,
716 int hx, hy, sx, sy, vx, vy;
719 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
721 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
723 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
724 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
726 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
729 /* Computes a value based on the mouse coordinates */
731 compute_v (GtkHSV *hsv,
742 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
746 angle = atan2 (dy, dx);
750 return angle / (2.0 * G_PI);
756 set_cross_grab (GtkHSV *hsv,
764 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
766 gdk_pointer_grab (priv->window, FALSE,
767 (GDK_POINTER_MOTION_MASK
768 | GDK_POINTER_MOTION_HINT_MASK
769 | GDK_BUTTON_RELEASE_MASK),
773 gdk_cursor_unref (cursor);
776 /* Button_press_event handler for the HSV color selector */
778 gtk_hsv_button_press (GtkWidget *widget,
779 GdkEventButton *event)
785 hsv = GTK_HSV (widget);
788 if (priv->mode != DRAG_NONE || event->button != 1)
794 if (is_in_ring (hsv, x, y))
797 set_cross_grab (hsv, event->time);
799 gtk_hsv_set_color (hsv,
800 compute_v (hsv, x, y),
804 gtk_widget_grab_focus (widget);
805 priv->focus_on_ring = TRUE;
810 if (is_in_triangle (hsv, x, y))
814 priv->mode = DRAG_SV;
815 set_cross_grab (hsv, event->time);
817 compute_sv (hsv, x, y, &s, &v);
818 gtk_hsv_set_color (hsv, priv->h, s, v);
820 gtk_widget_grab_focus (widget);
821 priv->focus_on_ring = FALSE;
829 /* Button_release_event handler for the HSV color selector */
831 gtk_hsv_button_release (GtkWidget *widget,
832 GdkEventButton *event)
839 hsv = GTK_HSV (widget);
842 if (priv->mode == DRAG_NONE || event->button != 1)
845 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
846 * can see that this is the final color state.
850 priv->mode = DRAG_NONE;
856 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
857 else if (mode == DRAG_SV) {
860 compute_sv (hsv, x, y, &s, &v);
861 gtk_hsv_set_color (hsv, priv->h, s, v);
863 g_assert_not_reached ();
865 gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
870 /* Motion_notify_event handler for the HSV color selector */
872 gtk_hsv_motion (GtkWidget *widget,
873 GdkEventMotion *event)
879 GdkModifierType mods;
881 hsv = GTK_HSV (widget);
884 if (priv->mode == DRAG_NONE)
889 gdk_window_get_pointer (priv->window, &ix, &iy, &mods);
899 if (priv->mode == DRAG_H)
901 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
904 else if (priv->mode == DRAG_SV)
908 compute_sv (hsv, x, y, &s, &v);
909 gtk_hsv_set_color (hsv, priv->h, s, v);
913 g_assert_not_reached ();
921 set_source_surface (cairo_t *cr,
922 cairo_surface_t *surface)
924 cairo_pattern_t *pattern;
925 cairo_matrix_t *matrix;
928 pattern = cairo_pattern_create_for_surface (surface);
930 cairo_current_point (cr, &x, &y);
931 matrix = cairo_matrix_create ();
932 cairo_matrix_translate (matrix, -x, -y);
933 cairo_pattern_set_matrix (pattern, matrix);
934 cairo_matrix_destroy (matrix);
936 cairo_set_pattern (cr, pattern);
937 cairo_pattern_destroy (pattern);
940 /* Paints the hue ring */
942 paint_ring (GtkHSV *hsv,
949 GtkWidget *widget = GTK_WIDGET (hsv);
952 gdouble dx, dy, dist;
954 gdouble inner, outer;
959 cairo_surface_t *source;
964 gtk_widget_style_get (widget,
965 "focus-line-width", &focus_width,
966 "focus-padding", &focus_pad,
971 center = widget->requisition.width / 2.0;
973 outer = priv->size / 2.0;
974 inner = outer - priv->ring_width;
976 /* Create an image initialized with the ring colors */
978 buf = g_new (guint32, width * height);
980 for (yy = 0; yy < height; yy++)
982 p = buf + yy * width;
984 dy = -(yy + y - center);
986 for (xx = 0; xx < width; xx++)
988 dx = xx + x - center;
990 dist = dx * dx + dy * dy;
991 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
997 angle = atan2 (dy, dx);
1001 hue = angle / (2.0 * G_PI);
1006 hsv_to_rgb (&r, &g, &b);
1008 *p++ = (((int)floor (r * 255 + 0.5) << 16) |
1009 ((int)floor (g * 255 + 0.5) << 8) |
1010 (int)floor (b * 255 + 0.5));
1014 source = cairo_image_surface_create_for_data ((char *)buf,
1016 width, height, 4 * width);
1018 /* Now draw the value marker onto the source image, so that it
1019 * will get properly clipped at the edges of the ring
1021 source_cr = cairo_create ();
1022 cairo_set_target_surface (source_cr, source);
1027 hsv_to_rgb (&r, &g, &b);
1029 if (INTENSITY (r, g, b) > 0.5)
1030 cairo_set_rgb_color (source_cr, 0., 0., 0.);
1032 cairo_set_rgb_color (source_cr, 1., 1., 1.);
1034 cairo_move_to (source_cr, -x + center, - y + center);
1035 cairo_line_to (source_cr,
1036 -x + center + cos (priv->h * 2.0 * G_PI) * center,
1037 -y + center - sin (priv->h * 2.0 * G_PI) * center);
1038 cairo_stroke (source_cr);
1039 cairo_destroy (source_cr);
1041 /* Draw the ring using the source image */
1045 cairo_move_to (cr, x, y);
1046 set_source_surface (cr, source);
1047 cairo_surface_destroy (source);
1049 cairo_set_line_width (cr, priv->ring_width);
1050 cairo_new_path (cr);
1053 priv->size / 2. - priv->ring_width / 2.,
1062 /* Converts an HSV triplet to an integer RGB triplet */
1064 get_color (gdouble h,
1071 hsv_to_rgb (&h, &s, &v);
1073 *r = floor (h * 255 + 0.5);
1074 *g = floor (s * 255 + 0.5);
1075 *b = floor (v * 255 + 0.5);
1078 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1080 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
1081 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
1084 /* Number of pixels we extend out from the edges when creating
1085 * color source to avoid artifacts
1089 /* Paints the HSV triangle */
1091 paint_triangle (GtkHSV *hsv,
1098 GtkWidget *widget = GTK_WIDGET (hsv);
1100 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1101 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1102 gint x2, y2, r2, g2, b2; /* Second vertex */
1103 gint x3, y3, r3, g3, b3; /* Third vertex */
1106 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1108 gint x_interp, y_interp;
1109 gint x_start, x_end;
1110 cairo_surface_t *source;
1116 /* Compute triangle's vertices */
1118 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1122 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1126 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1130 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1159 /* Shade the triangle */
1161 buf = g_new (guint32, width * height);
1163 for (yy = 0; yy < height; yy++)
1165 p = buf + yy * width;
1167 if (yy + y >= y1 - PAD && yy + y < y3 + PAD) {
1168 y_interp = CLAMP (yy + y, y1, y3);
1172 xl = LERP (x1, x2, y1, y2, y_interp);
1174 rl = LERP (r1, r2, y1, y2, y_interp);
1175 gl = LERP (g1, g2, y1, y2, y_interp);
1176 bl = LERP (b1, b2, y1, y2, y_interp);
1180 xl = LERP (x2, x3, y2, y3, y_interp);
1182 rl = LERP (r2, r3, y2, y3, y_interp);
1183 gl = LERP (g2, g3, y2, y3, y_interp);
1184 bl = LERP (b2, b3, y2, y3, y_interp);
1187 xr = LERP (x1, x3, y1, y3, y_interp);
1189 rr = LERP (r1, r3, y1, y3, y_interp);
1190 gr = LERP (g1, g3, y1, y3, y_interp);
1191 br = LERP (b1, b3, y1, y3, y_interp);
1201 x_start = MAX (xl - PAD, x);
1202 x_end = MIN (xr + PAD, x + width);
1206 for (xx = x_start; 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));
1217 source = cairo_image_surface_create_for_data ((char *)buf,
1219 width, height, 4 * width);
1221 /* Draw a triangle with the image as a source */
1223 cairo_move_to (cr, x, y);
1224 set_source_surface (cr, source);
1225 cairo_surface_destroy (source);
1227 cairo_move_to (cr, x1, y1);
1228 cairo_line_to (cr, x2, y2);
1229 cairo_line_to (cr, x3, y3);
1230 cairo_close_path (cr);
1235 /* Draw value marker */
1237 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1238 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1243 hsv_to_rgb (&r, &g, &b);
1245 if (INTENSITY (r, g, b) > 0.5)
1247 detail = "colorwheel_light";
1248 cairo_set_rgb_color (cr, 0., 0., 0.);
1252 detail = "colorwheel_dark";
1253 cairo_set_rgb_color (cr, 1., 1., 1.);
1257 #define FOCUS_RADIUS 6
1259 cairo_new_path (cr);
1260 cairo_arc (cr, xx, yy, RADIUS, 0, 2 * M_PI);
1263 /* Draw focus outline */
1265 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1266 !priv->focus_on_ring)
1271 gtk_widget_style_get (widget,
1272 "focus-line-width", &focus_width,
1273 "focus-padding", &focus_pad,
1276 gtk_paint_focus (widget->style, widget->window,
1277 GTK_WIDGET_STATE (widget),
1278 NULL, widget, detail,
1279 widget->allocation.x + xx - FOCUS_RADIUS - focus_width - focus_pad,
1280 widget->allocation.y + yy - FOCUS_RADIUS - focus_width - focus_pad,
1281 2 * (FOCUS_RADIUS + focus_width + focus_pad),
1282 2 * (FOCUS_RADIUS + focus_width + focus_pad));
1287 /* Paints the contents of the HSV color selector */
1296 paint_ring (hsv, cr, x, y, width, height);
1297 paint_triangle (hsv, cr, x, y, width, height);
1300 /* Expose_event handler for the HSV color selector */
1302 gtk_hsv_expose (GtkWidget *widget,
1303 GdkEventExpose *event)
1307 GdkRectangle rect, dest;
1310 hsv = GTK_HSV (widget);
1313 if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
1316 rect.x = widget->allocation.x;
1317 rect.y = widget->allocation.y;
1318 rect.width = widget->allocation.width;
1319 rect.height = widget->allocation.height;
1321 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1324 cr = gdk_drawable_create_cairo_context (widget->window);
1326 cairo_translate (cr, widget->allocation.x, widget->allocation.y);
1328 dest.x - widget->allocation.x,
1329 dest.y - widget->allocation.y,
1330 dest.width, dest.height);
1332 if (GTK_WIDGET_HAS_FOCUS (hsv) && priv->focus_on_ring)
1333 gtk_paint_focus (widget->style, widget->window,
1334 GTK_WIDGET_STATE (widget),
1335 &event->area, widget, NULL,
1336 widget->allocation.x,
1337 widget->allocation.y,
1338 widget->allocation.width,
1339 widget->allocation.height);
1345 gtk_hsv_focus (GtkWidget *widget,
1346 GtkDirectionType dir)
1351 hsv = GTK_HSV (widget);
1354 if (!GTK_WIDGET_HAS_FOCUS (hsv))
1356 if (dir == GTK_DIR_TAB_BACKWARD)
1357 priv->focus_on_ring = FALSE;
1359 priv->focus_on_ring = TRUE;
1361 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1368 if (priv->focus_on_ring)
1371 priv->focus_on_ring = TRUE;
1375 if (priv->focus_on_ring)
1376 priv->focus_on_ring = FALSE;
1382 case GTK_DIR_TAB_BACKWARD:
1383 if (priv->focus_on_ring)
1386 priv->focus_on_ring = TRUE;
1390 case GTK_DIR_TAB_FORWARD:
1391 if (priv->focus_on_ring)
1392 priv->focus_on_ring = FALSE;
1398 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1407 * Creates a new HSV color selector.
1409 * Return value: A newly-created HSV color selector.
1414 return g_object_new (GTK_TYPE_HSV, NULL);
1418 * gtk_hsv_set_color:
1419 * @hsv: An HSV color selector.
1424 * Sets the current color in an HSV color selector. Color component values must
1425 * be in the [0.0, 1.0] range.
1428 gtk_hsv_set_color (GtkHSV *hsv,
1435 g_return_if_fail (hsv != NULL);
1436 g_return_if_fail (GTK_IS_HSV (hsv));
1437 g_return_if_fail (h >= 0.0 && h <= 1.0);
1438 g_return_if_fail (s >= 0.0 && s <= 1.0);
1439 g_return_if_fail (v >= 0.0 && v <= 1.0);
1447 g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1449 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1453 * gtk_hsv_get_color:
1454 * @hsv: An HSV color selector.
1455 * @h: Return value for the hue.
1456 * @s: Return value for the saturation.
1457 * @v: Return value for the value.
1459 * Queries the current color in an HSV color selector. Returned values will be
1460 * in the [0.0, 1.0] range.
1463 gtk_hsv_get_color (GtkHSV *hsv, double *h, double *s, double *v)
1467 g_return_if_fail (GTK_IS_HSV (hsv));
1482 * gtk_hsv_set_metrics:
1483 * @hsv: An HSV color selector.
1484 * @size: Diameter for the hue ring.
1485 * @ring_width: Width of the hue ring.
1487 * Sets the size and ring width of an HSV color selector.
1490 gtk_hsv_set_metrics (GtkHSV *hsv,
1497 g_return_if_fail (GTK_IS_HSV (hsv));
1498 g_return_if_fail (size > 0);
1499 g_return_if_fail (ring_width > 0);
1500 g_return_if_fail (2 * ring_width + 1 <= size);
1504 same_size = (priv->size == size);
1507 priv->ring_width = ring_width;
1510 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1512 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1516 * gtk_hsv_get_metrics:
1517 * @hsv: An HSV color selector.
1518 * @size: Return value for the diameter of the hue ring.
1519 * @ring_width: Return value for the width of the hue ring.
1521 * Queries the size and ring width of an HSV color selector.
1524 gtk_hsv_get_metrics (GtkHSV *hsv,
1530 g_return_if_fail (GTK_IS_HSV (hsv));
1538 *ring_width = priv->ring_width;
1542 * gtk_hsv_is_adjusting:
1545 * An HSV color selector can be said to be adjusting if multiple rapid changes
1546 * are being made to its value, for example, when the user is adjusting the
1547 * value with the mouse. This function queries whether the HSV color selector
1548 * is being adjusted or not.
1550 * Return value: TRUE if clients can ignore changes to the color value, since
1551 * they may be transitory, or FALSE if they should consider the color value
1552 * status to be final.
1555 gtk_hsv_is_adjusting (GtkHSV *hsv)
1559 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1563 return priv->mode != DRAG_NONE;
1571 * @r: Return value for the red component.
1572 * @g: Return value for the green component.
1573 * @b: Return value for the blue component.
1575 * Converts a color from HSV space to RGB. Input values must be in the
1576 * [0.0, 1.0] range; output values will be in the same range.
1579 gtk_hsv_to_rgb (gdouble h,
1586 g_return_if_fail (h >= 0.0 && h <= 1.0);
1587 g_return_if_fail (s >= 0.0 && s <= 1.0);
1588 g_return_if_fail (v >= 0.0 && v <= 1.0);
1590 hsv_to_rgb (&h, &s, &v);
1607 * @h: Return value for the hue component.
1608 * @s: Return value for the saturation component.
1609 * @v: Return value for the value component.
1611 * Converts a color from RGB space to HSV. Input values must be in the
1612 * [0.0, 1.0] range; output values will be in the same range.
1615 gtk_rgb_to_hsv (gdouble r,
1622 g_return_if_fail (r >= 0.0 && r <= 1.0);
1623 g_return_if_fail (g >= 0.0 && g <= 1.0);
1624 g_return_if_fail (b >= 0.0 && b <= 1.0);
1626 rgb_to_hsv (&r, &g, &b);
1639 gtk_hsv_move (GtkHSV *hsv,
1640 GtkDirectionType dir)
1643 gdouble hue, sat, val;
1644 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1645 gint x, y; /* position in triangle */
1653 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1655 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1656 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1658 #define HUE_DELTA 0.002
1662 if (priv->focus_on_ring)
1667 compute_sv (hsv, x, y, &sat, &val);
1672 if (priv->focus_on_ring)
1677 compute_sv (hsv, x, y, &sat, &val);
1682 if (priv->focus_on_ring)
1687 compute_sv (hsv, x, y, &sat, &val);
1692 if (priv->focus_on_ring)
1698 compute_sv (hsv, x, y, &sat, &val);
1703 /* we don't care about the tab directions */
1713 gtk_hsv_set_color (hsv, hue, sat, val);