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