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.
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkbindings.h"
32 #include "gtkcontainer.h"
33 #include "gtkmarshalers.h"
36 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
37 * file for a list of people on the GTK+ Team. See the ChangeLog
38 * files for a list of changes. These files are distributed with
39 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
42 /* Default width/height */
43 #define DEFAULT_SIZE 100
45 /* Default ring width */
46 #define DEFAULT_RING_WIDTH 10
56 /* Private part of the GtkHSV structure */
63 /* Size and ring width */
67 /* Window for capturing events */
76 guint focus_on_ring : 1;
90 static void gtk_hsv_class_init (GtkHSVClass *class);
91 static void gtk_hsv_init (GtkHSV *hsv);
92 static void gtk_hsv_destroy (GtkObject *object);
93 static void gtk_hsv_map (GtkWidget *widget);
94 static void gtk_hsv_unmap (GtkWidget *widget);
95 static void gtk_hsv_realize (GtkWidget *widget);
96 static void gtk_hsv_unrealize (GtkWidget *widget);
97 static void gtk_hsv_size_request (GtkWidget *widget,
98 GtkRequisition *requisition);
99 static void gtk_hsv_size_allocate (GtkWidget *widget,
100 GtkAllocation *allocation);
101 static gint gtk_hsv_button_press (GtkWidget *widget,
102 GdkEventButton *event);
103 static gint gtk_hsv_button_release (GtkWidget *widget,
104 GdkEventButton *event);
105 static gint gtk_hsv_motion (GtkWidget *widget,
106 GdkEventMotion *event);
107 static gint gtk_hsv_expose (GtkWidget *widget,
108 GdkEventExpose *event);
109 static gboolean gtk_hsv_focus (GtkWidget *widget,
110 GtkDirectionType direction);
111 static void gtk_hsv_move (GtkHSV *hsv,
112 GtkDirectionType dir);
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)
382 GtkHSV *hsv = GTK_HSV (widget);
383 HSVPrivate *priv = hsv->priv;
387 gtk_widget_style_get (widget,
388 "focus-line-width", &focus_width,
389 "focus-padding", &focus_pad,
392 requisition->width = priv->size + 2 * (focus_width + focus_pad);
393 requisition->height = priv->size + 2 * (focus_width + focus_pad);
396 /* Size_allocate handler for the HSV color selector */
398 gtk_hsv_size_allocate (GtkWidget *widget,
399 GtkAllocation *allocation)
404 hsv = GTK_HSV (widget);
407 widget->allocation = *allocation;
409 if (GTK_WIDGET_REALIZED (widget))
410 gdk_window_move_resize (priv->window,
418 /* Utility functions */
420 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
422 /* Converts from HSV to RGB */
424 hsv_to_rgb (gdouble *h,
428 gdouble hue, saturation, value;
447 p = value * (1.0 - saturation);
448 q = value * (1.0 - saturation * f);
449 t = value * (1.0 - saturation * (1.0 - f));
490 g_assert_not_reached ();
495 /* Converts from RGB to HSV */
497 rgb_to_hsv (gdouble *r,
501 gdouble red, green, blue;
540 s = (max - min) / max;
551 h = (green - blue) / delta;
552 else if (green == max)
553 h = 2 + (blue - red) / delta;
554 else if (blue == max)
555 h = 4 + (red - green) / delta;
570 /* Computes the vertices of the saturation/value triangle */
572 compute_triangle (GtkHSV *hsv,
582 gdouble inner, outer;
587 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
588 outer = priv->size / 2.0;
589 inner = outer - priv->ring_width;
590 angle = priv->h * 2.0 * G_PI;
592 *hx = floor (center + cos (angle) * inner + 0.5);
593 *hy = floor (center - sin (angle) * inner + 0.5);
594 *sx = floor (center + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
595 *sy = floor (center - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
596 *vx = floor (center + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
597 *vy = floor (center - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
600 /* Computes whether a point is inside the hue ring */
602 is_in_ring (GtkHSV *hsv,
607 gdouble dx, dy, dist;
608 gdouble center, inner, outer;
612 center = priv->size / 2.0;
613 outer = priv->size / 2.0;
614 inner = outer - priv->ring_width;
618 dist = dx * dx + dy * dy;
620 return (dist >= inner * inner && dist <= outer * outer);
623 /* Computes a saturation/value pair based on the mouse coordinates */
625 compute_sv (GtkHSV *hsv,
632 int ihx, ihy, isx, isy, ivx, ivy;
633 double hx, hy, sx, sy, vx, vy;
638 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
639 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
649 if (vx * (x - sx) + vy * (y - sy) < 0.0)
652 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
653 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
660 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
663 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
664 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
671 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
674 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
675 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
684 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
685 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
697 if (fabs (hy - vy) < fabs (hx - vx))
698 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
700 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
710 /* Computes whether a point is inside the saturation/value triangle */
712 is_in_triangle (GtkHSV *hsv,
716 int hx, hy, sx, sy, vx, vy;
719 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
721 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
723 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
724 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
726 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
729 /* Computes a value based on the mouse coordinates */
731 compute_v (GtkHSV *hsv,
742 center = GTK_WIDGET (hsv)->requisition.width / 2.0;
746 angle = atan2 (dy, dx);
750 return angle / (2.0 * G_PI);
756 set_cross_grab (GtkHSV *hsv,
764 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
766 gdk_pointer_grab (priv->window, FALSE,
767 (GDK_POINTER_MOTION_MASK
768 | GDK_POINTER_MOTION_HINT_MASK
769 | GDK_BUTTON_RELEASE_MASK),
773 gdk_cursor_unref (cursor);
776 /* Button_press_event handler for the HSV color selector */
778 gtk_hsv_button_press (GtkWidget *widget,
779 GdkEventButton *event)
785 hsv = GTK_HSV (widget);
788 if (priv->mode != DRAG_NONE || event->button != 1)
794 if (is_in_ring (hsv, x, y))
797 set_cross_grab (hsv, event->time);
799 gtk_hsv_set_color (hsv,
800 compute_v (hsv, x, y),
804 gtk_widget_grab_focus (widget);
805 priv->focus_on_ring = TRUE;
810 if (is_in_triangle (hsv, x, y))
814 priv->mode = DRAG_SV;
815 set_cross_grab (hsv, event->time);
817 compute_sv (hsv, x, y, &s, &v);
818 gtk_hsv_set_color (hsv, priv->h, s, v);
820 gtk_widget_grab_focus (widget);
821 priv->focus_on_ring = FALSE;
829 /* Button_release_event handler for the HSV color selector */
831 gtk_hsv_button_release (GtkWidget *widget,
832 GdkEventButton *event)
839 hsv = GTK_HSV (widget);
842 if (priv->mode == DRAG_NONE || event->button != 1)
845 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
846 * can see that this is the final color state.
850 priv->mode = DRAG_NONE;
856 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
857 else if (mode == DRAG_SV) {
860 compute_sv (hsv, x, y, &s, &v);
861 gtk_hsv_set_color (hsv, priv->h, s, v);
863 g_assert_not_reached ();
865 gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
870 /* Motion_notify_event handler for the HSV color selector */
872 gtk_hsv_motion (GtkWidget *widget,
873 GdkEventMotion *event)
879 GdkModifierType mods;
881 hsv = GTK_HSV (widget);
884 if (priv->mode == DRAG_NONE)
889 gdk_window_get_pointer (priv->window, &ix, &iy, &mods);
899 if (priv->mode == DRAG_H)
901 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
904 else if (priv->mode == DRAG_SV)
908 compute_sv (hsv, x, y, &s, &v);
909 gtk_hsv_set_color (hsv, priv->h, s, v);
913 g_assert_not_reached ();
920 /* Paints the hue ring */
922 paint_ring (GtkHSV *hsv,
923 GdkDrawable *drawable,
929 GtkWidget *widget = GTK_WIDGET (hsv);
932 gdouble dx, dy, dist;
934 gdouble inner, outer;
945 gtk_widget_style_get (widget,
946 "focus-line-width", &focus_width,
947 "focus-padding", &focus_pad,
952 center = widget->requisition.width / 2.0;
954 outer = priv->size / 2.0;
955 inner = outer - priv->ring_width;
959 buf = g_new (guchar, width * height * 3);
961 for (yy = 0; yy < height; yy++)
963 p = buf + yy * width * 3;
965 dy = -(yy + y - center);
967 for (xx = 0; xx < width; xx++)
969 dx = xx + x - center;
971 dist = dx * dx + dy * dy;
972 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
980 angle = atan2 (dy, dx);
984 hue = angle / (2.0 * G_PI);
989 hsv_to_rgb (&r, &g, &b);
991 *p++ = floor (r * 255 + 0.5);
992 *p++ = floor (g * 255 + 0.5);
993 *p++ = floor (b * 255 + 0.5);
997 /* Create clipping mask */
999 mask = gdk_pixmap_new (GTK_WIDGET (hsv)->window, width, height, 1);
1001 gc = gdk_gc_new (mask);
1004 gdk_gc_set_foreground (gc, &color);
1005 gdk_draw_rectangle (mask, gc, TRUE,
1006 0, 0, width, height);
1010 gdk_gc_set_foreground (gc, &color);
1011 gdk_draw_arc (mask, gc, TRUE,
1012 focus_width + focus_pad - x,
1013 focus_width + focus_pad - y,
1014 priv->size - 1, priv->size - 1,
1018 gdk_gc_set_foreground (gc, &color);
1019 gdk_draw_arc (mask, gc, TRUE,
1020 focus_width + focus_pad - x + priv->ring_width - 1,
1021 focus_width + focus_pad - y + priv->ring_width - 1,
1022 priv->size - 2 * priv->ring_width + 1, priv->size - 2 * priv->ring_width + 1,
1025 g_object_unref (gc);
1027 gdk_gc_set_clip_mask (priv->gc, mask);
1028 gdk_gc_set_clip_origin (priv->gc, 0, 0);
1032 gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
1038 /* Draw value marker */
1043 hsv_to_rgb (&r, &g, &b);
1045 if (INTENSITY (r, g, b) > 0.5)
1048 color.green = 0x0000;
1049 color.blue = 0x0000;
1054 color.green = 0xffff;
1055 color.blue = 0xffff;
1058 gdk_gc_set_rgb_fg_color (priv->gc, &color);
1060 gdk_draw_line (drawable, priv->gc,
1061 -x + center, -y + center,
1062 -x + center + cos (priv->h * 2.0 * G_PI) * center,
1063 -y + center - sin (priv->h * 2.0 * G_PI) * center);
1065 gdk_gc_set_clip_mask (priv->gc, NULL);
1066 g_object_unref (mask);
1070 /* Draw ring outline */
1072 if (GTK_WIDGET_HAS_FOCUS (hsv) && priv->focus_on_ring)
1074 gtk_paint_focus (widget->style, drawable,
1075 GTK_WIDGET_STATE (widget),
1077 x, y, width, height);
1081 /* Converts an HSV triplet to an integer RGB triplet */
1083 get_color (gdouble h,
1090 hsv_to_rgb (&h, &s, &v);
1092 *r = floor (h * 255 + 0.5);
1093 *g = floor (s * 255 + 0.5);
1094 *b = floor (v * 255 + 0.5);
1097 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1099 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
1100 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
1103 /* Paints the HSV triangle */
1105 paint_triangle (GtkHSV *hsv,
1106 GdkDrawable *drawable,
1112 GtkWidget *widget = GTK_WIDGET (hsv);
1114 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1115 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1116 gint x2, y2, r2, g2, b2; /* Second vertex */
1117 gint x3, y3, r3, g3, b3; /* Third vertex */
1120 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1131 /* Compute triangle's vertices */
1133 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1137 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1141 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1145 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1174 /* Shade the triangle */
1176 buf = g_new (guchar, width * height * 3);
1178 for (yy = 0; yy < height; yy++)
1180 p = buf + yy * width * 3;
1182 if (yy + y < y1 || yy + y > y3)
1183 for (xx = 0; xx < width; xx++)
1192 xl = LERP (x1, x2, y1, y2, yy + y);
1194 rl = LERP (r1, r2, y1, y2, yy + y);
1195 gl = LERP (g1, g2, y1, y2, yy + y);
1196 bl = LERP (b1, b2, y1, y2, yy + y);
1200 xl = LERP (x2, x3, y2, y3, yy + y);
1202 rl = LERP (r2, r3, y2, y3, yy + y);
1203 gl = LERP (g2, g3, y2, y3, yy + y);
1204 bl = LERP (b2, b3, y2, y3, yy + y);
1207 xr = LERP (x1, x3, y1, y3, yy + y);
1209 rr = LERP (r1, r3, y1, y3, yy + y);
1210 gr = LERP (g1, g3, y1, y3, yy + y);
1211 br = LERP (b1, b3, y1, y3, yy + y);
1221 for (xx = 0; xx < width; xx++)
1223 if (xx + x < xl || xx + x > xr)
1231 *p++ = LERP (rl, rr, xl, xr, xx + x);
1232 *p++ = LERP (gl, gr, xl, xr, xx + x);
1233 *p++ = LERP (bl, br, xl, xr, xx + x);
1239 /* Create clipping mask */
1241 mask = gdk_pixmap_new (widget->window, width, height, 1);
1243 gc = gdk_gc_new (mask);
1246 gdk_gc_set_foreground (gc, &color);
1247 gdk_draw_rectangle (mask, gc, TRUE,
1248 0, 0, width, height);
1251 gdk_gc_set_foreground (gc, &color);
1253 points[0].x = x1 - x;
1254 points[0].y = y1 - y;
1255 points[1].x = x2 - x;
1256 points[1].y = y2 - y;
1257 points[2].x = x3 - x;
1258 points[2].y = y3 - y;
1259 gdk_draw_polygon (mask, gc, TRUE, points, 3);
1261 g_object_unref (gc);
1263 gdk_gc_set_clip_mask (priv->gc, mask);
1264 gdk_gc_set_clip_origin (priv->gc, 0, 0);
1268 gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
1274 gdk_gc_set_clip_mask (priv->gc, NULL);
1275 g_object_unref (mask);
1279 /* Draw value marker */
1281 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5) - x;
1282 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5) - y;
1287 hsv_to_rgb (&r, &g, &b);
1289 if (INTENSITY (r, g, b) > 0.5)
1291 detail = "colorwheel_light";
1293 color.green = 0x0000;
1294 color.blue = 0x0000;
1298 detail = "colorwheel_dark";
1300 color.green = 0xffff;
1301 color.blue = 0xffff;
1304 gdk_gc_set_rgb_fg_color (priv->gc, &color);
1306 #define OUTER_RADIUS 4
1307 #define INNER_RADIUS 3
1308 #define FOCUS_RADIUS 6
1310 gdk_draw_arc (drawable, priv->gc, FALSE,
1311 xx - OUTER_RADIUS, yy - OUTER_RADIUS,
1312 OUTER_RADIUS * 2, OUTER_RADIUS * 2,
1314 gdk_draw_arc (drawable, priv->gc, FALSE,
1315 xx - INNER_RADIUS, yy - INNER_RADIUS,
1316 INNER_RADIUS * 2, INNER_RADIUS * 2,
1319 /* Draw focus outline */
1321 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1322 !priv->focus_on_ring)
1327 gtk_widget_style_get (widget,
1328 "focus-line-width", &focus_width,
1329 "focus-padding", &focus_pad,
1332 gtk_paint_focus (widget->style, drawable,
1333 GTK_WIDGET_STATE (widget),
1334 NULL, widget, detail,
1335 xx - FOCUS_RADIUS - focus_width - focus_pad,
1336 yy - FOCUS_RADIUS - focus_width - focus_pad,
1337 2 * (FOCUS_RADIUS + focus_width + focus_pad),
1338 2 * (FOCUS_RADIUS + focus_width + focus_pad));
1343 /* Paints the contents of the HSV color selector */
1346 GdkDrawable *drawable,
1352 paint_ring (hsv, drawable, x, y, width, height);
1353 paint_triangle (hsv, drawable, x, y, width, height);
1356 /* Expose_event handler for the HSV color selector */
1358 gtk_hsv_expose (GtkWidget *widget,
1359 GdkEventExpose *event)
1363 GdkRectangle rect, dest;
1366 hsv = GTK_HSV (widget);
1369 if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
1372 rect.x = widget->allocation.x;
1373 rect.y = widget->allocation.y;
1374 rect.width = widget->allocation.width;
1375 rect.height = widget->allocation.height;
1377 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1380 pixmap = gdk_pixmap_new (widget->window, dest.width, dest.height,
1381 gtk_widget_get_visual (widget)->depth);
1387 gdk_draw_rectangle (pixmap,
1388 widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
1390 0, 0, dest.width, dest.height);
1392 dest.x - widget->allocation.x, dest.y - widget->allocation.y,
1393 dest.width, dest.height);
1395 gdk_draw_drawable (widget->window,
1401 event->area.width, event->area.height);
1403 g_object_unref (pixmap);
1409 gtk_hsv_focus (GtkWidget *widget,
1410 GtkDirectionType dir)
1415 hsv = GTK_HSV (widget);
1418 if (!GTK_WIDGET_HAS_FOCUS (hsv))
1420 if (dir == GTK_DIR_TAB_BACKWARD)
1421 priv->focus_on_ring = FALSE;
1423 priv->focus_on_ring = TRUE;
1425 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1432 if (priv->focus_on_ring)
1435 priv->focus_on_ring = TRUE;
1439 if (priv->focus_on_ring)
1440 priv->focus_on_ring = FALSE;
1446 case GTK_DIR_TAB_BACKWARD:
1447 if (priv->focus_on_ring)
1450 priv->focus_on_ring = TRUE;
1454 case GTK_DIR_TAB_FORWARD:
1455 if (priv->focus_on_ring)
1456 priv->focus_on_ring = FALSE;
1462 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1471 * Creates a new HSV color selector.
1473 * Return value: A newly-created HSV color selector.
1478 return g_object_new (GTK_TYPE_HSV, NULL);
1482 * gtk_hsv_set_color:
1483 * @hsv: An HSV color selector.
1488 * Sets the current color in an HSV color selector. Color component values must
1489 * be in the [0.0, 1.0] range.
1492 gtk_hsv_set_color (GtkHSV *hsv,
1499 g_return_if_fail (hsv != NULL);
1500 g_return_if_fail (GTK_IS_HSV (hsv));
1501 g_return_if_fail (h >= 0.0 && h <= 1.0);
1502 g_return_if_fail (s >= 0.0 && s <= 1.0);
1503 g_return_if_fail (v >= 0.0 && v <= 1.0);
1511 g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1513 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1517 * gtk_hsv_get_color:
1518 * @hsv: An HSV color selector.
1519 * @h: Return value for the hue.
1520 * @s: Return value for the saturation.
1521 * @v: Return value for the value.
1523 * Queries the current color in an HSV color selector. Returned values will be
1524 * in the [0.0, 1.0] range.
1527 gtk_hsv_get_color (GtkHSV *hsv, double *h, double *s, double *v)
1531 g_return_if_fail (GTK_IS_HSV (hsv));
1546 * gtk_hsv_set_metrics:
1547 * @hsv: An HSV color selector.
1548 * @size: Diameter for the hue ring.
1549 * @ring_width: Width of the hue ring.
1551 * Sets the size and ring width of an HSV color selector.
1554 gtk_hsv_set_metrics (GtkHSV *hsv,
1561 g_return_if_fail (GTK_IS_HSV (hsv));
1562 g_return_if_fail (size > 0);
1563 g_return_if_fail (ring_width > 0);
1564 g_return_if_fail (2 * ring_width + 1 <= size);
1568 same_size = (priv->size == size);
1571 priv->ring_width = ring_width;
1574 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1576 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1580 * gtk_hsv_get_metrics:
1581 * @hsv: An HSV color selector.
1582 * @size: Return value for the diameter of the hue ring.
1583 * @ring_width: Return value for the width of the hue ring.
1585 * Queries the size and ring width of an HSV color selector.
1588 gtk_hsv_get_metrics (GtkHSV *hsv,
1594 g_return_if_fail (GTK_IS_HSV (hsv));
1602 *ring_width = priv->ring_width;
1606 * gtk_hsv_is_adjusting:
1609 * An HSV color selector can be said to be adjusting if multiple rapid changes
1610 * are being made to its value, for example, when the user is adjusting the
1611 * value with the mouse. This function queries whether the HSV color selector
1612 * is being adjusted or not.
1614 * Return value: TRUE if clients can ignore changes to the color value, since
1615 * they may be transitory, or FALSE if they should consider the color value
1616 * status to be final.
1619 gtk_hsv_is_adjusting (GtkHSV *hsv)
1623 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1627 return priv->mode != DRAG_NONE;
1635 * @r: Return value for the red component.
1636 * @g: Return value for the green component.
1637 * @b: Return value for the blue component.
1639 * Converts a color from HSV space to RGB. Input values must be in the
1640 * [0.0, 1.0] range; output values will be in the same range.
1643 gtk_hsv_to_rgb (gdouble h,
1650 g_return_if_fail (h >= 0.0 && h <= 1.0);
1651 g_return_if_fail (s >= 0.0 && s <= 1.0);
1652 g_return_if_fail (v >= 0.0 && v <= 1.0);
1654 hsv_to_rgb (&h, &s, &v);
1671 * @h: Return value for the hue component.
1672 * @s: Return value for the saturation component.
1673 * @v: Return value for the value component.
1675 * Converts a color from RGB space to HSV. Input values must be in the
1676 * [0.0, 1.0] range; output values will be in the same range.
1679 gtk_rgb_to_hsv (gdouble r,
1686 g_return_if_fail (r >= 0.0 && r <= 1.0);
1687 g_return_if_fail (g >= 0.0 && g <= 1.0);
1688 g_return_if_fail (b >= 0.0 && b <= 1.0);
1690 rgb_to_hsv (&r, &g, &b);
1703 gtk_hsv_move (GtkHSV *hsv,
1704 GtkDirectionType dir)
1707 gdouble hue, sat, val;
1708 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1709 gint x, y; /* position in triangle */
1717 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1719 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1720 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1722 #define HUE_DELTA 0.002
1726 if (priv->focus_on_ring)
1731 compute_sv (hsv, x, y, &sat, &val);
1736 if (priv->focus_on_ring)
1741 compute_sv (hsv, x, y, &sat, &val);
1746 if (priv->focus_on_ring)
1751 compute_sv (hsv, x, y, &sat, &val);
1756 if (priv->focus_on_ring)
1762 compute_sv (hsv, x, y, &sat, &val);
1767 /* we don't care about the tab directions */
1777 gtk_hsv_set_color (hsv, hue, sat, val);