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 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
699 /* Computes whether a point is inside the saturation/value triangle */
701 is_in_triangle (GtkHSV *hsv,
705 int hx, hy, sx, sy, vx, vy;
708 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
710 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
712 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
713 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
715 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
718 /* Computes a value based on the mouse coordinates */
720 compute_v (GtkHSV *hsv,
731 center = priv->size / 2.0;
735 angle = atan2 (dy, dx);
739 return angle / (2.0 * G_PI);
745 set_cross_grab (GtkHSV *hsv,
753 cursor = gdk_cursor_new (GDK_CROSSHAIR);
754 gdk_pointer_grab (priv->window, FALSE,
755 (GDK_POINTER_MOTION_MASK
756 | GDK_POINTER_MOTION_HINT_MASK
757 | GDK_BUTTON_RELEASE_MASK),
761 gdk_cursor_destroy (cursor);
764 /* Button_press_event handler for the HSV color selector */
766 gtk_hsv_button_press (GtkWidget *widget,
767 GdkEventButton *event)
773 hsv = GTK_HSV (widget);
776 if (priv->mode != DRAG_NONE || event->button != 1)
782 if (is_in_ring (hsv, x, y))
785 set_cross_grab (hsv, event->time);
787 gtk_hsv_set_color (hsv,
788 compute_v (hsv, x, y),
792 gtk_widget_grab_focus (widget);
793 priv->focus_on_ring = TRUE;
798 if (is_in_triangle (hsv, x, y))
802 priv->mode = DRAG_SV;
803 set_cross_grab (hsv, event->time);
805 compute_sv (hsv, x, y, &s, &v);
806 gtk_hsv_set_color (hsv, priv->h, s, v);
808 gtk_widget_grab_focus (widget);
809 priv->focus_on_ring = FALSE;
817 /* Button_release_event handler for the HSV color selector */
819 gtk_hsv_button_release (GtkWidget *widget,
820 GdkEventButton *event)
827 hsv = GTK_HSV (widget);
830 if (priv->mode == DRAG_NONE || event->button != 1)
833 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
834 * can see that this is the final color state.
838 priv->mode = DRAG_NONE;
844 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
845 else if (mode == DRAG_SV) {
848 compute_sv (hsv, x, y, &s, &v);
849 gtk_hsv_set_color (hsv, priv->h, s, v);
851 g_assert_not_reached ();
853 gdk_pointer_ungrab (event->time);
858 /* Motion_notify_event handler for the HSV color selector */
860 gtk_hsv_motion (GtkWidget *widget,
861 GdkEventMotion *event)
867 GdkModifierType mods;
869 hsv = GTK_HSV (widget);
872 if (priv->mode == DRAG_NONE)
877 gdk_window_get_pointer (priv->window, &ix, &iy, &mods);
887 if (priv->mode == DRAG_H)
889 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
892 else if (priv->mode == DRAG_SV)
896 compute_sv (hsv, x, y, &s, &v);
897 gtk_hsv_set_color (hsv, priv->h, s, v);
901 g_assert_not_reached ();
908 /* Paints the hue ring */
910 paint_ring (GtkHSV *hsv,
911 GdkDrawable *drawable,
919 gdouble dx, dy, dist;
921 gdouble inner, outer;
932 center = priv->size / 2.0;
934 outer = priv->size / 2.0;
935 inner = outer - priv->ring_width;
939 buf = g_new (guchar, width * height * 3);
941 for (yy = 0; yy < height; yy++)
943 p = buf + yy * width * 3;
945 dy = -(yy + y - center);
947 for (xx = 0; xx < width; xx++)
949 dx = xx + x - center;
951 dist = dx * dx + dy * dy;
952 if (dist < (inner * inner) || dist > (outer * outer))
960 angle = atan2 (dy, dx);
964 hue = angle / (2.0 * G_PI);
969 hsv_to_rgb (&r, &g, &b);
971 *p++ = floor (r * 255 + 0.5);
972 *p++ = floor (g * 255 + 0.5);
973 *p++ = floor (b * 255 + 0.5);
977 /* Create clipping mask */
979 mask = gdk_pixmap_new (NULL, width, height, 1);
980 gc = gdk_gc_new (mask);
983 gdk_gc_set_foreground (gc, &color);
984 gdk_draw_rectangle (mask, gc, TRUE,
985 0, 0, width, height);
989 gdk_gc_set_foreground (gc, &color);
990 gdk_draw_arc (mask, gc, TRUE,
992 priv->size - 1, priv->size - 1,
996 gdk_gc_set_foreground (gc, &color);
997 gdk_draw_arc (mask, gc, TRUE,
998 -x + priv->ring_width - 1, -y + priv->ring_width - 1,
999 priv->size - 2 * priv->ring_width + 1, priv->size - 2 * priv->ring_width + 1,
1004 gdk_gc_set_clip_mask (priv->gc, mask);
1005 gdk_gc_set_clip_origin (priv->gc, 0, 0);
1009 gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
1015 /* Draw value marker */
1020 hsv_to_rgb (&r, &g, &b);
1022 if (INTENSITY (r, g, b) > 0.5)
1023 gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1025 gdk_rgb_gc_set_foreground (priv->gc, 0xffffff);
1027 gdk_draw_line (drawable, priv->gc,
1028 -x + center, -y + center,
1029 -x + center + cos (priv->h * 2.0 * G_PI) * center,
1030 -y + center - sin (priv->h * 2.0 * G_PI) * center);
1032 gdk_gc_set_clip_mask (priv->gc, NULL);
1033 gdk_bitmap_unref (mask);
1037 /* Draw ring outline */
1039 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1040 priv->focus_on_ring)
1042 gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1044 gdk_draw_arc (drawable, priv->gc, FALSE,
1046 priv->size - 1, priv->size - 1,
1048 gdk_draw_arc (drawable, priv->gc, FALSE,
1049 -x + priv->ring_width - 1, -y + priv->ring_width - 1,
1050 priv->size - 2 * priv->ring_width + 1, priv->size - 2 * priv->ring_width + 1,
1055 /* Converts an HSV triplet to an integer RGB triplet */
1057 get_color (gdouble h,
1064 hsv_to_rgb (&h, &s, &v);
1066 *r = floor (h * 255 + 0.5);
1067 *g = floor (s * 255 + 0.5);
1068 *b = floor (v * 255 + 0.5);
1071 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1073 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
1074 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
1077 /* Paints the HSV triangle */
1079 paint_triangle (GtkHSV *hsv,
1080 GdkDrawable *drawable,
1087 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1088 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1089 gint x2, y2, r2, g2, b2; /* Second vertex */
1090 gint x3, y3, r3, g3, b3; /* Third vertex */
1093 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1103 /* Compute triangle's vertices */
1105 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1109 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1113 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1117 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1146 /* Shade the triangle */
1148 buf = g_new (guchar, width * height * 3);
1150 for (yy = 0; yy < height; yy++)
1152 p = buf + yy * width * 3;
1154 if (yy + y < y1 || yy + y > y3)
1155 for (xx = 0; xx < width; xx++)
1164 xl = LERP (x1, x2, y1, y2, yy + y);
1166 rl = LERP (r1, r2, y1, y2, yy + y);
1167 gl = LERP (g1, g2, y1, y2, yy + y);
1168 bl = LERP (b1, b2, y1, y2, yy + y);
1172 xl = LERP (x2, x3, y2, y3, yy + y);
1174 rl = LERP (r2, r3, y2, y3, yy + y);
1175 gl = LERP (g2, g3, y2, y3, yy + y);
1176 bl = LERP (b2, b3, y2, y3, yy + y);
1179 xr = LERP (x1, x3, y1, y3, yy + y);
1181 rr = LERP (r1, r3, y1, y3, yy + y);
1182 gr = LERP (g1, g3, y1, y3, yy + y);
1183 br = LERP (b1, b3, y1, y3, yy + y);
1193 for (xx = 0; xx < width; xx++)
1195 if (xx + x < xl || xx + x > xr)
1203 *p++ = LERP (rl, rr, xl, xr, xx + x);
1204 *p++ = LERP (gl, gr, xl, xr, xx + x);
1205 *p++ = LERP (bl, br, xl, xr, xx + x);
1211 /* Create clipping mask */
1213 mask = gdk_pixmap_new (NULL, width, height, 1);
1214 gc = gdk_gc_new (mask);
1217 gdk_gc_set_foreground (gc, &color);
1218 gdk_draw_rectangle (mask, gc, TRUE,
1219 0, 0, width, height);
1222 gdk_gc_set_foreground (gc, &color);
1224 points[0].x = x1 - x;
1225 points[0].y = y1 - y;
1226 points[1].x = x2 - x;
1227 points[1].y = y2 - y;
1228 points[2].x = x3 - x;
1229 points[2].y = y3 - y;
1230 gdk_draw_polygon (mask, gc, TRUE, points, 3);
1234 gdk_gc_set_clip_mask (priv->gc, mask);
1235 gdk_gc_set_clip_origin (priv->gc, 0, 0);
1239 gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
1245 gdk_gc_set_clip_mask (priv->gc, NULL);
1246 gdk_bitmap_unref (mask);
1250 /* Draw triangle focus outline */
1252 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1253 !priv->focus_on_ring)
1255 gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1257 gdk_draw_polygon (drawable, priv->gc, FALSE, points, 3);
1260 /* Draw value marker */
1262 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1263 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1268 hsv_to_rgb (&r, &g, &b);
1270 if (INTENSITY (r, g, b) > 0.5)
1271 gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1273 gdk_rgb_gc_set_foreground (priv->gc, 0xffffff);
1275 gdk_draw_arc (drawable, priv->gc, FALSE,
1279 gdk_draw_arc (drawable, priv->gc, FALSE,
1285 /* Paints the contents of the HSV color selector */
1288 GdkDrawable *drawable,
1294 paint_ring (hsv, drawable, x, y, width, height);
1295 paint_triangle (hsv, drawable, x, y, width, height);
1298 /* Expose_event handler for the HSV color selector */
1300 gtk_hsv_expose (GtkWidget *widget,
1301 GdkEventExpose *event)
1305 GdkRectangle rect, dest;
1308 hsv = GTK_HSV (widget);
1311 if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
1314 rect.x = widget->allocation.x;
1315 rect.y = widget->allocation.y;
1316 rect.width = widget->allocation.width;
1317 rect.height = widget->allocation.height;
1319 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1322 pixmap = gdk_pixmap_new (widget->window, dest.width, dest.height,
1323 gtk_widget_get_visual (widget)->depth);
1329 gdk_draw_rectangle (pixmap,
1330 widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
1332 0, 0, dest.width, dest.height);
1334 dest.x - widget->allocation.x, dest.y - widget->allocation.y,
1335 dest.width, dest.height);
1337 gdk_draw_pixmap (widget->window,
1343 event->area.width, event->area.height);
1345 gdk_pixmap_unref (pixmap);
1351 gtk_hsv_focus (GtkContainer *container,
1352 GtkDirectionType dir)
1357 hsv = GTK_HSV (container);
1360 if (!GTK_WIDGET_DRAWABLE (container) ||
1361 !GTK_WIDGET_IS_SENSITIVE (container))
1364 if (!GTK_WIDGET_HAS_FOCUS (hsv))
1366 if (dir == GTK_DIR_TAB_BACKWARD)
1367 priv->focus_on_ring = FALSE;
1369 priv->focus_on_ring = TRUE;
1371 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1378 if (priv->focus_on_ring)
1381 priv->focus_on_ring = TRUE;
1385 if (priv->focus_on_ring)
1386 priv->focus_on_ring = FALSE;
1392 case GTK_DIR_TAB_BACKWARD:
1393 if (priv->focus_on_ring)
1396 priv->focus_on_ring = TRUE;
1400 case GTK_DIR_TAB_FORWARD:
1401 if (priv->focus_on_ring)
1402 priv->focus_on_ring = FALSE;
1408 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1417 * Creates a new HSV color selector.
1419 * Return value: A newly-created HSV color selector.
1424 return GTK_WIDGET (gtk_type_new (GTK_TYPE_HSV));
1428 * gtk_hsv_set_color:
1429 * @hsv: An HSV color selector.
1434 * Sets the current color in an HSV color selector. Color component values must
1435 * be in the [0.0, 1.0] range.
1438 gtk_hsv_set_color (GtkHSV *hsv,
1445 g_return_if_fail (hsv != NULL);
1446 g_return_if_fail (GTK_IS_HSV (hsv));
1447 g_return_if_fail (h >= 0.0 && h <= 1.0);
1448 g_return_if_fail (s >= 0.0 && s <= 1.0);
1449 g_return_if_fail (v >= 0.0 && v <= 1.0);
1457 gtk_signal_emit (GTK_OBJECT (hsv), hsv_signals[CHANGED]);
1459 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1463 * gtk_hsv_get_color:
1464 * @hsv: An HSV color selector.
1465 * @h: Return value for the hue.
1466 * @s: Return value for the saturation.
1467 * @v: Return value for the value.
1469 * Queries the current color in an HSV color selector. Returned values will be
1470 * in the [0.0, 1.0] range.
1473 gtk_hsv_get_color (GtkHSV *hsv, double *h, double *s, double *v)
1477 g_return_if_fail (hsv != NULL);
1478 g_return_if_fail (GTK_IS_HSV (hsv));
1493 * gtk_hsv_set_metrics:
1494 * @hsv: An HSV color selector.
1495 * @size: Diameter for the hue ring.
1496 * @ring_width: Width of the hue ring.
1498 * Sets the size and ring width of an HSV color selector.
1501 gtk_hsv_set_metrics (GtkHSV *hsv,
1508 g_return_if_fail (hsv != NULL);
1509 g_return_if_fail (GTK_IS_HSV (hsv));
1510 g_return_if_fail (size > 0);
1511 g_return_if_fail (ring_width > 0);
1512 g_return_if_fail (2 * ring_width + 1 <= size);
1516 same_size = (priv->size == size);
1519 priv->ring_width = ring_width;
1522 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1524 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1528 * gtk_hsv_get_metrics:
1529 * @hsv: An HSV color selector.
1530 * @size: Return value for the diameter of the hue ring.
1531 * @ring_width: Return value for the width of the hue ring.
1533 * Queries the size and ring width of an HSV color selector.
1536 gtk_hsv_get_metrics (GtkHSV *hsv,
1542 g_return_if_fail (hsv != NULL);
1543 g_return_if_fail (GTK_IS_HSV (hsv));
1551 *ring_width = priv->ring_width;
1555 * gtk_hsv_is_adjusting:
1558 * An HSV color selector can be said to be adjusting if multiple rapid changes
1559 * are being made to its value, for example, when the user is adjusting the
1560 * value with the mouse. This function queries whether the HSV color selector
1561 * is being adjusted or not.
1563 * Return value: TRUE if clients can ignore changes to the color value, since
1564 * they may be transitory, or FALSE if they should consider the color value
1565 * status to be final.
1568 gtk_hsv_is_adjusting (GtkHSV *hsv)
1572 g_return_val_if_fail (hsv != NULL, FALSE);
1573 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1577 return priv->mode != DRAG_NONE;
1585 * @r: Return value for the red component.
1586 * @g: Return value for the green component.
1587 * @b: Return value for the blue component.
1589 * Converts a color from HSV space to RGB. Input values must be in the
1590 * [0.0, 1.0] range; output values will be in the same range.
1593 gtk_hsv_to_rgb (gdouble h,
1600 g_return_if_fail (h >= 0.0 && h <= 1.0);
1601 g_return_if_fail (s >= 0.0 && s <= 1.0);
1602 g_return_if_fail (v >= 0.0 && v <= 1.0);
1604 hsv_to_rgb (&h, &s, &v);
1621 * @h: Return value for the hue component.
1622 * @s: Return value for the saturation component.
1623 * @v: Return value for the value component.
1625 * Converts a color from RGB space to HSV. Input values must be in the
1626 * [0.0, 1.0] range; output values will be in the same range.
1629 gtk_rgb_to_hsv (gdouble r,
1636 g_return_if_fail (r >= 0.0 && r <= 1.0);
1637 g_return_if_fail (g >= 0.0 && g <= 1.0);
1638 g_return_if_fail (b >= 0.0 && b <= 1.0);
1640 rgb_to_hsv (&r, &g, &b);
1653 gtk_hsv_move (GtkHSV *hsv,
1654 GtkDirectionType dir)
1657 gdouble hue, sat, val;
1658 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1659 gint x, y; /* position in triangle */
1667 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1669 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1670 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1672 #define HUE_DELTA 0.002
1676 if (priv->focus_on_ring)
1681 compute_sv (hsv, x, y, &sat, &val);
1686 if (priv->focus_on_ring)
1691 compute_sv (hsv, x, y, &sat, &val);
1696 if (priv->focus_on_ring)
1701 compute_sv (hsv, x, y, &sat, &val);
1706 if (priv->focus_on_ring)
1712 compute_sv (hsv, x, y, &sat, &val);
1717 /* we don't care about the tab directions */
1727 gtk_hsv_set_color (hsv, hue, sat, val);