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 #include "gtksignal.h"
28 #include "gdk/gdkkeysyms.h"
29 #include "gtkbindings.h"
30 #include "gtkcontainer.h"
33 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
34 * file for a list of people on the GTK+ Team. See the ChangeLog
35 * files for a list of changes. These files are distributed with
36 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
39 /* Default width/height */
40 #define DEFAULT_SIZE 100
42 /* Default ring width */
43 #define DEFAULT_RING_WIDTH 10
53 /* Private part of the GtkHSV structure */
60 /* Size and ring width */
64 /* Window for capturing events */
73 guint focus_on_ring : 1;
87 static void gtk_hsv_class_init (GtkHSVClass *class);
88 static void gtk_hsv_init (GtkHSV *hsv);
89 static void gtk_hsv_destroy (GtkObject *object);
90 static void gtk_hsv_map (GtkWidget *widget);
91 static void gtk_hsv_unmap (GtkWidget *widget);
92 static void gtk_hsv_realize (GtkWidget *widget);
93 static void gtk_hsv_unrealize (GtkWidget *widget);
94 static void gtk_hsv_size_request (GtkWidget *widget,
95 GtkRequisition *requisition);
96 static void gtk_hsv_size_allocate (GtkWidget *widget,
97 GtkAllocation *allocation);
98 static gint gtk_hsv_button_press (GtkWidget *widget,
99 GdkEventButton *event);
100 static gint gtk_hsv_button_release (GtkWidget *widget,
101 GdkEventButton *event);
102 static gint gtk_hsv_motion (GtkWidget *widget,
103 GdkEventMotion *event);
104 static gint gtk_hsv_expose (GtkWidget *widget,
105 GdkEventExpose *event);
106 static gboolean gtk_hsv_focus (GtkContainer *container,
107 GtkDirectionType direction);
109 static void gtk_hsv_move (GtkHSV *hsv,
110 GtkDirectionType dir);
112 static guint hsv_signals[LAST_SIGNAL];
113 static GtkWidgetClass *parent_class;
120 * Registers the &GtkHSV class if necessary, and returns the type ID associated
123 * Return value: The type ID of the &GtkHSV class.
126 gtk_hsv_get_type (void)
128 static GtkType hsv_type = 0;
131 static const GtkTypeInfo hsv_info = {
134 sizeof (GtkHSVClass),
135 (GtkClassInitFunc) gtk_hsv_class_init,
136 (GtkObjectInitFunc) gtk_hsv_init,
137 NULL, /* reserved_1 */
138 NULL, /* reserved_2 */
139 (GtkClassInitFunc) NULL
142 hsv_type = gtk_type_unique (GTK_TYPE_CONTAINER, &hsv_info);
148 /* Class initialization function for the HSV color selector */
150 gtk_hsv_class_init (GtkHSVClass *class)
152 GtkObjectClass *object_class;
153 GtkWidgetClass *widget_class;
154 GtkContainerClass *container_class;
155 GtkHSVClass *hsv_class;
156 GtkBindingSet *binding_set;
158 object_class = (GtkObjectClass *) class;
159 widget_class = (GtkWidgetClass *) class;
160 container_class = GTK_CONTAINER_CLASS (class);
161 hsv_class = GTK_HSV_CLASS (class);
163 parent_class = gtk_type_class (GTK_TYPE_WIDGET);
165 object_class->destroy = gtk_hsv_destroy;
167 widget_class->map = gtk_hsv_map;
168 widget_class->unmap = gtk_hsv_unmap;
169 widget_class->realize = gtk_hsv_realize;
170 widget_class->unrealize = gtk_hsv_unrealize;
171 widget_class->size_request = gtk_hsv_size_request;
172 widget_class->size_allocate = gtk_hsv_size_allocate;
173 widget_class->button_press_event = gtk_hsv_button_press;
174 widget_class->button_release_event = gtk_hsv_button_release;
175 widget_class->motion_notify_event = gtk_hsv_motion;
176 widget_class->expose_event = gtk_hsv_expose;
178 container_class->focus = gtk_hsv_focus;
180 hsv_class->move = gtk_hsv_move;
182 hsv_signals[CHANGED] =
183 gtk_signal_new ("changed",
185 GTK_CLASS_TYPE (object_class),
186 GTK_SIGNAL_OFFSET (GtkHSVClass, changed),
187 gtk_marshal_VOID__VOID,
191 gtk_signal_new ("move",
192 GTK_RUN_LAST | GTK_RUN_ACTION,
193 GTK_CLASS_TYPE (object_class),
194 GTK_SIGNAL_OFFSET (GtkHSVClass, move),
195 gtk_marshal_VOID__ENUM,
196 GTK_TYPE_NONE, 1, GTK_TYPE_DIRECTION_TYPE);
198 binding_set = gtk_binding_set_by_class (class);
200 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
202 GTK_TYPE_ENUM, GTK_DIR_UP);
203 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
205 GTK_TYPE_ENUM, GTK_DIR_UP);
207 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
209 GTK_TYPE_ENUM, GTK_DIR_DOWN);
210 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
212 GTK_TYPE_ENUM, GTK_DIR_DOWN);
215 gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
217 GTK_TYPE_ENUM, GTK_DIR_RIGHT);
218 gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
220 GTK_TYPE_ENUM, GTK_DIR_RIGHT);
222 gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
224 GTK_TYPE_ENUM, GTK_DIR_LEFT);
225 gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
227 GTK_TYPE_ENUM, GTK_DIR_LEFT);
230 /* Object initialization function for the HSV color selector */
232 gtk_hsv_init (GtkHSV *hsv)
236 priv = g_new0 (HSVPrivate, 1);
239 GTK_WIDGET_SET_FLAGS (hsv, GTK_NO_WINDOW);
240 GTK_WIDGET_SET_FLAGS (hsv, GTK_CAN_FOCUS);
246 priv->size = DEFAULT_SIZE;
247 priv->ring_width = DEFAULT_RING_WIDTH;
250 /* Destroy handler for the HSV color selector */
252 gtk_hsv_destroy (GtkObject *object)
256 g_return_if_fail (GTK_IS_HSV (object));
258 hsv = GTK_HSV (object);
266 GTK_OBJECT_CLASS (parent_class)->destroy (object);
269 /* Default signal handlers */
271 /* Map handler for the HSV color selector */
273 gtk_hsv_map (GtkWidget *widget)
278 hsv = GTK_HSV (widget);
281 if (GTK_WIDGET_MAPPED (widget))
284 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
286 gdk_window_show (priv->window);
289 /* Unmap handler for the HSV color selector */
291 gtk_hsv_unmap (GtkWidget *widget)
296 hsv = GTK_HSV (widget);
299 if (!GTK_WIDGET_MAPPED (widget))
302 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
304 gdk_window_hide (priv->window);
307 /* Realize handler for the HSV color selector */
309 gtk_hsv_realize (GtkWidget *widget)
315 GdkWindow *parent_window;
317 hsv = GTK_HSV (widget);
320 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
324 attr.window_type = GDK_WINDOW_CHILD;
325 attr.x = widget->allocation.x;
326 attr.y = widget->allocation.y;
327 attr.width = widget->allocation.width;
328 attr.height = widget->allocation.height;
329 attr.wclass = GDK_INPUT_ONLY;
330 attr.event_mask = gtk_widget_get_events (widget);
331 attr.event_mask |= (GDK_KEY_PRESS_MASK
332 | GDK_BUTTON_PRESS_MASK
333 | GDK_BUTTON_RELEASE_MASK
334 | GDK_POINTER_MOTION_MASK);
336 attr_mask = GDK_WA_X | GDK_WA_Y;
338 parent_window = gtk_widget_get_parent_window (widget);
340 widget->window = parent_window;
341 gdk_window_ref (widget->window);
343 priv->window = gdk_window_new (parent_window, &attr, attr_mask);
344 gdk_window_set_user_data (priv->window, hsv);
346 widget->style = gtk_style_attach (widget->style, widget->window);
350 priv->gc = gdk_gc_new (parent_window);
353 /* Unrealize handler for the HSV color selector */
355 gtk_hsv_unrealize (GtkWidget *widget)
360 hsv = GTK_HSV (widget);
363 gdk_window_set_user_data (priv->window, NULL);
364 gdk_window_destroy (priv->window);
367 gdk_gc_unref (priv->gc);
370 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
371 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
374 /* Size_request handler for the HSV color selector */
376 gtk_hsv_size_request (GtkWidget *widget,
377 GtkRequisition *requisition)
382 hsv = GTK_HSV (widget);
385 requisition->width = priv->size;
386 requisition->height = priv->size;
389 /* Size_allocate handler for the HSV color selector */
391 gtk_hsv_size_allocate (GtkWidget *widget,
392 GtkAllocation *allocation)
397 hsv = GTK_HSV (widget);
400 widget->allocation = *allocation;
402 if (GTK_WIDGET_REALIZED (widget))
403 gdk_window_move_resize (priv->window,
411 /* Utility functions */
413 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
415 /* Converts from HSV to RGB */
417 hsv_to_rgb (gdouble *h,
421 gdouble hue, saturation, value;
440 p = value * (1.0 - saturation);
441 q = value * (1.0 - saturation * f);
442 t = value * (1.0 - saturation * (1.0 - f));
483 g_assert_not_reached ();
488 /* Converts from RGB to HSV */
490 rgb_to_hsv (gdouble *r,
494 gdouble red, green, blue;
533 s = (max - min) / max;
544 h = (green - blue) / delta;
545 else if (green == max)
546 h = 2 + (blue - red) / delta;
547 else if (blue == max)
548 h = 4 + (red - green) / delta;
563 /* Computes the vertices of the saturation/value triangle */
565 compute_triangle (GtkHSV *hsv,
575 gdouble inner, outer;
580 center = priv->size / 2.0;
581 outer = priv->size / 2.0;
582 inner = outer - priv->ring_width;
583 angle = priv->h * 2.0 * G_PI;
585 *hx = floor (center + cos (angle) * inner + 0.5);
586 *hy = floor (center - sin (angle) * inner + 0.5);
587 *sx = floor (center + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
588 *sy = floor (center - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
589 *vx = floor (center + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
590 *vy = floor (center - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
593 /* Computes whether a point is inside the hue ring */
595 is_in_ring (GtkHSV *hsv,
600 gdouble dx, dy, dist;
601 gdouble center, inner, outer;
605 center = priv->size / 2.0;
606 outer = priv->size / 2.0;
607 inner = outer - priv->ring_width;
611 dist = dx * dx + dy * dy;
613 return (dist >= inner * inner && dist <= outer * outer);
616 /* Computes a saturation/value pair based on the mouse coordinates */
618 compute_sv (GtkHSV *hsv,
625 int ihx, ihy, isx, isy, ivx, ivy;
626 double hx, hy, sx, sy, vx, vy;
631 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
632 center = priv->size / 2.0;
642 if (vx * (x - sx) + vy * (y - sy) < 0.0)
645 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
646 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
653 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
656 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
657 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
664 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
667 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
668 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
677 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
678 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
690 if (fabs (hy - vy) < fabs (hx - vx))
691 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
693 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
703 /* Computes whether a point is inside the saturation/value triangle */
705 is_in_triangle (GtkHSV *hsv,
709 int hx, hy, sx, sy, vx, vy;
712 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
714 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
716 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
717 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
719 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
722 /* Computes a value based on the mouse coordinates */
724 compute_v (GtkHSV *hsv,
735 center = priv->size / 2.0;
739 angle = atan2 (dy, dx);
743 return angle / (2.0 * G_PI);
749 set_cross_grab (GtkHSV *hsv,
757 cursor = gdk_cursor_new (GDK_CROSSHAIR);
758 gdk_pointer_grab (priv->window, FALSE,
759 (GDK_POINTER_MOTION_MASK
760 | GDK_POINTER_MOTION_HINT_MASK
761 | GDK_BUTTON_RELEASE_MASK),
765 gdk_cursor_destroy (cursor);
768 /* Button_press_event handler for the HSV color selector */
770 gtk_hsv_button_press (GtkWidget *widget,
771 GdkEventButton *event)
777 hsv = GTK_HSV (widget);
780 if (priv->mode != DRAG_NONE || event->button != 1)
786 if (is_in_ring (hsv, x, y))
789 set_cross_grab (hsv, event->time);
791 gtk_hsv_set_color (hsv,
792 compute_v (hsv, x, y),
796 gtk_widget_grab_focus (widget);
797 priv->focus_on_ring = TRUE;
802 if (is_in_triangle (hsv, x, y))
806 priv->mode = DRAG_SV;
807 set_cross_grab (hsv, event->time);
809 compute_sv (hsv, x, y, &s, &v);
810 gtk_hsv_set_color (hsv, priv->h, s, v);
812 gtk_widget_grab_focus (widget);
813 priv->focus_on_ring = FALSE;
821 /* Button_release_event handler for the HSV color selector */
823 gtk_hsv_button_release (GtkWidget *widget,
824 GdkEventButton *event)
831 hsv = GTK_HSV (widget);
834 if (priv->mode == DRAG_NONE || event->button != 1)
837 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
838 * can see that this is the final color state.
842 priv->mode = DRAG_NONE;
848 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
849 else if (mode == DRAG_SV) {
852 compute_sv (hsv, x, y, &s, &v);
853 gtk_hsv_set_color (hsv, priv->h, s, v);
855 g_assert_not_reached ();
857 gdk_pointer_ungrab (event->time);
862 /* Motion_notify_event handler for the HSV color selector */
864 gtk_hsv_motion (GtkWidget *widget,
865 GdkEventMotion *event)
871 GdkModifierType mods;
873 hsv = GTK_HSV (widget);
876 if (priv->mode == DRAG_NONE)
881 gdk_window_get_pointer (priv->window, &ix, &iy, &mods);
891 if (priv->mode == DRAG_H)
893 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
896 else if (priv->mode == DRAG_SV)
900 compute_sv (hsv, x, y, &s, &v);
901 gtk_hsv_set_color (hsv, priv->h, s, v);
905 g_assert_not_reached ();
912 /* Paints the hue ring */
914 paint_ring (GtkHSV *hsv,
915 GdkDrawable *drawable,
923 gdouble dx, dy, dist;
925 gdouble inner, outer;
936 center = priv->size / 2.0;
938 outer = priv->size / 2.0;
939 inner = outer - priv->ring_width;
943 buf = g_new (guchar, width * height * 3);
945 for (yy = 0; yy < height; yy++)
947 p = buf + yy * width * 3;
949 dy = -(yy + y - center);
951 for (xx = 0; xx < width; xx++)
953 dx = xx + x - center;
955 dist = dx * dx + dy * dy;
956 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
964 angle = atan2 (dy, dx);
968 hue = angle / (2.0 * G_PI);
973 hsv_to_rgb (&r, &g, &b);
975 *p++ = floor (r * 255 + 0.5);
976 *p++ = floor (g * 255 + 0.5);
977 *p++ = floor (b * 255 + 0.5);
981 /* Create clipping mask */
983 mask = gdk_pixmap_new (NULL, width, height, 1);
984 gc = gdk_gc_new (mask);
987 gdk_gc_set_foreground (gc, &color);
988 gdk_draw_rectangle (mask, gc, TRUE,
989 0, 0, width, height);
993 gdk_gc_set_foreground (gc, &color);
994 gdk_draw_arc (mask, gc, TRUE,
996 priv->size - 1, priv->size - 1,
1000 gdk_gc_set_foreground (gc, &color);
1001 gdk_draw_arc (mask, gc, TRUE,
1002 -x + priv->ring_width - 1, -y + priv->ring_width - 1,
1003 priv->size - 2 * priv->ring_width + 1, priv->size - 2 * priv->ring_width + 1,
1008 gdk_gc_set_clip_mask (priv->gc, mask);
1009 gdk_gc_set_clip_origin (priv->gc, 0, 0);
1013 gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
1019 /* Draw value marker */
1024 hsv_to_rgb (&r, &g, &b);
1026 if (INTENSITY (r, g, b) > 0.5)
1027 gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1029 gdk_rgb_gc_set_foreground (priv->gc, 0xffffff);
1031 gdk_draw_line (drawable, priv->gc,
1032 -x + center, -y + center,
1033 -x + center + cos (priv->h * 2.0 * G_PI) * center,
1034 -y + center - sin (priv->h * 2.0 * G_PI) * center);
1036 gdk_gc_set_clip_mask (priv->gc, NULL);
1037 gdk_bitmap_unref (mask);
1041 /* Draw ring outline */
1043 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1044 priv->focus_on_ring)
1046 gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1048 gdk_draw_arc (drawable, priv->gc, FALSE,
1050 priv->size - 1, priv->size - 1,
1052 gdk_draw_arc (drawable, priv->gc, FALSE,
1053 -x + priv->ring_width - 1, -y + priv->ring_width - 1,
1054 priv->size - 2 * priv->ring_width + 1, priv->size - 2 * priv->ring_width + 1,
1059 /* Converts an HSV triplet to an integer RGB triplet */
1061 get_color (gdouble h,
1068 hsv_to_rgb (&h, &s, &v);
1070 *r = floor (h * 255 + 0.5);
1071 *g = floor (s * 255 + 0.5);
1072 *b = floor (v * 255 + 0.5);
1075 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1077 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
1078 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
1081 /* Paints the HSV triangle */
1083 paint_triangle (GtkHSV *hsv,
1084 GdkDrawable *drawable,
1091 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1092 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1093 gint x2, y2, r2, g2, b2; /* Second vertex */
1094 gint x3, y3, r3, g3, b3; /* Third vertex */
1097 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1107 /* Compute triangle's vertices */
1109 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1113 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1117 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1121 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1150 /* Shade the triangle */
1152 buf = g_new (guchar, width * height * 3);
1154 for (yy = 0; yy < height; yy++)
1156 p = buf + yy * width * 3;
1158 if (yy + y < y1 || yy + y > y3)
1159 for (xx = 0; xx < width; xx++)
1168 xl = LERP (x1, x2, y1, y2, yy + y);
1170 rl = LERP (r1, r2, y1, y2, yy + y);
1171 gl = LERP (g1, g2, y1, y2, yy + y);
1172 bl = LERP (b1, b2, y1, y2, yy + y);
1176 xl = LERP (x2, x3, y2, y3, yy + y);
1178 rl = LERP (r2, r3, y2, y3, yy + y);
1179 gl = LERP (g2, g3, y2, y3, yy + y);
1180 bl = LERP (b2, b3, y2, y3, yy + y);
1183 xr = LERP (x1, x3, y1, y3, yy + y);
1185 rr = LERP (r1, r3, y1, y3, yy + y);
1186 gr = LERP (g1, g3, y1, y3, yy + y);
1187 br = LERP (b1, b3, y1, y3, yy + y);
1197 for (xx = 0; xx < width; xx++)
1199 if (xx + x < xl || xx + x > xr)
1207 *p++ = LERP (rl, rr, xl, xr, xx + x);
1208 *p++ = LERP (gl, gr, xl, xr, xx + x);
1209 *p++ = LERP (bl, br, xl, xr, xx + x);
1215 /* Create clipping mask */
1217 mask = gdk_pixmap_new (NULL, width, height, 1);
1218 gc = gdk_gc_new (mask);
1221 gdk_gc_set_foreground (gc, &color);
1222 gdk_draw_rectangle (mask, gc, TRUE,
1223 0, 0, width, height);
1226 gdk_gc_set_foreground (gc, &color);
1228 points[0].x = x1 - x;
1229 points[0].y = y1 - y;
1230 points[1].x = x2 - x;
1231 points[1].y = y2 - y;
1232 points[2].x = x3 - x;
1233 points[2].y = y3 - y;
1234 gdk_draw_polygon (mask, gc, TRUE, points, 3);
1238 gdk_gc_set_clip_mask (priv->gc, mask);
1239 gdk_gc_set_clip_origin (priv->gc, 0, 0);
1243 gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
1249 gdk_gc_set_clip_mask (priv->gc, NULL);
1250 gdk_bitmap_unref (mask);
1254 /* Draw triangle focus outline */
1256 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1257 !priv->focus_on_ring)
1259 gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1261 gdk_draw_polygon (drawable, priv->gc, FALSE, points, 3);
1264 /* Draw value marker */
1266 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1267 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1272 hsv_to_rgb (&r, &g, &b);
1274 if (INTENSITY (r, g, b) > 0.5)
1275 gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1277 gdk_rgb_gc_set_foreground (priv->gc, 0xffffff);
1279 gdk_draw_arc (drawable, priv->gc, FALSE,
1283 gdk_draw_arc (drawable, priv->gc, FALSE,
1289 /* Paints the contents of the HSV color selector */
1292 GdkDrawable *drawable,
1298 paint_ring (hsv, drawable, x, y, width, height);
1299 paint_triangle (hsv, drawable, x, y, width, height);
1302 /* Expose_event handler for the HSV color selector */
1304 gtk_hsv_expose (GtkWidget *widget,
1305 GdkEventExpose *event)
1309 GdkRectangle rect, dest;
1312 hsv = GTK_HSV (widget);
1315 if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
1318 rect.x = widget->allocation.x;
1319 rect.y = widget->allocation.y;
1320 rect.width = widget->allocation.width;
1321 rect.height = widget->allocation.height;
1323 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1326 pixmap = gdk_pixmap_new (widget->window, dest.width, dest.height,
1327 gtk_widget_get_visual (widget)->depth);
1333 gdk_draw_rectangle (pixmap,
1334 widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
1336 0, 0, dest.width, dest.height);
1338 dest.x - widget->allocation.x, dest.y - widget->allocation.y,
1339 dest.width, dest.height);
1341 gdk_draw_pixmap (widget->window,
1347 event->area.width, event->area.height);
1349 gdk_pixmap_unref (pixmap);
1355 gtk_hsv_focus (GtkContainer *container,
1356 GtkDirectionType dir)
1361 hsv = GTK_HSV (container);
1364 if (!GTK_WIDGET_DRAWABLE (container) ||
1365 !GTK_WIDGET_IS_SENSITIVE (container))
1368 if (!GTK_WIDGET_HAS_FOCUS (hsv))
1370 if (dir == GTK_DIR_TAB_BACKWARD)
1371 priv->focus_on_ring = FALSE;
1373 priv->focus_on_ring = TRUE;
1375 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1382 if (priv->focus_on_ring)
1385 priv->focus_on_ring = TRUE;
1389 if (priv->focus_on_ring)
1390 priv->focus_on_ring = FALSE;
1396 case GTK_DIR_TAB_BACKWARD:
1397 if (priv->focus_on_ring)
1400 priv->focus_on_ring = TRUE;
1404 case GTK_DIR_TAB_FORWARD:
1405 if (priv->focus_on_ring)
1406 priv->focus_on_ring = FALSE;
1412 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1421 * Creates a new HSV color selector.
1423 * Return value: A newly-created HSV color selector.
1428 return GTK_WIDGET (gtk_type_new (GTK_TYPE_HSV));
1432 * gtk_hsv_set_color:
1433 * @hsv: An HSV color selector.
1438 * Sets the current color in an HSV color selector. Color component values must
1439 * be in the [0.0, 1.0] range.
1442 gtk_hsv_set_color (GtkHSV *hsv,
1449 g_return_if_fail (hsv != NULL);
1450 g_return_if_fail (GTK_IS_HSV (hsv));
1451 g_return_if_fail (h >= 0.0 && h <= 1.0);
1452 g_return_if_fail (s >= 0.0 && s <= 1.0);
1453 g_return_if_fail (v >= 0.0 && v <= 1.0);
1461 gtk_signal_emit (GTK_OBJECT (hsv), hsv_signals[CHANGED]);
1463 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1467 * gtk_hsv_get_color:
1468 * @hsv: An HSV color selector.
1469 * @h: Return value for the hue.
1470 * @s: Return value for the saturation.
1471 * @v: Return value for the value.
1473 * Queries the current color in an HSV color selector. Returned values will be
1474 * in the [0.0, 1.0] range.
1477 gtk_hsv_get_color (GtkHSV *hsv, double *h, double *s, double *v)
1481 g_return_if_fail (hsv != NULL);
1482 g_return_if_fail (GTK_IS_HSV (hsv));
1497 * gtk_hsv_set_metrics:
1498 * @hsv: An HSV color selector.
1499 * @size: Diameter for the hue ring.
1500 * @ring_width: Width of the hue ring.
1502 * Sets the size and ring width of an HSV color selector.
1505 gtk_hsv_set_metrics (GtkHSV *hsv,
1512 g_return_if_fail (hsv != NULL);
1513 g_return_if_fail (GTK_IS_HSV (hsv));
1514 g_return_if_fail (size > 0);
1515 g_return_if_fail (ring_width > 0);
1516 g_return_if_fail (2 * ring_width + 1 <= size);
1520 same_size = (priv->size == size);
1523 priv->ring_width = ring_width;
1526 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1528 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1532 * gtk_hsv_get_metrics:
1533 * @hsv: An HSV color selector.
1534 * @size: Return value for the diameter of the hue ring.
1535 * @ring_width: Return value for the width of the hue ring.
1537 * Queries the size and ring width of an HSV color selector.
1540 gtk_hsv_get_metrics (GtkHSV *hsv,
1546 g_return_if_fail (hsv != NULL);
1547 g_return_if_fail (GTK_IS_HSV (hsv));
1555 *ring_width = priv->ring_width;
1559 * gtk_hsv_is_adjusting:
1562 * An HSV color selector can be said to be adjusting if multiple rapid changes
1563 * are being made to its value, for example, when the user is adjusting the
1564 * value with the mouse. This function queries whether the HSV color selector
1565 * is being adjusted or not.
1567 * Return value: TRUE if clients can ignore changes to the color value, since
1568 * they may be transitory, or FALSE if they should consider the color value
1569 * status to be final.
1572 gtk_hsv_is_adjusting (GtkHSV *hsv)
1576 g_return_val_if_fail (hsv != NULL, FALSE);
1577 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1581 return priv->mode != DRAG_NONE;
1589 * @r: Return value for the red component.
1590 * @g: Return value for the green component.
1591 * @b: Return value for the blue component.
1593 * Converts a color from HSV space to RGB. Input values must be in the
1594 * [0.0, 1.0] range; output values will be in the same range.
1597 gtk_hsv_to_rgb (gdouble h,
1604 g_return_if_fail (h >= 0.0 && h <= 1.0);
1605 g_return_if_fail (s >= 0.0 && s <= 1.0);
1606 g_return_if_fail (v >= 0.0 && v <= 1.0);
1608 hsv_to_rgb (&h, &s, &v);
1625 * @h: Return value for the hue component.
1626 * @s: Return value for the saturation component.
1627 * @v: Return value for the value component.
1629 * Converts a color from RGB space to HSV. Input values must be in the
1630 * [0.0, 1.0] range; output values will be in the same range.
1633 gtk_rgb_to_hsv (gdouble r,
1640 g_return_if_fail (r >= 0.0 && r <= 1.0);
1641 g_return_if_fail (g >= 0.0 && g <= 1.0);
1642 g_return_if_fail (b >= 0.0 && b <= 1.0);
1644 rgb_to_hsv (&r, &g, &b);
1657 gtk_hsv_move (GtkHSV *hsv,
1658 GtkDirectionType dir)
1661 gdouble hue, sat, val;
1662 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1663 gint x, y; /* position in triangle */
1671 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1673 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1674 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1676 #define HUE_DELTA 0.002
1680 if (priv->focus_on_ring)
1685 compute_sv (hsv, x, y, &sat, &val);
1690 if (priv->focus_on_ring)
1695 compute_sv (hsv, x, y, &sat, &val);
1700 if (priv->focus_on_ring)
1705 compute_sv (hsv, x, y, &sat, &val);
1710 if (priv->focus_on_ring)
1716 compute_sv (hsv, x, y, &sat, &val);
1721 /* we don't care about the tab directions */
1731 gtk_hsv_set_color (hsv, hue, sat, val);