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,
635 int ihx, ihy, isx, isy, ivx, ivy;
636 double hx, hy, sx, sy, vx, vy;
639 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
640 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
650 if (vx * (x - sx) + vy * (y - sy) < 0.0)
653 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
654 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
661 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
664 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
665 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
672 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
675 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
676 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
685 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
686 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
698 if (fabs (hy - vy) < fabs (hx - vx))
699 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
701 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
711 /* Computes whether a point is inside the saturation/value triangle */
713 is_in_triangle (GtkHSV *hsv,
717 int hx, hy, sx, sy, vx, vy;
720 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
722 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
724 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
725 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
727 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
730 /* Computes a value based on the mouse coordinates */
732 compute_v (GtkHSV *hsv,
740 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
744 angle = atan2 (dy, dx);
748 return angle / (2.0 * G_PI);
754 set_cross_grab (GtkHSV *hsv,
762 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
764 gdk_pointer_grab (priv->window, FALSE,
765 (GDK_POINTER_MOTION_MASK
766 | GDK_POINTER_MOTION_HINT_MASK
767 | GDK_BUTTON_RELEASE_MASK),
771 gdk_cursor_unref (cursor);
775 gtk_hsv_grab_broken (GtkWidget *widget,
776 GdkEventGrabBroken *event)
778 GtkHSV *hsv = GTK_HSV (widget);
783 priv->mode = DRAG_NONE;
788 /* Button_press_event handler for the HSV color selector */
790 gtk_hsv_button_press (GtkWidget *widget,
791 GdkEventButton *event)
797 hsv = GTK_HSV (widget);
800 if (priv->mode != DRAG_NONE || event->button != 1)
806 if (is_in_ring (hsv, x, y))
809 set_cross_grab (hsv, event->time);
811 gtk_hsv_set_color (hsv,
812 compute_v (hsv, x, y),
816 gtk_widget_grab_focus (widget);
817 priv->focus_on_ring = TRUE;
822 if (is_in_triangle (hsv, x, y))
826 priv->mode = DRAG_SV;
827 set_cross_grab (hsv, event->time);
829 compute_sv (hsv, x, y, &s, &v);
830 gtk_hsv_set_color (hsv, priv->h, s, v);
832 gtk_widget_grab_focus (widget);
833 priv->focus_on_ring = FALSE;
841 /* Button_release_event handler for the HSV color selector */
843 gtk_hsv_button_release (GtkWidget *widget,
844 GdkEventButton *event)
851 hsv = GTK_HSV (widget);
854 if (priv->mode == DRAG_NONE || event->button != 1)
857 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
858 * can see that this is the final color state.
862 priv->mode = DRAG_NONE;
868 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
869 else if (mode == DRAG_SV) {
872 compute_sv (hsv, x, y, &s, &v);
873 gtk_hsv_set_color (hsv, priv->h, s, v);
875 g_assert_not_reached ();
877 gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
882 /* Motion_notify_event handler for the HSV color selector */
884 gtk_hsv_motion (GtkWidget *widget,
885 GdkEventMotion *event)
891 GdkModifierType mods;
893 hsv = GTK_HSV (widget);
896 if (priv->mode == DRAG_NONE)
901 gdk_window_get_pointer (priv->window, &ix, &iy, &mods);
911 if (priv->mode == DRAG_H)
913 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
916 else if (priv->mode == DRAG_SV)
920 compute_sv (hsv, x, y, &s, &v);
921 gtk_hsv_set_color (hsv, priv->h, s, v);
925 g_assert_not_reached ();
932 /* Paints the hue ring */
934 paint_ring (GtkHSV *hsv,
941 GtkWidget *widget = GTK_WIDGET (hsv);
944 gdouble dx, dy, dist;
946 gdouble inner, outer;
951 cairo_surface_t *source;
956 gtk_widget_style_get (widget,
957 "focus-line-width", &focus_width,
958 "focus-padding", &focus_pad,
963 center = widget->requisition.width / 2.0;
965 outer = priv->size / 2.0;
966 inner = outer - priv->ring_width;
968 /* Create an image initialized with the ring colors */
970 buf = g_new (guint32, width * height);
972 for (yy = 0; yy < height; yy++)
974 p = buf + yy * width;
976 dy = -(yy + y - center);
978 for (xx = 0; xx < width; xx++)
980 dx = xx + x - center;
982 dist = dx * dx + dy * dy;
983 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
989 angle = atan2 (dy, dx);
993 hue = angle / (2.0 * G_PI);
998 hsv_to_rgb (&r, &g, &b);
1000 *p++ = (((int)floor (r * 255 + 0.5) << 16) |
1001 ((int)floor (g * 255 + 0.5) << 8) |
1002 (int)floor (b * 255 + 0.5));
1006 source = cairo_image_surface_create_for_data ((char *)buf,
1008 width, height, 4 * width);
1010 /* Now draw the value marker onto the source image, so that it
1011 * will get properly clipped at the edges of the ring
1013 source_cr = cairo_create (source);
1018 hsv_to_rgb (&r, &g, &b);
1020 if (INTENSITY (r, g, b) > 0.5)
1021 cairo_set_source_rgb (source_cr, 0., 0., 0.);
1023 cairo_set_source_rgb (source_cr, 1., 1., 1.);
1025 cairo_move_to (source_cr, -x + center, - y + center);
1026 cairo_line_to (source_cr,
1027 -x + center + cos (priv->h * 2.0 * G_PI) * center,
1028 -y + center - sin (priv->h * 2.0 * G_PI) * center);
1029 cairo_stroke (source_cr);
1030 cairo_destroy (source_cr);
1032 /* Draw the ring using the source image */
1036 cairo_set_source_surface (cr, source, x, y);
1037 cairo_surface_destroy (source);
1039 cairo_set_line_width (cr, priv->ring_width);
1040 cairo_new_path (cr);
1043 priv->size / 2. - priv->ring_width / 2.,
1052 /* Converts an HSV triplet to an integer RGB triplet */
1054 get_color (gdouble h,
1061 hsv_to_rgb (&h, &s, &v);
1063 *r = floor (h * 255 + 0.5);
1064 *g = floor (s * 255 + 0.5);
1065 *b = floor (v * 255 + 0.5);
1068 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1070 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
1071 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
1074 /* Number of pixels we extend out from the edges when creating
1075 * color source to avoid artifacts
1079 /* Paints the HSV triangle */
1081 paint_triangle (GtkHSV *hsv,
1088 GtkWidget *widget = GTK_WIDGET (hsv);
1090 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1091 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1092 gint x2, y2, r2, g2, b2; /* Second vertex */
1093 gint x3, y3, r3, g3, b3; /* Third vertex */
1095 guint32 *buf, *p, c;
1096 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1098 gint x_interp, y_interp;
1099 gint x_start, x_end;
1100 cairo_surface_t *source;
1106 /* Compute triangle's vertices */
1108 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1112 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1116 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1120 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1149 /* Shade the triangle */
1151 buf = g_new (guint32, width * height);
1153 for (yy = 0; yy < height; yy++)
1155 p = buf + yy * width;
1157 if (yy + y >= y1 - PAD && yy + y < y3 + PAD) {
1158 y_interp = CLAMP (yy + y, y1, y3);
1162 xl = LERP (x1, x2, y1, y2, y_interp);
1164 rl = LERP (r1, r2, y1, y2, y_interp);
1165 gl = LERP (g1, g2, y1, y2, y_interp);
1166 bl = LERP (b1, b2, y1, y2, y_interp);
1170 xl = LERP (x2, x3, y2, y3, y_interp);
1172 rl = LERP (r2, r3, y2, y3, y_interp);
1173 gl = LERP (g2, g3, y2, y3, y_interp);
1174 bl = LERP (b2, b3, y2, y3, y_interp);
1177 xr = LERP (x1, x3, y1, y3, y_interp);
1179 rr = LERP (r1, r3, y1, y3, y_interp);
1180 gr = LERP (g1, g3, y1, y3, y_interp);
1181 br = LERP (b1, b3, y1, y3, y_interp);
1191 x_start = MAX (xl - PAD, x);
1192 x_end = MIN (xr + PAD, x + width);
1193 x_start = MIN (x_start, x_end);
1195 c = (rl << 16) | (gl << 8) | bl;
1197 for (xx = x; xx < x_start; xx++)
1200 for (; xx < x_end; xx++)
1202 x_interp = CLAMP (xx, xl, xr);
1204 *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
1205 (LERP (gl, gr, xl, xr, x_interp) << 8) |
1206 LERP (bl, br, xl, xr, x_interp));
1209 c = (rr << 16) | (gr << 8) | br;
1211 for (; xx < x + width; xx++)
1216 source = cairo_image_surface_create_for_data ((char *)buf,
1218 width, height, 4 * width);
1220 /* Draw a triangle with the image as a source */
1222 cairo_set_source_surface (cr, source, x, y);
1223 cairo_surface_destroy (source);
1225 cairo_move_to (cr, x1, y1);
1226 cairo_line_to (cr, x2, y2);
1227 cairo_line_to (cr, x3, y3);
1228 cairo_close_path (cr);
1233 /* Draw value marker */
1235 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1236 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1241 hsv_to_rgb (&r, &g, &b);
1243 if (INTENSITY (r, g, b) > 0.5)
1245 detail = "colorwheel_light";
1246 cairo_set_source_rgb (cr, 0., 0., 0.);
1250 detail = "colorwheel_dark";
1251 cairo_set_source_rgb (cr, 1., 1., 1.);
1255 #define FOCUS_RADIUS 6
1257 cairo_new_path (cr);
1258 cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
1261 /* Draw focus outline */
1263 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1264 !priv->focus_on_ring)
1269 gtk_widget_style_get (widget,
1270 "focus-line-width", &focus_width,
1271 "focus-padding", &focus_pad,
1274 gtk_paint_focus (widget->style, widget->window,
1275 GTK_WIDGET_STATE (widget),
1276 NULL, widget, detail,
1277 widget->allocation.x + xx - FOCUS_RADIUS - focus_width - focus_pad,
1278 widget->allocation.y + yy - FOCUS_RADIUS - focus_width - focus_pad,
1279 2 * (FOCUS_RADIUS + focus_width + focus_pad),
1280 2 * (FOCUS_RADIUS + focus_width + focus_pad));
1285 /* Paints the contents of the HSV color selector */
1294 paint_ring (hsv, cr, x, y, width, height);
1295 paint_triangle (hsv, cr, x, y, width, height);
1298 /* Expose_event handler for the HSV color selector */
1300 gtk_hsv_expose (GtkWidget *widget,
1301 GdkEventExpose *event)
1305 GdkRectangle rect, dest;
1308 hsv = GTK_HSV (widget);
1311 if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
1314 rect.x = widget->allocation.x;
1315 rect.y = widget->allocation.y;
1316 rect.width = widget->allocation.width;
1317 rect.height = widget->allocation.height;
1319 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1322 cr = gdk_cairo_create (widget->window);
1324 cairo_translate (cr, widget->allocation.x, widget->allocation.y);
1326 dest.x - widget->allocation.x,
1327 dest.y - widget->allocation.y,
1328 dest.width, dest.height);
1331 if (GTK_WIDGET_HAS_FOCUS (hsv) && priv->focus_on_ring)
1332 gtk_paint_focus (widget->style, widget->window,
1333 GTK_WIDGET_STATE (widget),
1334 &event->area, widget, NULL,
1335 widget->allocation.x,
1336 widget->allocation.y,
1337 widget->allocation.width,
1338 widget->allocation.height);
1344 gtk_hsv_focus (GtkWidget *widget,
1345 GtkDirectionType dir)
1350 hsv = GTK_HSV (widget);
1353 if (!GTK_WIDGET_HAS_FOCUS (hsv))
1355 if (dir == GTK_DIR_TAB_BACKWARD)
1356 priv->focus_on_ring = FALSE;
1358 priv->focus_on_ring = TRUE;
1360 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1367 if (priv->focus_on_ring)
1370 priv->focus_on_ring = TRUE;
1374 if (priv->focus_on_ring)
1375 priv->focus_on_ring = FALSE;
1381 case GTK_DIR_TAB_BACKWARD:
1382 if (priv->focus_on_ring)
1385 priv->focus_on_ring = TRUE;
1389 case GTK_DIR_TAB_FORWARD:
1390 if (priv->focus_on_ring)
1391 priv->focus_on_ring = FALSE;
1397 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1406 * Creates a new HSV color selector.
1408 * Return value: A newly-created HSV color selector.
1413 return g_object_new (GTK_TYPE_HSV, NULL);
1417 * gtk_hsv_set_color:
1418 * @hsv: An HSV color selector.
1423 * Sets the current color in an HSV color selector. Color component values must
1424 * be in the [0.0, 1.0] range.
1427 gtk_hsv_set_color (GtkHSV *hsv,
1434 g_return_if_fail (hsv != NULL);
1435 g_return_if_fail (GTK_IS_HSV (hsv));
1436 g_return_if_fail (h >= 0.0 && h <= 1.0);
1437 g_return_if_fail (s >= 0.0 && s <= 1.0);
1438 g_return_if_fail (v >= 0.0 && v <= 1.0);
1446 g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1448 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1452 * gtk_hsv_get_color:
1453 * @hsv: An HSV color selector.
1454 * @h: Return value for the hue.
1455 * @s: Return value for the saturation.
1456 * @v: Return value for the value.
1458 * Queries the current color in an HSV color selector. Returned values will be
1459 * in the [0.0, 1.0] range.
1462 gtk_hsv_get_color (GtkHSV *hsv, double *h, double *s, double *v)
1466 g_return_if_fail (GTK_IS_HSV (hsv));
1481 * gtk_hsv_set_metrics:
1482 * @hsv: An HSV color selector.
1483 * @size: Diameter for the hue ring.
1484 * @ring_width: Width of the hue ring.
1486 * Sets the size and ring width of an HSV color selector.
1489 gtk_hsv_set_metrics (GtkHSV *hsv,
1496 g_return_if_fail (GTK_IS_HSV (hsv));
1497 g_return_if_fail (size > 0);
1498 g_return_if_fail (ring_width > 0);
1499 g_return_if_fail (2 * ring_width + 1 <= size);
1503 same_size = (priv->size == size);
1506 priv->ring_width = ring_width;
1509 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1511 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1515 * gtk_hsv_get_metrics:
1516 * @hsv: An HSV color selector.
1517 * @size: Return value for the diameter of the hue ring.
1518 * @ring_width: Return value for the width of the hue ring.
1520 * Queries the size and ring width of an HSV color selector.
1523 gtk_hsv_get_metrics (GtkHSV *hsv,
1529 g_return_if_fail (GTK_IS_HSV (hsv));
1537 *ring_width = priv->ring_width;
1541 * gtk_hsv_is_adjusting:
1544 * An HSV color selector can be said to be adjusting if multiple rapid changes
1545 * are being made to its value, for example, when the user is adjusting the
1546 * value with the mouse. This function queries whether the HSV color selector
1547 * is being adjusted or not.
1549 * Return value: TRUE if clients can ignore changes to the color value, since
1550 * they may be transitory, or FALSE if they should consider the color value
1551 * status to be final.
1554 gtk_hsv_is_adjusting (GtkHSV *hsv)
1558 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1562 return priv->mode != DRAG_NONE;
1570 * @r: Return value for the red component.
1571 * @g: Return value for the green component.
1572 * @b: Return value for the blue component.
1574 * Converts a color from HSV space to RGB. Input values must be in the
1575 * [0.0, 1.0] range; output values will be in the same range.
1578 gtk_hsv_to_rgb (gdouble h,
1585 g_return_if_fail (h >= 0.0 && h <= 1.0);
1586 g_return_if_fail (s >= 0.0 && s <= 1.0);
1587 g_return_if_fail (v >= 0.0 && v <= 1.0);
1589 hsv_to_rgb (&h, &s, &v);
1606 * @h: Return value for the hue component.
1607 * @s: Return value for the saturation component.
1608 * @v: Return value for the value component.
1610 * Converts a color from RGB space to HSV. Input values must be in the
1611 * [0.0, 1.0] range; output values will be in the same range.
1614 gtk_rgb_to_hsv (gdouble r,
1621 g_return_if_fail (r >= 0.0 && r <= 1.0);
1622 g_return_if_fail (g >= 0.0 && g <= 1.0);
1623 g_return_if_fail (b >= 0.0 && b <= 1.0);
1625 rgb_to_hsv (&r, &g, &b);
1638 gtk_hsv_move (GtkHSV *hsv,
1639 GtkDirectionType dir)
1642 gdouble hue, sat, val;
1643 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1644 gint x, y; /* position in triangle */
1652 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1654 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1655 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1657 #define HUE_DELTA 0.002
1661 if (priv->focus_on_ring)
1666 compute_sv (hsv, x, y, &sat, &val);
1671 if (priv->focus_on_ring)
1676 compute_sv (hsv, x, y, &sat, &val);
1681 if (priv->focus_on_ring)
1686 compute_sv (hsv, x, y, &sat, &val);
1691 if (priv->focus_on_ring)
1697 compute_sv (hsv, x, y, &sat, &val);
1702 /* we don't care about the tab directions */
1712 gtk_hsv_set_color (hsv, hue, sat, val);
1715 #define __GTK_HSV_C__
1716 #include "gtkaliasdef.c"