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 GtkAllocation allocation;
265 GdkWindow *parent_window;
269 gtk_widget_set_realized (widget, TRUE);
273 gtk_widget_get_allocation (widget, &allocation);
275 attr.window_type = GDK_WINDOW_CHILD;
276 attr.x = allocation.x;
277 attr.y = allocation.y;
278 attr.width = allocation.width;
279 attr.height = allocation.height;
280 attr.wclass = GDK_INPUT_ONLY;
281 attr.event_mask = gtk_widget_get_events (widget);
282 attr.event_mask |= (GDK_KEY_PRESS_MASK
283 | GDK_BUTTON_PRESS_MASK
284 | GDK_BUTTON_RELEASE_MASK
285 | GDK_POINTER_MOTION_MASK
286 | GDK_ENTER_NOTIFY_MASK
287 | GDK_LEAVE_NOTIFY_MASK);
288 attr_mask = GDK_WA_X | GDK_WA_Y;
290 parent_window = gtk_widget_get_parent_window (widget);
291 gtk_widget_set_window (widget, parent_window);
292 g_object_ref (parent_window);
294 priv->window = gdk_window_new (parent_window, &attr, attr_mask);
295 gdk_window_set_user_data (priv->window, hsv);
297 gtk_widget_style_attach (widget);
300 /* Unrealize handler for the HSV color selector */
302 gtk_hsv_unrealize (GtkWidget *widget)
304 GtkHSV *hsv = GTK_HSV (widget);
305 GtkHSVPriv *priv = hsv->priv;
307 gdk_window_set_user_data (priv->window, NULL);
308 gdk_window_destroy (priv->window);
311 GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unrealize (widget);
314 /* Size_request handler for the HSV color selector */
316 gtk_hsv_size_request (GtkWidget *widget,
317 GtkRequisition *requisition)
319 GtkHSV *hsv = GTK_HSV (widget);
320 GtkHSVPriv *priv = hsv->priv;
324 gtk_widget_style_get (widget,
325 "focus-line-width", &focus_width,
326 "focus-padding", &focus_pad,
329 requisition->width = priv->size + 2 * (focus_width + focus_pad);
330 requisition->height = priv->size + 2 * (focus_width + focus_pad);
333 /* Size_allocate handler for the HSV color selector */
335 gtk_hsv_size_allocate (GtkWidget *widget,
336 GtkAllocation *allocation)
338 GtkHSV *hsv = GTK_HSV (widget);
339 GtkHSVPriv *priv = hsv->priv;
341 gtk_widget_set_allocation (widget, allocation);
343 if (gtk_widget_get_realized (widget))
344 gdk_window_move_resize (priv->window,
352 /* Utility functions */
354 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
356 /* Converts from HSV to RGB */
358 hsv_to_rgb (gdouble *h,
362 gdouble hue, saturation, value;
381 p = value * (1.0 - saturation);
382 q = value * (1.0 - saturation * f);
383 t = value * (1.0 - saturation * (1.0 - f));
424 g_assert_not_reached ();
429 /* Converts from RGB to HSV */
431 rgb_to_hsv (gdouble *r,
435 gdouble red, green, blue;
474 s = (max - min) / max;
485 h = (green - blue) / delta;
486 else if (green == max)
487 h = 2 + (blue - red) / delta;
488 else if (blue == max)
489 h = 4 + (red - green) / delta;
504 /* Computes the vertices of the saturation/value triangle */
506 compute_triangle (GtkHSV *hsv,
514 GtkHSVPriv *priv = hsv->priv;
515 GtkAllocation allocation;
516 GtkWidget *widget = GTK_WIDGET (hsv);
519 gdouble inner, outer;
522 gtk_widget_get_allocation (widget, &allocation);
524 center_x = allocation.width / 2.0;
525 center_y = allocation.height / 2.0;
526 outer = priv->size / 2.0;
527 inner = outer - priv->ring_width;
528 angle = priv->h * 2.0 * G_PI;
530 *hx = floor (center_x + cos (angle) * inner + 0.5);
531 *hy = floor (center_y - sin (angle) * inner + 0.5);
532 *sx = floor (center_x + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
533 *sy = floor (center_y - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
534 *vx = floor (center_x + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
535 *vy = floor (center_y - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
538 /* Computes whether a point is inside the hue ring */
540 is_in_ring (GtkHSV *hsv,
544 GtkHSVPriv *priv = hsv->priv;
545 GtkAllocation allocation;
546 GtkWidget *widget = GTK_WIDGET (hsv);
547 gdouble dx, dy, dist;
550 gdouble inner, outer;
552 gtk_widget_get_allocation (widget, &allocation);
554 center_x = allocation.width / 2.0;
555 center_y = allocation.height / 2.0;
556 outer = priv->size / 2.0;
557 inner = outer - priv->ring_width;
561 dist = dx * dx + dy * dy;
563 return (dist >= inner * inner && dist <= outer * outer);
566 /* Computes a saturation/value pair based on the mouse coordinates */
568 compute_sv (GtkHSV *hsv,
574 GtkWidget *widget = GTK_WIDGET (hsv);
575 GtkAllocation allocation;
576 int ihx, ihy, isx, isy, ivx, ivy;
577 double hx, hy, sx, sy, vx, vy;
581 gtk_widget_get_allocation (widget, &allocation);
583 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
584 center_x = allocation.width / 2.0;
585 center_y = allocation.height / 2.0;
595 if (vx * (x - sx) + vy * (y - sy) < 0.0)
598 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
599 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
606 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
609 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
610 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
617 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
620 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
621 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
630 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
631 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
643 if (fabs (hy - vy) < fabs (hx - vx))
644 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
646 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
656 /* Computes whether a point is inside the saturation/value triangle */
658 is_in_triangle (GtkHSV *hsv,
662 int hx, hy, sx, sy, vx, vy;
665 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
667 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
669 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
670 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
672 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
675 /* Computes a value based on the mouse coordinates */
677 compute_v (GtkHSV *hsv,
681 GtkAllocation allocation;
682 GtkWidget *widget = GTK_WIDGET (hsv);
688 gtk_widget_get_allocation (widget, &allocation);
690 center_x = allocation.width / 2.0;
691 center_y = allocation.height / 2.0;
695 angle = atan2 (dy, dx);
699 return angle / (2.0 * G_PI);
705 set_cross_grab (GtkHSV *hsv,
708 GtkHSVPriv *priv = hsv->priv;
711 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
713 gdk_pointer_grab (priv->window, FALSE,
714 (GDK_POINTER_MOTION_MASK
715 | GDK_POINTER_MOTION_HINT_MASK
716 | GDK_BUTTON_RELEASE_MASK),
720 gdk_cursor_unref (cursor);
724 gtk_hsv_grab_broken (GtkWidget *widget,
725 GdkEventGrabBroken *event)
727 GtkHSV *hsv = GTK_HSV (widget);
728 GtkHSVPriv *priv = hsv->priv;
730 priv->mode = DRAG_NONE;
735 /* Button_press_event handler for the HSV color selector */
737 gtk_hsv_button_press (GtkWidget *widget,
738 GdkEventButton *event)
740 GtkHSV *hsv = GTK_HSV (widget);
741 GtkHSVPriv *priv = hsv->priv;
744 if (priv->mode != DRAG_NONE || event->button != 1)
750 if (is_in_ring (hsv, x, y))
753 set_cross_grab (hsv, event->time);
755 gtk_hsv_set_color (hsv,
756 compute_v (hsv, x, y),
760 gtk_widget_grab_focus (widget);
761 priv->focus_on_ring = TRUE;
766 if (is_in_triangle (hsv, x, y))
770 priv->mode = DRAG_SV;
771 set_cross_grab (hsv, event->time);
773 compute_sv (hsv, x, y, &s, &v);
774 gtk_hsv_set_color (hsv, priv->h, s, v);
776 gtk_widget_grab_focus (widget);
777 priv->focus_on_ring = FALSE;
785 /* Button_release_event handler for the HSV color selector */
787 gtk_hsv_button_release (GtkWidget *widget,
788 GdkEventButton *event)
790 GtkHSV *hsv = GTK_HSV (widget);
791 GtkHSVPriv *priv = hsv->priv;
795 if (priv->mode == DRAG_NONE || event->button != 1)
798 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
799 * can see that this is the final color state.
803 priv->mode = DRAG_NONE;
809 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
810 else if (mode == DRAG_SV) {
813 compute_sv (hsv, x, y, &s, &v);
814 gtk_hsv_set_color (hsv, priv->h, s, v);
816 g_assert_not_reached ();
818 gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
823 /* Motion_notify_event handler for the HSV color selector */
825 gtk_hsv_motion (GtkWidget *widget,
826 GdkEventMotion *event)
828 GtkHSV *hsv = GTK_HSV (widget);
829 GtkHSVPriv *priv = hsv->priv;
830 GdkModifierType mods;
833 if (priv->mode == DRAG_NONE)
836 gdk_event_request_motions (event);
841 if (priv->mode == DRAG_H)
843 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
846 else if (priv->mode == DRAG_SV)
850 compute_sv (hsv, x, y, &s, &v);
851 gtk_hsv_set_color (hsv, priv->h, s, v);
855 g_assert_not_reached ();
862 /* Paints the hue ring */
864 paint_ring (GtkHSV *hsv,
871 GtkHSVPriv *priv = hsv->priv;
872 GtkAllocation allocation;
873 GtkWidget *widget = GTK_WIDGET (hsv);
875 gdouble dx, dy, dist;
878 gdouble inner, outer;
883 cairo_surface_t *source;
889 gtk_widget_style_get (widget,
890 "focus-line-width", &focus_width,
891 "focus-padding", &focus_pad,
894 gtk_widget_get_allocation (widget, &allocation);
896 center_x = allocation.width / 2.0;
897 center_y = allocation.height / 2.0;
899 outer = priv->size / 2.0;
900 inner = outer - priv->ring_width;
902 /* Create an image initialized with the ring colors */
904 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
905 buf = g_new (guint32, height * stride / 4);
907 for (yy = 0; yy < height; yy++)
909 p = buf + yy * width;
911 dy = -(yy + y - center_y);
913 for (xx = 0; xx < width; xx++)
915 dx = xx + x - center_x;
917 dist = dx * dx + dy * dy;
918 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
924 angle = atan2 (dy, dx);
928 hue = angle / (2.0 * G_PI);
933 hsv_to_rgb (&r, &g, &b);
935 *p++ = (((int)floor (r * 255 + 0.5) << 16) |
936 ((int)floor (g * 255 + 0.5) << 8) |
937 (int)floor (b * 255 + 0.5));
941 source = cairo_image_surface_create_for_data ((unsigned char *)buf,
943 width, height, stride);
945 /* Now draw the value marker onto the source image, so that it
946 * will get properly clipped at the edges of the ring
948 source_cr = cairo_create (source);
953 hsv_to_rgb (&r, &g, &b);
955 if (INTENSITY (r, g, b) > 0.5)
956 cairo_set_source_rgb (source_cr, 0., 0., 0.);
958 cairo_set_source_rgb (source_cr, 1., 1., 1.);
960 cairo_move_to (source_cr, -x + center_x, - y + center_y);
961 cairo_line_to (source_cr,
962 -x + center_x + cos (priv->h * 2.0 * G_PI) * priv->size / 2,
963 -y + center_y - sin (priv->h * 2.0 * G_PI) * priv->size / 2);
964 cairo_stroke (source_cr);
965 cairo_destroy (source_cr);
967 /* Draw the ring using the source image */
971 cairo_set_source_surface (cr, source, x, y);
972 cairo_surface_destroy (source);
974 cairo_set_line_width (cr, priv->ring_width);
978 priv->size / 2. - priv->ring_width / 2.,
987 /* Converts an HSV triplet to an integer RGB triplet */
989 get_color (gdouble h,
996 hsv_to_rgb (&h, &s, &v);
998 *r = floor (h * 255 + 0.5);
999 *g = floor (s * 255 + 0.5);
1000 *b = floor (v * 255 + 0.5);
1003 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1005 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
1006 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
1009 /* Number of pixels we extend out from the edges when creating
1010 * color source to avoid artifacts
1014 /* Paints the HSV triangle */
1016 paint_triangle (GtkHSV *hsv,
1023 GtkHSVPriv *priv = hsv->priv;
1024 GtkWidget *widget = GTK_WIDGET (hsv);
1025 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1026 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1027 gint x2, y2, r2, g2, b2; /* Second vertex */
1028 gint x3, y3, r3, g3, b3; /* Third vertex */
1030 guint32 *buf, *p, c;
1031 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1033 gint x_interp, y_interp;
1034 gint x_start, x_end;
1035 cairo_surface_t *source;
1042 /* Compute triangle's vertices */
1044 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1048 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1052 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1056 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1085 /* Shade the triangle */
1087 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
1088 buf = g_new (guint32, height * stride / 4);
1090 for (yy = 0; yy < height; yy++)
1092 p = buf + yy * width;
1094 if (yy + y >= y1 - PAD && yy + y < y3 + PAD) {
1095 y_interp = CLAMP (yy + y, y1, y3);
1099 xl = LERP (x1, x2, y1, y2, y_interp);
1101 rl = LERP (r1, r2, y1, y2, y_interp);
1102 gl = LERP (g1, g2, y1, y2, y_interp);
1103 bl = LERP (b1, b2, y1, y2, y_interp);
1107 xl = LERP (x2, x3, y2, y3, y_interp);
1109 rl = LERP (r2, r3, y2, y3, y_interp);
1110 gl = LERP (g2, g3, y2, y3, y_interp);
1111 bl = LERP (b2, b3, y2, y3, y_interp);
1114 xr = LERP (x1, x3, y1, y3, y_interp);
1116 rr = LERP (r1, r3, y1, y3, y_interp);
1117 gr = LERP (g1, g3, y1, y3, y_interp);
1118 br = LERP (b1, b3, y1, y3, y_interp);
1128 x_start = MAX (xl - PAD, x);
1129 x_end = MIN (xr + PAD, x + width);
1130 x_start = MIN (x_start, x_end);
1132 c = (rl << 16) | (gl << 8) | bl;
1134 for (xx = x; xx < x_start; xx++)
1137 for (; xx < x_end; xx++)
1139 x_interp = CLAMP (xx, xl, xr);
1141 *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
1142 (LERP (gl, gr, xl, xr, x_interp) << 8) |
1143 LERP (bl, br, xl, xr, x_interp));
1146 c = (rr << 16) | (gr << 8) | br;
1148 for (; xx < x + width; xx++)
1153 source = cairo_image_surface_create_for_data ((unsigned char *)buf,
1155 width, height, stride);
1157 /* Draw a triangle with the image as a source */
1159 cairo_set_source_surface (cr, source, x, y);
1160 cairo_surface_destroy (source);
1162 cairo_move_to (cr, x1, y1);
1163 cairo_line_to (cr, x2, y2);
1164 cairo_line_to (cr, x3, y3);
1165 cairo_close_path (cr);
1170 /* Draw value marker */
1172 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1173 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1178 hsv_to_rgb (&r, &g, &b);
1180 if (INTENSITY (r, g, b) > 0.5)
1182 detail = "colorwheel_light";
1183 cairo_set_source_rgb (cr, 0., 0., 0.);
1187 detail = "colorwheel_dark";
1188 cairo_set_source_rgb (cr, 1., 1., 1.);
1192 #define FOCUS_RADIUS 6
1194 cairo_new_path (cr);
1195 cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
1198 /* Draw focus outline */
1200 if (gtk_widget_has_focus (widget) &&
1201 !priv->focus_on_ring)
1203 GtkAllocation allocation;
1207 gtk_widget_style_get (widget,
1208 "focus-line-width", &focus_width,
1209 "focus-padding", &focus_pad,
1212 gtk_widget_get_allocation (widget, &allocation);
1214 gtk_paint_focus (gtk_widget_get_style (widget),
1215 gtk_widget_get_window (widget),
1216 gtk_widget_get_state (widget),
1217 NULL, widget, detail,
1218 allocation.x + xx - FOCUS_RADIUS - focus_width - focus_pad,
1219 allocation.y + yy - FOCUS_RADIUS - focus_width - focus_pad,
1220 2 * (FOCUS_RADIUS + focus_width + focus_pad),
1221 2 * (FOCUS_RADIUS + focus_width + focus_pad));
1225 /* Paints the contents of the HSV color selector */
1234 paint_ring (hsv, cr, x, y, width, height);
1235 paint_triangle (hsv, cr, x, y, width, height);
1238 /* Expose_event handler for the HSV color selector */
1240 gtk_hsv_expose (GtkWidget *widget,
1241 GdkEventExpose *event)
1243 GtkHSV *hsv = GTK_HSV (widget);
1244 GtkHSVPriv *priv = hsv->priv;
1245 GtkAllocation allocation;
1246 GdkRectangle rect, dest;
1250 window = gtk_widget_get_window (widget);
1252 if (!(event->window == window && gtk_widget_is_drawable (widget)))
1255 gtk_widget_get_allocation (widget, &allocation);
1257 rect.x = allocation.x;
1258 rect.y = allocation.y;
1259 rect.width = allocation.width;
1260 rect.height = allocation.height;
1262 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1265 cr = gdk_cairo_create (window);
1267 cairo_translate (cr, allocation.x, allocation.y);
1269 dest.x - allocation.x,
1270 dest.y - allocation.y,
1271 dest.width, dest.height);
1274 if (gtk_widget_has_focus (widget) && priv->focus_on_ring)
1275 gtk_paint_focus (gtk_widget_get_style (widget),
1277 gtk_widget_get_state (widget),
1278 &event->area, widget, NULL,
1279 allocation.x, allocation.y,
1280 allocation.width, allocation.height);
1286 gtk_hsv_focus (GtkWidget *widget,
1287 GtkDirectionType dir)
1289 GtkHSV *hsv = GTK_HSV (widget);
1290 GtkHSVPriv *priv = hsv->priv;
1292 if (!gtk_widget_has_focus (widget))
1294 if (dir == GTK_DIR_TAB_BACKWARD)
1295 priv->focus_on_ring = FALSE;
1297 priv->focus_on_ring = TRUE;
1299 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1306 if (priv->focus_on_ring)
1309 priv->focus_on_ring = TRUE;
1313 if (priv->focus_on_ring)
1314 priv->focus_on_ring = FALSE;
1320 case GTK_DIR_TAB_BACKWARD:
1321 if (priv->focus_on_ring)
1324 priv->focus_on_ring = TRUE;
1328 case GTK_DIR_TAB_FORWARD:
1329 if (priv->focus_on_ring)
1330 priv->focus_on_ring = FALSE;
1336 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1344 * Creates a new HSV color selector.
1346 * Return value: A newly-created HSV color selector.
1353 return g_object_new (GTK_TYPE_HSV, NULL);
1357 * gtk_hsv_set_color:
1358 * @hsv: An HSV color selector
1363 * Sets the current color in an HSV color selector.
1364 * Color component values must be in the [0.0, 1.0] range.
1369 gtk_hsv_set_color (GtkHSV *hsv,
1376 g_return_if_fail (GTK_IS_HSV (hsv));
1377 g_return_if_fail (h >= 0.0 && h <= 1.0);
1378 g_return_if_fail (s >= 0.0 && s <= 1.0);
1379 g_return_if_fail (v >= 0.0 && v <= 1.0);
1387 g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1389 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1393 * gtk_hsv_get_color:
1394 * @hsv: An HSV color selector
1395 * @h: Return value for the hue
1396 * @s: Return value for the saturation
1397 * @v: Return value for the value
1399 * Queries the current color in an HSV color selector.
1400 * Returned values will be in the [0.0, 1.0] range.
1405 gtk_hsv_get_color (GtkHSV *hsv,
1410 GtkHSVPriv *priv = hsv->priv;
1412 g_return_if_fail (GTK_IS_HSV (hsv));
1427 * gtk_hsv_set_metrics:
1428 * @hsv: An HSV color selector
1429 * @size: Diameter for the hue ring
1430 * @ring_width: Width of the hue ring
1432 * Sets the size and ring width of an HSV color selector.
1437 gtk_hsv_set_metrics (GtkHSV *hsv,
1441 GtkHSVPriv *priv = hsv->priv;
1444 g_return_if_fail (GTK_IS_HSV (hsv));
1445 g_return_if_fail (size > 0);
1446 g_return_if_fail (ring_width > 0);
1447 g_return_if_fail (2 * ring_width + 1 <= size);
1451 same_size = (priv->size == size);
1454 priv->ring_width = ring_width;
1457 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1459 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1463 * gtk_hsv_get_metrics:
1464 * @hsv: An HSV color selector
1465 * @size: Return value for the diameter of the hue ring
1466 * @ring_width: Return value for the width of the hue ring
1468 * Queries the size and ring width of an HSV color selector.
1473 gtk_hsv_get_metrics (GtkHSV *hsv,
1477 GtkHSVPriv *priv = hsv->priv;
1479 g_return_if_fail (GTK_IS_HSV (hsv));
1487 *ring_width = priv->ring_width;
1491 * gtk_hsv_is_adjusting:
1494 * An HSV color selector can be said to be adjusting if multiple rapid
1495 * changes are being made to its value, for example, when the user is
1496 * adjusting the value with the mouse. This function queries whether
1497 * the HSV color selector is being adjusted or not.
1499 * Return value: %TRUE if clients can ignore changes to the color value,
1500 * since they may be transitory, or %FALSE if they should consider
1501 * the color value status to be final.
1506 gtk_hsv_is_adjusting (GtkHSV *hsv)
1508 GtkHSVPriv *priv = hsv->priv;
1510 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1514 return priv->mode != DRAG_NONE;
1522 * @r: Return value for the red component
1523 * @g: Return value for the green component
1524 * @b: Return value for the blue component
1526 * Converts a color from HSV space to RGB.
1527 * Input values must be in the [0.0, 1.0] range;
1528 * output values will be in the same range.
1533 gtk_hsv_to_rgb (gdouble h,
1540 g_return_if_fail (h >= 0.0 && h <= 1.0);
1541 g_return_if_fail (s >= 0.0 && s <= 1.0);
1542 g_return_if_fail (v >= 0.0 && v <= 1.0);
1544 hsv_to_rgb (&h, &s, &v);
1561 * @h: Return value for the hue component
1562 * @s: Return value for the saturation component
1563 * @v: Return value for the value component
1565 * Converts a color from RGB space to HSV.
1566 * Input values must be in the [0.0, 1.0] range;
1567 * output values will be in the same range.
1572 gtk_rgb_to_hsv (gdouble r,
1579 g_return_if_fail (r >= 0.0 && r <= 1.0);
1580 g_return_if_fail (g >= 0.0 && g <= 1.0);
1581 g_return_if_fail (b >= 0.0 && b <= 1.0);
1583 rgb_to_hsv (&r, &g, &b);
1596 gtk_hsv_move (GtkHSV *hsv,
1597 GtkDirectionType dir)
1599 GtkHSVPriv *priv = hsv->priv;
1600 gdouble hue, sat, val;
1601 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1602 gint x, y; /* position in triangle */
1608 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1610 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1611 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1613 #define HUE_DELTA 0.002
1617 if (priv->focus_on_ring)
1622 compute_sv (hsv, x, y, &sat, &val);
1627 if (priv->focus_on_ring)
1632 compute_sv (hsv, x, y, &sat, &val);
1637 if (priv->focus_on_ring)
1642 compute_sv (hsv, x, y, &sat, &val);
1647 if (priv->focus_on_ring)
1653 compute_sv (hsv, x, y, &sat, &val);
1658 /* we don't care about the tab directions */
1668 gtk_hsv_set_color (hsv, hue, sat, val);