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.
28 #include "gdk/gdkkeysyms.h"
29 #include "gtkbindings.h"
30 #include "gtkcontainer.h"
31 #include "gtkmarshalers.h"
34 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
35 * file for a list of people on the GTK+ Team. See the ChangeLog
36 * files for a list of changes. These files are distributed with
37 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
40 /* Default width/height */
41 #define DEFAULT_SIZE 100
43 /* Default ring width */
44 #define DEFAULT_RING_WIDTH 10
54 /* Private part of the GtkHSV structure */
61 /* Size and ring width */
65 /* Window for capturing events */
74 guint focus_on_ring : 1;
88 static void gtk_hsv_class_init (GtkHSVClass *class);
89 static void gtk_hsv_init (GtkHSV *hsv);
90 static void gtk_hsv_destroy (GtkObject *object);
91 static void gtk_hsv_map (GtkWidget *widget);
92 static void gtk_hsv_unmap (GtkWidget *widget);
93 static void gtk_hsv_realize (GtkWidget *widget);
94 static void gtk_hsv_unrealize (GtkWidget *widget);
95 static void gtk_hsv_size_request (GtkWidget *widget,
96 GtkRequisition *requisition);
97 static void gtk_hsv_size_allocate (GtkWidget *widget,
98 GtkAllocation *allocation);
99 static gint gtk_hsv_button_press (GtkWidget *widget,
100 GdkEventButton *event);
101 static gint gtk_hsv_button_release (GtkWidget *widget,
102 GdkEventButton *event);
103 static gint gtk_hsv_motion (GtkWidget *widget,
104 GdkEventMotion *event);
105 static gint gtk_hsv_expose (GtkWidget *widget,
106 GdkEventExpose *event);
107 static gboolean gtk_hsv_focus (GtkWidget *widget,
108 GtkDirectionType direction);
109 static void gtk_hsv_move (GtkHSV *hsv,
110 GtkDirectionType dir);
111 static GdkGC * gtk_hsv_get_focus_gc (GtkHSV *hsv,
114 static guint hsv_signals[LAST_SIGNAL];
115 static GtkWidgetClass *parent_class;
122 * Registers the &GtkHSV class if necessary, and returns the type ID associated
125 * Return value: The type ID of the &GtkHSV class.
128 gtk_hsv_get_type (void)
130 static GType hsv_type = 0;
133 static const GTypeInfo hsv_info = {
134 sizeof (GtkHSVClass),
135 NULL, /* base_init */
136 NULL, /* base_finalize */
137 (GClassInitFunc) gtk_hsv_class_init,
138 NULL, /* class_finalize */
139 NULL, /* class_data */
142 (GInstanceInitFunc) gtk_hsv_init,
145 hsv_type = g_type_register_static (GTK_TYPE_WIDGET, "GtkHSV",
152 /* Class initialization function for the HSV color selector */
154 gtk_hsv_class_init (GtkHSVClass *class)
156 GtkObjectClass *object_class;
157 GtkWidgetClass *widget_class;
158 GtkHSVClass *hsv_class;
159 GtkBindingSet *binding_set;
161 object_class = (GtkObjectClass *) class;
162 widget_class = (GtkWidgetClass *) class;
163 hsv_class = GTK_HSV_CLASS (class);
165 parent_class = g_type_class_peek_parent (class);
167 object_class->destroy = gtk_hsv_destroy;
169 widget_class->map = gtk_hsv_map;
170 widget_class->unmap = gtk_hsv_unmap;
171 widget_class->realize = gtk_hsv_realize;
172 widget_class->unrealize = gtk_hsv_unrealize;
173 widget_class->size_request = gtk_hsv_size_request;
174 widget_class->size_allocate = gtk_hsv_size_allocate;
175 widget_class->button_press_event = gtk_hsv_button_press;
176 widget_class->button_release_event = gtk_hsv_button_release;
177 widget_class->motion_notify_event = gtk_hsv_motion;
178 widget_class->expose_event = gtk_hsv_expose;
179 widget_class->focus = gtk_hsv_focus;
181 hsv_class->move = gtk_hsv_move;
183 hsv_signals[CHANGED] =
184 g_signal_new ("changed",
185 G_OBJECT_CLASS_TYPE (object_class),
187 G_STRUCT_OFFSET (GtkHSVClass, changed),
189 _gtk_marshal_VOID__VOID,
193 g_signal_new ("move",
194 G_OBJECT_CLASS_TYPE (object_class),
195 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
196 G_STRUCT_OFFSET (GtkHSVClass, move),
198 _gtk_marshal_VOID__ENUM,
200 GTK_TYPE_DIRECTION_TYPE);
202 binding_set = gtk_binding_set_by_class (class);
204 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
206 G_TYPE_ENUM, GTK_DIR_UP);
207 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
209 G_TYPE_ENUM, GTK_DIR_UP);
211 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
213 G_TYPE_ENUM, GTK_DIR_DOWN);
214 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
216 G_TYPE_ENUM, GTK_DIR_DOWN);
219 gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
221 G_TYPE_ENUM, GTK_DIR_RIGHT);
222 gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
224 G_TYPE_ENUM, GTK_DIR_RIGHT);
226 gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
228 G_TYPE_ENUM, GTK_DIR_LEFT);
229 gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
231 G_TYPE_ENUM, GTK_DIR_LEFT);
234 /* Object initialization function for the HSV color selector */
236 gtk_hsv_init (GtkHSV *hsv)
240 priv = g_new0 (HSVPrivate, 1);
243 GTK_WIDGET_SET_FLAGS (hsv, GTK_NO_WINDOW);
244 GTK_WIDGET_SET_FLAGS (hsv, GTK_CAN_FOCUS);
250 priv->size = DEFAULT_SIZE;
251 priv->ring_width = DEFAULT_RING_WIDTH;
254 /* Destroy handler for the HSV color selector */
256 gtk_hsv_destroy (GtkObject *object)
260 g_return_if_fail (GTK_IS_HSV (object));
262 hsv = GTK_HSV (object);
270 GTK_OBJECT_CLASS (parent_class)->destroy (object);
273 /* Default signal handlers */
276 /* Map handler for the HSV color selector */
279 gtk_hsv_map (GtkWidget *widget)
284 hsv = GTK_HSV (widget);
287 GTK_WIDGET_CLASS (parent_class)->map (widget);
289 gdk_window_show (priv->window);
292 /* Unmap handler for the HSV color selector */
295 gtk_hsv_unmap (GtkWidget *widget)
300 hsv = GTK_HSV (widget);
303 gdk_window_hide (priv->window);
305 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
308 /* Realize handler for the HSV color selector */
310 gtk_hsv_realize (GtkWidget *widget)
316 GdkWindow *parent_window;
318 hsv = GTK_HSV (widget);
321 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
325 attr.window_type = GDK_WINDOW_CHILD;
326 attr.x = widget->allocation.x;
327 attr.y = widget->allocation.y;
328 attr.width = widget->allocation.width;
329 attr.height = widget->allocation.height;
330 attr.wclass = GDK_INPUT_ONLY;
331 attr.event_mask = gtk_widget_get_events (widget);
332 attr.event_mask |= (GDK_KEY_PRESS_MASK
333 | GDK_BUTTON_PRESS_MASK
334 | GDK_BUTTON_RELEASE_MASK
335 | GDK_POINTER_MOTION_MASK
336 | GDK_ENTER_NOTIFY_MASK
337 | GDK_LEAVE_NOTIFY_MASK);
339 attr_mask = GDK_WA_X | GDK_WA_Y;
341 parent_window = gtk_widget_get_parent_window (widget);
343 widget->window = parent_window;
344 g_object_ref (widget->window);
346 priv->window = gdk_window_new (parent_window, &attr, attr_mask);
347 gdk_window_set_user_data (priv->window, hsv);
349 widget->style = gtk_style_attach (widget->style, widget->window);
353 priv->gc = gdk_gc_new (parent_window);
356 /* Unrealize handler for the HSV color selector */
358 gtk_hsv_unrealize (GtkWidget *widget)
363 hsv = GTK_HSV (widget);
366 gdk_window_set_user_data (priv->window, NULL);
367 gdk_window_destroy (priv->window);
370 g_object_unref (priv->gc);
373 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
374 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
377 /* Size_request handler for the HSV color selector */
379 gtk_hsv_size_request (GtkWidget *widget,
380 GtkRequisition *requisition)
385 hsv = GTK_HSV (widget);
388 requisition->width = priv->size;
389 requisition->height = priv->size;
392 /* Size_allocate handler for the HSV color selector */
394 gtk_hsv_size_allocate (GtkWidget *widget,
395 GtkAllocation *allocation)
400 hsv = GTK_HSV (widget);
403 widget->allocation = *allocation;
405 if (GTK_WIDGET_REALIZED (widget))
406 gdk_window_move_resize (priv->window,
414 /* Utility functions */
416 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
418 /* Converts from HSV to RGB */
420 hsv_to_rgb (gdouble *h,
424 gdouble hue, saturation, value;
443 p = value * (1.0 - saturation);
444 q = value * (1.0 - saturation * f);
445 t = value * (1.0 - saturation * (1.0 - f));
486 g_assert_not_reached ();
491 /* Converts from RGB to HSV */
493 rgb_to_hsv (gdouble *r,
497 gdouble red, green, blue;
536 s = (max - min) / max;
547 h = (green - blue) / delta;
548 else if (green == max)
549 h = 2 + (blue - red) / delta;
550 else if (blue == max)
551 h = 4 + (red - green) / delta;
566 /* Computes the vertices of the saturation/value triangle */
568 compute_triangle (GtkHSV *hsv,
578 gdouble inner, outer;
583 center = priv->size / 2.0;
584 outer = priv->size / 2.0;
585 inner = outer - priv->ring_width;
586 angle = priv->h * 2.0 * G_PI;
588 *hx = floor (center + cos (angle) * inner + 0.5);
589 *hy = floor (center - sin (angle) * inner + 0.5);
590 *sx = floor (center + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
591 *sy = floor (center - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
592 *vx = floor (center + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
593 *vy = floor (center - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
596 /* Computes whether a point is inside the hue ring */
598 is_in_ring (GtkHSV *hsv,
603 gdouble dx, dy, dist;
604 gdouble center, inner, outer;
608 center = priv->size / 2.0;
609 outer = priv->size / 2.0;
610 inner = outer - priv->ring_width;
614 dist = dx * dx + dy * dy;
616 return (dist >= inner * inner && dist <= outer * outer);
619 /* Computes a saturation/value pair based on the mouse coordinates */
621 compute_sv (GtkHSV *hsv,
628 int ihx, ihy, isx, isy, ivx, ivy;
629 double hx, hy, sx, sy, vx, vy;
634 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
635 center = priv->size / 2.0;
645 if (vx * (x - sx) + vy * (y - sy) < 0.0)
648 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
649 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
656 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
659 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
660 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
667 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
670 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
671 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
680 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
681 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
693 if (fabs (hy - vy) < fabs (hx - vx))
694 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
696 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
706 /* Computes whether a point is inside the saturation/value triangle */
708 is_in_triangle (GtkHSV *hsv,
712 int hx, hy, sx, sy, vx, vy;
715 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
717 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
719 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
720 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
722 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
725 /* Computes a value based on the mouse coordinates */
727 compute_v (GtkHSV *hsv,
738 center = priv->size / 2.0;
742 angle = atan2 (dy, dx);
746 return angle / (2.0 * G_PI);
752 set_cross_grab (GtkHSV *hsv,
760 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
762 gdk_pointer_grab (priv->window, FALSE,
763 (GDK_POINTER_MOTION_MASK
764 | GDK_POINTER_MOTION_HINT_MASK
765 | GDK_BUTTON_RELEASE_MASK),
769 gdk_cursor_unref (cursor);
772 /* Button_press_event handler for the HSV color selector */
774 gtk_hsv_button_press (GtkWidget *widget,
775 GdkEventButton *event)
781 hsv = GTK_HSV (widget);
784 if (priv->mode != DRAG_NONE || event->button != 1)
790 if (is_in_ring (hsv, x, y))
793 set_cross_grab (hsv, event->time);
795 gtk_hsv_set_color (hsv,
796 compute_v (hsv, x, y),
800 gtk_widget_grab_focus (widget);
801 priv->focus_on_ring = TRUE;
806 if (is_in_triangle (hsv, x, y))
810 priv->mode = DRAG_SV;
811 set_cross_grab (hsv, event->time);
813 compute_sv (hsv, x, y, &s, &v);
814 gtk_hsv_set_color (hsv, priv->h, s, v);
816 gtk_widget_grab_focus (widget);
817 priv->focus_on_ring = FALSE;
825 /* Button_release_event handler for the HSV color selector */
827 gtk_hsv_button_release (GtkWidget *widget,
828 GdkEventButton *event)
835 hsv = GTK_HSV (widget);
838 if (priv->mode == DRAG_NONE || event->button != 1)
841 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
842 * can see that this is the final color state.
846 priv->mode = DRAG_NONE;
852 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
853 else if (mode == DRAG_SV) {
856 compute_sv (hsv, x, y, &s, &v);
857 gtk_hsv_set_color (hsv, priv->h, s, v);
859 g_assert_not_reached ();
861 gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
866 /* Motion_notify_event handler for the HSV color selector */
868 gtk_hsv_motion (GtkWidget *widget,
869 GdkEventMotion *event)
875 GdkModifierType mods;
877 hsv = GTK_HSV (widget);
880 if (priv->mode == DRAG_NONE)
885 gdk_window_get_pointer (priv->window, &ix, &iy, &mods);
895 if (priv->mode == DRAG_H)
897 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
900 else if (priv->mode == DRAG_SV)
904 compute_sv (hsv, x, y, &s, &v);
905 gtk_hsv_set_color (hsv, priv->h, s, v);
909 g_assert_not_reached ();
916 /* Paints the hue ring */
918 paint_ring (GtkHSV *hsv,
919 GdkDrawable *drawable,
927 gdouble dx, dy, dist;
929 gdouble inner, outer;
940 center = priv->size / 2.0;
942 outer = priv->size / 2.0;
943 inner = outer - priv->ring_width;
947 buf = g_new (guchar, width * height * 3);
949 for (yy = 0; yy < height; yy++)
951 p = buf + yy * width * 3;
953 dy = -(yy + y - center);
955 for (xx = 0; xx < width; xx++)
957 dx = xx + x - center;
959 dist = dx * dx + dy * dy;
960 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
968 angle = atan2 (dy, dx);
972 hue = angle / (2.0 * G_PI);
977 hsv_to_rgb (&r, &g, &b);
979 *p++ = floor (r * 255 + 0.5);
980 *p++ = floor (g * 255 + 0.5);
981 *p++ = floor (b * 255 + 0.5);
985 /* Create clipping mask */
987 mask = gdk_pixmap_new (GTK_WIDGET (hsv)->window, width, height, 1);
989 gc = gdk_gc_new (mask);
992 gdk_gc_set_foreground (gc, &color);
993 gdk_draw_rectangle (mask, gc, TRUE,
994 0, 0, width, height);
998 gdk_gc_set_foreground (gc, &color);
999 gdk_draw_arc (mask, gc, TRUE,
1001 priv->size - 1, priv->size - 1,
1005 gdk_gc_set_foreground (gc, &color);
1006 gdk_draw_arc (mask, gc, TRUE,
1007 -x + priv->ring_width - 1, -y + priv->ring_width - 1,
1008 priv->size - 2 * priv->ring_width + 1, priv->size - 2 * priv->ring_width + 1,
1011 g_object_unref (gc);
1013 gdk_gc_set_clip_mask (priv->gc, mask);
1014 gdk_gc_set_clip_origin (priv->gc, 0, 0);
1018 gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
1024 /* Draw value marker */
1029 hsv_to_rgb (&r, &g, &b);
1031 if (INTENSITY (r, g, b) > 0.5)
1034 color.green = 0x0000;
1035 color.blue = 0x0000;
1040 color.green = 0xffff;
1041 color.blue = 0xffff;
1044 gdk_gc_set_rgb_fg_color (priv->gc, &color);
1046 gdk_draw_line (drawable, priv->gc,
1047 -x + center, -y + center,
1048 -x + center + cos (priv->h * 2.0 * G_PI) * center,
1049 -y + center - sin (priv->h * 2.0 * G_PI) * center);
1051 gdk_gc_set_clip_mask (priv->gc, NULL);
1052 g_object_unref (mask);
1056 /* Draw ring outline */
1058 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1059 priv->focus_on_ring)
1062 gint focus_halfwidth;
1063 GdkGC *gc = gtk_hsv_get_focus_gc (hsv, &focus_width);
1064 focus_halfwidth = (focus_width + 1) / 2;
1066 gdk_draw_arc (drawable, gc, FALSE,
1067 -x + focus_width/2, -y + focus_width/2,
1068 priv->size - focus_width, priv->size - focus_width,
1070 gdk_draw_arc (drawable, gc, FALSE,
1071 -x + priv->ring_width - focus_halfwidth, -y + priv->ring_width - focus_halfwidth,
1072 priv->size - 2 * priv->ring_width + focus_width,
1073 priv->size - 2 * priv->ring_width + focus_width,
1076 g_object_unref (gc);
1080 /* Converts an HSV triplet to an integer RGB triplet */
1082 get_color (gdouble h,
1089 hsv_to_rgb (&h, &s, &v);
1091 *r = floor (h * 255 + 0.5);
1092 *g = floor (s * 255 + 0.5);
1093 *b = floor (v * 255 + 0.5);
1096 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1098 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
1099 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
1102 /* Paints the HSV triangle */
1104 paint_triangle (GtkHSV *hsv,
1105 GdkDrawable *drawable,
1112 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1113 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1114 gint x2, y2, r2, g2, b2; /* Second vertex */
1115 gint x3, y3, r3, g3, b3; /* Third vertex */
1118 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1128 /* Compute triangle's vertices */
1130 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1134 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1138 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1142 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1171 /* Shade the triangle */
1173 buf = g_new (guchar, width * height * 3);
1175 for (yy = 0; yy < height; yy++)
1177 p = buf + yy * width * 3;
1179 if (yy + y < y1 || yy + y > y3)
1180 for (xx = 0; xx < width; xx++)
1189 xl = LERP (x1, x2, y1, y2, yy + y);
1191 rl = LERP (r1, r2, y1, y2, yy + y);
1192 gl = LERP (g1, g2, y1, y2, yy + y);
1193 bl = LERP (b1, b2, y1, y2, yy + y);
1197 xl = LERP (x2, x3, y2, y3, yy + y);
1199 rl = LERP (r2, r3, y2, y3, yy + y);
1200 gl = LERP (g2, g3, y2, y3, yy + y);
1201 bl = LERP (b2, b3, y2, y3, yy + y);
1204 xr = LERP (x1, x3, y1, y3, yy + y);
1206 rr = LERP (r1, r3, y1, y3, yy + y);
1207 gr = LERP (g1, g3, y1, y3, yy + y);
1208 br = LERP (b1, b3, y1, y3, yy + y);
1218 for (xx = 0; xx < width; xx++)
1220 if (xx + x < xl || xx + x > xr)
1228 *p++ = LERP (rl, rr, xl, xr, xx + x);
1229 *p++ = LERP (gl, gr, xl, xr, xx + x);
1230 *p++ = LERP (bl, br, xl, xr, xx + x);
1236 /* Create clipping mask */
1238 mask = gdk_pixmap_new (GTK_WIDGET (hsv)->window, width, height, 1);
1240 gc = gdk_gc_new (mask);
1243 gdk_gc_set_foreground (gc, &color);
1244 gdk_draw_rectangle (mask, gc, TRUE,
1245 0, 0, width, height);
1248 gdk_gc_set_foreground (gc, &color);
1250 points[0].x = x1 - x;
1251 points[0].y = y1 - y;
1252 points[1].x = x2 - x;
1253 points[1].y = y2 - y;
1254 points[2].x = x3 - x;
1255 points[2].y = y3 - y;
1256 gdk_draw_polygon (mask, gc, TRUE, points, 3);
1258 g_object_unref (gc);
1260 gdk_gc_set_clip_mask (priv->gc, mask);
1261 gdk_gc_set_clip_origin (priv->gc, 0, 0);
1265 gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
1271 gdk_gc_set_clip_mask (priv->gc, NULL);
1272 g_object_unref (mask);
1276 /* Draw triangle focus outline */
1278 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1279 !priv->focus_on_ring)
1281 gint focus_width = 1;
1282 GdkGC *gc = gtk_hsv_get_focus_gc (hsv, &focus_width);
1284 gdk_draw_polygon (drawable, gc, FALSE, points, 3);
1285 g_object_unref (gc);
1288 /* Draw value marker */
1290 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5) - x;
1291 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5) - y;
1296 hsv_to_rgb (&r, &g, &b);
1298 if (INTENSITY (r, g, b) > 0.5)
1301 color.green = 0x0000;
1302 color.blue = 0x0000;
1307 color.green = 0xffff;
1308 color.blue = 0xffff;
1311 gdk_gc_set_rgb_fg_color (priv->gc, &color);
1313 #define OUTER_RADIUS 4
1314 #define INNER_RADIUS 3
1316 gdk_draw_arc (drawable, priv->gc, FALSE,
1317 xx - OUTER_RADIUS, yy - OUTER_RADIUS,
1318 OUTER_RADIUS * 2, OUTER_RADIUS * 2,
1320 gdk_draw_arc (drawable, priv->gc, FALSE,
1321 xx - INNER_RADIUS, yy - INNER_RADIUS,
1322 INNER_RADIUS * 2, INNER_RADIUS * 2,
1326 /* Paints the contents of the HSV color selector */
1329 GdkDrawable *drawable,
1335 paint_ring (hsv, drawable, x, y, width, height);
1336 paint_triangle (hsv, drawable, x, y, width, height);
1339 /* Expose_event handler for the HSV color selector */
1341 gtk_hsv_expose (GtkWidget *widget,
1342 GdkEventExpose *event)
1346 GdkRectangle rect, dest;
1349 hsv = GTK_HSV (widget);
1352 if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
1355 rect.x = widget->allocation.x;
1356 rect.y = widget->allocation.y;
1357 rect.width = widget->allocation.width;
1358 rect.height = widget->allocation.height;
1360 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1363 pixmap = gdk_pixmap_new (widget->window, dest.width, dest.height,
1364 gtk_widget_get_visual (widget)->depth);
1370 gdk_draw_rectangle (pixmap,
1371 widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
1373 0, 0, dest.width, dest.height);
1375 dest.x - widget->allocation.x, dest.y - widget->allocation.y,
1376 dest.width, dest.height);
1378 gdk_draw_drawable (widget->window,
1384 event->area.width, event->area.height);
1386 g_object_unref (pixmap);
1392 gtk_hsv_focus (GtkWidget *widget,
1393 GtkDirectionType dir)
1398 hsv = GTK_HSV (widget);
1401 if (!GTK_WIDGET_HAS_FOCUS (hsv))
1403 if (dir == GTK_DIR_TAB_BACKWARD)
1404 priv->focus_on_ring = FALSE;
1406 priv->focus_on_ring = TRUE;
1408 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1415 if (priv->focus_on_ring)
1418 priv->focus_on_ring = TRUE;
1422 if (priv->focus_on_ring)
1423 priv->focus_on_ring = FALSE;
1429 case GTK_DIR_TAB_BACKWARD:
1430 if (priv->focus_on_ring)
1433 priv->focus_on_ring = TRUE;
1437 case GTK_DIR_TAB_FORWARD:
1438 if (priv->focus_on_ring)
1439 priv->focus_on_ring = FALSE;
1445 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1454 * Creates a new HSV color selector.
1456 * Return value: A newly-created HSV color selector.
1461 return g_object_new (GTK_TYPE_HSV, NULL);
1465 * gtk_hsv_set_color:
1466 * @hsv: An HSV color selector.
1471 * Sets the current color in an HSV color selector. Color component values must
1472 * be in the [0.0, 1.0] range.
1475 gtk_hsv_set_color (GtkHSV *hsv,
1482 g_return_if_fail (hsv != NULL);
1483 g_return_if_fail (GTK_IS_HSV (hsv));
1484 g_return_if_fail (h >= 0.0 && h <= 1.0);
1485 g_return_if_fail (s >= 0.0 && s <= 1.0);
1486 g_return_if_fail (v >= 0.0 && v <= 1.0);
1494 g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1496 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1500 * gtk_hsv_get_color:
1501 * @hsv: An HSV color selector.
1502 * @h: Return value for the hue.
1503 * @s: Return value for the saturation.
1504 * @v: Return value for the value.
1506 * Queries the current color in an HSV color selector. Returned values will be
1507 * in the [0.0, 1.0] range.
1510 gtk_hsv_get_color (GtkHSV *hsv, double *h, double *s, double *v)
1514 g_return_if_fail (GTK_IS_HSV (hsv));
1529 * gtk_hsv_set_metrics:
1530 * @hsv: An HSV color selector.
1531 * @size: Diameter for the hue ring.
1532 * @ring_width: Width of the hue ring.
1534 * Sets the size and ring width of an HSV color selector.
1537 gtk_hsv_set_metrics (GtkHSV *hsv,
1544 g_return_if_fail (GTK_IS_HSV (hsv));
1545 g_return_if_fail (size > 0);
1546 g_return_if_fail (ring_width > 0);
1547 g_return_if_fail (2 * ring_width + 1 <= size);
1551 same_size = (priv->size == size);
1554 priv->ring_width = ring_width;
1557 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1559 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1563 * gtk_hsv_get_metrics:
1564 * @hsv: An HSV color selector.
1565 * @size: Return value for the diameter of the hue ring.
1566 * @ring_width: Return value for the width of the hue ring.
1568 * Queries the size and ring width of an HSV color selector.
1571 gtk_hsv_get_metrics (GtkHSV *hsv,
1577 g_return_if_fail (GTK_IS_HSV (hsv));
1585 *ring_width = priv->ring_width;
1589 * gtk_hsv_is_adjusting:
1592 * An HSV color selector can be said to be adjusting if multiple rapid changes
1593 * are being made to its value, for example, when the user is adjusting the
1594 * value with the mouse. This function queries whether the HSV color selector
1595 * is being adjusted or not.
1597 * Return value: TRUE if clients can ignore changes to the color value, since
1598 * they may be transitory, or FALSE if they should consider the color value
1599 * status to be final.
1602 gtk_hsv_is_adjusting (GtkHSV *hsv)
1606 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1610 return priv->mode != DRAG_NONE;
1618 * @r: Return value for the red component.
1619 * @g: Return value for the green component.
1620 * @b: Return value for the blue component.
1622 * Converts a color from HSV space to RGB. Input values must be in the
1623 * [0.0, 1.0] range; output values will be in the same range.
1626 gtk_hsv_to_rgb (gdouble h,
1633 g_return_if_fail (h >= 0.0 && h <= 1.0);
1634 g_return_if_fail (s >= 0.0 && s <= 1.0);
1635 g_return_if_fail (v >= 0.0 && v <= 1.0);
1637 hsv_to_rgb (&h, &s, &v);
1654 * @h: Return value for the hue component.
1655 * @s: Return value for the saturation component.
1656 * @v: Return value for the value component.
1658 * Converts a color from RGB space to HSV. Input values must be in the
1659 * [0.0, 1.0] range; output values will be in the same range.
1662 gtk_rgb_to_hsv (gdouble r,
1669 g_return_if_fail (r >= 0.0 && r <= 1.0);
1670 g_return_if_fail (g >= 0.0 && g <= 1.0);
1671 g_return_if_fail (b >= 0.0 && b <= 1.0);
1673 rgb_to_hsv (&r, &g, &b);
1686 gtk_hsv_move (GtkHSV *hsv,
1687 GtkDirectionType dir)
1690 gdouble hue, sat, val;
1691 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1692 gint x, y; /* position in triangle */
1700 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1702 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1703 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1705 #define HUE_DELTA 0.002
1709 if (priv->focus_on_ring)
1714 compute_sv (hsv, x, y, &sat, &val);
1719 if (priv->focus_on_ring)
1724 compute_sv (hsv, x, y, &sat, &val);
1729 if (priv->focus_on_ring)
1734 compute_sv (hsv, x, y, &sat, &val);
1739 if (priv->focus_on_ring)
1745 compute_sv (hsv, x, y, &sat, &val);
1750 /* we don't care about the tab directions */
1760 gtk_hsv_set_color (hsv, hue, sat, val);
1764 gtk_hsv_get_focus_gc (GtkHSV *hsv,
1768 GtkWidget *widget = GTK_WIDGET (hsv);
1771 focus_gc = gdk_gc_new (widget->window);
1772 gdk_gc_copy (focus_gc, widget->style->fg_gc[GTK_WIDGET_STATE (widget)]);
1774 gtk_widget_style_get (widget,
1775 "focus-line-width", line_width,
1776 "focus-line-pattern", (gchar *)&dash_list,
1779 gdk_gc_set_line_attributes (focus_gc, *line_width,
1780 dash_list[0] ? GDK_LINE_ON_OFF_DASH : GDK_LINE_SOLID,
1781 GDK_CAP_BUTT, GDK_JOIN_MITER);
1783 gdk_gc_set_dashes (focus_gc, 0, dash_list, strlen (dash_list));