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_destroy (GtkObject *object);
92 static void gtk_hsv_map (GtkWidget *widget);
93 static void gtk_hsv_unmap (GtkWidget *widget);
94 static void gtk_hsv_realize (GtkWidget *widget);
95 static void gtk_hsv_unrealize (GtkWidget *widget);
96 static void gtk_hsv_size_request (GtkWidget *widget,
97 GtkRequisition *requisition);
98 static void gtk_hsv_size_allocate (GtkWidget *widget,
99 GtkAllocation *allocation);
100 static gint gtk_hsv_button_press (GtkWidget *widget,
101 GdkEventButton *event);
102 static gint gtk_hsv_button_release (GtkWidget *widget,
103 GdkEventButton *event);
104 static gint gtk_hsv_motion (GtkWidget *widget,
105 GdkEventMotion *event);
106 static gint gtk_hsv_expose (GtkWidget *widget,
107 GdkEventExpose *event);
108 static gboolean gtk_hsv_grab_broken (GtkWidget *widget,
109 GdkEventGrabBroken *event);
110 static gboolean gtk_hsv_focus (GtkWidget *widget,
111 GtkDirectionType direction);
112 static void gtk_hsv_move (GtkHSV *hsv,
113 GtkDirectionType dir);
115 static guint hsv_signals[LAST_SIGNAL];
117 G_DEFINE_TYPE (GtkHSV, gtk_hsv, GTK_TYPE_WIDGET)
119 /* Class initialization function for the HSV color selector */
121 gtk_hsv_class_init (GtkHSVClass *class)
123 GObjectClass *gobject_class;
124 GtkObjectClass *object_class;
125 GtkWidgetClass *widget_class;
126 GtkHSVClass *hsv_class;
127 GtkBindingSet *binding_set;
129 gobject_class = (GObjectClass *) class;
130 object_class = (GtkObjectClass *) class;
131 widget_class = (GtkWidgetClass *) class;
132 hsv_class = GTK_HSV_CLASS (class);
134 object_class->destroy = gtk_hsv_destroy;
136 widget_class->map = gtk_hsv_map;
137 widget_class->unmap = gtk_hsv_unmap;
138 widget_class->realize = gtk_hsv_realize;
139 widget_class->unrealize = gtk_hsv_unrealize;
140 widget_class->size_request = gtk_hsv_size_request;
141 widget_class->size_allocate = gtk_hsv_size_allocate;
142 widget_class->button_press_event = gtk_hsv_button_press;
143 widget_class->button_release_event = gtk_hsv_button_release;
144 widget_class->motion_notify_event = gtk_hsv_motion;
145 widget_class->expose_event = gtk_hsv_expose;
146 widget_class->focus = gtk_hsv_focus;
147 widget_class->grab_broken_event = gtk_hsv_grab_broken;
149 hsv_class->move = gtk_hsv_move;
151 hsv_signals[CHANGED] =
152 g_signal_new (I_("changed"),
153 G_OBJECT_CLASS_TYPE (object_class),
155 G_STRUCT_OFFSET (GtkHSVClass, changed),
157 _gtk_marshal_VOID__VOID,
161 g_signal_new (I_("move"),
162 G_OBJECT_CLASS_TYPE (object_class),
163 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
164 G_STRUCT_OFFSET (GtkHSVClass, move),
166 _gtk_marshal_VOID__ENUM,
168 GTK_TYPE_DIRECTION_TYPE);
170 binding_set = gtk_binding_set_by_class (class);
172 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
174 G_TYPE_ENUM, GTK_DIR_UP);
175 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
177 G_TYPE_ENUM, GTK_DIR_UP);
179 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
181 G_TYPE_ENUM, GTK_DIR_DOWN);
182 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
184 G_TYPE_ENUM, GTK_DIR_DOWN);
187 gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
189 G_TYPE_ENUM, GTK_DIR_RIGHT);
190 gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
192 G_TYPE_ENUM, GTK_DIR_RIGHT);
194 gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
196 G_TYPE_ENUM, GTK_DIR_LEFT);
197 gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
199 G_TYPE_ENUM, GTK_DIR_LEFT);
201 g_type_class_add_private (gobject_class, sizeof (HSVPrivate));
204 /* Object initialization function for the HSV color selector */
206 gtk_hsv_init (GtkHSV *hsv)
210 priv = G_TYPE_INSTANCE_GET_PRIVATE (hsv, GTK_TYPE_HSV, HSVPrivate);
214 GTK_WIDGET_SET_FLAGS (hsv, GTK_NO_WINDOW);
215 GTK_WIDGET_SET_FLAGS (hsv, GTK_CAN_FOCUS);
221 priv->size = DEFAULT_SIZE;
222 priv->ring_width = DEFAULT_RING_WIDTH;
225 /* Destroy handler for the HSV color selector */
227 gtk_hsv_destroy (GtkObject *object)
229 GTK_OBJECT_CLASS (gtk_hsv_parent_class)->destroy (object);
232 /* Default signal handlers */
235 /* Map handler for the HSV color selector */
238 gtk_hsv_map (GtkWidget *widget)
243 hsv = GTK_HSV (widget);
246 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->map (widget);
248 gdk_window_show (priv->window);
251 /* Unmap handler for the HSV color selector */
254 gtk_hsv_unmap (GtkWidget *widget)
259 hsv = GTK_HSV (widget);
262 gdk_window_hide (priv->window);
264 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unmap (widget);
267 /* Realize handler for the HSV color selector */
269 gtk_hsv_realize (GtkWidget *widget)
275 GdkWindow *parent_window;
277 hsv = GTK_HSV (widget);
280 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
284 attr.window_type = GDK_WINDOW_CHILD;
285 attr.x = widget->allocation.x;
286 attr.y = widget->allocation.y;
287 attr.width = widget->allocation.width;
288 attr.height = widget->allocation.height;
289 attr.wclass = GDK_INPUT_ONLY;
290 attr.event_mask = gtk_widget_get_events (widget);
291 attr.event_mask |= (GDK_KEY_PRESS_MASK
292 | GDK_BUTTON_PRESS_MASK
293 | GDK_BUTTON_RELEASE_MASK
294 | GDK_POINTER_MOTION_MASK
295 | GDK_ENTER_NOTIFY_MASK
296 | GDK_LEAVE_NOTIFY_MASK);
298 attr_mask = GDK_WA_X | GDK_WA_Y;
300 parent_window = gtk_widget_get_parent_window (widget);
302 widget->window = parent_window;
303 g_object_ref (widget->window);
305 priv->window = gdk_window_new (parent_window, &attr, attr_mask);
306 gdk_window_set_user_data (priv->window, hsv);
308 widget->style = gtk_style_attach (widget->style, widget->window);
312 priv->gc = gdk_gc_new (parent_window);
315 /* Unrealize handler for the HSV color selector */
317 gtk_hsv_unrealize (GtkWidget *widget)
322 hsv = GTK_HSV (widget);
325 gdk_window_set_user_data (priv->window, NULL);
326 gdk_window_destroy (priv->window);
329 g_object_unref (priv->gc);
332 if (GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unrealize)
333 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unrealize (widget);
336 /* Size_request handler for the HSV color selector */
338 gtk_hsv_size_request (GtkWidget *widget,
339 GtkRequisition *requisition)
341 GtkHSV *hsv = GTK_HSV (widget);
342 HSVPrivate *priv = hsv->priv;
346 gtk_widget_style_get (widget,
347 "focus-line-width", &focus_width,
348 "focus-padding", &focus_pad,
351 requisition->width = priv->size + 2 * (focus_width + focus_pad);
352 requisition->height = priv->size + 2 * (focus_width + focus_pad);
355 /* Size_allocate handler for the HSV color selector */
357 gtk_hsv_size_allocate (GtkWidget *widget,
358 GtkAllocation *allocation)
363 hsv = GTK_HSV (widget);
366 widget->allocation = *allocation;
368 if (GTK_WIDGET_REALIZED (widget))
369 gdk_window_move_resize (priv->window,
377 /* Utility functions */
379 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
381 /* Converts from HSV to RGB */
383 hsv_to_rgb (gdouble *h,
387 gdouble hue, saturation, value;
406 p = value * (1.0 - saturation);
407 q = value * (1.0 - saturation * f);
408 t = value * (1.0 - saturation * (1.0 - f));
449 g_assert_not_reached ();
454 /* Converts from RGB to HSV */
456 rgb_to_hsv (gdouble *r,
460 gdouble red, green, blue;
499 s = (max - min) / max;
510 h = (green - blue) / delta;
511 else if (green == max)
512 h = 2 + (blue - red) / delta;
513 else if (blue == max)
514 h = 4 + (red - green) / delta;
529 /* Computes the vertices of the saturation/value triangle */
531 compute_triangle (GtkHSV *hsv,
541 gdouble inner, outer;
546 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
547 outer = priv->size / 2.0;
548 inner = outer - priv->ring_width;
549 angle = priv->h * 2.0 * G_PI;
551 *hx = floor (center + cos (angle) * inner + 0.5);
552 *hy = floor (center - sin (angle) * inner + 0.5);
553 *sx = floor (center + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
554 *sy = floor (center - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
555 *vx = floor (center + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
556 *vy = floor (center - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
559 /* Computes whether a point is inside the hue ring */
561 is_in_ring (GtkHSV *hsv,
566 gdouble dx, dy, dist;
567 gdouble center, inner, outer;
571 center = priv->size / 2.0;
572 outer = priv->size / 2.0;
573 inner = outer - priv->ring_width;
577 dist = dx * dx + dy * dy;
579 return (dist >= inner * inner && dist <= outer * outer);
582 /* Computes a saturation/value pair based on the mouse coordinates */
584 compute_sv (GtkHSV *hsv,
590 int ihx, ihy, isx, isy, ivx, ivy;
591 double hx, hy, sx, sy, vx, vy;
594 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
595 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
605 if (vx * (x - sx) + vy * (y - sy) < 0.0)
608 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
609 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
616 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
619 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
620 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
627 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
630 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
631 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
640 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
641 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
653 if (fabs (hy - vy) < fabs (hx - vx))
654 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
656 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
666 /* Computes whether a point is inside the saturation/value triangle */
668 is_in_triangle (GtkHSV *hsv,
672 int hx, hy, sx, sy, vx, vy;
675 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
677 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
679 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
680 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
682 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
685 /* Computes a value based on the mouse coordinates */
687 compute_v (GtkHSV *hsv,
695 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
699 angle = atan2 (dy, dx);
703 return angle / (2.0 * G_PI);
709 set_cross_grab (GtkHSV *hsv,
717 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
719 gdk_pointer_grab (priv->window, FALSE,
720 (GDK_POINTER_MOTION_MASK
721 | GDK_POINTER_MOTION_HINT_MASK
722 | GDK_BUTTON_RELEASE_MASK),
726 gdk_cursor_unref (cursor);
730 gtk_hsv_grab_broken (GtkWidget *widget,
731 GdkEventGrabBroken *event)
733 GtkHSV *hsv = GTK_HSV (widget);
738 priv->mode = DRAG_NONE;
743 /* Button_press_event handler for the HSV color selector */
745 gtk_hsv_button_press (GtkWidget *widget,
746 GdkEventButton *event)
752 hsv = GTK_HSV (widget);
755 if (priv->mode != DRAG_NONE || event->button != 1)
761 if (is_in_ring (hsv, x, y))
764 set_cross_grab (hsv, event->time);
766 gtk_hsv_set_color (hsv,
767 compute_v (hsv, x, y),
771 gtk_widget_grab_focus (widget);
772 priv->focus_on_ring = TRUE;
777 if (is_in_triangle (hsv, x, y))
781 priv->mode = DRAG_SV;
782 set_cross_grab (hsv, event->time);
784 compute_sv (hsv, x, y, &s, &v);
785 gtk_hsv_set_color (hsv, priv->h, s, v);
787 gtk_widget_grab_focus (widget);
788 priv->focus_on_ring = FALSE;
796 /* Button_release_event handler for the HSV color selector */
798 gtk_hsv_button_release (GtkWidget *widget,
799 GdkEventButton *event)
806 hsv = GTK_HSV (widget);
809 if (priv->mode == DRAG_NONE || event->button != 1)
812 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
813 * can see that this is the final color state.
817 priv->mode = DRAG_NONE;
823 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
824 else if (mode == DRAG_SV) {
827 compute_sv (hsv, x, y, &s, &v);
828 gtk_hsv_set_color (hsv, priv->h, s, v);
830 g_assert_not_reached ();
832 gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
837 /* Motion_notify_event handler for the HSV color selector */
839 gtk_hsv_motion (GtkWidget *widget,
840 GdkEventMotion *event)
846 GdkModifierType mods;
848 hsv = GTK_HSV (widget);
851 if (priv->mode == DRAG_NONE)
854 gdk_event_request_motions (event);
859 if (priv->mode == DRAG_H)
861 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
864 else if (priv->mode == DRAG_SV)
868 compute_sv (hsv, x, y, &s, &v);
869 gtk_hsv_set_color (hsv, priv->h, s, v);
873 g_assert_not_reached ();
880 /* Paints the hue ring */
882 paint_ring (GtkHSV *hsv,
889 GtkWidget *widget = GTK_WIDGET (hsv);
892 gdouble dx, dy, dist;
894 gdouble inner, outer;
899 cairo_surface_t *source;
904 gtk_widget_style_get (widget,
905 "focus-line-width", &focus_width,
906 "focus-padding", &focus_pad,
911 center = widget->requisition.width / 2.0;
913 outer = priv->size / 2.0;
914 inner = outer - priv->ring_width;
916 /* Create an image initialized with the ring colors */
918 buf = g_new (guint32, width * height);
920 for (yy = 0; yy < height; yy++)
922 p = buf + yy * width;
924 dy = -(yy + y - center);
926 for (xx = 0; xx < width; xx++)
928 dx = xx + x - center;
930 dist = dx * dx + dy * dy;
931 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
937 angle = atan2 (dy, dx);
941 hue = angle / (2.0 * G_PI);
946 hsv_to_rgb (&r, &g, &b);
948 *p++ = (((int)floor (r * 255 + 0.5) << 16) |
949 ((int)floor (g * 255 + 0.5) << 8) |
950 (int)floor (b * 255 + 0.5));
954 source = cairo_image_surface_create_for_data ((char *)buf,
956 width, height, 4 * width);
958 /* Now draw the value marker onto the source image, so that it
959 * will get properly clipped at the edges of the ring
961 source_cr = cairo_create (source);
966 hsv_to_rgb (&r, &g, &b);
968 if (INTENSITY (r, g, b) > 0.5)
969 cairo_set_source_rgb (source_cr, 0., 0., 0.);
971 cairo_set_source_rgb (source_cr, 1., 1., 1.);
973 cairo_move_to (source_cr, -x + center, - y + center);
974 cairo_line_to (source_cr,
975 -x + center + cos (priv->h * 2.0 * G_PI) * center,
976 -y + center - sin (priv->h * 2.0 * G_PI) * center);
977 cairo_stroke (source_cr);
978 cairo_destroy (source_cr);
980 /* Draw the ring using the source image */
984 cairo_set_source_surface (cr, source, x, y);
985 cairo_surface_destroy (source);
987 cairo_set_line_width (cr, priv->ring_width);
991 priv->size / 2. - priv->ring_width / 2.,
1000 /* Converts an HSV triplet to an integer RGB triplet */
1002 get_color (gdouble h,
1009 hsv_to_rgb (&h, &s, &v);
1011 *r = floor (h * 255 + 0.5);
1012 *g = floor (s * 255 + 0.5);
1013 *b = floor (v * 255 + 0.5);
1016 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1018 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
1019 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
1022 /* Number of pixels we extend out from the edges when creating
1023 * color source to avoid artifacts
1027 /* Paints the HSV triangle */
1029 paint_triangle (GtkHSV *hsv,
1036 GtkWidget *widget = GTK_WIDGET (hsv);
1038 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1039 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1040 gint x2, y2, r2, g2, b2; /* Second vertex */
1041 gint x3, y3, r3, g3, b3; /* Third vertex */
1043 guint32 *buf, *p, c;
1044 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1046 gint x_interp, y_interp;
1047 gint x_start, x_end;
1048 cairo_surface_t *source;
1054 /* Compute triangle's vertices */
1056 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1060 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1064 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1068 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1097 /* Shade the triangle */
1099 buf = g_new (guint32, width * height);
1101 for (yy = 0; yy < height; yy++)
1103 p = buf + yy * width;
1105 if (yy + y >= y1 - PAD && yy + y < y3 + PAD) {
1106 y_interp = CLAMP (yy + y, y1, y3);
1110 xl = LERP (x1, x2, y1, y2, y_interp);
1112 rl = LERP (r1, r2, y1, y2, y_interp);
1113 gl = LERP (g1, g2, y1, y2, y_interp);
1114 bl = LERP (b1, b2, y1, y2, y_interp);
1118 xl = LERP (x2, x3, y2, y3, y_interp);
1120 rl = LERP (r2, r3, y2, y3, y_interp);
1121 gl = LERP (g2, g3, y2, y3, y_interp);
1122 bl = LERP (b2, b3, y2, y3, y_interp);
1125 xr = LERP (x1, x3, y1, y3, y_interp);
1127 rr = LERP (r1, r3, y1, y3, y_interp);
1128 gr = LERP (g1, g3, y1, y3, y_interp);
1129 br = LERP (b1, b3, y1, y3, y_interp);
1139 x_start = MAX (xl - PAD, x);
1140 x_end = MIN (xr + PAD, x + width);
1141 x_start = MIN (x_start, x_end);
1143 c = (rl << 16) | (gl << 8) | bl;
1145 for (xx = x; xx < x_start; xx++)
1148 for (; xx < x_end; xx++)
1150 x_interp = CLAMP (xx, xl, xr);
1152 *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
1153 (LERP (gl, gr, xl, xr, x_interp) << 8) |
1154 LERP (bl, br, xl, xr, x_interp));
1157 c = (rr << 16) | (gr << 8) | br;
1159 for (; xx < x + width; xx++)
1164 source = cairo_image_surface_create_for_data ((char *)buf,
1166 width, height, 4 * width);
1168 /* Draw a triangle with the image as a source */
1170 cairo_set_source_surface (cr, source, x, y);
1171 cairo_surface_destroy (source);
1173 cairo_move_to (cr, x1, y1);
1174 cairo_line_to (cr, x2, y2);
1175 cairo_line_to (cr, x3, y3);
1176 cairo_close_path (cr);
1181 /* Draw value marker */
1183 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1184 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1189 hsv_to_rgb (&r, &g, &b);
1191 if (INTENSITY (r, g, b) > 0.5)
1193 detail = "colorwheel_light";
1194 cairo_set_source_rgb (cr, 0., 0., 0.);
1198 detail = "colorwheel_dark";
1199 cairo_set_source_rgb (cr, 1., 1., 1.);
1203 #define FOCUS_RADIUS 6
1205 cairo_new_path (cr);
1206 cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
1209 /* Draw focus outline */
1211 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1212 !priv->focus_on_ring)
1217 gtk_widget_style_get (widget,
1218 "focus-line-width", &focus_width,
1219 "focus-padding", &focus_pad,
1222 gtk_paint_focus (widget->style, widget->window,
1223 GTK_WIDGET_STATE (widget),
1224 NULL, widget, detail,
1225 widget->allocation.x + xx - FOCUS_RADIUS - focus_width - focus_pad,
1226 widget->allocation.y + yy - FOCUS_RADIUS - focus_width - focus_pad,
1227 2 * (FOCUS_RADIUS + focus_width + focus_pad),
1228 2 * (FOCUS_RADIUS + focus_width + focus_pad));
1233 /* Paints the contents of the HSV color selector */
1242 paint_ring (hsv, cr, x, y, width, height);
1243 paint_triangle (hsv, cr, x, y, width, height);
1246 /* Expose_event handler for the HSV color selector */
1248 gtk_hsv_expose (GtkWidget *widget,
1249 GdkEventExpose *event)
1253 GdkRectangle rect, dest;
1256 hsv = GTK_HSV (widget);
1259 if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
1262 rect.x = widget->allocation.x;
1263 rect.y = widget->allocation.y;
1264 rect.width = widget->allocation.width;
1265 rect.height = widget->allocation.height;
1267 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1270 cr = gdk_cairo_create (widget->window);
1272 cairo_translate (cr, widget->allocation.x, widget->allocation.y);
1274 dest.x - widget->allocation.x,
1275 dest.y - widget->allocation.y,
1276 dest.width, dest.height);
1279 if (GTK_WIDGET_HAS_FOCUS (hsv) && priv->focus_on_ring)
1280 gtk_paint_focus (widget->style, widget->window,
1281 GTK_WIDGET_STATE (widget),
1282 &event->area, widget, NULL,
1283 widget->allocation.x,
1284 widget->allocation.y,
1285 widget->allocation.width,
1286 widget->allocation.height);
1292 gtk_hsv_focus (GtkWidget *widget,
1293 GtkDirectionType dir)
1298 hsv = GTK_HSV (widget);
1301 if (!GTK_WIDGET_HAS_FOCUS (hsv))
1303 if (dir == GTK_DIR_TAB_BACKWARD)
1304 priv->focus_on_ring = FALSE;
1306 priv->focus_on_ring = TRUE;
1308 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1315 if (priv->focus_on_ring)
1318 priv->focus_on_ring = TRUE;
1322 if (priv->focus_on_ring)
1323 priv->focus_on_ring = FALSE;
1329 case GTK_DIR_TAB_BACKWARD:
1330 if (priv->focus_on_ring)
1333 priv->focus_on_ring = TRUE;
1337 case GTK_DIR_TAB_FORWARD:
1338 if (priv->focus_on_ring)
1339 priv->focus_on_ring = FALSE;
1345 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1354 * Creates a new HSV color selector.
1356 * Return value: A newly-created HSV color selector.
1361 return g_object_new (GTK_TYPE_HSV, NULL);
1365 * gtk_hsv_set_color:
1366 * @hsv: An HSV color selector.
1371 * Sets the current color in an HSV color selector. Color component values must
1372 * be in the [0.0, 1.0] range.
1375 gtk_hsv_set_color (GtkHSV *hsv,
1382 g_return_if_fail (GTK_IS_HSV (hsv));
1383 g_return_if_fail (h >= 0.0 && h <= 1.0);
1384 g_return_if_fail (s >= 0.0 && s <= 1.0);
1385 g_return_if_fail (v >= 0.0 && v <= 1.0);
1393 g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1395 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1399 * gtk_hsv_get_color:
1400 * @hsv: An HSV color selector.
1401 * @h: Return value for the hue.
1402 * @s: Return value for the saturation.
1403 * @v: Return value for the value.
1405 * Queries the current color in an HSV color selector. Returned values will be
1406 * in the [0.0, 1.0] range.
1409 gtk_hsv_get_color (GtkHSV *hsv, double *h, double *s, double *v)
1413 g_return_if_fail (GTK_IS_HSV (hsv));
1428 * gtk_hsv_set_metrics:
1429 * @hsv: An HSV color selector.
1430 * @size: Diameter for the hue ring.
1431 * @ring_width: Width of the hue ring.
1433 * Sets the size and ring width of an HSV color selector.
1436 gtk_hsv_set_metrics (GtkHSV *hsv,
1443 g_return_if_fail (GTK_IS_HSV (hsv));
1444 g_return_if_fail (size > 0);
1445 g_return_if_fail (ring_width > 0);
1446 g_return_if_fail (2 * ring_width + 1 <= size);
1450 same_size = (priv->size == size);
1453 priv->ring_width = ring_width;
1456 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1458 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1462 * gtk_hsv_get_metrics:
1463 * @hsv: An HSV color selector.
1464 * @size: Return value for the diameter of the hue ring.
1465 * @ring_width: Return value for the width of the hue ring.
1467 * Queries the size and ring width of an HSV color selector.
1470 gtk_hsv_get_metrics (GtkHSV *hsv,
1476 g_return_if_fail (GTK_IS_HSV (hsv));
1484 *ring_width = priv->ring_width;
1488 * gtk_hsv_is_adjusting:
1491 * An HSV color selector can be said to be adjusting if multiple rapid changes
1492 * are being made to its value, for example, when the user is adjusting the
1493 * value with the mouse. This function queries whether the HSV color selector
1494 * is being adjusted or not.
1496 * Return value: TRUE if clients can ignore changes to the color value, since
1497 * they may be transitory, or FALSE if they should consider the color value
1498 * status to be final.
1501 gtk_hsv_is_adjusting (GtkHSV *hsv)
1505 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1509 return priv->mode != DRAG_NONE;
1517 * @r: Return value for the red component.
1518 * @g: Return value for the green component.
1519 * @b: Return value for the blue component.
1521 * Converts a color from HSV space to RGB. Input values must be in the
1522 * [0.0, 1.0] range; output values will be in the same range.
1525 gtk_hsv_to_rgb (gdouble h,
1532 g_return_if_fail (h >= 0.0 && h <= 1.0);
1533 g_return_if_fail (s >= 0.0 && s <= 1.0);
1534 g_return_if_fail (v >= 0.0 && v <= 1.0);
1536 hsv_to_rgb (&h, &s, &v);
1553 * @h: Return value for the hue component.
1554 * @s: Return value for the saturation component.
1555 * @v: Return value for the value component.
1557 * Converts a color from RGB space to HSV. Input values must be in the
1558 * [0.0, 1.0] range; output values will be in the same range.
1561 gtk_rgb_to_hsv (gdouble r,
1568 g_return_if_fail (r >= 0.0 && r <= 1.0);
1569 g_return_if_fail (g >= 0.0 && g <= 1.0);
1570 g_return_if_fail (b >= 0.0 && b <= 1.0);
1572 rgb_to_hsv (&r, &g, &b);
1585 gtk_hsv_move (GtkHSV *hsv,
1586 GtkDirectionType dir)
1589 gdouble hue, sat, val;
1590 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1591 gint x, y; /* position in triangle */
1599 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1601 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1602 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1604 #define HUE_DELTA 0.002
1608 if (priv->focus_on_ring)
1613 compute_sv (hsv, x, y, &sat, &val);
1618 if (priv->focus_on_ring)
1623 compute_sv (hsv, x, y, &sat, &val);
1628 if (priv->focus_on_ring)
1633 compute_sv (hsv, x, y, &sat, &val);
1638 if (priv->focus_on_ring)
1644 compute_sv (hsv, x, y, &sat, &val);
1649 /* we don't care about the tab directions */
1659 gtk_hsv_set_color (hsv, hue, sat, val);
1662 #define __GTK_HSV_C__
1663 #include "gtkaliasdef.c"