]> Pileus Git - ~andy/gtk/blob - gtk/gtkhsv.c
API Change. Take GdkColor arguments instead of gdouble *. Leave the old
[~andy/gtk] / gtk / gtkhsv.c
1 /* HSV color selector for GTK+
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
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+)
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #include <math.h>
26 #include "gtksignal.h"
27 #include "gtkhsv.h"
28 #include "gdk/gdkkeysyms.h"
29 #include "gtkbindings.h"
30 #include "gtkcontainer.h"
31
32 /*
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/.
37  */
38
39 /* Default width/height */
40 #define DEFAULT_SIZE 100
41
42 /* Default ring width */
43 #define DEFAULT_RING_WIDTH 10
44
45
46 /* Dragging modes */
47 typedef enum {
48   DRAG_NONE,
49   DRAG_H,
50   DRAG_SV
51 } DragMode;
52
53 /* Private part of the GtkHSV structure */
54 typedef struct {
55   /* Color value */
56   double h;
57   double s;
58   double v;
59   
60   /* Size and ring width */
61   int size;
62   int ring_width;
63   
64   /* Window for capturing events */
65   GdkWindow *window;
66   
67   /* GC for drawing */
68   GdkGC *gc;
69   
70   /* Dragging mode */
71   DragMode mode;
72
73   guint focus_on_ring : 1;
74   
75 } HSVPrivate;
76
77 \f
78
79 /* Signal IDs */
80
81 enum {
82   CHANGED,
83   MOVE,
84   LAST_SIGNAL
85 };
86
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);
108
109 static void gtk_hsv_move           (GtkHSV          *hsv,
110                                     GtkDirectionType dir);
111
112 static guint hsv_signals[LAST_SIGNAL];
113 static GtkWidgetClass *parent_class;
114
115
116 /**
117  * gtk_hsv_get_type:
118  * @void:
119  *
120  * Registers the &GtkHSV class if necessary, and returns the type ID associated
121  * to it.
122  *
123  * Return value: The type ID of the &GtkHSV class.
124  **/
125 GtkType
126 gtk_hsv_get_type (void)
127 {
128   static GtkType hsv_type = 0;
129   
130   if (!hsv_type) {
131     static const GtkTypeInfo hsv_info = {
132       "GtkHSV",
133       sizeof (GtkHSV),
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
140     };
141     
142     hsv_type = gtk_type_unique (GTK_TYPE_CONTAINER, &hsv_info);
143   }
144   
145   return hsv_type;
146 }
147
148 /* Class initialization function for the HSV color selector */
149 static void
150 gtk_hsv_class_init (GtkHSVClass *class)
151 {
152   GtkObjectClass *object_class;
153   GtkWidgetClass *widget_class;
154   GtkContainerClass *container_class;
155   GtkHSVClass    *hsv_class;
156   GtkBindingSet *binding_set;
157   
158   object_class = (GtkObjectClass *) class;
159   widget_class = (GtkWidgetClass *) class;
160   container_class = GTK_CONTAINER_CLASS (class);
161   hsv_class = GTK_HSV_CLASS (class);
162   
163   parent_class = gtk_type_class (GTK_TYPE_WIDGET);  
164   
165   object_class->destroy = gtk_hsv_destroy;
166   
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;
177
178   container_class->focus = gtk_hsv_focus;
179   
180   hsv_class->move = gtk_hsv_move;
181   
182   hsv_signals[CHANGED] =
183     gtk_signal_new ("changed",
184                     GTK_RUN_FIRST,
185                     GTK_CLASS_TYPE (object_class),
186                     GTK_SIGNAL_OFFSET (GtkHSVClass, changed),
187                     gtk_marshal_VOID__VOID,
188                     GTK_TYPE_NONE, 0);
189
190   hsv_signals[MOVE] =
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);
197
198   binding_set = gtk_binding_set_by_class (class);
199
200   gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
201                                 "move", 1,
202                                 GTK_TYPE_ENUM, GTK_DIR_UP);
203   gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
204                                 "move", 1,
205                                 GTK_TYPE_ENUM, GTK_DIR_UP);
206   
207   gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
208                                 "move", 1,
209                                 GTK_TYPE_ENUM, GTK_DIR_DOWN);
210   gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
211                                 "move", 1,
212                                 GTK_TYPE_ENUM, GTK_DIR_DOWN);
213
214   
215   gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
216                                 "move", 1,
217                                 GTK_TYPE_ENUM, GTK_DIR_RIGHT);
218   gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
219                                 "move", 1,
220                                 GTK_TYPE_ENUM, GTK_DIR_RIGHT);
221   
222   gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
223                                 "move", 1,
224                                 GTK_TYPE_ENUM, GTK_DIR_LEFT);
225   gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
226                                 "move", 1,
227                                 GTK_TYPE_ENUM, GTK_DIR_LEFT);
228 }
229
230 /* Object initialization function for the HSV color selector */
231 static void
232 gtk_hsv_init (GtkHSV *hsv)
233 {
234   HSVPrivate *priv;
235   
236   priv = g_new0 (HSVPrivate, 1);
237   hsv->priv = priv;
238   
239   GTK_WIDGET_SET_FLAGS (hsv, GTK_NO_WINDOW);
240   GTK_WIDGET_SET_FLAGS (hsv, GTK_CAN_FOCUS);
241   
242   priv->h = 0.0;
243   priv->s = 0.0;
244   priv->v = 0.0;
245   
246   priv->size = DEFAULT_SIZE;
247   priv->ring_width = DEFAULT_RING_WIDTH;
248 }
249
250 /* Destroy handler for the HSV color selector */
251 static void
252 gtk_hsv_destroy (GtkObject *object)
253 {
254   GtkHSV *hsv;
255   
256   g_return_if_fail (GTK_IS_HSV (object));
257   
258   hsv = GTK_HSV (object);
259
260   if (hsv->priv)
261     {
262       g_free (hsv->priv);
263       hsv->priv = NULL;
264     }
265
266   GTK_OBJECT_CLASS (parent_class)->destroy (object);
267 }
268
269 /* Default signal handlers */
270
271 /* Map handler for the HSV color selector */
272 static void
273 gtk_hsv_map (GtkWidget *widget)
274 {
275   GtkHSV *hsv;
276   HSVPrivate *priv;
277   
278   hsv = GTK_HSV (widget);
279   priv = hsv->priv;
280   
281   if (GTK_WIDGET_MAPPED (widget))
282     return;
283   
284   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
285   
286   gdk_window_show (priv->window);
287 }
288
289 /* Unmap handler for the HSV color selector */
290 static void
291 gtk_hsv_unmap (GtkWidget *widget)
292 {
293   GtkHSV *hsv;
294   HSVPrivate *priv;
295   
296   hsv = GTK_HSV (widget);
297   priv = hsv->priv;
298   
299   if (!GTK_WIDGET_MAPPED (widget))
300     return;
301   
302   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
303   
304   gdk_window_hide (priv->window);
305 }
306
307 /* Realize handler for the HSV color selector */
308 static void
309 gtk_hsv_realize (GtkWidget *widget)
310 {
311   GtkHSV *hsv;
312   HSVPrivate *priv;
313   GdkWindowAttr attr;
314   int attr_mask;
315   GdkWindow *parent_window;
316   
317   hsv = GTK_HSV (widget);
318   priv = hsv->priv;
319   
320   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
321   
322   /* Create window */
323   
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   
336   attr_mask = GDK_WA_X | GDK_WA_Y;
337   
338   parent_window = gtk_widget_get_parent_window (widget);
339   
340   widget->window = parent_window;
341   gdk_window_ref (widget->window);
342   
343   priv->window = gdk_window_new (parent_window, &attr, attr_mask);
344   gdk_window_set_user_data (priv->window, hsv);
345   
346   widget->style = gtk_style_attach (widget->style, widget->window);
347   
348   /* Create GC */
349   
350   priv->gc = gdk_gc_new (parent_window);
351 }
352
353 /* Unrealize handler for the HSV color selector */
354 static void
355 gtk_hsv_unrealize (GtkWidget *widget)
356 {
357   GtkHSV *hsv;
358   HSVPrivate *priv;
359   
360   hsv = GTK_HSV (widget);
361   priv = hsv->priv;
362   
363   gdk_window_set_user_data (priv->window, NULL);
364   gdk_window_destroy (priv->window);
365   priv->window = NULL;
366   
367   gdk_gc_unref (priv->gc);
368   priv->gc = NULL;
369   
370   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
371     GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
372 }
373
374 /* Size_request handler for the HSV color selector */
375 static void
376 gtk_hsv_size_request (GtkWidget      *widget,
377                       GtkRequisition *requisition)
378 {
379   GtkHSV *hsv;
380   HSVPrivate *priv;
381   
382   hsv = GTK_HSV (widget);
383   priv = hsv->priv;
384   
385   requisition->width = priv->size;
386   requisition->height = priv->size;
387 }
388
389 /* Size_allocate handler for the HSV color selector */
390 static void
391 gtk_hsv_size_allocate (GtkWidget     *widget,
392                        GtkAllocation *allocation)
393 {
394   GtkHSV *hsv;
395   HSVPrivate *priv;
396   
397   hsv = GTK_HSV (widget);
398   priv = hsv->priv;
399   
400   widget->allocation = *allocation;
401   
402   if (GTK_WIDGET_REALIZED (widget))
403     gdk_window_move_resize (priv->window,
404                             allocation->x,
405                             allocation->y,
406                             allocation->width,
407                             allocation->height);
408 }
409
410
411 /* Utility functions */
412
413 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
414
415 /* Converts from HSV to RGB */
416 static void
417 hsv_to_rgb (gdouble *h,
418             gdouble *s,
419             gdouble *v)
420 {
421   gdouble hue, saturation, value;
422   gdouble f, p, q, t;
423   
424   if (*s == 0.0)
425     {
426       *h = *v;
427       *s = *v;
428       *v = *v; /* heh */
429     }
430   else
431     {
432       hue = *h * 6.0;
433       saturation = *s;
434       value = *v;
435       
436       if (hue == 6.0)
437         hue = 0.0;
438       
439       f = hue - (int) hue;
440       p = value * (1.0 - saturation);
441       q = value * (1.0 - saturation * f);
442       t = value * (1.0 - saturation * (1.0 - f));
443       
444       switch ((int) hue)
445         {
446         case 0:
447           *h = value;
448           *s = t;
449           *v = p;
450           break;
451           
452         case 1:
453           *h = q;
454           *s = value;
455           *v = p;
456           break;
457           
458         case 2:
459           *h = p;
460           *s = value;
461           *v = t;
462           break;
463           
464         case 3:
465           *h = p;
466           *s = q;
467           *v = value;
468           break;
469           
470         case 4:
471           *h = t;
472           *s = p;
473           *v = value;
474           break;
475           
476         case 5:
477           *h = value;
478           *s = p;
479           *v = q;
480           break;
481           
482         default:
483           g_assert_not_reached ();
484         }
485     }
486 }
487
488 /* Converts from RGB to HSV */
489 static void
490 rgb_to_hsv (gdouble *r,
491             gdouble *g,
492             gdouble *b)
493 {
494   gdouble red, green, blue;
495   gdouble h, s, v;
496   gdouble min, max;
497   gdouble delta;
498   
499   red = *r;
500   green = *g;
501   blue = *b;
502   
503   h = 0.0;
504   
505   if (red > green)
506     {
507       if (red > blue)
508         max = red;
509       else
510         max = blue;
511       
512       if (green < blue)
513         min = green;
514       else
515         min = blue;
516     }
517   else
518     {
519       if (green > blue)
520         max = green;
521       else
522         max = blue;
523       
524       if (red < blue)
525         min = red;
526       else
527         min = blue;
528     }
529   
530   v = max;
531   
532   if (max != 0.0)
533     s = (max - min) / max;
534   else
535     s = 0.0;
536   
537   if (s == 0.0)
538     h = 0.0;
539   else
540     {
541       delta = max - min;
542       
543       if (red == 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;
549       
550       h /= 6.0;
551       
552       if (h < 0.0)
553         h += 1.0;
554       else if (h > 1.0)
555         h -= 1.0;
556     }
557   
558   *r = h;
559   *g = s;
560   *b = v;
561 }
562
563 /* Computes the vertices of the saturation/value triangle */
564 static void
565 compute_triangle (GtkHSV *hsv,
566                   gint   *hx,
567                   gint   *hy,
568                   gint   *sx,
569                   gint   *sy,
570                   gint   *vx,
571                   gint   *vy)
572 {
573   HSVPrivate *priv;
574   gdouble center;
575   gdouble inner, outer;
576   gdouble angle;
577   
578   priv = hsv->priv;
579   
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;
584   
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);
591 }
592
593 /* Computes whether a point is inside the hue ring */
594 static gboolean
595 is_in_ring (GtkHSV *hsv,
596             gdouble x,
597             gdouble y)
598 {
599   HSVPrivate *priv;
600   gdouble dx, dy, dist;
601   gdouble center, inner, outer;
602   
603   priv = hsv->priv;
604   
605   center = priv->size / 2.0;
606   outer = priv->size / 2.0;
607   inner = outer - priv->ring_width;
608   
609   dx = x - center;
610   dy = center - y;
611   dist = dx * dx + dy * dy;
612   
613   return (dist >= inner * inner && dist <= outer * outer);
614 }
615
616 /* Computes a saturation/value pair based on the mouse coordinates */
617 static void
618 compute_sv (GtkHSV  *hsv,
619             gdouble  x,
620             gdouble  y,
621             gdouble *s,
622             gdouble *v)
623 {
624   HSVPrivate *priv;
625   int ihx, ihy, isx, isy, ivx, ivy;
626   double hx, hy, sx, sy, vx, vy;
627   double center;
628   
629   priv = hsv->priv;
630   
631   compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
632   center = priv->size / 2.0;
633   hx = ihx - center;
634   hy = center - ihy;
635   sx = isx - center;
636   sy = center - isy;
637   vx = ivx - center;
638   vy = center - ivy;
639   x -= center;
640   y = center - y;
641   
642   if (vx * (x - sx) + vy * (y - sy) < 0.0)
643     {
644       *s = 1.0;
645       *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
646             / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
647       
648       if (*v < 0.0)
649         *v = 0.0;
650       else if (*v > 1.0)
651         *v = 1.0;
652     }
653   else if (hx * (x - sx) + hy * (y - sy) < 0.0)
654     {
655       *s = 0.0;
656       *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
657             / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
658       
659       if (*v < 0.0)
660         *v = 0.0;
661       else if (*v > 1.0)
662         *v = 1.0;
663     }
664   else if (sx * (x - hx) + sy * (y - hy) < 0.0)
665     {
666       *v = 1.0;
667       *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
668             ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
669       
670       if (*s < 0.0)
671         *s = 0.0;
672       else if (*s > 1.0)
673         *s = 1.0;
674     }
675   else
676     {
677       *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
678             / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
679       
680       if (*v<= 0.0)
681         {
682           *v = 0.0;
683           *s = 0.0;
684         }
685       else
686         {
687           if (*v > 1.0)
688             *v = 1.0;
689
690           if (fabs (hy - vy) < fabs (hx - vx))
691             *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
692           else
693             *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
694             
695           if (*s < 0.0)
696             *s = 0.0;
697           else if (*s > 1.0)
698             *s = 1.0;
699         }
700     }
701 }
702
703 /* Computes whether a point is inside the saturation/value triangle */
704 static gboolean
705 is_in_triangle (GtkHSV *hsv,
706                 gdouble x,
707                 gdouble y)
708 {
709   int hx, hy, sx, sy, vx, vy;
710   double det, s, v;
711   
712   compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
713   
714   det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
715   
716   s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
717   v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
718   
719   return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
720 }
721
722 /* Computes a value based on the mouse coordinates */
723 static double
724 compute_v (GtkHSV *hsv,
725            gdouble x,
726            gdouble y)
727 {
728   HSVPrivate *priv;
729   double center;
730   double dx, dy;
731   double angle;
732   
733   priv = hsv->priv;
734   
735   center = priv->size / 2.0;
736   dx = x - center;
737   dy = center - y;
738   
739   angle = atan2 (dy, dx);
740   if (angle < 0.0)
741     angle += 2.0 * G_PI;
742   
743   return angle / (2.0 * G_PI);
744 }
745
746 /* Event handlers */
747
748 static void
749 set_cross_grab (GtkHSV *hsv,
750                 guint32 time)
751 {
752   HSVPrivate *priv;
753   GdkCursor *cursor;
754   
755   priv = hsv->priv;
756   
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),
762                     NULL,
763                     cursor,
764                     time);
765   gdk_cursor_destroy (cursor);
766 }
767
768 /* Button_press_event handler for the HSV color selector */
769 static gint
770 gtk_hsv_button_press (GtkWidget      *widget,
771                       GdkEventButton *event)
772 {
773   GtkHSV *hsv;
774   HSVPrivate *priv;
775   double x, y;
776   
777   hsv = GTK_HSV (widget);
778   priv = hsv->priv;
779   
780   if (priv->mode != DRAG_NONE || event->button != 1)
781     return FALSE;
782   
783   x = event->x;
784   y = event->y;
785   
786   if (is_in_ring (hsv, x, y))
787     {
788       priv->mode = DRAG_H;
789       set_cross_grab (hsv, event->time);
790       
791       gtk_hsv_set_color (hsv,
792                          compute_v (hsv, x, y),
793                          priv->s,
794                          priv->v);
795
796       gtk_widget_grab_focus (widget);
797       priv->focus_on_ring = TRUE;
798       
799       return TRUE;
800     }
801   
802   if (is_in_triangle (hsv, x, y))
803     {
804       gdouble s, v;
805       
806       priv->mode = DRAG_SV;
807       set_cross_grab (hsv, event->time);
808       
809       compute_sv (hsv, x, y, &s, &v);
810       gtk_hsv_set_color (hsv, priv->h, s, v);
811
812       gtk_widget_grab_focus (widget);
813       priv->focus_on_ring = FALSE;
814       
815       return TRUE;
816     }
817   
818   return FALSE;
819 }
820
821 /* Button_release_event handler for the HSV color selector */
822 static gint
823 gtk_hsv_button_release (GtkWidget      *widget,
824                         GdkEventButton *event)
825 {
826   GtkHSV *hsv;
827   HSVPrivate *priv;
828   DragMode mode;
829   gdouble x, y;
830   
831   hsv = GTK_HSV (widget);
832   priv = hsv->priv;
833   
834   if (priv->mode == DRAG_NONE || event->button != 1)
835     return FALSE;
836   
837   /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
838    * can see that this is the final color state.
839    */
840   
841   mode = priv->mode;
842   priv->mode = DRAG_NONE;
843   
844   x = event->x;
845   y = event->y;
846   
847   if (mode == DRAG_H)
848     gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
849   else if (mode == DRAG_SV) {
850     double s, v;
851     
852     compute_sv (hsv, x, y, &s, &v);
853     gtk_hsv_set_color (hsv, priv->h, s, v);
854   } else
855     g_assert_not_reached ();
856   
857   gdk_pointer_ungrab (event->time);
858   
859   return TRUE;
860 }
861
862 /* Motion_notify_event handler for the HSV color selector */
863 static gint
864 gtk_hsv_motion (GtkWidget      *widget,
865                 GdkEventMotion *event)
866 {
867   GtkHSV *hsv;
868   HSVPrivate *priv;
869   double x, y;
870   gint ix, iy;
871   GdkModifierType mods;
872   
873   hsv = GTK_HSV (widget);
874   priv = hsv->priv;
875   
876   if (priv->mode == DRAG_NONE)
877     return FALSE;
878   
879   if (event->is_hint)
880     {
881       gdk_window_get_pointer (priv->window, &ix, &iy, &mods);
882       x = ix;
883       y = iy;
884     }
885   else
886     {
887       x = event->x;
888       y = event->y;
889     }
890   
891   if (priv->mode == DRAG_H)
892     {
893       gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
894       return TRUE;
895     }
896   else if (priv->mode == DRAG_SV)
897     {
898       double s, v;
899       
900       compute_sv (hsv, x, y, &s, &v);
901       gtk_hsv_set_color (hsv, priv->h, s, v);
902       return TRUE;
903     }
904   
905   g_assert_not_reached ();
906   return FALSE;
907 }
908
909
910 /* Redrawing */
911
912 /* Paints the hue ring */
913 static void
914 paint_ring (GtkHSV      *hsv,
915             GdkDrawable *drawable,
916             gint         x,
917             gint         y,
918             gint         width,
919             gint         height)
920 {
921   HSVPrivate *priv;
922   int xx, yy;
923   gdouble dx, dy, dist;
924   gdouble center;
925   gdouble inner, outer;
926   guchar *buf, *p;
927   gdouble angle;
928   gdouble hue;
929   gdouble r, g, b;
930   GdkBitmap *mask;
931   GdkGC *gc;
932   GdkColor color;
933   
934   priv = hsv->priv;
935   
936   center = priv->size / 2.0;
937   
938   outer = priv->size / 2.0;
939   inner = outer - priv->ring_width;
940   
941   /* Paint the ring */
942   
943   buf = g_new (guchar, width * height * 3);
944   
945   for (yy = 0; yy < height; yy++)
946     {
947       p = buf + yy * width * 3;
948       
949       dy = -(yy + y - center);
950       
951       for (xx = 0; xx < width; xx++)
952         {
953           dx = xx + x - center;
954           
955           dist = dx * dx + dy * dy;
956           if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
957             {
958               *p++ = 0;
959               *p++ = 0;
960               *p++ = 0;
961               continue;
962             }
963           
964           angle = atan2 (dy, dx);
965           if (angle < 0.0)
966             angle += 2.0 * G_PI;
967           
968           hue = angle / (2.0 * G_PI);
969           
970           r = hue;
971           g = 1.0;
972           b = 1.0;
973           hsv_to_rgb (&r, &g, &b);
974           
975           *p++ = floor (r * 255 + 0.5);
976           *p++ = floor (g * 255 + 0.5);
977           *p++ = floor (b * 255 + 0.5);
978         }
979     }
980   
981   /* Create clipping mask */
982   
983   mask = gdk_pixmap_new (NULL, width, height, 1);
984   gc = gdk_gc_new (mask);
985   
986   color.pixel = 0;
987   gdk_gc_set_foreground (gc, &color);
988   gdk_draw_rectangle (mask, gc, TRUE,
989                       0, 0, width, height);
990   
991   
992   color.pixel = 1;
993   gdk_gc_set_foreground (gc, &color);
994   gdk_draw_arc (mask, gc, TRUE,
995                 -x, -y,
996                 priv->size - 1, priv->size - 1,
997                 0, 360 * 64);
998   
999   color.pixel = 0;
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,
1004                 0, 360 * 64);
1005   
1006   gdk_gc_unref (gc);
1007   
1008   gdk_gc_set_clip_mask (priv->gc, mask);
1009   gdk_gc_set_clip_origin (priv->gc, 0, 0);
1010   
1011   /* Draw ring */
1012   
1013   gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
1014                                 GDK_RGB_DITHER_MAX,
1015                                 buf,
1016                                 width * 3,
1017                                 x, y);
1018   
1019   /* Draw value marker */
1020   
1021   r = priv->h;
1022   g = 1.0;
1023   b = 1.0;
1024   hsv_to_rgb (&r, &g, &b);
1025   
1026   if (INTENSITY (r, g, b) > 0.5)
1027     gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1028   else
1029     gdk_rgb_gc_set_foreground (priv->gc, 0xffffff);
1030   
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);
1035   
1036   gdk_gc_set_clip_mask (priv->gc, NULL);
1037   gdk_bitmap_unref (mask);
1038   
1039   g_free (buf);
1040   
1041   /* Draw ring outline */
1042
1043   if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1044       priv->focus_on_ring)
1045     {
1046       gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1047       
1048       gdk_draw_arc (drawable, priv->gc, FALSE,
1049                     -x, -y,
1050                     priv->size - 1, priv->size - 1,
1051                     0, 360 * 64);
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,
1055                     0, 360 * 64);
1056     }
1057 }
1058
1059 /* Converts an HSV triplet to an integer RGB triplet */
1060 static void
1061 get_color (gdouble h,
1062            gdouble s,
1063            gdouble v,
1064            gint   *r,
1065            gint   *g,
1066            gint   *b)
1067 {
1068   hsv_to_rgb (&h, &s, &v);
1069   
1070   *r = floor (h * 255 + 0.5);
1071   *g = floor (s * 255 + 0.5);
1072   *b = floor (v * 255 + 0.5);
1073 }
1074
1075 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1076
1077 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0)                                       \
1078                                ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1)))     \
1079                                : (a))
1080
1081 /* Paints the HSV triangle */
1082 static void
1083 paint_triangle (GtkHSV      *hsv,
1084                 GdkDrawable *drawable,
1085                 gint         x,
1086                 gint         y,
1087                 gint         width,
1088                 gint         height)
1089 {
1090   HSVPrivate *priv;
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 */
1095   gint t;
1096   guchar *buf, *p;
1097   gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1098   gint xx, yy;
1099   GdkBitmap *mask;
1100   GdkGC *gc;
1101   GdkColor color;
1102   GdkPoint points[3];
1103   gdouble r, g, b;
1104   
1105   priv = hsv->priv;
1106   
1107   /* Compute triangle's vertices */
1108   
1109   compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1110   
1111   x1 = hx;
1112   y1 = hy;
1113   get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1114   
1115   x2 = sx;
1116   y2 = sy;
1117   get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1118   
1119   x3 = vx;
1120   y3 = vy;
1121   get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1122   
1123   if (y2 > y3)
1124     {
1125       SWAP (x2, x3, t);
1126       SWAP (y2, y3, t);
1127       SWAP (r2, r3, t);
1128       SWAP (g2, g3, t);
1129       SWAP (b2, b3, t);
1130     }
1131   
1132   if (y1 > y3)
1133     {
1134       SWAP (x1, x3, t);
1135       SWAP (y1, y3, t);
1136       SWAP (r1, r3, t);
1137       SWAP (g1, g3, t);
1138       SWAP (b1, b3, t);
1139     }
1140   
1141   if (y1 > y2)
1142     {
1143       SWAP (x1, x2, t);
1144       SWAP (y1, y2, t);
1145       SWAP (r1, r2, t);
1146       SWAP (g1, g2, t);
1147       SWAP (b1, b2, t);
1148     }
1149   
1150   /* Shade the triangle */
1151   
1152   buf = g_new (guchar, width * height * 3);
1153   
1154   for (yy = 0; yy < height; yy++)
1155     {
1156       p = buf + yy * width * 3;
1157       
1158       if (yy + y < y1 || yy + y > y3)
1159         for (xx = 0; xx < width; xx++)
1160           {
1161             *p++ = 0;
1162             *p++ = 0;
1163             *p++ = 0;
1164           }
1165       else {
1166         if (yy + y < y2)
1167           {
1168             xl = LERP (x1, x2, y1, y2, yy + y);
1169             
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);
1173           }
1174         else
1175           {
1176             xl = LERP (x2, x3, y2, y3, yy + y);
1177             
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);
1181           }
1182         
1183         xr = LERP (x1, x3, y1, y3, yy + y);
1184         
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);
1188         
1189         if (xl > xr)
1190           {
1191             SWAP (xl, xr, t);
1192             SWAP (rl, rr, t);
1193             SWAP (gl, gr, t);
1194             SWAP (bl, br, t);
1195           }
1196         
1197         for (xx = 0; xx < width; xx++)
1198           {
1199             if (xx + x < xl || xx + x > xr)
1200               {
1201                 *p++ = 0;
1202                 *p++ = 0;
1203                 *p++ = 0;
1204               }
1205             else
1206               {
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);
1210               }
1211           }
1212       }
1213     }
1214   
1215   /* Create clipping mask */
1216   
1217   mask = gdk_pixmap_new (NULL, width, height, 1);
1218   gc = gdk_gc_new (mask);
1219   
1220   color.pixel = 0;
1221   gdk_gc_set_foreground (gc, &color);
1222   gdk_draw_rectangle (mask, gc, TRUE,
1223                       0, 0, width, height);
1224   
1225   color.pixel = 1;
1226   gdk_gc_set_foreground (gc, &color);
1227   
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);
1235   
1236   gdk_gc_unref (gc);
1237   
1238   gdk_gc_set_clip_mask (priv->gc, mask);
1239   gdk_gc_set_clip_origin (priv->gc, 0, 0);
1240   
1241   /* Draw triangle */
1242   
1243   gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
1244                                 GDK_RGB_DITHER_MAX,
1245                                 buf,
1246                                 width * 3,
1247                                 x, y);
1248   
1249   gdk_gc_set_clip_mask (priv->gc, NULL);
1250   gdk_bitmap_unref (mask);
1251   
1252   g_free (buf);
1253   
1254   /* Draw triangle focus outline */
1255
1256   if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1257       !priv->focus_on_ring)
1258     {
1259       gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1260   
1261       gdk_draw_polygon (drawable, priv->gc, FALSE, points, 3);
1262     }
1263   
1264   /* Draw value marker */
1265   
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);
1268   
1269   r = priv->h;
1270   g = priv->s;
1271   b = priv->v;
1272   hsv_to_rgb (&r, &g, &b);
1273   
1274   if (INTENSITY (r, g, b) > 0.5)
1275     gdk_rgb_gc_set_foreground (priv->gc, 0x000000);
1276   else
1277     gdk_rgb_gc_set_foreground (priv->gc, 0xffffff);
1278   
1279   gdk_draw_arc (drawable, priv->gc, FALSE,
1280                 xx - 3, yy - 3,
1281                 6, 6,
1282                 0, 360 * 64);
1283   gdk_draw_arc (drawable, priv->gc, FALSE,
1284                 xx - 2, yy - 2,
1285                 4, 4,
1286                 0, 360 * 64);
1287 }
1288
1289 /* Paints the contents of the HSV color selector */
1290 static void
1291 paint (GtkHSV      *hsv,
1292        GdkDrawable *drawable,
1293        gint         x,
1294        gint         y,
1295        gint         width,
1296        gint         height)
1297 {
1298   paint_ring (hsv, drawable, x, y, width, height);
1299   paint_triangle (hsv, drawable, x, y, width, height);
1300 }
1301
1302 /* Expose_event handler for the HSV color selector */
1303 static gint
1304 gtk_hsv_expose (GtkWidget      *widget,
1305                 GdkEventExpose *event)
1306 {
1307   GtkHSV *hsv;
1308   HSVPrivate *priv;
1309   GdkRectangle rect, dest;
1310   GdkPixmap *pixmap;
1311   
1312   hsv = GTK_HSV (widget);
1313   priv = hsv->priv;
1314   
1315   if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
1316     return FALSE;
1317   
1318   rect.x = widget->allocation.x;
1319   rect.y = widget->allocation.y;
1320   rect.width = widget->allocation.width;
1321   rect.height = widget->allocation.height;
1322   
1323   if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1324     return FALSE;
1325   
1326   pixmap = gdk_pixmap_new (widget->window, dest.width, dest.height,
1327                            gtk_widget_get_visual (widget)->depth);
1328   
1329   rect = dest;
1330   rect.x = 0;
1331   rect.y = 0;
1332   
1333   gdk_draw_rectangle (pixmap,
1334                       widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
1335                       TRUE,
1336                       0, 0, dest.width, dest.height);
1337   paint (hsv, pixmap,
1338          dest.x - widget->allocation.x, dest.y - widget->allocation.y,
1339          dest.width, dest.height);
1340   
1341   gdk_draw_pixmap (widget->window,
1342                    priv->gc,
1343                    pixmap,
1344                    0, 0,
1345                    dest.x,
1346                    dest.y,
1347                    event->area.width, event->area.height);
1348   
1349   gdk_pixmap_unref (pixmap);
1350   
1351   return FALSE;
1352 }
1353
1354 static gboolean
1355 gtk_hsv_focus (GtkContainer    *container,
1356                GtkDirectionType dir)
1357 {
1358   GtkHSV *hsv;
1359   HSVPrivate *priv;
1360
1361   hsv = GTK_HSV (container);
1362   priv = hsv->priv;
1363   
1364   if (!GTK_WIDGET_DRAWABLE (container) ||
1365       !GTK_WIDGET_IS_SENSITIVE (container))
1366     return FALSE;
1367
1368   if (!GTK_WIDGET_HAS_FOCUS (hsv))
1369     {
1370       if (dir == GTK_DIR_TAB_BACKWARD)
1371         priv->focus_on_ring = FALSE;
1372       else
1373         priv->focus_on_ring = TRUE;
1374
1375       gtk_widget_grab_focus (GTK_WIDGET (hsv));
1376       return TRUE;
1377     }
1378   
1379   switch (dir)
1380     {
1381     case GTK_DIR_UP:
1382       if (priv->focus_on_ring)
1383         return FALSE;
1384       else
1385         priv->focus_on_ring = TRUE;
1386       break;
1387
1388     case GTK_DIR_DOWN:
1389       if (priv->focus_on_ring)
1390         priv->focus_on_ring = FALSE;
1391       else
1392         return FALSE;
1393       break;
1394
1395     case GTK_DIR_LEFT:
1396     case GTK_DIR_TAB_BACKWARD:
1397       if (priv->focus_on_ring)
1398         return FALSE;
1399       else
1400         priv->focus_on_ring = TRUE;
1401       break;
1402
1403     case GTK_DIR_RIGHT:
1404     case GTK_DIR_TAB_FORWARD:
1405       if (priv->focus_on_ring)
1406         priv->focus_on_ring = FALSE;
1407       else
1408         return FALSE;
1409       break;
1410     }
1411
1412   gtk_widget_queue_draw (GTK_WIDGET (hsv));
1413
1414   return TRUE;
1415 }
1416
1417 /**
1418  * gtk_hsv_new:
1419  * @void:
1420  *
1421  * Creates a new HSV color selector.
1422  *
1423  * Return value: A newly-created HSV color selector.
1424  **/
1425 GtkWidget*
1426 gtk_hsv_new (void)
1427 {
1428   return GTK_WIDGET (gtk_type_new (GTK_TYPE_HSV));
1429 }
1430
1431 /**
1432  * gtk_hsv_set_color:
1433  * @hsv: An HSV color selector.
1434  * @h: Hue.
1435  * @s: Saturation.
1436  * @v: Value.
1437  *
1438  * Sets the current color in an HSV color selector.  Color component values must
1439  * be in the [0.0, 1.0] range.
1440  **/
1441 void
1442 gtk_hsv_set_color (GtkHSV *hsv,
1443                    gdouble h,
1444                    gdouble s,
1445                    gdouble v)
1446 {
1447   HSVPrivate *priv;
1448   
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);
1454   
1455   priv = hsv->priv;
1456   
1457   priv->h = h;
1458   priv->s = s;
1459   priv->v = v;
1460   
1461   gtk_signal_emit (GTK_OBJECT (hsv), hsv_signals[CHANGED]);
1462   
1463   gtk_widget_queue_draw (GTK_WIDGET (hsv));
1464 }
1465
1466 /**
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.
1472  *
1473  * Queries the current color in an HSV color selector.  Returned values will be
1474  * in the [0.0, 1.0] range.
1475  **/
1476 void
1477 gtk_hsv_get_color (GtkHSV *hsv, double *h, double *s, double *v)
1478 {
1479   HSVPrivate *priv;
1480   
1481   g_return_if_fail (hsv != NULL);
1482   g_return_if_fail (GTK_IS_HSV (hsv));
1483   
1484   priv = hsv->priv;
1485   
1486   if (h)
1487     *h = priv->h;
1488   
1489   if (s)
1490     *s = priv->s;
1491   
1492   if (v)
1493     *v = priv->v;
1494 }
1495
1496 /**
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.
1501  *
1502  * Sets the size and ring width of an HSV color selector.
1503  **/
1504 void
1505 gtk_hsv_set_metrics (GtkHSV *hsv,
1506                      gint    size,
1507                      gint    ring_width)
1508 {
1509   HSVPrivate *priv;
1510   int same_size;
1511   
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);
1517   
1518   priv = hsv->priv;
1519   
1520   same_size = (priv->size == size);
1521   
1522   priv->size = size;
1523   priv->ring_width = ring_width;
1524   
1525   if (same_size)
1526     gtk_widget_queue_draw (GTK_WIDGET (hsv));
1527   else
1528     gtk_widget_queue_resize (GTK_WIDGET (hsv));
1529 }
1530
1531 /**
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.
1536  *
1537  * Queries the size and ring width of an HSV color selector.
1538  **/
1539 void
1540 gtk_hsv_get_metrics (GtkHSV *hsv,
1541                      gint   *size,
1542                      gint   *ring_width)
1543 {
1544   HSVPrivate *priv;
1545   
1546   g_return_if_fail (hsv != NULL);
1547   g_return_if_fail (GTK_IS_HSV (hsv));
1548   
1549   priv = hsv->priv;
1550   
1551   if (size)
1552     *size = priv->size;
1553   
1554   if (ring_width)
1555     *ring_width = priv->ring_width;
1556 }
1557
1558 /**
1559  * gtk_hsv_is_adjusting:
1560  * @hsv:
1561  *
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.
1566  *
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.
1570  **/
1571 gboolean
1572 gtk_hsv_is_adjusting (GtkHSV *hsv)
1573 {
1574   HSVPrivate *priv;
1575   
1576   g_return_val_if_fail (hsv != NULL, FALSE);
1577   g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1578   
1579   priv = hsv->priv;
1580
1581   return priv->mode != DRAG_NONE;
1582 }
1583
1584 /**
1585  * gtk_hsv_to_rgb:
1586  * @h: Hue.
1587  * @s: Saturation.
1588  * @v: Value.
1589  * @r: Return value for the red component.
1590  * @g: Return value for the green component.
1591  * @b: Return value for the blue component.
1592  * 
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.
1595  **/
1596 void
1597 gtk_hsv_to_rgb (gdouble  h,
1598                 gdouble  s,
1599                 gdouble  v,
1600                 gdouble *r,
1601                 gdouble *g,
1602                 gdouble *b)
1603 {
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);
1607   
1608   hsv_to_rgb (&h, &s, &v);
1609   
1610   if (r)
1611     *r = h;
1612   
1613   if (g)
1614     *g = s;
1615   
1616   if (b)
1617     *b = v;
1618 }
1619
1620 /**
1621  * gtk_hsv_to_rgb:
1622  * @r: Red.
1623  * @g: Green.
1624  * @b: Blue.
1625  * @h: Return value for the hue component.
1626  * @s: Return value for the saturation component.
1627  * @v: Return value for the value component.
1628  * 
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.
1631  **/
1632 void
1633 gtk_rgb_to_hsv (gdouble  r,
1634                 gdouble  g,
1635                 gdouble  b,
1636                 gdouble *h,
1637                 gdouble *s,
1638                 gdouble *v)
1639 {
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);
1643   
1644   rgb_to_hsv (&r, &g, &b);
1645   
1646   if (h)
1647     *h = r;
1648   
1649   if (s)
1650     *s = g;
1651   
1652   if (v)
1653     *v = b;
1654 }
1655
1656 static void
1657 gtk_hsv_move (GtkHSV          *hsv,
1658               GtkDirectionType dir)
1659 {
1660   HSVPrivate *priv;
1661   gdouble hue, sat, val;
1662   gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1663   gint x, y; /* position in triangle */
1664   
1665   priv = hsv->priv;
1666
1667   hue = priv->h;
1668   sat = priv->s;
1669   val = priv->v;
1670
1671   compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1672
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);
1675
1676 #define HUE_DELTA 0.002
1677   switch (dir)
1678     {
1679     case GTK_DIR_UP:
1680       if (priv->focus_on_ring)
1681         hue += HUE_DELTA;
1682       else
1683         {
1684           y -= 1;
1685           compute_sv (hsv, x, y, &sat, &val);
1686         }
1687       break;
1688
1689     case GTK_DIR_DOWN:
1690       if (priv->focus_on_ring)
1691         hue -= HUE_DELTA;
1692       else
1693         {
1694           y += 1;
1695           compute_sv (hsv, x, y, &sat, &val);
1696         }
1697       break;
1698
1699     case GTK_DIR_LEFT:
1700       if (priv->focus_on_ring)
1701         hue += HUE_DELTA;
1702       else
1703         {
1704           x -= 1;
1705           compute_sv (hsv, x, y, &sat, &val);
1706         }
1707       break;
1708
1709     case GTK_DIR_RIGHT:
1710       if (priv->focus_on_ring)
1711         hue -= HUE_DELTA
1712           ;
1713       else
1714         {
1715           x += 1;
1716           compute_sv (hsv, x, y, &sat, &val);
1717         }
1718       break;
1719
1720     default:
1721       /* we don't care about the tab directions */
1722       break;
1723     }
1724
1725   /* Wrap */
1726   if (hue < 0.0)
1727     hue = 1.0;
1728   else if (hue > 1.0)
1729     hue = 0.0;
1730   
1731   gtk_hsv_set_color (hsv, hue, sat, val);
1732 }
1733