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"
44 /* Default width/height */
45 #define DEFAULT_SIZE 100
47 /* Default ring width */
48 #define DEFAULT_RING_WIDTH 10
58 /* Private part of the GtkHSV structure */
66 /* Size and ring width */
70 /* Window for capturing events */
76 guint focus_on_ring : 1;
88 static void gtk_hsv_destroy (GtkObject *object);
89 static void gtk_hsv_map (GtkWidget *widget);
90 static void gtk_hsv_unmap (GtkWidget *widget);
91 static void gtk_hsv_realize (GtkWidget *widget);
92 static void gtk_hsv_unrealize (GtkWidget *widget);
93 static void gtk_hsv_size_request (GtkWidget *widget,
94 GtkRequisition *requisition);
95 static void gtk_hsv_size_allocate (GtkWidget *widget,
96 GtkAllocation *allocation);
97 static gint gtk_hsv_button_press (GtkWidget *widget,
98 GdkEventButton *event);
99 static gint gtk_hsv_button_release (GtkWidget *widget,
100 GdkEventButton *event);
101 static gint gtk_hsv_motion (GtkWidget *widget,
102 GdkEventMotion *event);
103 static gint gtk_hsv_expose (GtkWidget *widget,
104 GdkEventExpose *event);
105 static gboolean gtk_hsv_grab_broken (GtkWidget *widget,
106 GdkEventGrabBroken *event);
107 static gboolean gtk_hsv_focus (GtkWidget *widget,
108 GtkDirectionType direction);
109 static void gtk_hsv_move (GtkHSV *hsv,
110 GtkDirectionType dir);
112 static guint hsv_signals[LAST_SIGNAL];
114 G_DEFINE_TYPE (GtkHSV, gtk_hsv, GTK_TYPE_WIDGET)
116 /* Class initialization function for the HSV color selector */
118 gtk_hsv_class_init (GtkHSVClass *class)
120 GObjectClass *gobject_class;
121 GtkObjectClass *object_class;
122 GtkWidgetClass *widget_class;
123 GtkHSVClass *hsv_class;
124 GtkBindingSet *binding_set;
126 gobject_class = (GObjectClass *) class;
127 object_class = (GtkObjectClass *) class;
128 widget_class = (GtkWidgetClass *) class;
129 hsv_class = GTK_HSV_CLASS (class);
131 object_class->destroy = gtk_hsv_destroy;
133 widget_class->map = gtk_hsv_map;
134 widget_class->unmap = gtk_hsv_unmap;
135 widget_class->realize = gtk_hsv_realize;
136 widget_class->unrealize = gtk_hsv_unrealize;
137 widget_class->size_request = gtk_hsv_size_request;
138 widget_class->size_allocate = gtk_hsv_size_allocate;
139 widget_class->button_press_event = gtk_hsv_button_press;
140 widget_class->button_release_event = gtk_hsv_button_release;
141 widget_class->motion_notify_event = gtk_hsv_motion;
142 widget_class->expose_event = gtk_hsv_expose;
143 widget_class->focus = gtk_hsv_focus;
144 widget_class->grab_broken_event = gtk_hsv_grab_broken;
146 hsv_class->move = gtk_hsv_move;
148 hsv_signals[CHANGED] =
149 g_signal_new (I_("changed"),
150 G_OBJECT_CLASS_TYPE (object_class),
152 G_STRUCT_OFFSET (GtkHSVClass, changed),
154 _gtk_marshal_VOID__VOID,
158 g_signal_new (I_("move"),
159 G_OBJECT_CLASS_TYPE (object_class),
160 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
161 G_STRUCT_OFFSET (GtkHSVClass, move),
163 _gtk_marshal_VOID__ENUM,
165 GTK_TYPE_DIRECTION_TYPE);
167 binding_set = gtk_binding_set_by_class (class);
169 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
171 G_TYPE_ENUM, GTK_DIR_UP);
172 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
174 G_TYPE_ENUM, GTK_DIR_UP);
176 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
178 G_TYPE_ENUM, GTK_DIR_DOWN);
179 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
181 G_TYPE_ENUM, GTK_DIR_DOWN);
184 gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
186 G_TYPE_ENUM, GTK_DIR_RIGHT);
187 gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
189 G_TYPE_ENUM, GTK_DIR_RIGHT);
191 gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
193 G_TYPE_ENUM, GTK_DIR_LEFT);
194 gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
196 G_TYPE_ENUM, GTK_DIR_LEFT);
198 g_type_class_add_private (gobject_class, sizeof (GtkHSVPriv));
201 /* Object initialization function for the HSV color selector */
203 gtk_hsv_init (GtkHSV *hsv)
207 priv = G_TYPE_INSTANCE_GET_PRIVATE (hsv, GTK_TYPE_HSV, GtkHSVPriv);
211 gtk_widget_set_has_window (GTK_WIDGET (hsv), FALSE);
212 gtk_widget_set_can_focus (GTK_WIDGET (hsv), TRUE);
218 priv->size = DEFAULT_SIZE;
219 priv->ring_width = DEFAULT_RING_WIDTH;
222 /* Destroy handler for the HSV color selector */
224 gtk_hsv_destroy (GtkObject *object)
226 GTK_OBJECT_CLASS (gtk_hsv_parent_class)->destroy (object);
229 /* Default signal handlers */
232 /* Map handler for the HSV color selector */
235 gtk_hsv_map (GtkWidget *widget)
237 GtkHSV *hsv = GTK_HSV (widget);
238 GtkHSVPriv *priv = hsv->priv;
240 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->map (widget);
242 gdk_window_show (priv->window);
245 /* Unmap handler for the HSV color selector */
248 gtk_hsv_unmap (GtkWidget *widget)
250 GtkHSV *hsv = GTK_HSV (widget);
251 GtkHSVPriv *priv = hsv->priv;
253 gdk_window_hide (priv->window);
255 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unmap (widget);
258 /* Realize handler for the HSV color selector */
260 gtk_hsv_realize (GtkWidget *widget)
262 GtkHSV *hsv = GTK_HSV (widget);
263 GtkHSVPriv *priv = hsv->priv;
264 GdkWindow *parent_window;
268 gtk_widget_set_realized (widget, TRUE);
272 attr.window_type = GDK_WINDOW_CHILD;
273 attr.x = widget->allocation.x;
274 attr.y = widget->allocation.y;
275 attr.width = widget->allocation.width;
276 attr.height = widget->allocation.height;
277 attr.wclass = GDK_INPUT_ONLY;
278 attr.event_mask = gtk_widget_get_events (widget);
279 attr.event_mask |= (GDK_KEY_PRESS_MASK
280 | GDK_BUTTON_PRESS_MASK
281 | GDK_BUTTON_RELEASE_MASK
282 | GDK_POINTER_MOTION_MASK
283 | GDK_ENTER_NOTIFY_MASK
284 | GDK_LEAVE_NOTIFY_MASK);
286 attr_mask = GDK_WA_X | GDK_WA_Y;
288 parent_window = gtk_widget_get_parent_window (widget);
290 widget->window = parent_window;
291 g_object_ref (widget->window);
293 priv->window = gdk_window_new (parent_window, &attr, attr_mask);
294 gdk_window_set_user_data (priv->window, hsv);
296 widget->style = gtk_style_attach (widget->style, widget->window);
299 /* Unrealize handler for the HSV color selector */
301 gtk_hsv_unrealize (GtkWidget *widget)
303 GtkHSV *hsv = GTK_HSV (widget);
304 GtkHSVPriv *priv = hsv->priv;
306 gdk_window_set_user_data (priv->window, NULL);
307 gdk_window_destroy (priv->window);
310 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unrealize (widget);
313 /* Size_request handler for the HSV color selector */
315 gtk_hsv_size_request (GtkWidget *widget,
316 GtkRequisition *requisition)
318 GtkHSV *hsv = GTK_HSV (widget);
319 GtkHSVPriv *priv = hsv->priv;
323 gtk_widget_style_get (widget,
324 "focus-line-width", &focus_width,
325 "focus-padding", &focus_pad,
328 requisition->width = priv->size + 2 * (focus_width + focus_pad);
329 requisition->height = priv->size + 2 * (focus_width + focus_pad);
332 /* Size_allocate handler for the HSV color selector */
334 gtk_hsv_size_allocate (GtkWidget *widget,
335 GtkAllocation *allocation)
337 GtkHSV *hsv = GTK_HSV (widget);
338 GtkHSVPriv *priv = hsv->priv;
340 widget->allocation = *allocation;
342 if (gtk_widget_get_realized (widget))
343 gdk_window_move_resize (priv->window,
351 /* Utility functions */
353 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
355 /* Converts from HSV to RGB */
357 hsv_to_rgb (gdouble *h,
361 gdouble hue, saturation, value;
380 p = value * (1.0 - saturation);
381 q = value * (1.0 - saturation * f);
382 t = value * (1.0 - saturation * (1.0 - f));
423 g_assert_not_reached ();
428 /* Converts from RGB to HSV */
430 rgb_to_hsv (gdouble *r,
434 gdouble red, green, blue;
473 s = (max - min) / max;
484 h = (green - blue) / delta;
485 else if (green == max)
486 h = 2 + (blue - red) / delta;
487 else if (blue == max)
488 h = 4 + (red - green) / delta;
503 /* Computes the vertices of the saturation/value triangle */
505 compute_triangle (GtkHSV *hsv,
513 GtkHSVPriv *priv = hsv->priv;
516 gdouble inner, outer;
519 center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
520 center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
521 outer = priv->size / 2.0;
522 inner = outer - priv->ring_width;
523 angle = priv->h * 2.0 * G_PI;
525 *hx = floor (center_x + cos (angle) * inner + 0.5);
526 *hy = floor (center_y - sin (angle) * inner + 0.5);
527 *sx = floor (center_x + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
528 *sy = floor (center_y - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
529 *vx = floor (center_x + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
530 *vy = floor (center_y - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
533 /* Computes whether a point is inside the hue ring */
535 is_in_ring (GtkHSV *hsv,
539 GtkHSVPriv *priv = hsv->priv;
540 gdouble dx, dy, dist;
543 gdouble inner, outer;
545 center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
546 center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
547 outer = priv->size / 2.0;
548 inner = outer - priv->ring_width;
552 dist = dx * dx + dy * dy;
554 return (dist >= inner * inner && dist <= outer * outer);
557 /* Computes a saturation/value pair based on the mouse coordinates */
559 compute_sv (GtkHSV *hsv,
565 int ihx, ihy, isx, isy, ivx, ivy;
566 double hx, hy, sx, sy, vx, vy;
570 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
571 center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
572 center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
582 if (vx * (x - sx) + vy * (y - sy) < 0.0)
585 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
586 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
593 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
596 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
597 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
604 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
607 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
608 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
617 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
618 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
630 if (fabs (hy - vy) < fabs (hx - vx))
631 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
633 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
643 /* Computes whether a point is inside the saturation/value triangle */
645 is_in_triangle (GtkHSV *hsv,
649 int hx, hy, sx, sy, vx, vy;
652 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
654 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
656 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
657 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
659 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
662 /* Computes a value based on the mouse coordinates */
664 compute_v (GtkHSV *hsv,
673 center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
674 center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
678 angle = atan2 (dy, dx);
682 return angle / (2.0 * G_PI);
688 set_cross_grab (GtkHSV *hsv,
691 GtkHSVPriv *priv = hsv->priv;
694 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
696 gdk_pointer_grab (priv->window, FALSE,
697 (GDK_POINTER_MOTION_MASK
698 | GDK_POINTER_MOTION_HINT_MASK
699 | GDK_BUTTON_RELEASE_MASK),
703 gdk_cursor_unref (cursor);
707 gtk_hsv_grab_broken (GtkWidget *widget,
708 GdkEventGrabBroken *event)
710 GtkHSV *hsv = GTK_HSV (widget);
711 GtkHSVPriv *priv = hsv->priv;
713 priv->mode = DRAG_NONE;
718 /* Button_press_event handler for the HSV color selector */
720 gtk_hsv_button_press (GtkWidget *widget,
721 GdkEventButton *event)
723 GtkHSV *hsv = GTK_HSV (widget);
724 GtkHSVPriv *priv = hsv->priv;
727 if (priv->mode != DRAG_NONE || event->button != 1)
733 if (is_in_ring (hsv, x, y))
736 set_cross_grab (hsv, event->time);
738 gtk_hsv_set_color (hsv,
739 compute_v (hsv, x, y),
743 gtk_widget_grab_focus (widget);
744 priv->focus_on_ring = TRUE;
749 if (is_in_triangle (hsv, x, y))
753 priv->mode = DRAG_SV;
754 set_cross_grab (hsv, event->time);
756 compute_sv (hsv, x, y, &s, &v);
757 gtk_hsv_set_color (hsv, priv->h, s, v);
759 gtk_widget_grab_focus (widget);
760 priv->focus_on_ring = FALSE;
768 /* Button_release_event handler for the HSV color selector */
770 gtk_hsv_button_release (GtkWidget *widget,
771 GdkEventButton *event)
773 GtkHSV *hsv = GTK_HSV (widget);
774 GtkHSVPriv *priv = hsv->priv;
778 if (priv->mode == DRAG_NONE || event->button != 1)
781 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
782 * can see that this is the final color state.
786 priv->mode = DRAG_NONE;
792 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
793 else if (mode == DRAG_SV) {
796 compute_sv (hsv, x, y, &s, &v);
797 gtk_hsv_set_color (hsv, priv->h, s, v);
799 g_assert_not_reached ();
801 gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
806 /* Motion_notify_event handler for the HSV color selector */
808 gtk_hsv_motion (GtkWidget *widget,
809 GdkEventMotion *event)
811 GtkHSV *hsv = GTK_HSV (widget);
812 GtkHSVPriv *priv = hsv->priv;
813 GdkModifierType mods;
816 if (priv->mode == DRAG_NONE)
819 gdk_event_request_motions (event);
824 if (priv->mode == DRAG_H)
826 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
829 else if (priv->mode == DRAG_SV)
833 compute_sv (hsv, x, y, &s, &v);
834 gtk_hsv_set_color (hsv, priv->h, s, v);
838 g_assert_not_reached ();
845 /* Paints the hue ring */
847 paint_ring (GtkHSV *hsv,
854 GtkHSVPriv *priv = hsv->priv;
855 GtkWidget *widget = GTK_WIDGET (hsv);
857 gdouble dx, dy, dist;
860 gdouble inner, outer;
865 cairo_surface_t *source;
871 gtk_widget_style_get (widget,
872 "focus-line-width", &focus_width,
873 "focus-padding", &focus_pad,
878 center_x = widget->allocation.width / 2.0;
879 center_y = widget->allocation.height / 2.0;
881 outer = priv->size / 2.0;
882 inner = outer - priv->ring_width;
884 /* Create an image initialized with the ring colors */
886 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
887 buf = g_new (guint32, height * stride / 4);
889 for (yy = 0; yy < height; yy++)
891 p = buf + yy * width;
893 dy = -(yy + y - center_y);
895 for (xx = 0; xx < width; xx++)
897 dx = xx + x - center_x;
899 dist = dx * dx + dy * dy;
900 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
906 angle = atan2 (dy, dx);
910 hue = angle / (2.0 * G_PI);
915 hsv_to_rgb (&r, &g, &b);
917 *p++ = (((int)floor (r * 255 + 0.5) << 16) |
918 ((int)floor (g * 255 + 0.5) << 8) |
919 (int)floor (b * 255 + 0.5));
923 source = cairo_image_surface_create_for_data ((unsigned char *)buf,
925 width, height, stride);
927 /* Now draw the value marker onto the source image, so that it
928 * will get properly clipped at the edges of the ring
930 source_cr = cairo_create (source);
935 hsv_to_rgb (&r, &g, &b);
937 if (INTENSITY (r, g, b) > 0.5)
938 cairo_set_source_rgb (source_cr, 0., 0., 0.);
940 cairo_set_source_rgb (source_cr, 1., 1., 1.);
942 cairo_move_to (source_cr, -x + center_x, - y + center_y);
943 cairo_line_to (source_cr,
944 -x + center_x + cos (priv->h * 2.0 * G_PI) * priv->size / 2,
945 -y + center_y - sin (priv->h * 2.0 * G_PI) * priv->size / 2);
946 cairo_stroke (source_cr);
947 cairo_destroy (source_cr);
949 /* Draw the ring using the source image */
953 cairo_set_source_surface (cr, source, x, y);
954 cairo_surface_destroy (source);
956 cairo_set_line_width (cr, priv->ring_width);
960 priv->size / 2. - priv->ring_width / 2.,
969 /* Converts an HSV triplet to an integer RGB triplet */
971 get_color (gdouble h,
978 hsv_to_rgb (&h, &s, &v);
980 *r = floor (h * 255 + 0.5);
981 *g = floor (s * 255 + 0.5);
982 *b = floor (v * 255 + 0.5);
985 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
987 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
988 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
991 /* Number of pixels we extend out from the edges when creating
992 * color source to avoid artifacts
996 /* Paints the HSV triangle */
998 paint_triangle (GtkHSV *hsv,
1005 GtkHSVPriv *priv = hsv->priv;
1006 GtkWidget *widget = GTK_WIDGET (hsv);
1007 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1008 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1009 gint x2, y2, r2, g2, b2; /* Second vertex */
1010 gint x3, y3, r3, g3, b3; /* Third vertex */
1012 guint32 *buf, *p, c;
1013 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1015 gint x_interp, y_interp;
1016 gint x_start, x_end;
1017 cairo_surface_t *source;
1024 /* Compute triangle's vertices */
1026 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1030 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1034 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1038 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1067 /* Shade the triangle */
1069 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
1070 buf = g_new (guint32, height * stride / 4);
1072 for (yy = 0; yy < height; yy++)
1074 p = buf + yy * width;
1076 if (yy + y >= y1 - PAD && yy + y < y3 + PAD) {
1077 y_interp = CLAMP (yy + y, y1, y3);
1081 xl = LERP (x1, x2, y1, y2, y_interp);
1083 rl = LERP (r1, r2, y1, y2, y_interp);
1084 gl = LERP (g1, g2, y1, y2, y_interp);
1085 bl = LERP (b1, b2, y1, y2, y_interp);
1089 xl = LERP (x2, x3, y2, y3, y_interp);
1091 rl = LERP (r2, r3, y2, y3, y_interp);
1092 gl = LERP (g2, g3, y2, y3, y_interp);
1093 bl = LERP (b2, b3, y2, y3, y_interp);
1096 xr = LERP (x1, x3, y1, y3, y_interp);
1098 rr = LERP (r1, r3, y1, y3, y_interp);
1099 gr = LERP (g1, g3, y1, y3, y_interp);
1100 br = LERP (b1, b3, y1, y3, y_interp);
1110 x_start = MAX (xl - PAD, x);
1111 x_end = MIN (xr + PAD, x + width);
1112 x_start = MIN (x_start, x_end);
1114 c = (rl << 16) | (gl << 8) | bl;
1116 for (xx = x; xx < x_start; xx++)
1119 for (; xx < x_end; xx++)
1121 x_interp = CLAMP (xx, xl, xr);
1123 *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
1124 (LERP (gl, gr, xl, xr, x_interp) << 8) |
1125 LERP (bl, br, xl, xr, x_interp));
1128 c = (rr << 16) | (gr << 8) | br;
1130 for (; xx < x + width; xx++)
1135 source = cairo_image_surface_create_for_data ((unsigned char *)buf,
1137 width, height, stride);
1139 /* Draw a triangle with the image as a source */
1141 cairo_set_source_surface (cr, source, x, y);
1142 cairo_surface_destroy (source);
1144 cairo_move_to (cr, x1, y1);
1145 cairo_line_to (cr, x2, y2);
1146 cairo_line_to (cr, x3, y3);
1147 cairo_close_path (cr);
1152 /* Draw value marker */
1154 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1155 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1160 hsv_to_rgb (&r, &g, &b);
1162 if (INTENSITY (r, g, b) > 0.5)
1164 detail = "colorwheel_light";
1165 cairo_set_source_rgb (cr, 0., 0., 0.);
1169 detail = "colorwheel_dark";
1170 cairo_set_source_rgb (cr, 1., 1., 1.);
1174 #define FOCUS_RADIUS 6
1176 cairo_new_path (cr);
1177 cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
1180 /* Draw focus outline */
1182 if (gtk_widget_has_focus (widget) &&
1183 !priv->focus_on_ring)
1188 gtk_widget_style_get (widget,
1189 "focus-line-width", &focus_width,
1190 "focus-padding", &focus_pad,
1193 gtk_paint_focus (widget->style, widget->window,
1194 gtk_widget_get_state (widget),
1195 NULL, widget, detail,
1196 widget->allocation.x + xx - FOCUS_RADIUS - focus_width - focus_pad,
1197 widget->allocation.y + yy - FOCUS_RADIUS - focus_width - focus_pad,
1198 2 * (FOCUS_RADIUS + focus_width + focus_pad),
1199 2 * (FOCUS_RADIUS + focus_width + focus_pad));
1204 /* Paints the contents of the HSV color selector */
1213 paint_ring (hsv, cr, x, y, width, height);
1214 paint_triangle (hsv, cr, x, y, width, height);
1217 /* Expose_event handler for the HSV color selector */
1219 gtk_hsv_expose (GtkWidget *widget,
1220 GdkEventExpose *event)
1222 GtkHSV *hsv = GTK_HSV (widget);
1223 GtkHSVPriv *priv = hsv->priv;
1224 GdkRectangle rect, dest;
1227 if (!(event->window == widget->window && gtk_widget_is_drawable (widget)))
1230 rect.x = widget->allocation.x;
1231 rect.y = widget->allocation.y;
1232 rect.width = widget->allocation.width;
1233 rect.height = widget->allocation.height;
1235 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1238 cr = gdk_cairo_create (widget->window);
1240 cairo_translate (cr, widget->allocation.x, widget->allocation.y);
1242 dest.x - widget->allocation.x,
1243 dest.y - widget->allocation.y,
1244 dest.width, dest.height);
1247 if (gtk_widget_has_focus (widget) && priv->focus_on_ring)
1248 gtk_paint_focus (widget->style, widget->window,
1249 gtk_widget_get_state (widget),
1250 &event->area, widget, NULL,
1251 widget->allocation.x,
1252 widget->allocation.y,
1253 widget->allocation.width,
1254 widget->allocation.height);
1260 gtk_hsv_focus (GtkWidget *widget,
1261 GtkDirectionType dir)
1263 GtkHSV *hsv = GTK_HSV (widget);
1264 GtkHSVPriv *priv = hsv->priv;
1266 if (!gtk_widget_has_focus (widget))
1268 if (dir == GTK_DIR_TAB_BACKWARD)
1269 priv->focus_on_ring = FALSE;
1271 priv->focus_on_ring = TRUE;
1273 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1280 if (priv->focus_on_ring)
1283 priv->focus_on_ring = TRUE;
1287 if (priv->focus_on_ring)
1288 priv->focus_on_ring = FALSE;
1294 case GTK_DIR_TAB_BACKWARD:
1295 if (priv->focus_on_ring)
1298 priv->focus_on_ring = TRUE;
1302 case GTK_DIR_TAB_FORWARD:
1303 if (priv->focus_on_ring)
1304 priv->focus_on_ring = FALSE;
1310 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1318 * Creates a new HSV color selector.
1320 * Return value: A newly-created HSV color selector.
1327 return g_object_new (GTK_TYPE_HSV, NULL);
1331 * gtk_hsv_set_color:
1332 * @hsv: An HSV color selector
1337 * Sets the current color in an HSV color selector.
1338 * Color component values must be in the [0.0, 1.0] range.
1343 gtk_hsv_set_color (GtkHSV *hsv,
1350 g_return_if_fail (GTK_IS_HSV (hsv));
1351 g_return_if_fail (h >= 0.0 && h <= 1.0);
1352 g_return_if_fail (s >= 0.0 && s <= 1.0);
1353 g_return_if_fail (v >= 0.0 && v <= 1.0);
1361 g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1363 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1367 * gtk_hsv_get_color:
1368 * @hsv: An HSV color selector
1369 * @h: Return value for the hue
1370 * @s: Return value for the saturation
1371 * @v: Return value for the value
1373 * Queries the current color in an HSV color selector.
1374 * Returned values will be in the [0.0, 1.0] range.
1379 gtk_hsv_get_color (GtkHSV *hsv,
1384 GtkHSVPriv *priv = hsv->priv;
1386 g_return_if_fail (GTK_IS_HSV (hsv));
1401 * gtk_hsv_set_metrics:
1402 * @hsv: An HSV color selector
1403 * @size: Diameter for the hue ring
1404 * @ring_width: Width of the hue ring
1406 * Sets the size and ring width of an HSV color selector.
1411 gtk_hsv_set_metrics (GtkHSV *hsv,
1415 GtkHSVPriv *priv = hsv->priv;
1418 g_return_if_fail (GTK_IS_HSV (hsv));
1419 g_return_if_fail (size > 0);
1420 g_return_if_fail (ring_width > 0);
1421 g_return_if_fail (2 * ring_width + 1 <= size);
1425 same_size = (priv->size == size);
1428 priv->ring_width = ring_width;
1431 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1433 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1437 * gtk_hsv_get_metrics:
1438 * @hsv: An HSV color selector
1439 * @size: Return value for the diameter of the hue ring
1440 * @ring_width: Return value for the width of the hue ring
1442 * Queries the size and ring width of an HSV color selector.
1447 gtk_hsv_get_metrics (GtkHSV *hsv,
1451 GtkHSVPriv *priv = hsv->priv;
1453 g_return_if_fail (GTK_IS_HSV (hsv));
1461 *ring_width = priv->ring_width;
1465 * gtk_hsv_is_adjusting:
1468 * An HSV color selector can be said to be adjusting if multiple rapid
1469 * changes are being made to its value, for example, when the user is
1470 * adjusting the value with the mouse. This function queries whether
1471 * the HSV color selector is being adjusted or not.
1473 * Return value: %TRUE if clients can ignore changes to the color value,
1474 * since they may be transitory, or %FALSE if they should consider
1475 * the color value status to be final.
1480 gtk_hsv_is_adjusting (GtkHSV *hsv)
1482 GtkHSVPriv *priv = hsv->priv;
1484 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1488 return priv->mode != DRAG_NONE;
1496 * @r: Return value for the red component
1497 * @g: Return value for the green component
1498 * @b: Return value for the blue component
1500 * Converts a color from HSV space to RGB.
1501 * Input values must be in the [0.0, 1.0] range;
1502 * output values will be in the same range.
1507 gtk_hsv_to_rgb (gdouble h,
1514 g_return_if_fail (h >= 0.0 && h <= 1.0);
1515 g_return_if_fail (s >= 0.0 && s <= 1.0);
1516 g_return_if_fail (v >= 0.0 && v <= 1.0);
1518 hsv_to_rgb (&h, &s, &v);
1535 * @h: Return value for the hue component
1536 * @s: Return value for the saturation component
1537 * @v: Return value for the value component
1539 * Converts a color from RGB space to HSV.
1540 * Input values must be in the [0.0, 1.0] range;
1541 * output values will be in the same range.
1546 gtk_rgb_to_hsv (gdouble r,
1553 g_return_if_fail (r >= 0.0 && r <= 1.0);
1554 g_return_if_fail (g >= 0.0 && g <= 1.0);
1555 g_return_if_fail (b >= 0.0 && b <= 1.0);
1557 rgb_to_hsv (&r, &g, &b);
1570 gtk_hsv_move (GtkHSV *hsv,
1571 GtkDirectionType dir)
1573 GtkHSVPriv *priv = hsv->priv;
1574 gdouble hue, sat, val;
1575 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1576 gint x, y; /* position in triangle */
1582 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1584 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1585 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1587 #define HUE_DELTA 0.002
1591 if (priv->focus_on_ring)
1596 compute_sv (hsv, x, y, &sat, &val);
1601 if (priv->focus_on_ring)
1606 compute_sv (hsv, x, y, &sat, &val);
1611 if (priv->focus_on_ring)
1616 compute_sv (hsv, x, y, &sat, &val);
1621 if (priv->focus_on_ring)
1627 compute_sv (hsv, x, y, &sat, &val);
1632 /* we don't care about the tab directions */
1642 gtk_hsv_set_color (hsv, hue, sat, val);