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
335 | GDK_ENTER_NOTIFY_MASK
336 | GDK_LEAVE_NOTIFY_MASK);
338 attr_mask = GDK_WA_X | GDK_WA_Y;
340 parent_window = gtk_widget_get_parent_window (widget);
342 widget->window = parent_window;
343 gdk_window_ref (widget->window);
345 priv->window = gdk_window_new (parent_window, &attr, attr_mask);
346 gdk_window_set_user_data (priv->window, hsv);
348 widget->style = gtk_style_attach (widget->style, widget->window);
352 priv->gc = gdk_gc_new (parent_window);
355 /* Unrealize handler for the HSV color selector */
357 gtk_hsv_unrealize (GtkWidget *widget)
362 hsv = GTK_HSV (widget);
365 gdk_window_set_user_data (priv->window, NULL);
366 gdk_window_destroy (priv->window);
369 gdk_gc_unref (priv->gc);
372 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
373 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
376 /* Size_request handler for the HSV color selector */
378 gtk_hsv_size_request (GtkWidget *widget,
379 GtkRequisition *requisition)
384 hsv = GTK_HSV (widget);
387 requisition->width = priv->size;
388 requisition->height = priv->size;
391 /* Size_allocate handler for the HSV color selector */
393 gtk_hsv_size_allocate (GtkWidget *widget,
394 GtkAllocation *allocation)
399 hsv = GTK_HSV (widget);
402 widget->allocation = *allocation;
404 if (GTK_WIDGET_REALIZED (widget))
405 gdk_window_move_resize (priv->window,
413 /* Utility functions */
415 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
417 /* Converts from HSV to RGB */
419 hsv_to_rgb (gdouble *h,
423 gdouble hue, saturation, value;
442 p = value * (1.0 - saturation);
443 q = value * (1.0 - saturation * f);
444 t = value * (1.0 - saturation * (1.0 - f));
485 g_assert_not_reached ();
490 /* Converts from RGB to HSV */
492 rgb_to_hsv (gdouble *r,
496 gdouble red, green, blue;
535 s = (max - min) / max;
546 h = (green - blue) / delta;
547 else if (green == max)
548 h = 2 + (blue - red) / delta;
549 else if (blue == max)
550 h = 4 + (red - green) / delta;
565 /* Computes the vertices of the saturation/value triangle */
567 compute_triangle (GtkHSV *hsv,
577 gdouble inner, outer;
582 center = priv->size / 2.0;
583 outer = priv->size / 2.0;
584 inner = outer - priv->ring_width;
585 angle = priv->h * 2.0 * G_PI;
587 *hx = floor (center + cos (angle) * inner + 0.5);
588 *hy = floor (center - sin (angle) * inner + 0.5);
589 *sx = floor (center + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
590 *sy = floor (center - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
591 *vx = floor (center + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
592 *vy = floor (center - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
595 /* Computes whether a point is inside the hue ring */
597 is_in_ring (GtkHSV *hsv,
602 gdouble dx, dy, dist;
603 gdouble center, inner, outer;
607 center = priv->size / 2.0;
608 outer = priv->size / 2.0;
609 inner = outer - priv->ring_width;
613 dist = dx * dx + dy * dy;
615 return (dist >= inner * inner && dist <= outer * outer);
618 /* Computes a saturation/value pair based on the mouse coordinates */
620 compute_sv (GtkHSV *hsv,
627 int ihx, ihy, isx, isy, ivx, ivy;
628 double hx, hy, sx, sy, vx, vy;
633 compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
634 center = priv->size / 2.0;
644 if (vx * (x - sx) + vy * (y - sy) < 0.0)
647 *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
648 / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
655 else if (hx * (x - sx) + hy * (y - sy) < 0.0)
658 *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
659 / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
666 else if (sx * (x - hx) + sy * (y - hy) < 0.0)
669 *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
670 ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
679 *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
680 / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
692 if (fabs (hy - vy) < fabs (hx - vx))
693 *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
695 *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
705 /* Computes whether a point is inside the saturation/value triangle */
707 is_in_triangle (GtkHSV *hsv,
711 int hx, hy, sx, sy, vx, vy;
714 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
716 det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
718 s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
719 v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
721 return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
724 /* Computes a value based on the mouse coordinates */
726 compute_v (GtkHSV *hsv,
737 center = priv->size / 2.0;
741 angle = atan2 (dy, dx);
745 return angle / (2.0 * G_PI);
751 set_cross_grab (GtkHSV *hsv,
759 cursor = gdk_cursor_new (GDK_CROSSHAIR);
760 gdk_pointer_grab (priv->window, FALSE,
761 (GDK_POINTER_MOTION_MASK
762 | GDK_POINTER_MOTION_HINT_MASK
763 | GDK_BUTTON_RELEASE_MASK),
767 gdk_cursor_destroy (cursor);
770 /* Button_press_event handler for the HSV color selector */
772 gtk_hsv_button_press (GtkWidget *widget,
773 GdkEventButton *event)
779 hsv = GTK_HSV (widget);
782 if (priv->mode != DRAG_NONE || event->button != 1)
788 if (is_in_ring (hsv, x, y))
791 set_cross_grab (hsv, event->time);
793 gtk_hsv_set_color (hsv,
794 compute_v (hsv, x, y),
798 gtk_widget_grab_focus (widget);
799 priv->focus_on_ring = TRUE;
804 if (is_in_triangle (hsv, x, y))
808 priv->mode = DRAG_SV;
809 set_cross_grab (hsv, event->time);
811 compute_sv (hsv, x, y, &s, &v);
812 gtk_hsv_set_color (hsv, priv->h, s, v);
814 gtk_widget_grab_focus (widget);
815 priv->focus_on_ring = FALSE;
823 /* Button_release_event handler for the HSV color selector */
825 gtk_hsv_button_release (GtkWidget *widget,
826 GdkEventButton *event)
833 hsv = GTK_HSV (widget);
836 if (priv->mode == DRAG_NONE || event->button != 1)
839 /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
840 * can see that this is the final color state.
844 priv->mode = DRAG_NONE;
850 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
851 else if (mode == DRAG_SV) {
854 compute_sv (hsv, x, y, &s, &v);
855 gtk_hsv_set_color (hsv, priv->h, s, v);
857 g_assert_not_reached ();
859 gdk_pointer_ungrab (event->time);
864 /* Motion_notify_event handler for the HSV color selector */
866 gtk_hsv_motion (GtkWidget *widget,
867 GdkEventMotion *event)
873 GdkModifierType mods;
875 hsv = GTK_HSV (widget);
878 if (priv->mode == DRAG_NONE)
883 gdk_window_get_pointer (priv->window, &ix, &iy, &mods);
893 if (priv->mode == DRAG_H)
895 gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
898 else if (priv->mode == DRAG_SV)
902 compute_sv (hsv, x, y, &s, &v);
903 gtk_hsv_set_color (hsv, priv->h, s, v);
907 g_assert_not_reached ();
914 /* Paints the hue ring */
916 paint_ring (GtkHSV *hsv,
917 GdkDrawable *drawable,
925 gdouble dx, dy, dist;
927 gdouble inner, outer;
938 center = priv->size / 2.0;
940 outer = priv->size / 2.0;
941 inner = outer - priv->ring_width;
945 buf = g_new (guchar, width * height * 3);
947 for (yy = 0; yy < height; yy++)
949 p = buf + yy * width * 3;
951 dy = -(yy + y - center);
953 for (xx = 0; xx < width; xx++)
955 dx = xx + x - center;
957 dist = dx * dx + dy * dy;
958 if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
966 angle = atan2 (dy, dx);
970 hue = angle / (2.0 * G_PI);
975 hsv_to_rgb (&r, &g, &b);
977 *p++ = floor (r * 255 + 0.5);
978 *p++ = floor (g * 255 + 0.5);
979 *p++ = floor (b * 255 + 0.5);
983 /* Create clipping mask */
985 mask = gdk_pixmap_new (NULL, width, height, 1);
986 gc = gdk_gc_new (mask);
989 gdk_gc_set_foreground (gc, &color);
990 gdk_draw_rectangle (mask, gc, TRUE,
991 0, 0, width, height);
995 gdk_gc_set_foreground (gc, &color);
996 gdk_draw_arc (mask, gc, TRUE,
998 priv->size - 1, priv->size - 1,
1002 gdk_gc_set_foreground (gc, &color);
1003 gdk_draw_arc (mask, gc, TRUE,
1004 -x + priv->ring_width - 1, -y + priv->ring_width - 1,
1005 priv->size - 2 * priv->ring_width + 1, priv->size - 2 * priv->ring_width + 1,
1010 gdk_gc_set_clip_mask (priv->gc, mask);
1011 gdk_gc_set_clip_origin (priv->gc, 0, 0);
1015 gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
1021 /* Draw value marker */
1026 hsv_to_rgb (&r, &g, &b);
1028 if (INTENSITY (r, g, b) > 0.5)
1029 gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1031 gdk_rgb_gc_set_foreground (priv->gc, 0xffffff);
1033 gdk_draw_line (drawable, priv->gc,
1034 -x + center, -y + center,
1035 -x + center + cos (priv->h * 2.0 * G_PI) * center,
1036 -y + center - sin (priv->h * 2.0 * G_PI) * center);
1038 gdk_gc_set_clip_mask (priv->gc, NULL);
1039 gdk_bitmap_unref (mask);
1043 /* Draw ring outline */
1045 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1046 priv->focus_on_ring)
1048 gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1050 gdk_draw_arc (drawable, priv->gc, FALSE,
1052 priv->size - 1, priv->size - 1,
1054 gdk_draw_arc (drawable, priv->gc, FALSE,
1055 -x + priv->ring_width - 1, -y + priv->ring_width - 1,
1056 priv->size - 2 * priv->ring_width + 1, priv->size - 2 * priv->ring_width + 1,
1061 /* Converts an HSV triplet to an integer RGB triplet */
1063 get_color (gdouble h,
1070 hsv_to_rgb (&h, &s, &v);
1072 *r = floor (h * 255 + 0.5);
1073 *g = floor (s * 255 + 0.5);
1074 *b = floor (v * 255 + 0.5);
1077 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1079 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0) \
1080 ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1))) \
1083 /* Paints the HSV triangle */
1085 paint_triangle (GtkHSV *hsv,
1086 GdkDrawable *drawable,
1093 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1094 gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1095 gint x2, y2, r2, g2, b2; /* Second vertex */
1096 gint x3, y3, r3, g3, b3; /* Third vertex */
1099 gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1109 /* Compute triangle's vertices */
1111 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1115 get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1119 get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1123 get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1152 /* Shade the triangle */
1154 buf = g_new (guchar, width * height * 3);
1156 for (yy = 0; yy < height; yy++)
1158 p = buf + yy * width * 3;
1160 if (yy + y < y1 || yy + y > y3)
1161 for (xx = 0; xx < width; xx++)
1170 xl = LERP (x1, x2, y1, y2, yy + y);
1172 rl = LERP (r1, r2, y1, y2, yy + y);
1173 gl = LERP (g1, g2, y1, y2, yy + y);
1174 bl = LERP (b1, b2, y1, y2, yy + y);
1178 xl = LERP (x2, x3, y2, y3, yy + y);
1180 rl = LERP (r2, r3, y2, y3, yy + y);
1181 gl = LERP (g2, g3, y2, y3, yy + y);
1182 bl = LERP (b2, b3, y2, y3, yy + y);
1185 xr = LERP (x1, x3, y1, y3, yy + y);
1187 rr = LERP (r1, r3, y1, y3, yy + y);
1188 gr = LERP (g1, g3, y1, y3, yy + y);
1189 br = LERP (b1, b3, y1, y3, yy + y);
1199 for (xx = 0; xx < width; xx++)
1201 if (xx + x < xl || xx + x > xr)
1209 *p++ = LERP (rl, rr, xl, xr, xx + x);
1210 *p++ = LERP (gl, gr, xl, xr, xx + x);
1211 *p++ = LERP (bl, br, xl, xr, xx + x);
1217 /* Create clipping mask */
1219 mask = gdk_pixmap_new (NULL, width, height, 1);
1220 gc = gdk_gc_new (mask);
1223 gdk_gc_set_foreground (gc, &color);
1224 gdk_draw_rectangle (mask, gc, TRUE,
1225 0, 0, width, height);
1228 gdk_gc_set_foreground (gc, &color);
1230 points[0].x = x1 - x;
1231 points[0].y = y1 - y;
1232 points[1].x = x2 - x;
1233 points[1].y = y2 - y;
1234 points[2].x = x3 - x;
1235 points[2].y = y3 - y;
1236 gdk_draw_polygon (mask, gc, TRUE, points, 3);
1240 gdk_gc_set_clip_mask (priv->gc, mask);
1241 gdk_gc_set_clip_origin (priv->gc, 0, 0);
1245 gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
1251 gdk_gc_set_clip_mask (priv->gc, NULL);
1252 gdk_bitmap_unref (mask);
1256 /* Draw triangle focus outline */
1258 if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1259 !priv->focus_on_ring)
1261 gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1263 gdk_draw_polygon (drawable, priv->gc, FALSE, points, 3);
1266 /* Draw value marker */
1268 xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1269 yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1274 hsv_to_rgb (&r, &g, &b);
1276 if (INTENSITY (r, g, b) > 0.5)
1277 gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1279 gdk_rgb_gc_set_foreground (priv->gc, 0xffffff);
1281 #define OUTER_RADIUS 4
1282 #define INNER_RADIUS 3
1284 gdk_draw_arc (drawable, priv->gc, FALSE,
1285 xx - OUTER_RADIUS, yy - OUTER_RADIUS,
1286 OUTER_RADIUS * 2, OUTER_RADIUS * 2,
1288 gdk_draw_arc (drawable, priv->gc, FALSE,
1289 xx - INNER_RADIUS, yy - INNER_RADIUS,
1290 INNER_RADIUS * 2, INNER_RADIUS * 2,
1294 /* Paints the contents of the HSV color selector */
1297 GdkDrawable *drawable,
1303 paint_ring (hsv, drawable, x, y, width, height);
1304 paint_triangle (hsv, drawable, x, y, width, height);
1307 /* Expose_event handler for the HSV color selector */
1309 gtk_hsv_expose (GtkWidget *widget,
1310 GdkEventExpose *event)
1314 GdkRectangle rect, dest;
1317 hsv = GTK_HSV (widget);
1320 if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
1323 rect.x = widget->allocation.x;
1324 rect.y = widget->allocation.y;
1325 rect.width = widget->allocation.width;
1326 rect.height = widget->allocation.height;
1328 if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1331 pixmap = gdk_pixmap_new (widget->window, dest.width, dest.height,
1332 gtk_widget_get_visual (widget)->depth);
1338 gdk_draw_rectangle (pixmap,
1339 widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
1341 0, 0, dest.width, dest.height);
1343 dest.x - widget->allocation.x, dest.y - widget->allocation.y,
1344 dest.width, dest.height);
1346 gdk_draw_pixmap (widget->window,
1352 event->area.width, event->area.height);
1354 gdk_pixmap_unref (pixmap);
1360 gtk_hsv_focus (GtkContainer *container,
1361 GtkDirectionType dir)
1366 hsv = GTK_HSV (container);
1369 if (!GTK_WIDGET_DRAWABLE (container) ||
1370 !GTK_WIDGET_IS_SENSITIVE (container))
1373 if (!GTK_WIDGET_HAS_FOCUS (hsv))
1375 if (dir == GTK_DIR_TAB_BACKWARD)
1376 priv->focus_on_ring = FALSE;
1378 priv->focus_on_ring = TRUE;
1380 gtk_widget_grab_focus (GTK_WIDGET (hsv));
1387 if (priv->focus_on_ring)
1390 priv->focus_on_ring = TRUE;
1394 if (priv->focus_on_ring)
1395 priv->focus_on_ring = FALSE;
1401 case GTK_DIR_TAB_BACKWARD:
1402 if (priv->focus_on_ring)
1405 priv->focus_on_ring = TRUE;
1409 case GTK_DIR_TAB_FORWARD:
1410 if (priv->focus_on_ring)
1411 priv->focus_on_ring = FALSE;
1417 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1426 * Creates a new HSV color selector.
1428 * Return value: A newly-created HSV color selector.
1433 return GTK_WIDGET (gtk_type_new (GTK_TYPE_HSV));
1437 * gtk_hsv_set_color:
1438 * @hsv: An HSV color selector.
1443 * Sets the current color in an HSV color selector. Color component values must
1444 * be in the [0.0, 1.0] range.
1447 gtk_hsv_set_color (GtkHSV *hsv,
1454 g_return_if_fail (hsv != NULL);
1455 g_return_if_fail (GTK_IS_HSV (hsv));
1456 g_return_if_fail (h >= 0.0 && h <= 1.0);
1457 g_return_if_fail (s >= 0.0 && s <= 1.0);
1458 g_return_if_fail (v >= 0.0 && v <= 1.0);
1466 gtk_signal_emit (GTK_OBJECT (hsv), hsv_signals[CHANGED]);
1468 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1472 * gtk_hsv_get_color:
1473 * @hsv: An HSV color selector.
1474 * @h: Return value for the hue.
1475 * @s: Return value for the saturation.
1476 * @v: Return value for the value.
1478 * Queries the current color in an HSV color selector. Returned values will be
1479 * in the [0.0, 1.0] range.
1482 gtk_hsv_get_color (GtkHSV *hsv, double *h, double *s, double *v)
1486 g_return_if_fail (hsv != NULL);
1487 g_return_if_fail (GTK_IS_HSV (hsv));
1502 * gtk_hsv_set_metrics:
1503 * @hsv: An HSV color selector.
1504 * @size: Diameter for the hue ring.
1505 * @ring_width: Width of the hue ring.
1507 * Sets the size and ring width of an HSV color selector.
1510 gtk_hsv_set_metrics (GtkHSV *hsv,
1517 g_return_if_fail (hsv != NULL);
1518 g_return_if_fail (GTK_IS_HSV (hsv));
1519 g_return_if_fail (size > 0);
1520 g_return_if_fail (ring_width > 0);
1521 g_return_if_fail (2 * ring_width + 1 <= size);
1525 same_size = (priv->size == size);
1528 priv->ring_width = ring_width;
1531 gtk_widget_queue_draw (GTK_WIDGET (hsv));
1533 gtk_widget_queue_resize (GTK_WIDGET (hsv));
1537 * gtk_hsv_get_metrics:
1538 * @hsv: An HSV color selector.
1539 * @size: Return value for the diameter of the hue ring.
1540 * @ring_width: Return value for the width of the hue ring.
1542 * Queries the size and ring width of an HSV color selector.
1545 gtk_hsv_get_metrics (GtkHSV *hsv,
1551 g_return_if_fail (hsv != NULL);
1552 g_return_if_fail (GTK_IS_HSV (hsv));
1560 *ring_width = priv->ring_width;
1564 * gtk_hsv_is_adjusting:
1567 * An HSV color selector can be said to be adjusting if multiple rapid changes
1568 * are being made to its value, for example, when the user is adjusting the
1569 * value with the mouse. This function queries whether the HSV color selector
1570 * is being adjusted or not.
1572 * Return value: TRUE if clients can ignore changes to the color value, since
1573 * they may be transitory, or FALSE if they should consider the color value
1574 * status to be final.
1577 gtk_hsv_is_adjusting (GtkHSV *hsv)
1581 g_return_val_if_fail (hsv != NULL, FALSE);
1582 g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1586 return priv->mode != DRAG_NONE;
1594 * @r: Return value for the red component.
1595 * @g: Return value for the green component.
1596 * @b: Return value for the blue component.
1598 * Converts a color from HSV space to RGB. Input values must be in the
1599 * [0.0, 1.0] range; output values will be in the same range.
1602 gtk_hsv_to_rgb (gdouble h,
1609 g_return_if_fail (h >= 0.0 && h <= 1.0);
1610 g_return_if_fail (s >= 0.0 && s <= 1.0);
1611 g_return_if_fail (v >= 0.0 && v <= 1.0);
1613 hsv_to_rgb (&h, &s, &v);
1630 * @h: Return value for the hue component.
1631 * @s: Return value for the saturation component.
1632 * @v: Return value for the value component.
1634 * Converts a color from RGB space to HSV. Input values must be in the
1635 * [0.0, 1.0] range; output values will be in the same range.
1638 gtk_rgb_to_hsv (gdouble r,
1645 g_return_if_fail (r >= 0.0 && r <= 1.0);
1646 g_return_if_fail (g >= 0.0 && g <= 1.0);
1647 g_return_if_fail (b >= 0.0 && b <= 1.0);
1649 rgb_to_hsv (&r, &g, &b);
1662 gtk_hsv_move (GtkHSV *hsv,
1663 GtkDirectionType dir)
1666 gdouble hue, sat, val;
1667 gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1668 gint x, y; /* position in triangle */
1676 compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1678 x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1679 y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1681 #define HUE_DELTA 0.002
1685 if (priv->focus_on_ring)
1690 compute_sv (hsv, x, y, &sat, &val);
1695 if (priv->focus_on_ring)
1700 compute_sv (hsv, x, y, &sat, &val);
1705 if (priv->focus_on_ring)
1710 compute_sv (hsv, x, y, &sat, &val);
1715 if (priv->focus_on_ring)
1721 compute_sv (hsv, x, y, &sat, &val);
1726 /* we don't care about the tab directions */
1736 gtk_hsv_set_color (hsv, hue, sat, val);