gimpcolorwheel.c 42.9 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+)
 *          Michael Natterer <mitch@gimp.org> (ported back to GIMP)
 *
10 11 12 13
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
19
 *
20 21
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 23 24 25 26 27 28 29 30 31 32
 */

/*
 * 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/.
 */

#include "config.h"

33 34
#include <gegl.h>

35 36 37
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

38
#include <libgimpconfig/gimpconfig.h>
39
#include <libgimpcolor/gimpcolor.h>
40
#include <libgimpmath/gimpmath.h>
41
#include <libgimpwidgets/gimpwidgets.h>
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

#include "gimpcolorwheel.h"


/* Default ring fraction */
#define DEFAULT_FRACTION 0.1

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

/* Default ring width */
#define DEFAULT_RING_WIDTH 10


/* Dragging modes */
typedef enum
{
  DRAG_NONE,
  DRAG_H,
  DRAG_SV
} DragMode;

/* Private part of the GimpColorWheel structure */
typedef struct
{
  /* Color value */
  gdouble h;
  gdouble s;
  gdouble v;

  /* ring_width is this fraction of size */
  gdouble ring_fraction;

  /* Size and ring width */
  gint size;
  gint ring_width;

  /* Window for capturing events */
  GdkWindow *window;

  /* Dragging mode */
  DragMode mode;

  guint focus_on_ring : 1;
86 87 88

  GimpColorConfig    *config;
  GimpColorTransform *transform;
89 90 91 92 93 94 95 96 97
} GimpColorWheelPrivate;

enum
{
  CHANGED,
  MOVE,
  LAST_SIGNAL
};

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
static void     gimp_color_wheel_dispose           (GObject            *object);

static void     gimp_color_wheel_map               (GtkWidget          *widget);
static void     gimp_color_wheel_unmap             (GtkWidget          *widget);
static void     gimp_color_wheel_realize           (GtkWidget          *widget);
static void     gimp_color_wheel_unrealize         (GtkWidget          *widget);
static void     gimp_color_wheel_size_request      (GtkWidget          *widget,
                                                    GtkRequisition     *requisition);
static void     gimp_color_wheel_size_allocate     (GtkWidget          *widget,
                                                    GtkAllocation      *allocation);
static gboolean gimp_color_wheel_button_press      (GtkWidget          *widget,
                                                    GdkEventButton     *event);
static gboolean gimp_color_wheel_button_release    (GtkWidget          *widget,
                                                    GdkEventButton     *event);
static gboolean gimp_color_wheel_motion            (GtkWidget          *widget,
                                                    GdkEventMotion     *event);
static gboolean gimp_color_wheel_expose            (GtkWidget          *widget,
                                                    GdkEventExpose     *event);
static gboolean gimp_color_wheel_grab_broken       (GtkWidget          *widget,
                                                    GdkEventGrabBroken *event);
static gboolean gimp_color_wheel_focus             (GtkWidget          *widget,
                                                    GtkDirectionType    direction);
static void     gimp_color_wheel_move              (GimpColorWheel     *wheel,
                                                    GtkDirectionType    dir);

static void     gimp_color_wheel_create_transform  (GimpColorWheel     *wheel);
static void     gimp_color_wheel_destroy_transform (GimpColorWheel     *wheel);
125 126 127 128


static guint wheel_signals[LAST_SIGNAL];

129
G_DEFINE_DYNAMIC_TYPE (GimpColorWheel, gimp_color_wheel, GTK_TYPE_WIDGET)
130 131 132 133

#define parent_class gimp_color_wheel_parent_class


134 135 136 137 138 139
void
color_wheel_register_type (GTypeModule *module)
{
  gimp_color_wheel_register_type (module);
}

140 141 142 143 144 145 146 147
static void
gimp_color_wheel_class_init (GimpColorWheelClass *class)
{
  GObjectClass        *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass      *widget_class = GTK_WIDGET_CLASS (class);
  GimpColorWheelClass *wheel_class  = GIMP_COLOR_WHEEL_CLASS (class);
  GtkBindingSet       *binding_set;

148 149
  object_class->dispose              = gimp_color_wheel_dispose;

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  widget_class->map                  = gimp_color_wheel_map;
  widget_class->unmap                = gimp_color_wheel_unmap;
  widget_class->realize              = gimp_color_wheel_realize;
  widget_class->unrealize            = gimp_color_wheel_unrealize;
  widget_class->size_request         = gimp_color_wheel_size_request;
  widget_class->size_allocate        = gimp_color_wheel_size_allocate;
  widget_class->button_press_event   = gimp_color_wheel_button_press;
  widget_class->button_release_event = gimp_color_wheel_button_release;
  widget_class->motion_notify_event  = gimp_color_wheel_motion;
  widget_class->expose_event         = gimp_color_wheel_expose;
  widget_class->focus                = gimp_color_wheel_focus;
  widget_class->grab_broken_event    = gimp_color_wheel_grab_broken;

  wheel_class->move                  = gimp_color_wheel_move;

  wheel_signals[CHANGED] =
    g_signal_new ("changed",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpColorWheelClass, changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  wheel_signals[MOVE] =
    g_signal_new ("move",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GimpColorWheelClass, move),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__ENUM,
                  G_TYPE_NONE, 1,
                  GTK_TYPE_DIRECTION_TYPE);

  binding_set = gtk_binding_set_by_class (class);

  gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
                                "move", 1,
                                G_TYPE_ENUM, GTK_DIR_UP);
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
                                "move", 1,
                                G_TYPE_ENUM, GTK_DIR_UP);

  gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
                                "move", 1,
                                G_TYPE_ENUM, GTK_DIR_DOWN);
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
                                "move", 1,
                                G_TYPE_ENUM, GTK_DIR_DOWN);


  gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
                                "move", 1,
                                G_TYPE_ENUM, GTK_DIR_RIGHT);
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
                                "move", 1,
                                G_TYPE_ENUM, GTK_DIR_RIGHT);

  gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
                                "move", 1,
                                G_TYPE_ENUM, GTK_DIR_LEFT);
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
                                "move", 1,
                                G_TYPE_ENUM, GTK_DIR_LEFT);

  g_type_class_add_private (object_class, sizeof (GimpColorWheelPrivate));
}

218 219 220 221 222
static void
gimp_color_wheel_class_finalize (GimpColorWheelClass *klass)
{
}

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
static void
gimp_color_wheel_init (GimpColorWheel *wheel)
{
  GimpColorWheelPrivate *priv;

  priv = G_TYPE_INSTANCE_GET_PRIVATE (wheel, GIMP_TYPE_COLOR_WHEEL,
                                      GimpColorWheelPrivate);

  wheel->priv = priv;

  gtk_widget_set_has_window (GTK_WIDGET (wheel), FALSE);
  gtk_widget_set_can_focus (GTK_WIDGET (wheel), TRUE);

  priv->ring_fraction = DEFAULT_FRACTION;
  priv->size          = DEFAULT_SIZE;
  priv->ring_width    = DEFAULT_RING_WIDTH;
239 240 241 242

  gimp_widget_track_monitor (GTK_WIDGET (wheel),
                             G_CALLBACK (gimp_color_wheel_destroy_transform),
                             NULL);
243 244
}

245 246 247 248 249 250 251 252 253 254
static void
gimp_color_wheel_dispose (GObject *object)
{
  GimpColorWheel *wheel = GIMP_COLOR_WHEEL (object);

  gimp_color_wheel_set_color_config (wheel, NULL);

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
static void
gimp_color_wheel_map (GtkWidget *widget)
{
  GimpColorWheel        *wheel = GIMP_COLOR_WHEEL (widget);
  GimpColorWheelPrivate *priv  = wheel->priv;

  GTK_WIDGET_CLASS (parent_class)->map (widget);

  gdk_window_show (priv->window);
}

static void
gimp_color_wheel_unmap (GtkWidget *widget)
{
  GimpColorWheel        *wheel = GIMP_COLOR_WHEEL (widget);
  GimpColorWheelPrivate *priv  = wheel->priv;

  gdk_window_hide (priv->window);

  GTK_WIDGET_CLASS (parent_class)->unmap (widget);
}

static void
gimp_color_wheel_realize (GtkWidget *widget)
{
  GimpColorWheel        *wheel = GIMP_COLOR_WHEEL (widget);
  GimpColorWheelPrivate *priv  = wheel->priv;
  GtkAllocation          allocation;
  GdkWindowAttr          attr;
  gint                   attr_mask;
  GdkWindow             *parent_window;

  gtk_widget_get_allocation (widget, &allocation);

  gtk_widget_set_realized (widget, TRUE);

  attr.window_type = GDK_WINDOW_CHILD;
  attr.x           = allocation.x;
  attr.y           = allocation.y;
  attr.width       = allocation.width;
  attr.height      = allocation.height;
  attr.wclass      = GDK_INPUT_ONLY;
  attr.event_mask  = (gtk_widget_get_events (widget) |
                      GDK_KEY_PRESS_MASK      |
                      GDK_BUTTON_PRESS_MASK   |
                      GDK_BUTTON_RELEASE_MASK |
                      GDK_POINTER_MOTION_MASK |
                      GDK_ENTER_NOTIFY_MASK   |
                      GDK_LEAVE_NOTIFY_MASK);

  attr_mask = GDK_WA_X | GDK_WA_Y;

  parent_window = gtk_widget_get_parent_window (widget);

  gtk_widget_set_window (widget, parent_window);
  g_object_ref (parent_window);

  priv->window = gdk_window_new (parent_window, &attr, attr_mask);
  gdk_window_set_user_data (priv->window, wheel);

  gtk_widget_style_attach (widget);
}

static void
gimp_color_wheel_unrealize (GtkWidget *widget)
{
  GimpColorWheel        *wheel = GIMP_COLOR_WHEEL (widget);
  GimpColorWheelPrivate *priv  = wheel->priv;

  gdk_window_set_user_data (priv->window, NULL);
  gdk_window_destroy (priv->window);
  priv->window = NULL;

  GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}

static void
gimp_color_wheel_size_request (GtkWidget      *widget,
                               GtkRequisition *requisition)
{
  gint focus_width;
  gint focus_pad;

  gtk_widget_style_get (widget,
                        "focus-line-width", &focus_width,
                        "focus-padding", &focus_pad,
                        NULL);

  requisition->width  = DEFAULT_SIZE + 2 * (focus_width + focus_pad);
  requisition->height = DEFAULT_SIZE + 2 * (focus_width + focus_pad);
}

static void
gimp_color_wheel_size_allocate (GtkWidget     *widget,
                                GtkAllocation *allocation)
{
  GimpColorWheel        *wheel = GIMP_COLOR_WHEEL (widget);
  GimpColorWheelPrivate *priv  = wheel->priv;
  gint                   focus_width;
  gint                   focus_pad;

356
  GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

  gtk_widget_style_get (widget,
                        "focus-line-width", &focus_width,
                        "focus-padding",    &focus_pad,
                        NULL);

  priv->size = MIN (allocation->width  - 2 * (focus_width + focus_pad),
                    allocation->height - 2 * (focus_width + focus_pad));

  priv->ring_width = priv->size * priv->ring_fraction;

  if (gtk_widget_get_realized (widget))
    gdk_window_move_resize (priv->window,
                            allocation->x,
                            allocation->y,
                            allocation->width,
                            allocation->height);
}


/* Utility functions */

/* Converts from HSV to RGB */
static void
hsv_to_rgb (gdouble *h,
            gdouble *s,
            gdouble *v)
{
  gdouble hue, saturation, value;
  gdouble f, p, q, t;

  if (*s == 0.0)
    {
      *h = *v;
      *s = *v;
392
   /* *v = *v; -- heh */
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 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 487 488 489 490 491 492 493 494 495 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 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 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 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 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 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
    }
  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 ();
        }
    }
}

/* Computes the vertices of the saturation/value triangle */
static void
compute_triangle (GimpColorWheel *wheel,
                  gint           *hx,
                  gint           *hy,
                  gint           *sx,
                  gint           *sy,
                  gint           *vx,
                  gint           *vy)
{
  GimpColorWheelPrivate *priv = wheel->priv;
  GtkAllocation          allocation;
  gdouble                center_x;
  gdouble                center_y;
  gdouble                inner, outer;
  gdouble                angle;

  gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation);

  center_x = allocation.width / 2.0;
  center_y = allocation.height / 2.0;

  outer = priv->size / 2.0;
  inner = outer - priv->ring_width;
  angle = priv->h * 2.0 * G_PI;

  *hx = floor (center_x + cos (angle) * inner + 0.5);
  *hy = floor (center_y - sin (angle) * inner + 0.5);
  *sx = floor (center_x + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
  *sy = floor (center_y - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
  *vx = floor (center_x + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
  *vy = floor (center_y - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
}

/* Computes whether a point is inside the hue ring */
static gboolean
is_in_ring (GimpColorWheel *wheel,
            gdouble         x,
            gdouble         y)
{
  GimpColorWheelPrivate *priv = wheel->priv;
  GtkAllocation          allocation;
  gdouble                dx, dy, dist;
  gdouble                center_x;
  gdouble                center_y;
  gdouble                inner, outer;

  gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation);

  center_x = allocation.width / 2.0;
  center_y = allocation.height / 2.0;

  outer = priv->size / 2.0;
  inner = outer - priv->ring_width;

  dx = x - center_x;
  dy = center_y - y;
  dist = dx * dx + dy * dy;

  return (dist >= inner * inner && dist <= outer * outer);
}

/* Computes a saturation/value pair based on the mouse coordinates */
static void
compute_sv (GimpColorWheel *wheel,
            gdouble         x,
            gdouble         y,
            gdouble        *s,
            gdouble        *v)
{
  GtkAllocation allocation;
  gint          ihx, ihy, isx, isy, ivx, ivy;
  gdouble       hx, hy, sx, sy, vx, vy;
  gdouble       center_x;
  gdouble       center_y;

  gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation);

  compute_triangle (wheel, &ihx, &ihy, &isx, &isy, &ivx, &ivy);

  center_x = allocation.width / 2.0;
  center_y = allocation.height / 2.0;

  hx = ihx - center_x;
  hy = center_y - ihy;
  sx = isx - center_x;
  sy = center_y - isy;
  vx = ivx - center_x;
  vy = center_y - ivy;
  x -= center_x;
  y = center_y - 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;

          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));

          if (*s < 0.0)
            *s = 0.0;
          else if (*s > 1.0)
            *s = 1.0;
        }
    }
}

/* Computes whether a point is inside the saturation/value triangle */
static gboolean
is_in_triangle (GimpColorWheel *wheel,
                gdouble         x,
                gdouble         y)
{
  gint    hx, hy, sx, sy, vx, vy;
  gdouble det, s, v;

  compute_triangle (wheel, &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);
}

/* Computes a value based on the mouse coordinates */
static double
compute_v (GimpColorWheel *wheel,
           gdouble         x,
           gdouble         y)
{
  GtkAllocation allocation;
  gdouble       center_x;
  gdouble       center_y;
  gdouble       dx, dy;
  gdouble       angle;

  gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation);

  center_x = allocation.width / 2.0;
  center_y = allocation.height / 2.0;

  dx = x - center_x;
  dy = center_y - y;

  angle = atan2 (dy, dx);
  if (angle < 0.0)
    angle += 2.0 * G_PI;

  return angle / (2.0 * G_PI);
}

static void
set_cross_grab (GimpColorWheel *wheel,
                guint32         time)
{
  GimpColorWheelPrivate *priv = wheel->priv;
  GdkCursor             *cursor;

  cursor =
    gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (wheel)),
                                GDK_CROSSHAIR);

  gdk_pointer_grab (priv->window, FALSE,
                    GDK_POINTER_MOTION_MASK      |
                    GDK_POINTER_MOTION_HINT_MASK |
                    GDK_BUTTON_RELEASE_MASK,
                    NULL, cursor, time);
  gdk_cursor_unref (cursor);
}

static gboolean
gimp_color_wheel_grab_broken (GtkWidget          *widget,
                              GdkEventGrabBroken *event)
{
  GimpColorWheel        *wheel = GIMP_COLOR_WHEEL (widget);
  GimpColorWheelPrivate *priv  = wheel->priv;

  priv->mode = DRAG_NONE;

  return TRUE;
}

static gboolean
gimp_color_wheel_button_press (GtkWidget      *widget,
                               GdkEventButton *event)
{
  GimpColorWheel        *wheel = GIMP_COLOR_WHEEL (widget);
  GimpColorWheelPrivate *priv  = wheel->priv;
  gdouble                x, y;

  if (priv->mode != DRAG_NONE || event->button != 1)
    return FALSE;

  x = event->x;
  y = event->y;

  if (is_in_ring (wheel, x, y))
    {
      priv->mode = DRAG_H;
      set_cross_grab (wheel, event->time);

      gimp_color_wheel_set_color (wheel,
                                  compute_v (wheel, x, y),
                                  priv->s,
                                  priv->v);

      gtk_widget_grab_focus (widget);
      priv->focus_on_ring = TRUE;

      return TRUE;
    }

  if (is_in_triangle (wheel, x, y))
    {
      gdouble s, v;

      priv->mode = DRAG_SV;
      set_cross_grab (wheel, event->time);

      compute_sv (wheel, x, y, &s, &v);
      gimp_color_wheel_set_color (wheel, priv->h, s, v);

      gtk_widget_grab_focus (widget);
      priv->focus_on_ring = FALSE;

      return TRUE;
    }

  return FALSE;
}

static gboolean
gimp_color_wheel_button_release (GtkWidget      *widget,
                                 GdkEventButton *event)
{
  GimpColorWheel        *wheel = GIMP_COLOR_WHEEL (widget);
  GimpColorWheelPrivate *priv  = wheel->priv;
  DragMode               mode;
  gdouble                x, y;

  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)
    {
      gimp_color_wheel_set_color (wheel,
                                  compute_v (wheel, x, y), priv->s, priv->v);
    }
  else if (mode == DRAG_SV)
    {
      gdouble s, v;

      compute_sv (wheel, x, y, &s, &v);
      gimp_color_wheel_set_color (wheel, priv->h, s, v);
    }
  else
    g_assert_not_reached ();

  gdk_display_pointer_ungrab (gdk_window_get_display (event->window),
                              event->time);

  return TRUE;
}

static gboolean
gimp_color_wheel_motion (GtkWidget      *widget,
                         GdkEventMotion *event)
{
  GimpColorWheel        *wheel = GIMP_COLOR_WHEEL (widget);
  GimpColorWheelPrivate *priv  = wheel->priv;
  gdouble                x, y;

  if (priv->mode == DRAG_NONE)
    return FALSE;

  gdk_event_request_motions (event);
  x = event->x;
  y = event->y;

  if (priv->mode == DRAG_H)
    {
      gimp_color_wheel_set_color (wheel,
                                  compute_v (wheel, x, y), priv->s, priv->v);
      return TRUE;
    }
  else if (priv->mode == DRAG_SV)
    {
      gdouble s, v;

      compute_sv (wheel, x, y, &s, &v);
      gimp_color_wheel_set_color (wheel, priv->h, s, v);
      return TRUE;
    }

  g_assert_not_reached ();

  return FALSE;
}


/* Redrawing */

/* Paints the hue ring */
static void
paint_ring (GimpColorWheel *wheel,
            cairo_t        *cr,
            gint            x,
            gint            y,
            gint            width,
            gint            height)
{
  GtkWidget             *widget = GTK_WIDGET (wheel);
  GimpColorWheelPrivate *priv   = wheel->priv;
  GtkAllocation          allocation;
  gint                   xx, yy;
  gdouble                dx, dy, dist;
  gdouble                center_x;
  gdouble                center_y;
  gdouble                inner, outer;
  guint32               *buf, *p;
  gdouble                angle;
  gdouble                hue;
  gdouble                r, g, b;
  cairo_surface_t       *source;
  cairo_t               *source_cr;
  gint                   stride;
  gint                   focus_width;
  gint                   focus_pad;

  gtk_widget_get_allocation (GTK_WIDGET (wheel), &allocation);

  gtk_widget_style_get (widget,
                        "focus-line-width", &focus_width,
                        "focus-padding", &focus_pad,
                        NULL);

  center_x = allocation.width / 2.0;
  center_y = allocation.height / 2.0;

  outer = priv->size / 2.0;
  inner = outer - priv->ring_width;

  /* Create an image initialized with the ring colors */

  stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
  buf = g_new (guint32, height * stride / 4);

  for (yy = 0; yy < height; yy++)
    {
      p = buf + yy * width;

      dy = -(yy + y - center_y);

      for (xx = 0; xx < width; xx++)
        {
          dx = xx + x - center_x;

          dist = dx * dx + dy * dy;
          if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
            {
              *p++ = 0;
              continue;
            }

          angle = atan2 (dy, dx);
          if (angle < 0.0)
            angle += 2.0 * G_PI;

          hue = angle / (2.0 * G_PI);

          r = hue;
          g = 1.0;
          b = 1.0;
          hsv_to_rgb (&r, &g, &b);

          *p++ = (((int)floor (r * 255 + 0.5) << 16) |
                  ((int)floor (g * 255 + 0.5) << 8) |
                  (int)floor (b * 255 + 0.5));
        }
    }

891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
  if (priv->transform)
    {
      const Babl *format = babl_format ("cairo-RGB24");
      guchar     *b      = (guchar *) buf;
      gint        i;

      for (i = 0; i < height; i++)
        {
          gimp_color_transform_process_pixels (priv->transform,
                                               format, b,
                                               format, b,
                                               width);

          b  += stride;
        }
    }

  source = cairo_image_surface_create_for_data ((guchar *) buf,
909 910 911 912 913 914 915 916 917 918 919 920 921
                                                CAIRO_FORMAT_RGB24,
                                                width, height, stride);

  /* Now draw the value marker onto the source image, so that it
   * will get properly clipped at the edges of the ring
   */
  source_cr = cairo_create (source);

  r = priv->h;
  g = 1.0;
  b = 1.0;
  hsv_to_rgb (&r, &g, &b);

922
  if (GIMP_RGB_LUMINANCE (r, g, b) > 0.5)
923 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 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    cairo_set_source_rgb (source_cr, 0., 0., 0.);
  else
    cairo_set_source_rgb (source_cr, 1., 1., 1.);

  cairo_move_to (source_cr, -x + center_x, - y + center_y);
  cairo_line_to (source_cr,
                 -x + center_x + cos (priv->h * 2.0 * G_PI) * priv->size / 2,
                 -y + center_y - sin (priv->h * 2.0 * G_PI) * priv->size / 2);
  cairo_stroke (source_cr);
  cairo_destroy (source_cr);

  /* Draw the ring using the source image */

  cairo_save (cr);

  cairo_set_source_surface (cr, source, x, y);
  cairo_surface_destroy (source);

  cairo_set_line_width (cr, priv->ring_width);
  cairo_new_path (cr);
  cairo_arc (cr,
             center_x, center_y,
             priv->size / 2. - priv->ring_width / 2.,
             0, 2 * G_PI);
  cairo_stroke (cr);

  cairo_restore (cr);

  g_free (buf);
}

/* Converts an HSV triplet to an integer RGB triplet */
static void
get_color (gdouble  h,
           gdouble  s,
           gdouble  v,
           gint    *r,
           gint    *g,
           gint    *b)
{
  hsv_to_rgb (&h, &s, &v);

  *r = floor (h * 255 + 0.5);
  *g = floor (s * 255 + 0.5);
  *b = floor (v * 255 + 0.5);
}

#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))

/* Number of pixels we extend out from the edges when creating
 * color source to avoid artifacts
 */
#define PAD 3

/* Paints the HSV triangle */
static void
paint_triangle (GimpColorWheel *wheel,
                cairo_t        *cr,
                gint            x,
                gint            y,
                gint            width,
                gint            height)
{
  GtkWidget             *widget = GTK_WIDGET (wheel);
  GimpColorWheelPrivate *priv   = wheel->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;
  guint32               *buf, *p, c;
  gint                   xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
  gint                   xx, yy;
  gint                   x_interp, y_interp;
  gint                   x_start, x_end;
  cairo_surface_t       *source;
  gdouble                r, g, b;
  gchar                 *detail;
  gint                   stride;

  /* Compute triangle's vertices */

  compute_triangle (wheel, &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 */

  stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
  buf = g_new (guint32, height * stride / 4);

  for (yy = 0; yy < height; yy++)
    {
      p = buf + yy * width;

      if (yy + y >= y1 - PAD && yy + y < y3 + PAD)
        {
          y_interp = CLAMP (yy + y, y1, y3);

          if (y_interp < y2)
            {
              xl = LERP (x1, x2, y1, y2, y_interp);

              rl = LERP (r1, r2, y1, y2, y_interp);
              gl = LERP (g1, g2, y1, y2, y_interp);
              bl = LERP (b1, b2, y1, y2, y_interp);
            }
          else
            {
              xl = LERP (x2, x3, y2, y3, y_interp);

              rl = LERP (r2, r3, y2, y3, y_interp);
              gl = LERP (g2, g3, y2, y3, y_interp);
              bl = LERP (b2, b3, y2, y3, y_interp);
            }

          xr = LERP (x1, x3, y1, y3, y_interp);

          rr = LERP (r1, r3, y1, y3, y_interp);
          gr = LERP (g1, g3, y1, y3, y_interp);
          br = LERP (b1, b3, y1, y3, y_interp);

          if (xl > xr)
            {
              SWAP (xl, xr, t);
              SWAP (rl, rr, t);
              SWAP (gl, gr, t);
              SWAP (bl, br, t);
            }

          x_start = MAX (xl - PAD, x);
          x_end = MIN (xr + PAD, x + width);
          x_start = MIN (x_start, x_end);

          c = (rl << 16) | (gl << 8) | bl;

          for (xx = x; xx < x_start; xx++)
            *p++ = c;

          for (; xx < x_end; xx++)
            {
              x_interp = CLAMP (xx, xl, xr);

              *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
                      (LERP (gl, gr, xl, xr, x_interp) << 8) |
                      LERP (bl, br, xl, xr, x_interp));
            }

          c = (rr << 16) | (gr << 8) | br;

          for (; xx < x + width; xx++)
            *p++ = c;
        }
    }

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
  if (priv->transform)
    {
      const Babl *format = babl_format ("cairo-RGB24");
      guchar     *b      = (guchar *) buf;
      gint        i;

      for (i = 0; i < height; i++)
        {
          gimp_color_transform_process_pixels (priv->transform,
                                               format, b,
                                               format, b,
                                               width);

          b  += stride;
        }
    }

  source = cairo_image_surface_create_for_data ((guchar *) buf,
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
                                                CAIRO_FORMAT_RGB24,
                                                width, height, stride);

  /* Draw a triangle with the image as a source */

  cairo_set_source_surface (cr, source, x, y);
  cairo_surface_destroy (source);

  cairo_move_to (cr, x1, y1);
  cairo_line_to (cr, x2, y2);
  cairo_line_to (cr, x3, y3);
  cairo_close_path (cr);
  cairo_fill (cr);

  g_free (buf);

  /* Draw value marker */

  xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
  yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);

  r = priv->h;
  g = priv->s;
  b = priv->v;
  hsv_to_rgb (&r, &g, &b);

1163
  if (GIMP_RGB_LUMINANCE (r, g, b) > 0.5)
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 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
    {
      detail = "colorwheel_light";
      cairo_set_source_rgb (cr, 0., 0., 0.);
    }
  else
    {
      detail = "colorwheel_dark";
      cairo_set_source_rgb (cr, 1., 1., 1.);
    }

#define RADIUS 4
#define FOCUS_RADIUS 6

  cairo_new_path (cr);
  cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
  cairo_stroke (cr);

  /* Draw focus outline */

  if (gtk_widget_has_focus (widget) &&
      ! priv->focus_on_ring)
    {
      GtkAllocation allocation;
      gint          focus_width;
      gint          focus_pad;

      gtk_widget_get_allocation (widget, &allocation);

      gtk_widget_style_get (widget,
                            "focus-line-width", &focus_width,
                            "focus-padding", &focus_pad,
                            NULL);

      gtk_paint_focus (gtk_widget_get_style (widget),
                       gtk_widget_get_window (widget),
                       gtk_widget_get_state (widget),
                       NULL, widget, detail,
                       allocation.x + xx - FOCUS_RADIUS - focus_width - focus_pad,
                       allocation.y + yy - FOCUS_RADIUS - focus_width - focus_pad,
                       2 * (FOCUS_RADIUS + focus_width + focus_pad),
                       2 * (FOCUS_RADIUS + focus_width + focus_pad));
    }
}

/* Paints the contents of the HSV color selector */
static void
paint (GimpColorWheel *hsv,
       cairo_t        *cr,
       gint            x,
       gint            y,
       gint            width,
       gint            height)
{
1217 1218 1219 1220 1221
  GimpColorWheelPrivate *priv = hsv->priv;

  if (! priv->transform)
    gimp_color_wheel_create_transform (hsv);

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
  paint_ring (hsv, cr, x, y, width, height);
  paint_triangle (hsv, cr, x, y, width, height);
}

static gint
gimp_color_wheel_expose (GtkWidget      *widget,
                         GdkEventExpose *event)
{
  GimpColorWheel        *wheel = GIMP_COLOR_WHEEL (widget);
  GimpColorWheelPrivate *priv  = wheel->priv;
  GtkAllocation          allocation;
  GdkRectangle           dest;
  cairo_t               *cr;

  if (! (event->window == gtk_widget_get_window (widget) &&
         gtk_widget_is_drawable (widget)))
    return FALSE;

  gtk_widget_get_allocation (widget, &allocation);

  if (!gdk_rectangle_intersect (&event->area, &allocation, &dest))
    return FALSE;

  cr = gdk_cairo_create (gtk_widget_get_window (widget));

  cairo_translate (cr, allocation.x, allocation.y);
  paint (wheel, cr,
         dest.x - allocation.x,
         dest.y - allocation.y,
         dest.width, dest.height);
  cairo_destroy (cr);

  if (gtk_widget_has_focus (widget) && priv->focus_on_ring)
    gtk_paint_focus (gtk_widget_get_style (widget),
                     gtk_widget_get_window (widget),
                     gtk_widget_get_state (widget),
                     &event->area, widget, NULL,
                     allocation.x,
                     allocation.y,
                     allocation.width,
                     allocation.height);

  return FALSE;
}

static gboolean
gimp_color_wheel_focus (GtkWidget        *widget,
                        GtkDirectionType  dir)
{
  GimpColorWheel        *wheel = GIMP_COLOR_WHEEL (widget);
  GimpColorWheelPrivate *priv  = wheel->priv;

  if (!gtk_widget_has_focus (widget))
    {
      if (dir == GTK_DIR_TAB_BACKWARD)
        priv->focus_on_ring = FALSE;
      else
        priv->focus_on_ring = TRUE;

      gtk_widget_grab_focus (widget);
      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 (widget);

  return TRUE;
}

/**
 * gimp_color_wheel_new:
 *
 * Creates a new HSV color selector.
 *
 * Return value: A newly-created HSV color selector.
 *
1330
 * Since: 2.10
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
 */
GtkWidget*
gimp_color_wheel_new (void)
{
  return g_object_new (GIMP_TYPE_COLOR_WHEEL, NULL);
}

/**
 * gimp_color_wheel_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.
 *
1348
 * Since: 2.10
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 1378 1379 1380 1381 1382 1383
 */
void
gimp_color_wheel_set_color (GimpColorWheel *wheel,
                            gdouble         h,
                            gdouble         s,
                            gdouble         v)
{
  GimpColorWheelPrivate *priv;

  g_return_if_fail (GIMP_IS_COLOR_WHEEL (wheel));
  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 = wheel->priv;

  priv->h = h;
  priv->s = s;
  priv->v = v;

  g_signal_emit (wheel, wheel_signals[CHANGED], 0);

  gtk_widget_queue_draw (GTK_WIDGET (wheel));
}

/**
 * gimp_color_wheel_get_color:
 * @hsv: An HSV color selector
 * @h: (out): Return value for the hue
 * @s: (out): Return value for the saturation
 * @v: (out): 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.
 *
1384
 * Since: 2.10
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
 */
void
gimp_color_wheel_get_color (GimpColorWheel *wheel,
                            gdouble        *h,
                            gdouble        *s,
                            gdouble        *v)
{
  GimpColorWheelPrivate *priv;

  g_return_if_fail (GIMP_IS_COLOR_WHEEL (wheel));

  priv = wheel->priv;

  if (h) *h = priv->h;
  if (s) *s = priv->s;
  if (v) *v = priv->v;
}

/**
 * gimp_color_wheel_set_ring_fraction:
 * @ring: A wheel color selector
 * @fraction: Ring fraction
 *
 * Sets the ring fraction of a wheel color selector.
 *
1410
 * Since: 2.10
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
 */
void
gimp_color_wheel_set_ring_fraction (GimpColorWheel *hsv,
                                    gdouble         fraction)
{
  GimpColorWheelPrivate *priv;

  g_return_if_fail (GIMP_IS_COLOR_WHEEL (hsv));

  priv = hsv->priv;

  priv->ring_fraction = CLAMP (fraction, 0.01, 0.99);

  gtk_widget_queue_draw (GTK_WIDGET (hsv));
}

/**
 * gimp_color_wheel_get_ring_fraction:
 * @ring: A wheel color selector
 *
 * Returns value: The ring fraction of the wheel color selector.
 *
1433
 * Since: 2.10
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
 */
gdouble
gimp_color_wheel_get_ring_fraction (GimpColorWheel *wheel)
{
  GimpColorWheelPrivate *priv;

  g_return_val_if_fail (GIMP_IS_COLOR_WHEEL (wheel), DEFAULT_FRACTION);

  priv = wheel->priv;

  return priv->ring_fraction;
}

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
/**
 * gimp_color_wheel_set_color_config:
 * @wheel:  a #GimpColorWheel widget.
 * @config: a #GimpColorConfig object.
 *
 * Sets the color management configuration to use with this color wheel.
 *
 * Since: 2.10
 */
void
gimp_color_wheel_set_color_config (GimpColorWheel  *wheel,
                                   GimpColorConfig *config)
{
  GimpColorWheelPrivate *priv;

  g_return_if_fail (GIMP_IS_COLOR_WHEEL (wheel));
  g_return_if_fail (config == NULL || GIMP_IS_COLOR_CONFIG (config));

  priv = wheel->priv;

1467
  if (config != priv->config)
1468
    {
1469
      if (priv->config)
1470
        {
1471
          g_signal_handlers_disconnect_by_func (priv->config,
1472
                                                gimp_color_wheel_destroy_transform,
1473 1474 1475
                                                wheel);
          g_object_unref (priv->config);

1476
          gimp_color_wheel_destroy_transform (wheel);
1477 1478
        }

1479
      priv->config = config;
1480

1481 1482 1483
      if (priv->config)
        {
          g_object_ref (priv->config);
1484

1485 1486 1487
          g_signal_connect_swapped (priv->config, "notify",
                                    G_CALLBACK (gimp_color_wheel_destroy_transform),
                                    wheel);
1488
        }
1489 1490 1491
    }
}

1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
/**
 * gimp_color_wheel_is_adjusting:
 * @hsv: A #GimpColorWheel
 *
 * 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.
 *
1505
 * Since: 2.10
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
 */
gboolean
gimp_color_wheel_is_adjusting (GimpColorWheel *wheel)
{
  GimpColorWheelPrivate *priv;

  g_return_val_if_fail (GIMP_IS_COLOR_WHEEL (wheel), FALSE);

  priv = wheel->priv;

  return priv->mode != DRAG_NONE;
}

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

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

  compute_triangle (wheel, &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);

#define HUE_DELTA 0.002
  switch (dir)
    {
    case GTK_DIR_UP:
      if (priv->focus_on_ring)
        hue += HUE_DELTA;
      else
        {
          y -= 1;
          compute_sv (wheel, x, y, &sat, &val);
        }
      break;

    case GTK_DIR_DOWN:
      if (priv->focus_on_ring)
        hue -= HUE_DELTA;
      else
        {
          y += 1;
          compute_sv (wheel, x, y, &sat, &val);
        }
      break;

    case GTK_DIR_LEFT:
      if (priv->focus_on_ring)
        hue += HUE_DELTA;
      else
        {
          x -= 1;
          compute_sv (wheel, x, y, &sat, &val);
        }
      break;

    case GTK_DIR_RIGHT:
      if (priv->focus_on_ring)
        hue -= HUE_DELTA
          ;
      else
        {
          x += 1;
          compute_sv (wheel, 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;

  gimp_color_wheel_set_color (wheel, hue, sat, val);
}
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615

static void
gimp_color_wheel_create_transform (GimpColorWheel *wheel)
{
  GimpColorWheelPrivate *priv = wheel->priv;

  if (priv->config)
    {
      static GimpColorProfile *profile = NULL;

      const Babl *format = babl_format ("cairo-RGB24");

      if (G_UNLIKELY (! profile))
        profile = gimp_color_profile_new_rgb_srgb ();

      priv->transform = gimp_widget_get_color_transform (GTK_WIDGET (wheel),
                                                         priv->config,
                                                         profile,
                                                         format,
                                                         format);
    }
}
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629

static void
gimp_color_wheel_destroy_transform (GimpColorWheel *wheel)
{
  GimpColorWheelPrivate *priv = wheel->priv;

  if (priv->transform)
    {
      g_object_unref (priv->transform);
      priv->transform = NULL;
    }

  gtk_widget_queue_draw (GTK_WIDGET (wheel));
}