gtkhsv.c 38.1 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* HSV color selector for GTK+
 *
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Authors: Simon Budig <Simon.Budig@unix-ag.org> (original code)
 *          Federico Mena-Quintero <federico@gimp.org> (cleanup for GTK+)
 *          Jonathan Blandford <jrb@redhat.com> (cleanup for GTK+)
 *
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11 12 13 14 15 16
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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 21 22 23 24 25
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <math.h>
26
#include <string.h>
27
#include "gtkhsv.h"
28 29 30
#include "gdk/gdkkeysyms.h"
#include "gtkbindings.h"
#include "gtkcontainer.h"
31
#include "gtkmarshalers.h"
32

33 34 35 36 37 38
/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */
39 40 41 42 43 44 45 46 47 48

/* Default width/height */
#define DEFAULT_SIZE 100

/* Default ring width */
#define DEFAULT_RING_WIDTH 10


/* Dragging modes */
typedef enum {
49 50 51
  DRAG_NONE,
  DRAG_H,
  DRAG_SV
52 53 54 55
} DragMode;

/* Private part of the GtkHSV structure */
typedef struct {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  /* Color value */
  double h;
  double s;
  double v;
  
  /* Size and ring width */
  int size;
  int ring_width;
  
  /* Window for capturing events */
  GdkWindow *window;
  
  /* GC for drawing */
  GdkGC *gc;
  
  /* Dragging mode */
  DragMode mode;
73 74 75

  guint focus_on_ring : 1;
  
76 77 78 79 80 81 82
} HSVPrivate;



/* Signal IDs */

enum {
83
  CHANGED,
84
  MOVE,
85
  LAST_SIGNAL
86 87
};

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
static void     gtk_hsv_class_init     (GtkHSVClass      *class);
static void     gtk_hsv_init           (GtkHSV           *hsv);
static void     gtk_hsv_destroy        (GtkObject        *object);
static void     gtk_hsv_map            (GtkWidget        *widget);
static void     gtk_hsv_unmap          (GtkWidget        *widget);
static void     gtk_hsv_realize        (GtkWidget        *widget);
static void     gtk_hsv_unrealize      (GtkWidget        *widget);
static void     gtk_hsv_size_request   (GtkWidget        *widget,
					GtkRequisition   *requisition);
static void     gtk_hsv_size_allocate  (GtkWidget        *widget,
					GtkAllocation    *allocation);
static gint     gtk_hsv_button_press   (GtkWidget        *widget,
					GdkEventButton   *event);
static gint     gtk_hsv_button_release (GtkWidget        *widget,
					GdkEventButton   *event);
static gint     gtk_hsv_motion         (GtkWidget        *widget,
					GdkEventMotion   *event);
static gint     gtk_hsv_expose         (GtkWidget        *widget,
					GdkEventExpose   *event);
static gboolean gtk_hsv_focus          (GtkWidget        *widget,
					GtkDirectionType  direction);
static void     gtk_hsv_move           (GtkHSV           *hsv,
					GtkDirectionType  dir);
static GdkGC *  gtk_hsv_get_focus_gc   (GtkHSV           *hsv,
					gint             *line_width);
113 114 115 116 117 118 119 120 121 122 123 124 125 126

static guint hsv_signals[LAST_SIGNAL];
static GtkWidgetClass *parent_class;


/**
 * gtk_hsv_get_type:
 * @void:
 *
 * Registers the &GtkHSV class if necessary, and returns the type ID associated
 * to it.
 *
 * Return value: The type ID of the &GtkHSV class.
 **/
Manish Singh's avatar
Manish Singh committed
127
GType
128 129
gtk_hsv_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
130
  static GType hsv_type = 0;
131 132
  
  if (!hsv_type) {
Manish Singh's avatar
Manish Singh committed
133
    static const GTypeInfo hsv_info = {
134
      sizeof (GtkHSVClass),
Manish Singh's avatar
Manish Singh committed
135 136 137 138 139 140 141 142
      NULL,		/* base_init */
      NULL,		/* base_finalize */
      (GClassInitFunc) gtk_hsv_class_init,
      NULL,		/* class_finalize */
      NULL,		/* class_data */
      sizeof (GtkHSV),
      0,		/* n_preallocs */
      (GInstanceInitFunc) gtk_hsv_init,
143 144
    };
    
Manish Singh's avatar
Manish Singh committed
145 146
    hsv_type = g_type_register_static (GTK_TYPE_WIDGET, "GtkHSV",
				       &hsv_info, 0);
147 148 149
  }
  
  return hsv_type;
150 151 152 153 154 155
}

/* Class initialization function for the HSV color selector */
static void
gtk_hsv_class_init (GtkHSVClass *class)
{
156 157
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
158
  GtkHSVClass    *hsv_class;
Manish Singh's avatar
Manish Singh committed
159
  GtkBindingSet  *binding_set;
160 161 162
  
  object_class = (GtkObjectClass *) class;
  widget_class = (GtkWidgetClass *) class;
163
  hsv_class = GTK_HSV_CLASS (class);
164
  
Manish Singh's avatar
Manish Singh committed
165
  parent_class = g_type_class_peek_parent (class);
166 167 168
  
  object_class->destroy = gtk_hsv_destroy;
  
169 170
  widget_class->map = gtk_hsv_map;
  widget_class->unmap = gtk_hsv_unmap;                                      
171 172 173 174 175 176 177 178
  widget_class->realize = gtk_hsv_realize;
  widget_class->unrealize = gtk_hsv_unrealize;
  widget_class->size_request = gtk_hsv_size_request;
  widget_class->size_allocate = gtk_hsv_size_allocate;
  widget_class->button_press_event = gtk_hsv_button_press;
  widget_class->button_release_event = gtk_hsv_button_release;
  widget_class->motion_notify_event = gtk_hsv_motion;
  widget_class->expose_event = gtk_hsv_expose;
179
  widget_class->focus = gtk_hsv_focus;
180 181 182
  
  hsv_class->move = gtk_hsv_move;
  
183
  hsv_signals[CHANGED] =
Manish Singh's avatar
Manish Singh committed
184 185 186 187 188 189 190
    g_signal_new ("changed",
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkHSVClass, changed),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
191 192

  hsv_signals[MOVE] =
Manish Singh's avatar
Manish Singh committed
193 194 195 196 197 198 199 200
    g_signal_new ("move",
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkHSVClass, move),
		  NULL, NULL,
		  _gtk_marshal_VOID__ENUM,
		  G_TYPE_NONE, 1,
		  GTK_TYPE_DIRECTION_TYPE);
201 202 203 204 205

  binding_set = gtk_binding_set_by_class (class);

  gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
206
                                G_TYPE_ENUM, GTK_DIR_UP);
207 208
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
209
                                G_TYPE_ENUM, GTK_DIR_UP);
210 211 212
  
  gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
213
                                G_TYPE_ENUM, GTK_DIR_DOWN);
214 215
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
216
                                G_TYPE_ENUM, GTK_DIR_DOWN);
217 218 219 220

  
  gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
221
                                G_TYPE_ENUM, GTK_DIR_RIGHT);
222 223
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
224
                                G_TYPE_ENUM, GTK_DIR_RIGHT);
225 226 227
  
  gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
228
                                G_TYPE_ENUM, GTK_DIR_LEFT);
229 230
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
231
                                G_TYPE_ENUM, GTK_DIR_LEFT);
232 233 234 235 236 237
}

/* Object initialization function for the HSV color selector */
static void
gtk_hsv_init (GtkHSV *hsv)
{
238 239 240 241 242 243
  HSVPrivate *priv;
  
  priv = g_new0 (HSVPrivate, 1);
  hsv->priv = priv;
  
  GTK_WIDGET_SET_FLAGS (hsv, GTK_NO_WINDOW);
244
  GTK_WIDGET_SET_FLAGS (hsv, GTK_CAN_FOCUS);
245 246 247 248 249 250 251
  
  priv->h = 0.0;
  priv->s = 0.0;
  priv->v = 0.0;
  
  priv->size = DEFAULT_SIZE;
  priv->ring_width = DEFAULT_RING_WIDTH;
252 253 254 255 256 257
}

/* Destroy handler for the HSV color selector */
static void
gtk_hsv_destroy (GtkObject *object)
{
258 259 260 261 262 263 264 265 266 267 268 269 270
  GtkHSV *hsv;
  
  g_return_if_fail (GTK_IS_HSV (object));
  
  hsv = GTK_HSV (object);

  if (hsv->priv)
    {
      g_free (hsv->priv);
      hsv->priv = NULL;
    }

  GTK_OBJECT_CLASS (parent_class)->destroy (object);
271 272 273 274
}

/* Default signal handlers */

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    
/* Map handler for the HSV color selector */

static void
gtk_hsv_map (GtkWidget *widget)
{
  GtkHSV *hsv;
  HSVPrivate *priv;

  hsv = GTK_HSV (widget);
  priv = hsv->priv;

  GTK_WIDGET_CLASS (parent_class)->map (widget);

  gdk_window_show (priv->window);
}

/* Unmap handler for the HSV color selector */

static void
gtk_hsv_unmap (GtkWidget *widget)
{
  GtkHSV *hsv;
  HSVPrivate *priv;

  hsv = GTK_HSV (widget);
  priv = hsv->priv;

  gdk_window_hide (priv->window);

  GTK_WIDGET_CLASS (parent_class)->unmap (widget);
}                                                                           
                                      
308 309 310 311
/* Realize handler for the HSV color selector */
static void
gtk_hsv_realize (GtkWidget *widget)
{
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  GtkHSV *hsv;
  HSVPrivate *priv;
  GdkWindowAttr attr;
  int attr_mask;
  GdkWindow *parent_window;
  
  hsv = GTK_HSV (widget);
  priv = hsv->priv;
  
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
  
  /* Create window */
  
  attr.window_type = GDK_WINDOW_CHILD;
  attr.x = widget->allocation.x;
  attr.y = widget->allocation.y;
  attr.width = widget->allocation.width;
  attr.height = widget->allocation.height;
  attr.wclass = GDK_INPUT_ONLY;
  attr.event_mask = gtk_widget_get_events (widget);
332 333
  attr.event_mask |= (GDK_KEY_PRESS_MASK
                      | GDK_BUTTON_PRESS_MASK
334
		      | GDK_BUTTON_RELEASE_MASK
335 336 337
		      | GDK_POINTER_MOTION_MASK
                      | GDK_ENTER_NOTIFY_MASK
                      | GDK_LEAVE_NOTIFY_MASK);
338 339 340 341 342 343
  
  attr_mask = GDK_WA_X | GDK_WA_Y;
  
  parent_window = gtk_widget_get_parent_window (widget);
  
  widget->window = parent_window;
Manish Singh's avatar
Manish Singh committed
344
  g_object_ref (widget->window);
345 346 347 348 349 350 351 352 353
  
  priv->window = gdk_window_new (parent_window, &attr, attr_mask);
  gdk_window_set_user_data (priv->window, hsv);
  
  widget->style = gtk_style_attach (widget->style, widget->window);
  
  /* Create GC */
  
  priv->gc = gdk_gc_new (parent_window);
354 355 356 357 358 359
}

/* Unrealize handler for the HSV color selector */
static void
gtk_hsv_unrealize (GtkWidget *widget)
{
360 361 362 363 364 365 366 367 368 369
  GtkHSV *hsv;
  HSVPrivate *priv;
  
  hsv = GTK_HSV (widget);
  priv = hsv->priv;
  
  gdk_window_set_user_data (priv->window, NULL);
  gdk_window_destroy (priv->window);
  priv->window = NULL;
  
Manish Singh's avatar
Manish Singh committed
370
  g_object_unref (priv->gc);
371 372 373 374
  priv->gc = NULL;
  
  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
375 376 377 378
}

/* Size_request handler for the HSV color selector */
static void
379 380
gtk_hsv_size_request (GtkWidget      *widget,
		      GtkRequisition *requisition)
381
{
382 383 384 385 386 387 388 389
  GtkHSV *hsv;
  HSVPrivate *priv;
  
  hsv = GTK_HSV (widget);
  priv = hsv->priv;
  
  requisition->width = priv->size;
  requisition->height = priv->size;
390 391 392 393
}

/* Size_allocate handler for the HSV color selector */
static void
394 395
gtk_hsv_size_allocate (GtkWidget     *widget,
		       GtkAllocation *allocation)
396
{
397 398 399 400 401 402 403 404 405 406 407 408 409 410
  GtkHSV *hsv;
  HSVPrivate *priv;
  
  hsv = GTK_HSV (widget);
  priv = hsv->priv;
  
  widget->allocation = *allocation;
  
  if (GTK_WIDGET_REALIZED (widget))
    gdk_window_move_resize (priv->window,
			    allocation->x,
			    allocation->y,
			    allocation->width,
			    allocation->height);
411 412 413 414 415 416 417 418 419
}


/* Utility functions */

#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)

/* Converts from HSV to RGB */
static void
420 421 422
hsv_to_rgb (gdouble *h,
	    gdouble *s,
	    gdouble *v)
423
{
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
  gdouble hue, saturation, value;
  gdouble f, p, q, t;
  
  if (*s == 0.0)
    {
      *h = *v;
      *s = *v;
      *v = *v; /* heh */
    }
  else
    {
      hue = *h * 6.0;
      saturation = *s;
      value = *v;
      
      if (hue == 6.0)
	hue = 0.0;
      
      f = hue - (int) hue;
      p = value * (1.0 - saturation);
      q = value * (1.0 - saturation * f);
      t = value * (1.0 - saturation * (1.0 - f));
      
      switch ((int) hue)
	{
	case 0:
	  *h = value;
	  *s = t;
	  *v = p;
	  break;
	  
	case 1:
	  *h = q;
	  *s = value;
	  *v = p;
	  break;
	  
	case 2:
	  *h = p;
	  *s = value;
	  *v = t;
	  break;
	  
	case 3:
	  *h = p;
	  *s = q;
	  *v = value;
	  break;
	  
	case 4:
	  *h = t;
	  *s = p;
	  *v = value;
	  break;
	  
	case 5:
	  *h = value;
	  *s = p;
	  *v = q;
	  break;
	  
	default:
	  g_assert_not_reached ();
487
	}
488
    }
489 490 491 492
}

/* Converts from RGB to HSV */
static void
493 494 495
rgb_to_hsv (gdouble *r,
	    gdouble *g,
	    gdouble *b)
496
{
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
  gdouble red, green, blue;
  gdouble h, s, v;
  gdouble min, max;
  gdouble delta;
  
  red = *r;
  green = *g;
  blue = *b;
  
  h = 0.0;
  
  if (red > green)
    {
      if (red > blue)
	max = red;
      else
	max = blue;
      
      if (green < blue)
	min = green;
      else
	min = blue;
    }
  else
    {
      if (green > blue)
	max = green;
      else
	max = blue;
      
      if (red < blue)
	min = red;
      else
	min = blue;
    }
  
  v = max;
  
  if (max != 0.0)
    s = (max - min) / max;
  else
    s = 0.0;
  
  if (s == 0.0)
    h = 0.0;
  else
    {
      delta = max - min;
      
      if (red == max)
	h = (green - blue) / delta;
      else if (green == max)
	h = 2 + (blue - red) / delta;
      else if (blue == max)
	h = 4 + (red - green) / delta;
      
      h /= 6.0;
      
      if (h < 0.0)
	h += 1.0;
      else if (h > 1.0)
	h -= 1.0;
    }
  
  *r = h;
  *g = s;
  *b = v;
564 565 566 567
}

/* Computes the vertices of the saturation/value triangle */
static void
568 569 570 571 572 573 574
compute_triangle (GtkHSV *hsv,
		  gint   *hx,
		  gint   *hy,
		  gint   *sx,
		  gint   *sy,
		  gint   *vx,
		  gint   *vy)
575
{
576 577 578 579 580 581 582 583 584 585
  HSVPrivate *priv;
  gdouble center;
  gdouble inner, outer;
  gdouble angle;
  
  priv = hsv->priv;
  
  center = priv->size / 2.0;
  outer = priv->size / 2.0;
  inner = outer - priv->ring_width;
586
  angle = priv->h * 2.0 * G_PI;
587 588 589
  
  *hx = floor (center + cos (angle) * inner + 0.5);
  *hy = floor (center - sin (angle) * inner + 0.5);
590 591 592 593
  *sx = floor (center + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
  *sy = floor (center - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
  *vx = floor (center + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
  *vy = floor (center - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
594 595 596 597
}

/* Computes whether a point is inside the hue ring */
static gboolean
598 599 600
is_in_ring (GtkHSV *hsv,
	    gdouble x,
	    gdouble y)
601
{
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
  HSVPrivate *priv;
  gdouble dx, dy, dist;
  gdouble center, inner, outer;
  
  priv = hsv->priv;
  
  center = priv->size / 2.0;
  outer = priv->size / 2.0;
  inner = outer - priv->ring_width;
  
  dx = x - center;
  dy = center - y;
  dist = dx * dx + dy * dy;
  
  return (dist >= inner * inner && dist <= outer * outer);
617 618 619 620
}

/* Computes a saturation/value pair based on the mouse coordinates */
static void
621 622 623 624 625
compute_sv (GtkHSV  *hsv,
	    gdouble  x,
	    gdouble  y,
	    gdouble *s,
	    gdouble *v)
626
{
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
  HSVPrivate *priv;
  int ihx, ihy, isx, isy, ivx, ivy;
  double hx, hy, sx, sy, vx, vy;
  double center;
  
  priv = hsv->priv;
  
  compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
  center = priv->size / 2.0;
  hx = ihx - center;
  hy = center - ihy;
  sx = isx - center;
  sy = center - isy;
  vx = ivx - center;
  vy = center - ivy;
  x -= center;
  y = center - y;
  
  if (vx * (x - sx) + vy * (y - sy) < 0.0)
    {
      *s = 1.0;
      *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
	    / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
      
      if (*v < 0.0)
	*v = 0.0;
      else if (*v > 1.0)
	*v = 1.0;
    }
  else if (hx * (x - sx) + hy * (y - sy) < 0.0)
    {
      *s = 0.0;
      *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
	    / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
      
      if (*v < 0.0)
	*v = 0.0;
      else if (*v > 1.0)
	*v = 1.0;
    }
  else if (sx * (x - hx) + sy * (y - hy) < 0.0)
    {
      *v = 1.0;
      *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
	    ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
      
      if (*s < 0.0)
	*s = 0.0;
      else if (*s > 1.0)
	*s = 1.0;
    }
  else
    {
      *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
	    / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
      
      if (*v<= 0.0)
	{
	  *v = 0.0;
	  *s = 0.0;
	}
      else
	{
	  if (*v > 1.0)
	    *v = 1.0;
692 693 694 695 696 697

	  if (fabs (hy - vy) < fabs (hx - vx))
	    *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
	  else
	    *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
	    
698 699 700 701
	  if (*s < 0.0)
	    *s = 0.0;
	  else if (*s > 1.0)
	    *s = 1.0;
702
	}
703
    }
704 705 706 707
}

/* Computes whether a point is inside the saturation/value triangle */
static gboolean
708 709 710
is_in_triangle (GtkHSV *hsv,
		gdouble x,
		gdouble y)
711
{
712 713 714 715 716 717 718 719 720 721 722
  int hx, hy, sx, sy, vx, vy;
  double det, s, v;
  
  compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
  
  det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
  
  s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
  v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
  
  return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
723 724 725 726
}

/* Computes a value based on the mouse coordinates */
static double
727 728 729
compute_v (GtkHSV *hsv,
	   gdouble x,
	   gdouble y)
730
{
731 732 733 734 735 736 737 738 739 740 741 742 743
  HSVPrivate *priv;
  double center;
  double dx, dy;
  double angle;
  
  priv = hsv->priv;
  
  center = priv->size / 2.0;
  dx = x - center;
  dy = center - y;
  
  angle = atan2 (dy, dx);
  if (angle < 0.0)
744
    angle += 2.0 * G_PI;
745
  
746
  return angle / (2.0 * G_PI);
747 748 749 750 751
}

/* Event handlers */

static void
752 753
set_cross_grab (GtkHSV *hsv,
		guint32 time)
754
{
755 756 757 758 759
  HSVPrivate *priv;
  GdkCursor *cursor;
  
  priv = hsv->priv;
  
760 761
  cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
				       GDK_CROSSHAIR);
762 763 764 765 766 767 768
  gdk_pointer_grab (priv->window, FALSE,
		    (GDK_POINTER_MOTION_MASK
		     | GDK_POINTER_MOTION_HINT_MASK
		     | GDK_BUTTON_RELEASE_MASK),
		    NULL,
		    cursor,
		    time);
Manish Singh's avatar
Manish Singh committed
769
  gdk_cursor_unref (cursor);
770 771 772 773
}

/* Button_press_event handler for the HSV color selector */
static gint
774 775
gtk_hsv_button_press (GtkWidget      *widget,
		      GdkEventButton *event)
776
{
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
  GtkHSV *hsv;
  HSVPrivate *priv;
  double x, y;
  
  hsv = GTK_HSV (widget);
  priv = hsv->priv;
  
  if (priv->mode != DRAG_NONE || event->button != 1)
    return FALSE;
  
  x = event->x;
  y = event->y;
  
  if (is_in_ring (hsv, x, y))
    {
      priv->mode = DRAG_H;
      set_cross_grab (hsv, event->time);
      
      gtk_hsv_set_color (hsv,
			 compute_v (hsv, x, y),
			 priv->s,
			 priv->v);
799 800 801

      gtk_widget_grab_focus (widget);
      priv->focus_on_ring = TRUE;
802 803 804 805 806 807 808 809 810 811 812 813 814
      
      return TRUE;
    }
  
  if (is_in_triangle (hsv, x, y))
    {
      gdouble s, v;
      
      priv->mode = DRAG_SV;
      set_cross_grab (hsv, event->time);
      
      compute_sv (hsv, x, y, &s, &v);
      gtk_hsv_set_color (hsv, priv->h, s, v);
815 816 817 818

      gtk_widget_grab_focus (widget);
      priv->focus_on_ring = FALSE;
      
819 820 821 822
      return TRUE;
    }
  
  return FALSE;
823 824 825 826
}

/* Button_release_event handler for the HSV color selector */
static gint
827 828
gtk_hsv_button_release (GtkWidget      *widget,
			GdkEventButton *event)
829
{
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
  GtkHSV *hsv;
  HSVPrivate *priv;
  DragMode mode;
  gdouble x, y;
  
  hsv = GTK_HSV (widget);
  priv = hsv->priv;
  
  if (priv->mode == DRAG_NONE || event->button != 1)
    return FALSE;
  
  /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
   * can see that this is the final color state.
   */
  
  mode = priv->mode;
  priv->mode = DRAG_NONE;
  
  x = event->x;
  y = event->y;
  
  if (mode == DRAG_H)
    gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
  else if (mode == DRAG_SV) {
    double s, v;
    
    compute_sv (hsv, x, y, &s, &v);
    gtk_hsv_set_color (hsv, priv->h, s, v);
  } else
    g_assert_not_reached ();
  
861 862
  gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
			      event->time);
863
  return TRUE;
864 865 866 867
}

/* Motion_notify_event handler for the HSV color selector */
static gint
868 869
gtk_hsv_motion (GtkWidget      *widget,
		GdkEventMotion *event)
870
{
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
  GtkHSV *hsv;
  HSVPrivate *priv;
  double x, y;
  gint ix, iy;
  GdkModifierType mods;
  
  hsv = GTK_HSV (widget);
  priv = hsv->priv;
  
  if (priv->mode == DRAG_NONE)
    return FALSE;
  
  if (event->is_hint)
    {
      gdk_window_get_pointer (priv->window, &ix, &iy, &mods);
      x = ix;
      y = iy;
    }
  else
    {
      x = event->x;
      y = event->y;
    }
  
  if (priv->mode == DRAG_H)
    {
      gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
      return TRUE;
    }
  else if (priv->mode == DRAG_SV)
    {
      double s, v;
      
      compute_sv (hsv, x, y, &s, &v);
      gtk_hsv_set_color (hsv, priv->h, s, v);
      return TRUE;
    }
  
  g_assert_not_reached ();
  return FALSE;
911 912 913 914 915 916 917
}


/* Redrawing */

/* Paints the hue ring */
static void
918 919 920 921 922 923
paint_ring (GtkHSV      *hsv,
	    GdkDrawable *drawable,
	    gint         x,
	    gint         y,
	    gint         width,
	    gint         height)
924
{
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
  HSVPrivate *priv;
  int xx, yy;
  gdouble dx, dy, dist;
  gdouble center;
  gdouble inner, outer;
  guchar *buf, *p;
  gdouble angle;
  gdouble hue;
  gdouble r, g, b;
  GdkBitmap *mask;
  GdkGC *gc;
  GdkColor color;
  
  priv = hsv->priv;
  
  center = priv->size / 2.0;
  
  outer = priv->size / 2.0;
  inner = outer - priv->ring_width;
  
  /* Paint the ring */
  
  buf = g_new (guchar, width * height * 3);
  
  for (yy = 0; yy < height; yy++)
    {
      p = buf + yy * width * 3;
      
      dy = -(yy + y - center);
      
      for (xx = 0; xx < width; xx++)
	{
	  dx = xx + x - center;
	  
	  dist = dx * dx + dy * dy;
960
	  if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
961 962 963 964 965 966 967 968 969
	    {
	      *p++ = 0;
	      *p++ = 0;
	      *p++ = 0;
	      continue;
	    }
	  
	  angle = atan2 (dy, dx);
	  if (angle < 0.0)
970
	    angle += 2.0 * G_PI;
971
	  
972
	  hue = angle / (2.0 * G_PI);
973 974 975 976 977 978 979 980 981
	  
	  r = hue;
	  g = 1.0;
	  b = 1.0;
	  hsv_to_rgb (&r, &g, &b);
	  
	  *p++ = floor (r * 255 + 0.5);
	  *p++ = floor (g * 255 + 0.5);
	  *p++ = floor (b * 255 + 0.5);
982
	}
983 984 985 986
    }
  
  /* Create clipping mask */
  
987 988
  mask = gdk_pixmap_new (GTK_WIDGET (hsv)->window, width, height, 1);

989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
  gc = gdk_gc_new (mask);
  
  color.pixel = 0;
  gdk_gc_set_foreground (gc, &color);
  gdk_draw_rectangle (mask, gc, TRUE,
		      0, 0, width, height);
  
  
  color.pixel = 1;
  gdk_gc_set_foreground (gc, &color);
  gdk_draw_arc (mask, gc, TRUE,
		-x, -y,
		priv->size - 1, priv->size - 1,
		0, 360 * 64);
  
  color.pixel = 0;
  gdk_gc_set_foreground (gc, &color);
  gdk_draw_arc (mask, gc, TRUE,
		-x + priv->ring_width - 1, -y + priv->ring_width - 1,
		priv->size - 2 * priv->ring_width + 1, priv->size - 2 * priv->ring_width + 1,
		0, 360 * 64);
  
Manish Singh's avatar
Manish Singh committed
1011
  g_object_unref (gc);
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
  
  gdk_gc_set_clip_mask (priv->gc, mask);
  gdk_gc_set_clip_origin (priv->gc, 0, 0);
  
  /* Draw ring */
  
  gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
				GDK_RGB_DITHER_MAX,
				buf,
				width * 3,
				x, y);
  
  /* Draw value marker */
  
  r = priv->h;
  g = 1.0;
  b = 1.0;
  hsv_to_rgb (&r, &g, &b);
  
  if (INTENSITY (r, g, b) > 0.5)
1032 1033 1034 1035 1036
    {
      color.red = 0x0000;
      color.green = 0x0000;
      color.blue = 0x0000;
    }
1037
  else
1038 1039 1040 1041 1042 1043 1044
    {
      color.red = 0xffff;
      color.green = 0xffff;
      color.blue = 0xffff;
    }

  gdk_gc_set_rgb_fg_color (priv->gc, &color);
1045 1046 1047
  
  gdk_draw_line (drawable, priv->gc,
		 -x + center, -y + center,
1048 1049
		 -x + center + cos (priv->h * 2.0 * G_PI) * center,
		 -y + center - sin (priv->h * 2.0 * G_PI) * center);
1050 1051
  
  gdk_gc_set_clip_mask (priv->gc, NULL);
Manish Singh's avatar
Manish Singh committed
1052
  g_object_unref (mask);
1053 1054 1055 1056
  
  g_free (buf);
  
  /* Draw ring outline */
1057 1058 1059 1060

  if (GTK_WIDGET_HAS_FOCUS (hsv) &&
      priv->focus_on_ring)
    {
1061 1062 1063 1064
      gint focus_width;
      gint focus_halfwidth;
      GdkGC *gc = gtk_hsv_get_focus_gc (hsv, &focus_width);
      focus_halfwidth = (focus_width + 1) / 2;
1065
      
1066 1067 1068
      gdk_draw_arc (drawable, gc, FALSE,
                    -x + focus_width/2, -y + focus_width/2,
                    priv->size - focus_width, priv->size - focus_width,
1069
                    0, 360 * 64);
1070 1071 1072 1073
      gdk_draw_arc (drawable, gc, FALSE,
                    -x + priv->ring_width - focus_halfwidth, -y + priv->ring_width - focus_halfwidth,
                    priv->size - 2 * priv->ring_width + focus_width,
		    priv->size - 2 * priv->ring_width + focus_width,
1074
                    0, 360 * 64);
1075 1076
      
      g_object_unref (gc);
1077
    }
1078 1079 1080 1081
}

/* Converts an HSV triplet to an integer RGB triplet */
static void
1082 1083 1084 1085 1086 1087
get_color (gdouble h,
	   gdouble s,
	   gdouble v,
	   gint   *r,
	   gint   *g,
	   gint   *b)
1088
{
1089 1090 1091 1092 1093
  hsv_to_rgb (&h, &s, &v);
  
  *r = floor (h * 255 + 0.5);
  *g = floor (s * 255 + 0.5);
  *b = floor (v * 255 + 0.5);
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
}

#define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))

#define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0)					\
			       ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1)))	\
			       : (a))

/* Paints the HSV triangle */
static void
1104 1105 1106 1107 1108 1109
paint_triangle (GtkHSV      *hsv,
		GdkDrawable *drawable,
		gint         x,
		gint         y,
		gint         width,
		gint         height)
1110
{
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
  HSVPrivate *priv;
  gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
  gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
  gint x2, y2, r2, g2, b2; /* Second vertex */
  gint x3, y3, r3, g3, b3; /* Third vertex */
  gint t;
  guchar *buf, *p;
  gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
  gint xx, yy;
  GdkBitmap *mask;
  GdkGC *gc;
  GdkColor color;
  GdkPoint points[3];
  gdouble r, g, b;
  
  priv = hsv->priv;
  
  /* Compute triangle's vertices */
  
  compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
  
  x1 = hx;
  y1 = hy;
  get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
  
  x2 = sx;
  y2 = sy;
  get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
  
  x3 = vx;
  y3 = vy;
  get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
  
  if (y2 > y3)
    {
      SWAP (x2, x3, t);
      SWAP (y2, y3, t);
      SWAP (r2, r3, t);
      SWAP (g2, g3, t);
      SWAP (b2, b3, t);
    }
  
  if (y1 > y3)
    {
      SWAP (x1, x3, t);
      SWAP (y1, y3, t);
      SWAP (r1, r3, t);
      SWAP (g1, g3, t);
      SWAP (b1, b3, t);
    }
  
  if (y1 > y2)
    {
      SWAP (x1, x2, t);
      SWAP (y1, y2, t);
      SWAP (r1, r2, t);
      SWAP (g1, g2, t);
      SWAP (b1, b2, t);
    }
  
  /* Shade the triangle */
  
  buf = g_new (guchar, width * height * 3);
  
  for (yy = 0; yy < height; yy++)
    {
      p = buf + yy * width * 3;
      
      if (yy + y < y1 || yy + y > y3)
	for (xx = 0; xx < width; xx++)
	  {
	    *p++ = 0;
	    *p++ = 0;
	    *p++ = 0;
	  }
      else {
	if (yy + y < y2)
	  {
	    xl = LERP (x1, x2, y1, y2, yy + y);
	    
	    rl = LERP (r1, r2, y1, y2, yy + y);
	    gl = LERP (g1, g2, y1, y2, yy + y);
	    bl = LERP (b1, b2, y1, y2, yy + y);
	  }
1195
	else
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
	  {
	    xl = LERP (x2, x3, y2, y3, yy + y);
	    
	    rl = LERP (r2, r3, y2, y3, yy + y);
	    gl = LERP (g2, g3, y2, y3, yy + y);
	    bl = LERP (b2, b3, y2, y3, yy + y);
	  }
	
	xr = LERP (x1, x3, y1, y3, yy + y);
	
	rr = LERP (r1, r3, y1, y3, yy + y);
	gr = LERP (g1, g3, y1, y3, yy + y);
	br = LERP (b1, b3, y1, y3, yy + y);
	
	if (xl > xr)
	  {
	    SWAP (xl, xr, t);
	    SWAP (rl, rr, t);
	    SWAP (gl, gr, t);
	    SWAP (bl, br, t);
	  }
	
	for (xx = 0; xx < width; xx++)
	  {
	    if (xx + x < xl || xx + x > xr)
	      {
		*p++ = 0;
		*p++ = 0;
		*p++ = 0;
	      }
	    else
	      {
		*p++ = LERP (rl, rr, xl, xr, xx + x);
		*p++ = LERP (gl, gr, xl, xr, xx + x);
		*p++ = LERP (bl, br, xl, xr, xx + x);
	      }
	  }
      }
    }
  
  /* Create clipping mask */
  
1238 1239
  mask = gdk_pixmap_new (GTK_WIDGET (hsv)->window, width, height, 1);

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
  gc = gdk_gc_new (mask);
  
  color.pixel = 0;
  gdk_gc_set_foreground (gc, &color);
  gdk_draw_rectangle (mask, gc, TRUE,
		      0, 0, width, height);
  
  color.pixel = 1;
  gdk_gc_set_foreground (gc, &color);
  
  points[0].x = x1 - x;
  points[0].y = y1 - y;
  points[1].x = x2 - x;
  points[1].y = y2 - y;
  points[2].x = x3 - x;
  points[2].y = y3 - y;
  gdk_draw_polygon (mask, gc, TRUE, points, 3);
  
Manish Singh's avatar
Manish Singh committed
1258
  g_object_unref (gc);
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
  
  gdk_gc_set_clip_mask (priv->gc, mask);
  gdk_gc_set_clip_origin (priv->gc, 0, 0);
  
  /* Draw triangle */
  
  gdk_draw_rgb_image_dithalign (drawable, priv->gc, 0, 0, width, height,
				GDK_RGB_DITHER_MAX,
				buf,
				width * 3,
				x, y);
  
  gdk_gc_set_clip_mask (priv->gc, NULL);
Manish Singh's avatar
Manish Singh committed
1272
  g_object_unref (mask);
1273 1274 1275
  
  g_free (buf);
  
1276 1277 1278 1279 1280
  /* Draw triangle focus outline */

  if (GTK_WIDGET_HAS_FOCUS (hsv) &&
      !priv->focus_on_ring)
    {
1281 1282
      gint focus_width = 1;	    
      GdkGC *gc = gtk_hsv_get_focus_gc (hsv, &focus_width);
1283
  
1284 1285
      gdk_draw_polygon (drawable, gc, FALSE, points, 3);
      g_object_unref (gc);
1286
    }
1287 1288 1289
  
  /* Draw value marker */
  
1290 1291
  xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5) - x;
  yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5) - y;
1292 1293 1294 1295 1296
  
  r = priv->h;
  g = priv->s;
  b = priv->v;
  hsv_to_rgb (&r, &g, &b);
1297

1298
  if (INTENSITY (r, g, b) > 0.5)
1299 1300 1301 1302 1303
    {
      color.red = 0x0000;
      color.green = 0x0000;
      color.blue = 0x0000;
    }
1304
  else
1305 1306 1307 1308 1309 1310 1311
    {
      color.red = 0xffff;
      color.green = 0xffff;
      color.blue = 0xffff;
    }

  gdk_gc_set_rgb_fg_color (priv->gc, &color);
1312 1313 1314

#define OUTER_RADIUS 4
#define INNER_RADIUS 3 
1315 1316
  
  gdk_draw_arc (drawable, priv->gc, FALSE,
1317 1318
		xx - OUTER_RADIUS, yy - OUTER_RADIUS,
		OUTER_RADIUS * 2, OUTER_RADIUS * 2,
1319 1320
		0, 360 * 64);
  gdk_draw_arc (drawable, priv->gc, FALSE,
1321 1322
		xx - INNER_RADIUS, yy - INNER_RADIUS,
		INNER_RADIUS * 2, INNER_RADIUS * 2,
1323
		0, 360 * 64);
1324 1325 1326 1327
}

/* Paints the contents of the HSV color selector */
static void
1328 1329 1330 1331 1332 1333
paint (GtkHSV      *hsv,
       GdkDrawable *drawable,
       gint         x,
       gint         y,
       gint         width,
       gint         height)
1334
{
1335 1336
  paint_ring (hsv, drawable, x, y, width, height);
  paint_triangle (hsv, drawable, x, y, width, height);
1337 1338 1339 1340
}

/* Expose_event handler for the HSV color selector */
static gint
1341 1342
gtk_hsv_expose (GtkWidget      *widget,
		GdkEventExpose *event)
1343
{
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
  GtkHSV *hsv;
  HSVPrivate *priv;
  GdkRectangle rect, dest;
  GdkPixmap *pixmap;
  
  hsv = GTK_HSV (widget);
  priv = hsv->priv;
  
  if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
    return FALSE;
  
  rect.x = widget->allocation.x;
  rect.y = widget->allocation.y;
  rect.width = widget->allocation.width;
  rect.height = widget->allocation.height;
  
  if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
    return FALSE;
  
  pixmap = gdk_pixmap_new (widget->window, dest.width, dest.height,
			   gtk_widget_get_visual (widget)->depth);
  
  rect = dest;
  rect.x = 0;
  rect.y = 0;
  
  gdk_draw_rectangle (pixmap,
		      widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
		      TRUE,
		      0, 0, dest.width, dest.height);
  paint (hsv, pixmap,
	 dest.x - widget->allocation.x, dest.y - widget->allocation.y,
	 dest.width, dest.height);
  
Manish Singh's avatar
Manish Singh committed
1378 1379 1380 1381 1382 1383 1384
  gdk_draw_drawable (widget->window,
		     priv->gc,
		     pixmap,
		     0, 0,
		     dest.x,
		     dest.y,
		     event->area.width, event->area.height);
1385
  
Manish Singh's avatar
Manish Singh committed
1386
  g_object_unref (pixmap);
1387 1388
  
  return FALSE;
1389 1390
}

1391
static gboolean
1392
gtk_hsv_focus (GtkWidget       *widget,
1393 1394 1395 1396 1397
               GtkDirectionType dir)
{
  GtkHSV *hsv;
  HSVPrivate *priv;

1398
  hsv = GTK_HSV (widget);
1399 1400 1401 1402
  priv = hsv->priv;

  if (!GTK_WIDGET_HAS_FOCUS (hsv))
    {
1403 1404 1405 1406 1407
      if (dir == GTK_DIR_TAB_BACKWARD)
        priv->focus_on_ring = FALSE;
      else
        priv->focus_on_ring = TRUE;

1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
      gtk_widget_grab_focus (GTK_WIDGET (hsv));
      return TRUE;
    }
  
  switch (dir)
    {
    case GTK_DIR_UP:
      if (priv->focus_on_ring)
        return FALSE;
      else
        priv->focus_on_ring = TRUE;
      break;

    case GTK_DIR_DOWN:
      if (priv->focus_on_ring)
        priv->focus_on_ring = FALSE;
      else
        return FALSE;
      break;

    case GTK_DIR_LEFT:
    case GTK_DIR_TAB_BACKWARD:
      if (priv->focus_on_ring)
        return FALSE;
      else
        priv->focus_on_ring = TRUE;
      break;

    case GTK_DIR_RIGHT:
    case GTK_DIR_TAB_FORWARD:
      if (priv->focus_on_ring)
        priv->focus_on_ring = FALSE;
      else
        return FALSE;
      break;
    }

  gtk_widget_queue_draw (GTK_WIDGET (hsv));

  return TRUE;
}
1449 1450 1451 1452 1453 1454 1455 1456 1457

/**
 * gtk_hsv_new:
 * @void:
 *
 * Creates a new HSV color selector.
 *
 * Return value: A newly-created HSV color selector.
 **/
1458
GtkWidget*
1459 1460
gtk_hsv_new (void)
{
Manish Singh's avatar
Manish Singh committed
1461
  return g_object_new (GTK_TYPE_HSV, NULL);
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
}

/**
 * gtk_hsv_set_color:
 * @hsv: An HSV color selector.
 * @h: Hue.
 * @s: Saturation.
 * @v: Value.
 *
 * Sets the current color in an HSV color selector.  Color component values must
 * be in the [0.0, 1.0] range.
 **/
void
1475 1476 1477 1478
gtk_hsv_set_color (GtkHSV *hsv,
		   gdouble h,
		   gdouble s,
		   gdouble v)
1479
{
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
  HSVPrivate *priv;
  
  g_return_if_fail (hsv != NULL);
  g_return_if_fail (GTK_IS_HSV (hsv));
  g_return_if_fail (h >= 0.0 && h <= 1.0);
  g_return_if_fail (s >= 0.0 && s <= 1.0);
  g_return_if_fail (v >= 0.0 && v <= 1.0);
  
  priv = hsv->priv;
  
  priv->h = h;
  priv->s = s;
  priv->v = v;
  
Manish Singh's avatar
Manish Singh committed
1494
  g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1495 1496
  
  gtk_widget_queue_draw (GTK_WIDGET (hsv));
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
}

/**
 * gtk_hsv_get_color:
 * @hsv: An HSV color selector.
 * @h: Return value for the hue.
 * @s: Return value for the saturation.
 * @v: Return value for the value.
 *
 * Queries the current color in an HSV color selector.  Returned values will be
 * in the [0.0, 1.0] range.
 **/
void
gtk_hsv_get_color (GtkHSV *hsv, double *h, double *s, double *v)
{
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
  HSVPrivate *priv;
  
  g_return_if_fail (GTK_IS_HSV (hsv));
  
  priv = hsv->priv;
  
  if (h)
    *h = priv->h;
  
  if (s)
    *s = priv->s;
  
  if (v)
    *v = priv->v;
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
}

/**
 * gtk_hsv_set_metrics:
 * @hsv: An HSV color selector.
 * @size: Diameter for the hue ring.
 * @ring_width: Width of the hue ring.
 *
 * Sets the size and ring width of an HSV color selector.
 **/
void
1537 1538 1539
gtk_hsv_set_metrics (GtkHSV *hsv,
		     gint    size,
		     gint    ring_width)
1540
{
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
  HSVPrivate *priv;
  int same_size;
  
  g_return_if_fail (GTK_IS_HSV (hsv));
  g_return_if_fail (size > 0);
  g_return_if_fail (ring_width > 0);
  g_return_if_fail (2 * ring_width + 1 <= size);
  
  priv = hsv->priv;
  
  same_size = (priv->size == size);
  
  priv->size = size;
  priv->ring_width = ring_width;
  
  if (same_size)
    gtk_widget_queue_draw (GTK_WIDGET (hsv));
  else
    gtk_widget_queue_resize (GTK_WIDGET (hsv));
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
}

/**
 * gtk_hsv_get_metrics:
 * @hsv: An HSV color selector.
 * @size: Return value for the diameter of the hue ring.
 * @ring_width: Return value for the width of the hue ring.
 *
 * Queries the size and ring width of an HSV color selector.
 **/
void
1571 1572 1573
gtk_hsv_get_metrics (GtkHSV *hsv,
		     gint   *size,
		     gint   *ring_width)
1574
{
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
  HSVPrivate *priv;
  
  g_return_if_fail (GTK_IS_HSV (hsv));
  
  priv = hsv->priv;
  
  if (size)
    *size = priv->size;
  
  if (ring_width)
    *ring_width = priv->ring_width;
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
}

/**
 * gtk_hsv_is_adjusting:
 * @hsv:
 *
 * An HSV color selector can be said to be adjusting if multiple rapid changes
 * are being made to its value, for example, when the user is adjusting the
 * value with the mouse.  This function queries whether the HSV color selector
 * is being adjusted or not.
 *
 * Return value: TRUE if clients can ignore changes to the color value, since
 * they may be transitory, or FALSE if they should consider the color value
 * status to be final.
 **/
gboolean
gtk_hsv_is_adjusting (GtkHSV *hsv)
{
1604 1605 1606 1607 1608 1609 1610
  HSVPrivate *priv;
  
  g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
  
  priv = hsv->priv;

  return priv->mode != DRAG_NONE;
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
}

/**
 * gtk_hsv_to_rgb:
 * @h: Hue.
 * @s: Saturation.
 * @v: Value.
 * @r: Return value for the red component.
 * @g: Return value for the green component.
 * @b: Return value for the blue component.
 * 
 * Converts a color from HSV space to RGB.  Input values must be in the
 * [0.0, 1.0] range; output values will be in the same range.
 **/
void
1626 1627 1628 1629 1630 1631
gtk_hsv_to_rgb (gdouble  h,
		gdouble  s,
		gdouble  v,
		gdouble *r,
		gdouble *g,
		gdouble *b)
1632
{
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
  g_return_if_fail (h >= 0.0 && h <= 1.0);
  g_return_if_fail (s >= 0.0 && s <= 1.0);
  g_return_if_fail (v >= 0.0 && v <= 1.0);
  
  hsv_to_rgb (&h, &s, &v);
  
  if (r)
    *r = h;
  
  if (g)
    *g = s;
  
  if (b)
    *b = v;
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
}

/**
 * gtk_hsv_to_rgb:
 * @r: Red.
 * @g: Green.
 * @b: Blue.
 * @h: Return value for the hue component.
 * @s: Return value for the saturation component.
 * @v: Return value for the value component.
 * 
 * Converts a color from RGB space to HSV.  Input values must be in the
 * [0.0, 1.0] range; output values will be in the same range.
 **/
void
1662 1663 1664 1665 1666 1667
gtk_rgb_to_hsv (gdouble  r,
		gdouble  g,
		gdouble  b,
		gdouble *h,
		gdouble *s,
		gdouble *v)
1668
{
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
  g_return_if_fail (r >= 0.0 && r <= 1.0);
  g_return_if_fail (g >= 0.0 && g <= 1.0);
  g_return_if_fail (b >= 0.0 && b <= 1.0);
  
  rgb_to_hsv (&r, &g, &b);
  
  if (h)
    *h = r;
  
  if (s)
    *s = g;
  
  if (v)
    *v = b;
1683
}
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703

static void
gtk_hsv_move (GtkHSV          *hsv,
              GtkDirectionType dir)
{
  HSVPrivate *priv;
  gdouble hue, sat, val;
  gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
  gint x, y; /* position in triangle */
  
  priv = hsv->priv;

  hue = priv->h;
  sat = priv->s;
  val = priv->v;

  compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);

  x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
  y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1704 1705

#define HUE_DELTA 0.002
1706 1707 1708 1709
  switch (dir)
    {
    case GTK_DIR_UP:
      if (priv->focus_on_ring)
1710
        hue += HUE_DELTA;
1711 1712 1713 1714 1715 1716 1717 1718 1719
      else
        {
          y -= 1;
          compute_sv (hsv, x, y, &sat, &val);
        }
      break;

    case GTK_DIR_DOWN:
      if (priv->focus_on_ring)
1720
        hue -= HUE_DELTA;
1721 1722 1723 1724 1725 1726 1727 1728 1729
      else
        {
          y += 1;
          compute_sv (hsv, x, y, &sat, &val);
        }
      break;

    case GTK_DIR_LEFT:
      if (priv->focus_on_ring)
1730
        hue += HUE_DELTA;
1731 1732 1733 1734 1735 1736 1737 1738 1739
      else
        {
          x -= 1;
          compute_sv (hsv, x, y, &sat, &val);
        }
      break;

    case GTK_DIR_RIGHT:
      if (priv->focus_on_ring)
1740 1741
        hue -= HUE_DELTA
          ;
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
      else
        {
          x += 1;
          compute_sv (hsv, x, y, &sat, &val);
        }
      break;

    default:
      /* we don't care about the tab directions */
      break;
    }

  /* Wrap */
  if (hue < 0.0)
    hue = 1.0;
  else if (hue > 1.0)
    hue = 0.0;
  
  gtk_hsv_set_color (hsv, hue, sat, val);
}
1762

1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
static GdkGC *
gtk_hsv_get_focus_gc (GtkHSV *hsv,
		      gint   *line_width)
{
  GdkGC *focus_gc;
  GtkWidget *widget = GTK_WIDGET (hsv);
  gint8 *dash_list;
  
  focus_gc = gdk_gc_new (widget->window);
  gdk_gc_copy (focus_gc, widget->style->fg_gc[GTK_WIDGET_STATE (widget)]);

  gtk_widget_style_get (widget,
			"focus-line-width", line_width,
			"focus-line-pattern", (gchar *)&dash_list,
			NULL);
  
  gdk_gc_set_line_attributes (focus_gc, *line_width,
			      dash_list[0] ? GDK_LINE_ON_OFF_DASH : GDK_LINE_SOLID,
			      GDK_CAP_BUTT, GDK_JOIN_MITER);
  if (dash_list[0])
    gdk_gc_set_dashes (focus_gc, 0, dash_list, strlen (dash_list));

1785 1786
  g_free (dash_list);
  
1787 1788
  return focus_gc;
}