]> Pileus Git - ~andy/gtk/blob - gtk/gtkhsv.c
gtk/gtkimagemenuitem.c: use accessor functions to access GtkWidget
[~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 /*
26  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
27  * file for a list of people on the GTK+ Team.  See the ChangeLog
28  * files for a list of changes.  These files are distributed with
29  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30  */
31
32 #include "config.h"
33
34 #include <math.h>
35 #include <string.h>
36
37 #include "gdk/gdkkeysyms.h"
38
39 #include "gtkhsv.h"
40 #include "gtkbindings.h"
41 #include "gtkmarshalers.h"
42 #include "gtkintl.h"
43
44 /* Default width/height */
45 #define DEFAULT_SIZE 100
46
47 /* Default ring width */
48 #define DEFAULT_RING_WIDTH 10
49
50
51 /* Dragging modes */
52 typedef enum {
53   DRAG_NONE,
54   DRAG_H,
55   DRAG_SV
56 } DragMode;
57
58 /* Private part of the GtkHSV structure */
59 struct _GtkHSVPriv
60 {
61   /* Color value */
62   double h;
63   double s;
64   double v;
65   
66   /* Size and ring width */
67   int size;
68   int ring_width;
69   
70   /* Window for capturing events */
71   GdkWindow *window;
72   
73   /* Dragging mode */
74   DragMode mode;
75
76   guint focus_on_ring : 1;
77 };
78
79
80 /* Signal IDs */
81
82 enum {
83   CHANGED,
84   MOVE,
85   LAST_SIGNAL
86 };
87
88 static void     gtk_hsv_destroy        (GtkObject        *object);
89 static void     gtk_hsv_map            (GtkWidget        *widget);
90 static void     gtk_hsv_unmap          (GtkWidget        *widget);
91 static void     gtk_hsv_realize        (GtkWidget        *widget);
92 static void     gtk_hsv_unrealize      (GtkWidget        *widget);
93 static void     gtk_hsv_size_request   (GtkWidget        *widget,
94                                         GtkRequisition   *requisition);
95 static void     gtk_hsv_size_allocate  (GtkWidget        *widget,
96                                         GtkAllocation    *allocation);
97 static gint     gtk_hsv_button_press   (GtkWidget        *widget,
98                                         GdkEventButton   *event);
99 static gint     gtk_hsv_button_release (GtkWidget        *widget,
100                                         GdkEventButton   *event);
101 static gint     gtk_hsv_motion         (GtkWidget        *widget,
102                                         GdkEventMotion   *event);
103 static gint     gtk_hsv_expose         (GtkWidget        *widget,
104                                         GdkEventExpose   *event);
105 static gboolean gtk_hsv_grab_broken    (GtkWidget          *widget,
106                                         GdkEventGrabBroken *event);
107 static gboolean gtk_hsv_focus          (GtkWidget        *widget,
108                                         GtkDirectionType  direction);
109 static void     gtk_hsv_move           (GtkHSV           *hsv,
110                                         GtkDirectionType  dir);
111
112 static guint hsv_signals[LAST_SIGNAL];
113
114 G_DEFINE_TYPE (GtkHSV, gtk_hsv, GTK_TYPE_WIDGET)
115
116 /* Class initialization function for the HSV color selector */
117 static void
118 gtk_hsv_class_init (GtkHSVClass *class)
119 {
120   GObjectClass   *gobject_class;
121   GtkObjectClass *object_class;
122   GtkWidgetClass *widget_class;
123   GtkHSVClass    *hsv_class;
124   GtkBindingSet  *binding_set;
125   
126   gobject_class = (GObjectClass *) class;
127   object_class = (GtkObjectClass *) class;
128   widget_class = (GtkWidgetClass *) class;
129   hsv_class = GTK_HSV_CLASS (class);
130   
131   object_class->destroy = gtk_hsv_destroy;
132   
133   widget_class->map = gtk_hsv_map;
134   widget_class->unmap = gtk_hsv_unmap;                                      
135   widget_class->realize = gtk_hsv_realize;
136   widget_class->unrealize = gtk_hsv_unrealize;
137   widget_class->size_request = gtk_hsv_size_request;
138   widget_class->size_allocate = gtk_hsv_size_allocate;
139   widget_class->button_press_event = gtk_hsv_button_press;
140   widget_class->button_release_event = gtk_hsv_button_release;
141   widget_class->motion_notify_event = gtk_hsv_motion;
142   widget_class->expose_event = gtk_hsv_expose;
143   widget_class->focus = gtk_hsv_focus;
144   widget_class->grab_broken_event = gtk_hsv_grab_broken;
145   
146   hsv_class->move = gtk_hsv_move;
147   
148   hsv_signals[CHANGED] =
149     g_signal_new (I_("changed"),
150                   G_OBJECT_CLASS_TYPE (object_class),
151                   G_SIGNAL_RUN_FIRST,
152                   G_STRUCT_OFFSET (GtkHSVClass, changed),
153                   NULL, NULL,
154                   _gtk_marshal_VOID__VOID,
155                   G_TYPE_NONE, 0);
156
157   hsv_signals[MOVE] =
158     g_signal_new (I_("move"),
159                   G_OBJECT_CLASS_TYPE (object_class),
160                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
161                   G_STRUCT_OFFSET (GtkHSVClass, move),
162                   NULL, NULL,
163                   _gtk_marshal_VOID__ENUM,
164                   G_TYPE_NONE, 1,
165                   GTK_TYPE_DIRECTION_TYPE);
166
167   binding_set = gtk_binding_set_by_class (class);
168
169   gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
170                                 "move", 1,
171                                 G_TYPE_ENUM, GTK_DIR_UP);
172   gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
173                                 "move", 1,
174                                 G_TYPE_ENUM, GTK_DIR_UP);
175   
176   gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
177                                 "move", 1,
178                                 G_TYPE_ENUM, GTK_DIR_DOWN);
179   gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
180                                 "move", 1,
181                                 G_TYPE_ENUM, GTK_DIR_DOWN);
182
183   
184   gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
185                                 "move", 1,
186                                 G_TYPE_ENUM, GTK_DIR_RIGHT);
187   gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
188                                 "move", 1,
189                                 G_TYPE_ENUM, GTK_DIR_RIGHT);
190   
191   gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
192                                 "move", 1,
193                                 G_TYPE_ENUM, GTK_DIR_LEFT);
194   gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
195                                 "move", 1,
196                                 G_TYPE_ENUM, GTK_DIR_LEFT);
197
198   g_type_class_add_private (gobject_class, sizeof (GtkHSVPriv));
199 }
200
201 /* Object initialization function for the HSV color selector */
202 static void
203 gtk_hsv_init (GtkHSV *hsv)
204 {
205   GtkHSVPriv *priv;
206
207   priv = G_TYPE_INSTANCE_GET_PRIVATE (hsv, GTK_TYPE_HSV, GtkHSVPriv);
208   
209   hsv->priv = priv;
210
211   gtk_widget_set_has_window (GTK_WIDGET (hsv), FALSE);
212   gtk_widget_set_can_focus (GTK_WIDGET (hsv), TRUE);
213   
214   priv->h = 0.0;
215   priv->s = 0.0;
216   priv->v = 0.0;
217   
218   priv->size = DEFAULT_SIZE;
219   priv->ring_width = DEFAULT_RING_WIDTH;
220 }
221
222 /* Destroy handler for the HSV color selector */
223 static void
224 gtk_hsv_destroy (GtkObject *object)
225 {
226   GTK_OBJECT_CLASS (gtk_hsv_parent_class)->destroy (object);
227 }
228
229 /* Default signal handlers */
230
231     
232 /* Map handler for the HSV color selector */
233
234 static void
235 gtk_hsv_map (GtkWidget *widget)
236 {
237   GtkHSV *hsv = GTK_HSV (widget);
238   GtkHSVPriv *priv = hsv->priv;
239
240   GTK_WIDGET_CLASS (gtk_hsv_parent_class)->map (widget);
241
242   gdk_window_show (priv->window);
243 }
244
245 /* Unmap handler for the HSV color selector */
246
247 static void
248 gtk_hsv_unmap (GtkWidget *widget)
249 {
250   GtkHSV *hsv = GTK_HSV (widget);
251   GtkHSVPriv *priv = hsv->priv;
252
253   gdk_window_hide (priv->window);
254
255   GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unmap (widget);
256 }                                                                           
257                                       
258 /* Realize handler for the HSV color selector */
259 static void
260 gtk_hsv_realize (GtkWidget *widget)
261 {
262   GtkHSV *hsv = GTK_HSV (widget);
263   GtkHSVPriv *priv = hsv->priv;
264   GdkWindow *parent_window;
265   GdkWindowAttr attr;
266   int attr_mask;
267
268   gtk_widget_set_realized (widget, TRUE);
269   
270   /* Create window */
271   
272   attr.window_type = GDK_WINDOW_CHILD;
273   attr.x = widget->allocation.x;
274   attr.y = widget->allocation.y;
275   attr.width = widget->allocation.width;
276   attr.height = widget->allocation.height;
277   attr.wclass = GDK_INPUT_ONLY;
278   attr.event_mask = gtk_widget_get_events (widget);
279   attr.event_mask |= (GDK_KEY_PRESS_MASK
280                       | GDK_BUTTON_PRESS_MASK
281                       | GDK_BUTTON_RELEASE_MASK
282                       | GDK_POINTER_MOTION_MASK
283                       | GDK_ENTER_NOTIFY_MASK
284                       | GDK_LEAVE_NOTIFY_MASK);
285   
286   attr_mask = GDK_WA_X | GDK_WA_Y;
287   
288   parent_window = gtk_widget_get_parent_window (widget);
289   
290   widget->window = parent_window;
291   g_object_ref (widget->window);
292   
293   priv->window = gdk_window_new (parent_window, &attr, attr_mask);
294   gdk_window_set_user_data (priv->window, hsv);
295   
296   widget->style = gtk_style_attach (widget->style, widget->window);
297 }
298
299 /* Unrealize handler for the HSV color selector */
300 static void
301 gtk_hsv_unrealize (GtkWidget *widget)
302 {
303   GtkHSV *hsv = GTK_HSV (widget);
304   GtkHSVPriv *priv = hsv->priv;
305
306   gdk_window_set_user_data (priv->window, NULL);
307   gdk_window_destroy (priv->window);
308   priv->window = NULL;
309   
310   GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unrealize (widget);
311 }
312
313 /* Size_request handler for the HSV color selector */
314 static void
315 gtk_hsv_size_request (GtkWidget      *widget,
316                       GtkRequisition *requisition)
317 {
318   GtkHSV *hsv = GTK_HSV (widget);
319   GtkHSVPriv *priv = hsv->priv;
320   gint focus_width;
321   gint focus_pad;
322
323   gtk_widget_style_get (widget,
324                         "focus-line-width", &focus_width,
325                         "focus-padding", &focus_pad,
326                         NULL);
327   
328   requisition->width = priv->size + 2 * (focus_width + focus_pad);
329   requisition->height = priv->size + 2 * (focus_width + focus_pad);
330 }
331
332 /* Size_allocate handler for the HSV color selector */
333 static void
334 gtk_hsv_size_allocate (GtkWidget     *widget,
335                        GtkAllocation *allocation)
336 {
337   GtkHSV *hsv = GTK_HSV (widget);
338   GtkHSVPriv *priv = hsv->priv;
339
340   widget->allocation = *allocation;
341   
342   if (gtk_widget_get_realized (widget))
343     gdk_window_move_resize (priv->window,
344                             allocation->x,
345                             allocation->y,
346                             allocation->width,
347                             allocation->height);
348 }
349
350
351 /* Utility functions */
352
353 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
354
355 /* Converts from HSV to RGB */
356 static void
357 hsv_to_rgb (gdouble *h,
358             gdouble *s,
359             gdouble *v)
360 {
361   gdouble hue, saturation, value;
362   gdouble f, p, q, t;
363   
364   if (*s == 0.0)
365     {
366       *h = *v;
367       *s = *v;
368       *v = *v; /* heh */
369     }
370   else
371     {
372       hue = *h * 6.0;
373       saturation = *s;
374       value = *v;
375       
376       if (hue == 6.0)
377         hue = 0.0;
378       
379       f = hue - (int) hue;
380       p = value * (1.0 - saturation);
381       q = value * (1.0 - saturation * f);
382       t = value * (1.0 - saturation * (1.0 - f));
383       
384       switch ((int) hue)
385         {
386         case 0:
387           *h = value;
388           *s = t;
389           *v = p;
390           break;
391           
392         case 1:
393           *h = q;
394           *s = value;
395           *v = p;
396           break;
397           
398         case 2:
399           *h = p;
400           *s = value;
401           *v = t;
402           break;
403           
404         case 3:
405           *h = p;
406           *s = q;
407           *v = value;
408           break;
409           
410         case 4:
411           *h = t;
412           *s = p;
413           *v = value;
414           break;
415           
416         case 5:
417           *h = value;
418           *s = p;
419           *v = q;
420           break;
421           
422         default:
423           g_assert_not_reached ();
424         }
425     }
426 }
427
428 /* Converts from RGB to HSV */
429 static void
430 rgb_to_hsv (gdouble *r,
431             gdouble *g,
432             gdouble *b)
433 {
434   gdouble red, green, blue;
435   gdouble h, s, v;
436   gdouble min, max;
437   gdouble delta;
438   
439   red = *r;
440   green = *g;
441   blue = *b;
442   
443   h = 0.0;
444   
445   if (red > green)
446     {
447       if (red > blue)
448         max = red;
449       else
450         max = blue;
451       
452       if (green < blue)
453         min = green;
454       else
455         min = blue;
456     }
457   else
458     {
459       if (green > blue)
460         max = green;
461       else
462         max = blue;
463       
464       if (red < blue)
465         min = red;
466       else
467         min = blue;
468     }
469   
470   v = max;
471   
472   if (max != 0.0)
473     s = (max - min) / max;
474   else
475     s = 0.0;
476   
477   if (s == 0.0)
478     h = 0.0;
479   else
480     {
481       delta = max - min;
482       
483       if (red == max)
484         h = (green - blue) / delta;
485       else if (green == max)
486         h = 2 + (blue - red) / delta;
487       else if (blue == max)
488         h = 4 + (red - green) / delta;
489       
490       h /= 6.0;
491       
492       if (h < 0.0)
493         h += 1.0;
494       else if (h > 1.0)
495         h -= 1.0;
496     }
497   
498   *r = h;
499   *g = s;
500   *b = v;
501 }
502
503 /* Computes the vertices of the saturation/value triangle */
504 static void
505 compute_triangle (GtkHSV *hsv,
506                   gint   *hx,
507                   gint   *hy,
508                   gint   *sx,
509                   gint   *sy,
510                   gint   *vx,
511                   gint   *vy)
512 {
513   GtkHSVPriv *priv = hsv->priv;
514   gdouble center_x;
515   gdouble center_y;
516   gdouble inner, outer;
517   gdouble angle;
518
519   center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
520   center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
521   outer = priv->size / 2.0;
522   inner = outer - priv->ring_width;
523   angle = priv->h * 2.0 * G_PI;
524
525   *hx = floor (center_x + cos (angle) * inner + 0.5);
526   *hy = floor (center_y - sin (angle) * inner + 0.5);
527   *sx = floor (center_x + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
528   *sy = floor (center_y - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
529   *vx = floor (center_x + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
530   *vy = floor (center_y - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
531 }
532
533 /* Computes whether a point is inside the hue ring */
534 static gboolean
535 is_in_ring (GtkHSV *hsv,
536             gdouble x,
537             gdouble y)
538 {
539   GtkHSVPriv *priv = hsv->priv;
540   gdouble dx, dy, dist;
541   gdouble center_x;
542   gdouble center_y;
543   gdouble inner, outer;
544
545   center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
546   center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
547   outer = priv->size / 2.0;
548   inner = outer - priv->ring_width;
549
550   dx = x - center_x;
551   dy = center_y - y;
552   dist = dx * dx + dy * dy;
553
554   return (dist >= inner * inner && dist <= outer * outer);
555 }
556
557 /* Computes a saturation/value pair based on the mouse coordinates */
558 static void
559 compute_sv (GtkHSV  *hsv,
560             gdouble  x,
561             gdouble  y,
562             gdouble *s,
563             gdouble *v)
564 {
565   int ihx, ihy, isx, isy, ivx, ivy;
566   double hx, hy, sx, sy, vx, vy;
567   double center_x;
568   double center_y;
569
570   compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
571   center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
572   center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
573   hx = ihx - center_x;
574   hy = center_y - ihy;
575   sx = isx - center_x;
576   sy = center_y - isy;
577   vx = ivx - center_x;
578   vy = center_y - ivy;
579   x -= center_x;
580   y = center_y - y;
581   
582   if (vx * (x - sx) + vy * (y - sy) < 0.0)
583     {
584       *s = 1.0;
585       *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
586             / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
587       
588       if (*v < 0.0)
589         *v = 0.0;
590       else if (*v > 1.0)
591         *v = 1.0;
592     }
593   else if (hx * (x - sx) + hy * (y - sy) < 0.0)
594     {
595       *s = 0.0;
596       *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
597             / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
598       
599       if (*v < 0.0)
600         *v = 0.0;
601       else if (*v > 1.0)
602         *v = 1.0;
603     }
604   else if (sx * (x - hx) + sy * (y - hy) < 0.0)
605     {
606       *v = 1.0;
607       *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
608             ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
609       
610       if (*s < 0.0)
611         *s = 0.0;
612       else if (*s > 1.0)
613         *s = 1.0;
614     }
615   else
616     {
617       *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
618             / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
619       
620       if (*v<= 0.0)
621         {
622           *v = 0.0;
623           *s = 0.0;
624         }
625       else
626         {
627           if (*v > 1.0)
628             *v = 1.0;
629
630           if (fabs (hy - vy) < fabs (hx - vx))
631             *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
632           else
633             *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
634             
635           if (*s < 0.0)
636             *s = 0.0;
637           else if (*s > 1.0)
638             *s = 1.0;
639         }
640     }
641 }
642
643 /* Computes whether a point is inside the saturation/value triangle */
644 static gboolean
645 is_in_triangle (GtkHSV *hsv,
646                 gdouble x,
647                 gdouble y)
648 {
649   int hx, hy, sx, sy, vx, vy;
650   double det, s, v;
651   
652   compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
653   
654   det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
655   
656   s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
657   v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
658   
659   return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
660 }
661
662 /* Computes a value based on the mouse coordinates */
663 static double
664 compute_v (GtkHSV *hsv,
665            gdouble x,
666            gdouble y)
667 {
668   double center_x;
669   double center_y;
670   double dx, dy;
671   double angle;
672
673   center_x = GTK_WIDGET (hsv)->allocation.width / 2.0;
674   center_y = GTK_WIDGET (hsv)->allocation.height / 2.0;
675   dx = x - center_x;
676   dy = center_y - y;
677
678   angle = atan2 (dy, dx);
679   if (angle < 0.0)
680     angle += 2.0 * G_PI;
681
682   return angle / (2.0 * G_PI);
683 }
684
685 /* Event handlers */
686
687 static void
688 set_cross_grab (GtkHSV *hsv,
689                 guint32 time)
690 {
691   GtkHSVPriv *priv = hsv->priv;
692   GdkCursor *cursor;
693
694   cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
695                                        GDK_CROSSHAIR);
696   gdk_pointer_grab (priv->window, FALSE,
697                     (GDK_POINTER_MOTION_MASK
698                      | GDK_POINTER_MOTION_HINT_MASK
699                      | GDK_BUTTON_RELEASE_MASK),
700                     NULL,
701                     cursor,
702                     time);
703   gdk_cursor_unref (cursor);
704 }
705
706 static gboolean 
707 gtk_hsv_grab_broken (GtkWidget          *widget,
708                      GdkEventGrabBroken *event)
709 {
710   GtkHSV *hsv = GTK_HSV (widget);
711   GtkHSVPriv *priv = hsv->priv;
712
713   priv->mode = DRAG_NONE;
714   
715   return TRUE;
716 }
717
718 /* Button_press_event handler for the HSV color selector */
719 static gint
720 gtk_hsv_button_press (GtkWidget      *widget,
721                       GdkEventButton *event)
722 {
723   GtkHSV *hsv = GTK_HSV (widget);
724   GtkHSVPriv *priv = hsv->priv;
725   double x, y;
726
727   if (priv->mode != DRAG_NONE || event->button != 1)
728     return FALSE;
729   
730   x = event->x;
731   y = event->y;
732   
733   if (is_in_ring (hsv, x, y))
734     {
735       priv->mode = DRAG_H;
736       set_cross_grab (hsv, event->time);
737       
738       gtk_hsv_set_color (hsv,
739                          compute_v (hsv, x, y),
740                          priv->s,
741                          priv->v);
742
743       gtk_widget_grab_focus (widget);
744       priv->focus_on_ring = TRUE;
745       
746       return TRUE;
747     }
748   
749   if (is_in_triangle (hsv, x, y))
750     {
751       gdouble s, v;
752       
753       priv->mode = DRAG_SV;
754       set_cross_grab (hsv, event->time);
755       
756       compute_sv (hsv, x, y, &s, &v);
757       gtk_hsv_set_color (hsv, priv->h, s, v);
758
759       gtk_widget_grab_focus (widget);
760       priv->focus_on_ring = FALSE;
761       
762       return TRUE;
763     }
764   
765   return FALSE;
766 }
767
768 /* Button_release_event handler for the HSV color selector */
769 static gint
770 gtk_hsv_button_release (GtkWidget      *widget,
771                         GdkEventButton *event)
772 {
773   GtkHSV *hsv = GTK_HSV (widget);
774   GtkHSVPriv *priv = hsv->priv;
775   DragMode mode;
776   gdouble x, y;
777
778   if (priv->mode == DRAG_NONE || event->button != 1)
779     return FALSE;
780   
781   /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
782    * can see that this is the final color state.
783    */
784   
785   mode = priv->mode;
786   priv->mode = DRAG_NONE;
787   
788   x = event->x;
789   y = event->y;
790   
791   if (mode == DRAG_H)
792     gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
793   else if (mode == DRAG_SV) {
794     double s, v;
795     
796     compute_sv (hsv, x, y, &s, &v);
797     gtk_hsv_set_color (hsv, priv->h, s, v);
798   } else
799     g_assert_not_reached ();
800   
801   gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
802                               event->time);
803   return TRUE;
804 }
805
806 /* Motion_notify_event handler for the HSV color selector */
807 static gint
808 gtk_hsv_motion (GtkWidget      *widget,
809                 GdkEventMotion *event)
810 {
811   GtkHSV *hsv = GTK_HSV (widget);
812   GtkHSVPriv *priv = hsv->priv;
813   GdkModifierType mods;
814   double x, y;
815
816   if (priv->mode == DRAG_NONE)
817     return FALSE;
818   
819   gdk_event_request_motions (event);
820   x = event->x;
821   y = event->y;
822   mods = event->state;
823
824   if (priv->mode == DRAG_H)
825     {
826       gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
827       return TRUE;
828     }
829   else if (priv->mode == DRAG_SV)
830     {
831       double s, v;
832       
833       compute_sv (hsv, x, y, &s, &v);
834       gtk_hsv_set_color (hsv, priv->h, s, v);
835       return TRUE;
836     }
837   
838   g_assert_not_reached ();
839   return FALSE;
840 }
841
842
843 /* Redrawing */
844
845 /* Paints the hue ring */
846 static void
847 paint_ring (GtkHSV      *hsv,
848             cairo_t     *cr,
849             gint         x,
850             gint         y,
851             gint         width,
852             gint         height)
853 {
854   GtkHSVPriv *priv = hsv->priv;
855   GtkWidget *widget = GTK_WIDGET (hsv);
856   int xx, yy;
857   gdouble dx, dy, dist;
858   gdouble center_x;
859   gdouble center_y;
860   gdouble inner, outer;
861   guint32 *buf, *p;
862   gdouble angle;
863   gdouble hue;
864   gdouble r, g, b;
865   cairo_surface_t *source;
866   cairo_t *source_cr;
867   gint stride;
868   gint focus_width;
869   gint focus_pad;
870
871   gtk_widget_style_get (widget,
872                         "focus-line-width", &focus_width,
873                         "focus-padding", &focus_pad,
874                         NULL);
875   
876   priv = hsv->priv;
877
878   center_x = widget->allocation.width / 2.0;
879   center_y = widget->allocation.height / 2.0;
880
881   outer = priv->size / 2.0;
882   inner = outer - priv->ring_width;
883   
884   /* Create an image initialized with the ring colors */
885   
886   stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
887   buf = g_new (guint32, height * stride / 4);
888   
889   for (yy = 0; yy < height; yy++)
890     {
891       p = buf + yy * width;
892       
893       dy = -(yy + y - center_y);
894       
895       for (xx = 0; xx < width; xx++)
896         {
897           dx = xx + x - center_x;
898           
899           dist = dx * dx + dy * dy;
900           if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
901             {
902               *p++ = 0;
903               continue;
904             }
905           
906           angle = atan2 (dy, dx);
907           if (angle < 0.0)
908             angle += 2.0 * G_PI;
909           
910           hue = angle / (2.0 * G_PI);
911           
912           r = hue;
913           g = 1.0;
914           b = 1.0;
915           hsv_to_rgb (&r, &g, &b);
916           
917           *p++ = (((int)floor (r * 255 + 0.5) << 16) |
918                   ((int)floor (g * 255 + 0.5) << 8) |
919                   (int)floor (b * 255 + 0.5));
920         }
921     }
922
923   source = cairo_image_surface_create_for_data ((unsigned char *)buf,
924                                                 CAIRO_FORMAT_RGB24,
925                                                 width, height, stride);
926
927   /* Now draw the value marker onto the source image, so that it
928    * will get properly clipped at the edges of the ring
929    */
930   source_cr = cairo_create (source);
931   
932   r = priv->h;
933   g = 1.0;
934   b = 1.0;
935   hsv_to_rgb (&r, &g, &b);
936   
937   if (INTENSITY (r, g, b) > 0.5)
938     cairo_set_source_rgb (source_cr, 0., 0., 0.);
939   else
940     cairo_set_source_rgb (source_cr, 1., 1., 1.);
941
942   cairo_move_to (source_cr, -x + center_x, - y + center_y);
943   cairo_line_to (source_cr,
944                  -x + center_x + cos (priv->h * 2.0 * G_PI) * priv->size / 2,
945                  -y + center_y - sin (priv->h * 2.0 * G_PI) * priv->size / 2);
946   cairo_stroke (source_cr);
947   cairo_destroy (source_cr);
948
949   /* Draw the ring using the source image */
950
951   cairo_save (cr);
952     
953   cairo_set_source_surface (cr, source, x, y);
954   cairo_surface_destroy (source);
955
956   cairo_set_line_width (cr, priv->ring_width);
957   cairo_new_path (cr);
958   cairo_arc (cr,
959              center_x, center_y,
960              priv->size / 2. - priv->ring_width / 2.,
961              0, 2 * G_PI);
962   cairo_stroke (cr);
963   
964   cairo_restore (cr);
965   
966   g_free (buf);
967 }
968
969 /* Converts an HSV triplet to an integer RGB triplet */
970 static void
971 get_color (gdouble h,
972            gdouble s,
973            gdouble v,
974            gint   *r,
975            gint   *g,
976            gint   *b)
977 {
978   hsv_to_rgb (&h, &s, &v);
979   
980   *r = floor (h * 255 + 0.5);
981   *g = floor (s * 255 + 0.5);
982   *b = floor (v * 255 + 0.5);
983 }
984
985 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
986
987 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0)                                       \
988                                ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1)))     \
989                                : (a))
990
991 /* Number of pixels we extend out from the edges when creating
992  * color source to avoid artifacts
993  */
994 #define PAD 3
995
996 /* Paints the HSV triangle */
997 static void
998 paint_triangle (GtkHSV      *hsv,
999                 cairo_t     *cr,
1000                 gint         x,
1001                 gint         y,
1002                 gint         width,
1003                 gint         height)
1004 {
1005   GtkHSVPriv *priv = hsv->priv;
1006   GtkWidget *widget = GTK_WIDGET (hsv);
1007   gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1008   gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1009   gint x2, y2, r2, g2, b2; /* Second vertex */
1010   gint x3, y3, r3, g3, b3; /* Third vertex */
1011   gint t;
1012   guint32 *buf, *p, c;
1013   gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1014   gint xx, yy;
1015   gint x_interp, y_interp;
1016   gint x_start, x_end;
1017   cairo_surface_t *source;
1018   gdouble r, g, b;
1019   gchar *detail;
1020   gint stride;
1021   
1022   priv = hsv->priv;
1023   
1024   /* Compute triangle's vertices */
1025   
1026   compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1027   
1028   x1 = hx;
1029   y1 = hy;
1030   get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1031   
1032   x2 = sx;
1033   y2 = sy;
1034   get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1035   
1036   x3 = vx;
1037   y3 = vy;
1038   get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1039   
1040   if (y2 > y3)
1041     {
1042       SWAP (x2, x3, t);
1043       SWAP (y2, y3, t);
1044       SWAP (r2, r3, t);
1045       SWAP (g2, g3, t);
1046       SWAP (b2, b3, t);
1047     }
1048   
1049   if (y1 > y3)
1050     {
1051       SWAP (x1, x3, t);
1052       SWAP (y1, y3, t);
1053       SWAP (r1, r3, t);
1054       SWAP (g1, g3, t);
1055       SWAP (b1, b3, t);
1056     }
1057   
1058   if (y1 > y2)
1059     {
1060       SWAP (x1, x2, t);
1061       SWAP (y1, y2, t);
1062       SWAP (r1, r2, t);
1063       SWAP (g1, g2, t);
1064       SWAP (b1, b2, t);
1065     }
1066   
1067   /* Shade the triangle */
1068
1069   stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
1070   buf = g_new (guint32, height * stride / 4);
1071   
1072   for (yy = 0; yy < height; yy++)
1073     {
1074       p = buf + yy * width;
1075       
1076       if (yy + y >= y1 - PAD && yy + y < y3 + PAD) {
1077         y_interp = CLAMP (yy + y, y1, y3);
1078         
1079         if (y_interp < y2)
1080           {
1081             xl = LERP (x1, x2, y1, y2, y_interp);
1082             
1083             rl = LERP (r1, r2, y1, y2, y_interp);
1084             gl = LERP (g1, g2, y1, y2, y_interp);
1085             bl = LERP (b1, b2, y1, y2, y_interp);
1086           }
1087         else
1088           {
1089             xl = LERP (x2, x3, y2, y3, y_interp);
1090             
1091             rl = LERP (r2, r3, y2, y3, y_interp);
1092             gl = LERP (g2, g3, y2, y3, y_interp);
1093             bl = LERP (b2, b3, y2, y3, y_interp);
1094           }
1095         
1096         xr = LERP (x1, x3, y1, y3, y_interp);
1097         
1098         rr = LERP (r1, r3, y1, y3, y_interp);
1099         gr = LERP (g1, g3, y1, y3, y_interp);
1100         br = LERP (b1, b3, y1, y3, y_interp);
1101         
1102         if (xl > xr)
1103           {
1104             SWAP (xl, xr, t);
1105             SWAP (rl, rr, t);
1106             SWAP (gl, gr, t);
1107             SWAP (bl, br, t);
1108           }
1109
1110         x_start = MAX (xl - PAD, x);
1111         x_end = MIN (xr + PAD, x + width);
1112         x_start = MIN (x_start, x_end);
1113
1114         c = (rl << 16) | (gl << 8) | bl;
1115
1116         for (xx = x; xx < x_start; xx++)
1117           *p++ = c;
1118           
1119         for (; xx < x_end; xx++)
1120           {
1121             x_interp = CLAMP (xx, xl, xr);
1122                 
1123             *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
1124                     (LERP (gl, gr, xl, xr, x_interp) << 8) |
1125                     LERP (bl, br, xl, xr, x_interp));
1126           }
1127
1128         c = (rr << 16) | (gr << 8) | br;
1129
1130         for (; xx < x + width; xx++)
1131           *p++ = c;
1132       }
1133     }
1134
1135   source = cairo_image_surface_create_for_data ((unsigned char *)buf,
1136                                                 CAIRO_FORMAT_RGB24,
1137                                                 width, height, stride);
1138   
1139   /* Draw a triangle with the image as a source */
1140
1141   cairo_set_source_surface (cr, source, x, y);
1142   cairo_surface_destroy (source);
1143   
1144   cairo_move_to (cr, x1, y1);
1145   cairo_line_to (cr, x2, y2);
1146   cairo_line_to (cr, x3, y3);
1147   cairo_close_path (cr);
1148   cairo_fill (cr);
1149   
1150   g_free (buf);
1151   
1152   /* Draw value marker */
1153   
1154   xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1155   yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1156   
1157   r = priv->h;
1158   g = priv->s;
1159   b = priv->v;
1160   hsv_to_rgb (&r, &g, &b);
1161
1162   if (INTENSITY (r, g, b) > 0.5)
1163     {
1164       detail = "colorwheel_light";
1165       cairo_set_source_rgb (cr, 0., 0., 0.);
1166     }
1167   else
1168     {
1169       detail = "colorwheel_dark";
1170       cairo_set_source_rgb (cr, 1., 1., 1.);
1171     }
1172
1173 #define RADIUS 4
1174 #define FOCUS_RADIUS 6
1175
1176   cairo_new_path (cr);
1177   cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
1178   cairo_stroke (cr);
1179   
1180   /* Draw focus outline */
1181
1182   if (gtk_widget_has_focus (widget) &&
1183       !priv->focus_on_ring)
1184     {
1185       gint focus_width;
1186       gint focus_pad;
1187
1188       gtk_widget_style_get (widget,
1189                             "focus-line-width", &focus_width,
1190                             "focus-padding", &focus_pad,
1191                             NULL);
1192   
1193       gtk_paint_focus (widget->style, widget->window,
1194                        gtk_widget_get_state (widget),
1195                        NULL, widget, detail,
1196                        widget->allocation.x + xx - FOCUS_RADIUS - focus_width - focus_pad, 
1197                        widget->allocation.y + yy - FOCUS_RADIUS - focus_width - focus_pad, 
1198                        2 * (FOCUS_RADIUS + focus_width + focus_pad), 
1199                        2 * (FOCUS_RADIUS + focus_width + focus_pad));
1200     }
1201   
1202 }
1203
1204 /* Paints the contents of the HSV color selector */
1205 static void
1206 paint (GtkHSV      *hsv,
1207        cairo_t     *cr,
1208        gint         x,
1209        gint         y,
1210        gint         width,
1211        gint         height)
1212 {
1213   paint_ring (hsv, cr, x, y, width, height);
1214   paint_triangle (hsv, cr, x, y, width, height);
1215 }
1216
1217 /* Expose_event handler for the HSV color selector */
1218 static gint
1219 gtk_hsv_expose (GtkWidget      *widget,
1220                 GdkEventExpose *event)
1221 {
1222   GtkHSV *hsv = GTK_HSV (widget);
1223   GtkHSVPriv *priv = hsv->priv;
1224   GdkRectangle rect, dest;
1225   cairo_t *cr;
1226
1227   if (!(event->window == widget->window && gtk_widget_is_drawable (widget)))
1228     return FALSE;
1229
1230   rect.x = widget->allocation.x;
1231   rect.y = widget->allocation.y;
1232   rect.width = widget->allocation.width;
1233   rect.height = widget->allocation.height;
1234   
1235   if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1236     return FALSE;
1237   
1238   cr = gdk_cairo_create (widget->window);
1239
1240   cairo_translate (cr, widget->allocation.x, widget->allocation.y);
1241   paint (hsv, cr,
1242          dest.x - widget->allocation.x,
1243          dest.y - widget->allocation.y,
1244          dest.width, dest.height);
1245   cairo_destroy (cr);
1246
1247   if (gtk_widget_has_focus (widget) && priv->focus_on_ring)
1248     gtk_paint_focus (widget->style, widget->window,
1249                      gtk_widget_get_state (widget),
1250                      &event->area, widget, NULL,
1251                      widget->allocation.x,
1252                      widget->allocation.y, 
1253                      widget->allocation.width, 
1254                      widget->allocation.height);
1255
1256   return FALSE;
1257 }
1258
1259 static gboolean
1260 gtk_hsv_focus (GtkWidget       *widget,
1261                GtkDirectionType dir)
1262 {
1263   GtkHSV *hsv = GTK_HSV (widget);
1264   GtkHSVPriv *priv = hsv->priv;
1265
1266   if (!gtk_widget_has_focus (widget))
1267     {
1268       if (dir == GTK_DIR_TAB_BACKWARD)
1269         priv->focus_on_ring = FALSE;
1270       else
1271         priv->focus_on_ring = TRUE;
1272
1273       gtk_widget_grab_focus (GTK_WIDGET (hsv));
1274       return TRUE;
1275     }
1276   
1277   switch (dir)
1278     {
1279     case GTK_DIR_UP:
1280       if (priv->focus_on_ring)
1281         return FALSE;
1282       else
1283         priv->focus_on_ring = TRUE;
1284       break;
1285
1286     case GTK_DIR_DOWN:
1287       if (priv->focus_on_ring)
1288         priv->focus_on_ring = FALSE;
1289       else
1290         return FALSE;
1291       break;
1292
1293     case GTK_DIR_LEFT:
1294     case GTK_DIR_TAB_BACKWARD:
1295       if (priv->focus_on_ring)
1296         return FALSE;
1297       else
1298         priv->focus_on_ring = TRUE;
1299       break;
1300
1301     case GTK_DIR_RIGHT:
1302     case GTK_DIR_TAB_FORWARD:
1303       if (priv->focus_on_ring)
1304         priv->focus_on_ring = FALSE;
1305       else
1306         return FALSE;
1307       break;
1308     }
1309
1310   gtk_widget_queue_draw (GTK_WIDGET (hsv));
1311
1312   return TRUE;
1313 }
1314
1315 /**
1316  * gtk_hsv_new:
1317  *
1318  * Creates a new HSV color selector.
1319  *
1320  * Return value: A newly-created HSV color selector.
1321  *
1322  * Since: 2.14
1323  */
1324 GtkWidget*
1325 gtk_hsv_new (void)
1326 {
1327   return g_object_new (GTK_TYPE_HSV, NULL);
1328 }
1329
1330 /**
1331  * gtk_hsv_set_color:
1332  * @hsv: An HSV color selector
1333  * @h: Hue
1334  * @s: Saturation
1335  * @v: Value
1336  *
1337  * Sets the current color in an HSV color selector.
1338  * Color component values must be in the [0.0, 1.0] range.
1339  *
1340  * Since: 2.14
1341  */
1342 void
1343 gtk_hsv_set_color (GtkHSV *hsv,
1344                    gdouble h,
1345                    gdouble s,
1346                    gdouble v)
1347 {
1348   GtkHSVPriv *priv;
1349
1350   g_return_if_fail (GTK_IS_HSV (hsv));
1351   g_return_if_fail (h >= 0.0 && h <= 1.0);
1352   g_return_if_fail (s >= 0.0 && s <= 1.0);
1353   g_return_if_fail (v >= 0.0 && v <= 1.0);
1354
1355   priv = hsv->priv;
1356
1357   priv->h = h;
1358   priv->s = s;
1359   priv->v = v;
1360   
1361   g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1362   
1363   gtk_widget_queue_draw (GTK_WIDGET (hsv));
1364 }
1365
1366 /**
1367  * gtk_hsv_get_color:
1368  * @hsv: An HSV color selector
1369  * @h: Return value for the hue
1370  * @s: Return value for the saturation
1371  * @v: Return value for the value
1372  *
1373  * Queries the current color in an HSV color selector.
1374  * Returned values will be in the [0.0, 1.0] range.
1375  *
1376  * Since: 2.14
1377  */
1378 void
1379 gtk_hsv_get_color (GtkHSV *hsv,
1380                    double *h,
1381                    double *s,
1382                    double *v)
1383 {
1384   GtkHSVPriv *priv = hsv->priv;
1385
1386   g_return_if_fail (GTK_IS_HSV (hsv));
1387
1388   priv = hsv->priv;
1389   
1390   if (h)
1391     *h = priv->h;
1392   
1393   if (s)
1394     *s = priv->s;
1395   
1396   if (v)
1397     *v = priv->v;
1398 }
1399
1400 /**
1401  * gtk_hsv_set_metrics:
1402  * @hsv: An HSV color selector
1403  * @size: Diameter for the hue ring
1404  * @ring_width: Width of the hue ring
1405  *
1406  * Sets the size and ring width of an HSV color selector.
1407  *
1408  * Since: 2.14
1409  */
1410 void
1411 gtk_hsv_set_metrics (GtkHSV *hsv,
1412                      gint    size,
1413                      gint    ring_width)
1414 {
1415   GtkHSVPriv *priv = hsv->priv;
1416   int same_size;
1417
1418   g_return_if_fail (GTK_IS_HSV (hsv));
1419   g_return_if_fail (size > 0);
1420   g_return_if_fail (ring_width > 0);
1421   g_return_if_fail (2 * ring_width + 1 <= size);
1422
1423   priv = hsv->priv;
1424
1425   same_size = (priv->size == size);
1426   
1427   priv->size = size;
1428   priv->ring_width = ring_width;
1429   
1430   if (same_size)
1431     gtk_widget_queue_draw (GTK_WIDGET (hsv));
1432   else
1433     gtk_widget_queue_resize (GTK_WIDGET (hsv));
1434 }
1435
1436 /**
1437  * gtk_hsv_get_metrics:
1438  * @hsv: An HSV color selector
1439  * @size: Return value for the diameter of the hue ring
1440  * @ring_width: Return value for the width of the hue ring
1441  *
1442  * Queries the size and ring width of an HSV color selector.
1443  *
1444  * Since: 2.14
1445  */
1446 void
1447 gtk_hsv_get_metrics (GtkHSV *hsv,
1448                      gint   *size,
1449                      gint   *ring_width)
1450 {
1451   GtkHSVPriv *priv = hsv->priv;
1452
1453   g_return_if_fail (GTK_IS_HSV (hsv));
1454
1455   priv = hsv->priv;
1456
1457   if (size)
1458     *size = priv->size;
1459   
1460   if (ring_width)
1461     *ring_width = priv->ring_width;
1462 }
1463
1464 /**
1465  * gtk_hsv_is_adjusting:
1466  * @hsv: A #GtkHSV 
1467  *
1468  * An HSV color selector can be said to be adjusting if multiple rapid
1469  * changes are being made to its value, for example, when the user is 
1470  * adjusting the value with the mouse. This function queries whether 
1471  * the HSV color selector is being adjusted or not.
1472  *
1473  * Return value: %TRUE if clients can ignore changes to the color value,
1474  *     since they may be transitory, or %FALSE if they should consider
1475  *     the color value status to be final.
1476  *
1477  * Since: 2.14
1478  */
1479 gboolean
1480 gtk_hsv_is_adjusting (GtkHSV *hsv)
1481 {
1482   GtkHSVPriv *priv = hsv->priv;
1483
1484   g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1485
1486   priv = hsv->priv;
1487
1488   return priv->mode != DRAG_NONE;
1489 }
1490
1491 /**
1492  * gtk_hsv_to_rgb:
1493  * @h: Hue
1494  * @s: Saturation
1495  * @v: Value
1496  * @r: Return value for the red component
1497  * @g: Return value for the green component
1498  * @b: Return value for the blue component
1499  *
1500  * Converts a color from HSV space to RGB.
1501  * Input values must be in the [0.0, 1.0] range; 
1502  * output values will be in the same range.
1503  *
1504  * Since: 2.14
1505  */
1506 void
1507 gtk_hsv_to_rgb (gdouble  h,
1508                 gdouble  s,
1509                 gdouble  v,
1510                 gdouble *r,
1511                 gdouble *g,
1512                 gdouble *b)
1513 {
1514   g_return_if_fail (h >= 0.0 && h <= 1.0);
1515   g_return_if_fail (s >= 0.0 && s <= 1.0);
1516   g_return_if_fail (v >= 0.0 && v <= 1.0);
1517   
1518   hsv_to_rgb (&h, &s, &v);
1519   
1520   if (r)
1521     *r = h;
1522   
1523   if (g)
1524     *g = s;
1525   
1526   if (b)
1527     *b = v;
1528 }
1529
1530 /**
1531  * gtk_rgb_to_hsv:
1532  * @r: Red
1533  * @g: Green
1534  * @b: Blue
1535  * @h: Return value for the hue component
1536  * @s: Return value for the saturation component
1537  * @v: Return value for the value component
1538  *
1539  * Converts a color from RGB space to HSV.
1540  * Input values must be in the [0.0, 1.0] range;
1541  * output values will be in the same range.
1542  *
1543  * Since: 2.14
1544  */
1545 void
1546 gtk_rgb_to_hsv (gdouble  r,
1547                 gdouble  g,
1548                 gdouble  b,
1549                 gdouble *h,
1550                 gdouble *s,
1551                 gdouble *v)
1552 {
1553   g_return_if_fail (r >= 0.0 && r <= 1.0);
1554   g_return_if_fail (g >= 0.0 && g <= 1.0);
1555   g_return_if_fail (b >= 0.0 && b <= 1.0);
1556   
1557   rgb_to_hsv (&r, &g, &b);
1558   
1559   if (h)
1560     *h = r;
1561   
1562   if (s)
1563     *s = g;
1564   
1565   if (v)
1566     *v = b;
1567 }
1568
1569 static void
1570 gtk_hsv_move (GtkHSV          *hsv,
1571               GtkDirectionType dir)
1572 {
1573   GtkHSVPriv *priv = hsv->priv;
1574   gdouble hue, sat, val;
1575   gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1576   gint x, y; /* position in triangle */
1577
1578   hue = priv->h;
1579   sat = priv->s;
1580   val = priv->v;
1581
1582   compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1583
1584   x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1585   y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1586
1587 #define HUE_DELTA 0.002
1588   switch (dir)
1589     {
1590     case GTK_DIR_UP:
1591       if (priv->focus_on_ring)
1592         hue += HUE_DELTA;
1593       else
1594         {
1595           y -= 1;
1596           compute_sv (hsv, x, y, &sat, &val);
1597         }
1598       break;
1599
1600     case GTK_DIR_DOWN:
1601       if (priv->focus_on_ring)
1602         hue -= HUE_DELTA;
1603       else
1604         {
1605           y += 1;
1606           compute_sv (hsv, x, y, &sat, &val);
1607         }
1608       break;
1609
1610     case GTK_DIR_LEFT:
1611       if (priv->focus_on_ring)
1612         hue += HUE_DELTA;
1613       else
1614         {
1615           x -= 1;
1616           compute_sv (hsv, x, y, &sat, &val);
1617         }
1618       break;
1619
1620     case GTK_DIR_RIGHT:
1621       if (priv->focus_on_ring)
1622         hue -= HUE_DELTA
1623           ;
1624       else
1625         {
1626           x += 1;
1627           compute_sv (hsv, x, y, &sat, &val);
1628         }
1629       break;
1630
1631     default:
1632       /* we don't care about the tab directions */
1633       break;
1634     }
1635
1636   /* Wrap */
1637   if (hue < 0.0)
1638     hue = 1.0;
1639   else if (hue > 1.0)
1640     hue = 0.0;
1641   
1642   gtk_hsv_set_color (hsv, hue, sat, val);
1643 }