gimpcolorscale.c 29.6 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 4
 *
 * gimpcolorscale.c
5 6
 * Copyright (C) 2002-2010  Sven Neumann <sven@gimp.org>
 *                          Michael Natterer <mitch@gimp.org>
7
 *
8
 * This library is free software: you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or (at your option) any later version.
12 13 14 15
 *
 * 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
16 17 18
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library.  If not, see
20
 * <https://www.gnu.org/licenses/>.
21 22 23 24
 */

#include "config.h"

25 26
#include <string.h>

27
#include <gegl.h>
28 29 30
#include <gtk/gtk.h>

#include "libgimpbase/gimpbase.h"
31 32
#include "libgimpconfig/gimpconfig.h"
#include "libgimpcolor/gimpcolor.h"
33 34 35

#include "gimpwidgetstypes.h"

36
#include "gimpcairo-utils.h"
37
#include "gimpcolorscale.h"
38
#include "gimpwidgetsutils.h"
39 40


41 42 43 44 45 46 47 48 49
/**
 * SECTION: gimpcolorscale
 * @title: GimpColorScale
 * @short_description: Fancy colored sliders.
 *
 * Fancy colored sliders.
 **/


50 51 52 53 54 55 56
enum
{
  PROP_0,
  PROP_CHANNEL
};


57 58 59 60 61 62 63 64
typedef struct _GimpLCH  GimpLCH;

struct _GimpLCH
{
  gdouble l, c, h, a;
};


65 66
struct _GimpColorScalePrivate
{
67 68 69 70 71 72 73 74 75 76 77 78 79 80
  GimpColorConfig          *config;
  GimpColorTransform       *transform;
  guchar                    oog_color[3];

  GimpColorSelectorChannel  channel;
  GimpRGB                   rgb;
  GimpHSV                   hsv;

  guchar                   *buf;
  guint                     width;
  guint                     height;
  guint                     rowstride;

  gboolean                  needs_render;
81 82
};

83
#define GET_PRIVATE(obj) (((GimpColorScale *) (obj))->priv)
84 85


86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
static void     gimp_color_scale_dispose             (GObject          *object);
static void     gimp_color_scale_finalize            (GObject          *object);
static void     gimp_color_scale_get_property        (GObject          *object,
                                                      guint             property_id,
                                                      GValue           *value,
                                                      GParamSpec       *pspec);
static void     gimp_color_scale_set_property        (GObject          *object,
                                                      guint             property_id,
                                                      const GValue     *value,
                                                      GParamSpec       *pspec);

static void     gimp_color_scale_size_allocate       (GtkWidget        *widget,
                                                      GtkAllocation    *allocation);
static gboolean gimp_color_scale_draw                (GtkWidget        *widget,
                                                      cairo_t          *cr);

static void     gimp_color_scale_render              (GimpColorScale   *scale);
static void     gimp_color_scale_render_alpha        (GimpColorScale   *scale);

static void     gimp_color_scale_create_transform    (GimpColorScale   *scale);
static void     gimp_color_scale_destroy_transform   (GimpColorScale   *scale);
static void     gimp_color_scale_notify_config       (GimpColorConfig  *config,
                                                      const GParamSpec *pspec,
                                                      GimpColorScale   *scale);
110 111


112
G_DEFINE_TYPE_WITH_PRIVATE (GimpColorScale, gimp_color_scale, GTK_TYPE_SCALE)
113

114
#define parent_class gimp_color_scale_parent_class
115

116 117 118
static const Babl *fish_rgb_to_lch = NULL;
static const Babl *fish_lch_to_rgb = NULL;

119 120 121 122

static void
gimp_color_scale_class_init (GimpColorScaleClass *klass)
{
123
  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
124
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
125

126 127 128 129
  object_class->dispose       = gimp_color_scale_dispose;
  object_class->finalize      = gimp_color_scale_finalize;
  object_class->get_property  = gimp_color_scale_get_property;
  object_class->set_property  = gimp_color_scale_set_property;
130

131 132
  widget_class->size_allocate = gimp_color_scale_size_allocate;
  widget_class->draw          = gimp_color_scale_draw;
133 134 135 136 137 138

  /**
   * GimpColorScale:channel:
   *
   * The channel which is edited by the color scale.
   *
139
   * Since: 2.8
140 141
   */
  g_object_class_install_property (object_class, PROP_CHANNEL,
142 143 144
                                   g_param_spec_enum ("channel",
                                                      "Channel",
                                                      "The channel which is edited by the color scale",
145 146 147 148
                                                      GIMP_TYPE_COLOR_SELECTOR_CHANNEL,
                                                      GIMP_COLOR_SELECTOR_VALUE,
                                                      GIMP_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT));
149

150 151
  gtk_widget_class_set_css_name (widget_class, "GimpColorScale");

152 153 154 155
  fish_rgb_to_lch = babl_fish (babl_format ("R'G'B'A double"),
                               babl_format ("CIE LCH(ab) double"));
  fish_lch_to_rgb = babl_fish (babl_format ("CIE LCH(ab) double"),
                               babl_format ("R'G'B' double"));
156 157
}

158
static void
159
gimp_color_scale_init (GimpColorScale *scale)
160
{
161
  GimpColorScalePrivate *priv;
162
  GtkRange              *range = GTK_RANGE (scale);
163 164
  GtkCssProvider        *css;

165
  scale->priv = gimp_color_scale_get_instance_private (scale);
166 167 168

  priv = scale->priv;

169
  gtk_widget_set_can_focus (GTK_WIDGET (scale), TRUE);
Sven Neumann's avatar
Sven Neumann committed
170

171
  gtk_range_set_slider_size_fixed (range, TRUE);
172
  gtk_range_set_flippable (GTK_RANGE (scale), TRUE);
173
  gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
174

175 176
  priv->channel      = GIMP_COLOR_SELECTOR_VALUE;
  priv->needs_render = TRUE;
177

178 179 180
  gtk_orientable_set_orientation (GTK_ORIENTABLE (range),
                                  GTK_ORIENTATION_HORIZONTAL);

181 182
  gimp_rgba_set (&priv->rgb, 0.0, 0.0, 0.0, 1.0);
  gimp_rgb_to_hsv (&priv->rgb, &priv->hsv);
183 184 185 186

  gimp_widget_track_monitor (GTK_WIDGET (scale),
                             G_CALLBACK (gimp_color_scale_destroy_transform),
                             NULL);
187 188 189

  css = gtk_css_provider_new ();
  gtk_css_provider_load_from_data (css,
190 191
                                   "GimpColorScale {"
                                   "  padding:    2px 12px 2px 12px;"
192 193 194
                                   "  min-width:  24px;"
                                   "  min-height: 24px;"
                                   "}\n"
195 196 197 198
                                   "GimpColorScale contents trough {"
                                   "  min-width:  20px;"
                                   "  min-height: 20px;"
                                   "}\n"
199
                                   "GimpColorScale contents trough slider {"
200 201 202
                                   "  min-width:  12px;"
                                   "  min-height: 12px;"
                                   "  margin:     -6px -6px -6px -6px;"
203 204 205 206 207 208
                                   "}",
                                   -1, NULL);
  gtk_style_context_add_provider (gtk_widget_get_style_context (GTK_WIDGET (scale)),
                                  GTK_STYLE_PROVIDER (css),
                                  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  g_object_unref (css);
209 210 211
}

static void
212
gimp_color_scale_dispose (GObject *object)
213
{
214
  GimpColorScale *scale = GIMP_COLOR_SCALE (object);
215

216 217 218 219 220 221 222 223 224 225 226 227 228 229
  gimp_color_scale_set_color_config (scale, NULL);

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

static void
gimp_color_scale_finalize (GObject *object)
{
  GimpColorScalePrivate *priv = GET_PRIVATE (object);

  g_clear_pointer (&priv->buf, g_free);
  priv->width     = 0;
  priv->height    = 0;
  priv->rowstride = 0;
230

231
  G_OBJECT_CLASS (parent_class)->finalize (object);
232 233
}

234 235 236 237 238 239
static void
gimp_color_scale_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
240
  GimpColorScalePrivate *priv = GET_PRIVATE (object);
241 242 243 244

  switch (property_id)
    {
    case PROP_CHANNEL:
245
      g_value_set_enum (value, priv->channel);
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_color_scale_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  GimpColorScale *scale = GIMP_COLOR_SCALE (object);

  switch (property_id)
    {
    case PROP_CHANNEL:
      gimp_color_scale_set_channel (scale, g_value_get_enum (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

274 275
static void
gimp_color_scale_size_allocate (GtkWidget     *widget,
276
                                GtkAllocation *allocation)
277
{
278 279 280
  GimpColorScalePrivate *priv  = GET_PRIVATE (widget);
  GtkRange              *range = GTK_RANGE (widget);
  GdkRectangle           range_rect;
281

282
  GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
283

284
  gtk_range_get_range_rect (range, &range_rect);
285

286 287
  if (range_rect.width  != priv->width ||
      range_rect.height != priv->height)
288
    {
289 290
      priv->width  = range_rect.width;
      priv->height = range_rect.height;
291

292
      priv->rowstride = priv->width * 4;
293

294 295
      g_free (priv->buf);
      priv->buf = g_new (guchar, priv->rowstride * priv->height);
296

297
      priv->needs_render = TRUE;
298
    }
299 300 301
}

static gboolean
302 303
gimp_color_scale_draw (GtkWidget *widget,
                       cairo_t   *cr)
304
{
305 306 307
  GimpColorScale        *scale     = GIMP_COLOR_SCALE (widget);
  GimpColorScalePrivate *priv      = GET_PRIVATE (widget);
  GtkRange              *range     = GTK_RANGE (widget);
308
  GtkStyleContext       *context   = gtk_widget_get_style_context (widget);
309 310 311 312
  GdkRectangle           range_rect;
  GdkRectangle           area      = { 0, };
  cairo_surface_t       *buffer;
  gint                   slider_start;
313
  gint                   slider_end;
314
  gint                   slider_mid;
315
  gint                   slider_size;
316

317
  if (! priv->buf)
318 319
    return FALSE;

320
  gtk_range_get_range_rect (range, &range_rect);
321
  gtk_range_get_slider_range (range, &slider_start, &slider_end);
Sven Neumann's avatar
Sven Neumann committed
322

323 324
  slider_mid = slider_start + (slider_end - slider_start) / 2;
  slider_size = 6;
Sven Neumann's avatar
Sven Neumann committed
325

326
  if (priv->needs_render)
327 328 329
    {
      gimp_color_scale_render (scale);

330
      priv->needs_render = FALSE;
331
    }
Sven Neumann's avatar
Sven Neumann committed
332

333 334 335 336 337 338
  if (! priv->transform)
    gimp_color_scale_create_transform (scale);

  if (priv->transform)
    {
      const Babl *format = babl_format ("cairo-RGB24");
339 340
      guchar     *buf    = g_new (guchar, priv->rowstride * priv->height);
      guchar     *src    = priv->buf;
341 342 343
      guchar     *dest   = buf;
      gint        i;

344
      for (i = 0; i < priv->height; i++)
345 346 347 348
        {
          gimp_color_transform_process_pixels (priv->transform,
                                               format, src,
                                               format, dest,
349
                                               priv->width);
350

351 352
          src  += priv->rowstride;
          dest += priv->rowstride;
353 354 355 356
        }

      buffer = cairo_image_surface_create_for_data (buf,
                                                    CAIRO_FORMAT_RGB24,
357 358 359
                                                    priv->width,
                                                    priv->height,
                                                    priv->rowstride);
360 361 362 363 364
      cairo_surface_set_user_data (buffer, NULL,
                                   buf, (cairo_destroy_func_t) g_free);
    }
  else
    {
365
      buffer = cairo_image_surface_create_for_data (priv->buf,
366
                                                    CAIRO_FORMAT_RGB24,
367 368 369
                                                    priv->width,
                                                    priv->height,
                                                    priv->rowstride);
370
    }
371 372

  switch (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)))
373
    {
374 375
    case GTK_ORIENTATION_HORIZONTAL:
      cairo_set_source_surface (cr, buffer,
376
                                range_rect.x, range_rect.y);
377 378 379 380
      break;

    case GTK_ORIENTATION_VERTICAL:
      cairo_set_source_surface (cr, buffer,
381
                                range_rect.x, range_rect.y);
382
      break;
383 384
    }

385
  cairo_surface_destroy (buffer);
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414

  if (! gtk_widget_is_sensitive (widget))
    {
      static cairo_pattern_t *pattern = NULL;

      if (! pattern)
        {
          static const guchar  stipple[] = { 0,   255, 0, 0,
                                             255, 0,   0, 0 };
          cairo_surface_t     *surface;
          gint                 stride;

          stride = cairo_format_stride_for_width (CAIRO_FORMAT_A8, 2);

          surface = cairo_image_surface_create_for_data ((guchar *) stipple,
                                                         CAIRO_FORMAT_A8,
                                                         2, 2, stride);
          pattern = cairo_pattern_create_for_surface (surface);
          cairo_surface_destroy (surface);

          cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
        }

      cairo_mask (cr, pattern);
    }
  else
    {
      cairo_paint (cr);
    }
415

416
  if (gtk_widget_has_focus (widget))
417
    gtk_render_focus (context, cr,
418 419 420
                      0, 0,
                      gtk_widget_get_allocated_width  (widget),
                      gtk_widget_get_allocated_height (widget));
421

422
  switch (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)))
423 424
    {
    case GTK_ORIENTATION_HORIZONTAL:
425 426 427 428
      area.x      = slider_mid - slider_size;
      area.y      = range_rect.y;
      area.width  = 2 * slider_size;
      area.height = range_rect.height;
429 430 431
      break;

    case GTK_ORIENTATION_VERTICAL:
432 433 434 435
      area.x      = range_rect.x;
      area.y      = slider_mid - slider_size;
      area.width  = range_rect.width;
      area.height = 2 * slider_size;
436 437 438
      break;
    }

439
  if (gtk_widget_is_sensitive (widget))
440
    cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
441
  else
442
    cairo_set_source_rgb (cr, 0.2, 0.2, 0.2);
443 444

  switch (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)))
445
    {
446 447 448 449 450
    case GTK_ORIENTATION_HORIZONTAL:
      cairo_move_to (cr, area.x, area.y);
      cairo_line_to (cr, area.x + area.width, area.y);
      cairo_line_to (cr,
                     area.x + area.width / 2 + 0.5,
451
                     area.y + slider_size);
452 453 454 455 456 457
      break;

    case GTK_ORIENTATION_VERTICAL:
      cairo_move_to (cr, area.x, area.y);
      cairo_line_to (cr, area.x, area.y + area.height);
      cairo_line_to (cr,
458
                     area.x + slider_size,
459 460
                     area.y + area.height / 2 + 0.5);
      break;
461
    }
462

463 464 465 466
  cairo_close_path (cr);
  cairo_fill (cr);

  if (gtk_widget_is_sensitive (widget))
467
    cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
468
  else
469
    cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
470 471 472 473 474 475 476 477

  switch (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)))
    {
    case GTK_ORIENTATION_HORIZONTAL:
      cairo_move_to (cr, area.x, area.y + area.height);
      cairo_line_to (cr, area.x + area.width, area.y + area.height);
      cairo_line_to (cr,
                     area.x + area.width / 2 + 0.5,
478
                     area.y + area.height - slider_size);
479 480 481 482 483 484
      break;

    case GTK_ORIENTATION_VERTICAL:
      cairo_move_to (cr, area.x + area.width, area.y);
      cairo_line_to (cr, area.x + area.width, area.y + area.height);
      cairo_line_to (cr,
485
                     area.x + area.width - slider_size,
486 487 488 489 490 491 492
                     area.y + area.height / 2 + 0.5);
      break;
    }

  cairo_close_path (cr);
  cairo_fill (cr);

493 494 495
  return FALSE;
}

496 497 498 499
/**
 * gimp_color_scale_new:
 * @orientation: the scale's orientation (horizontal or vertical)
 * @channel: the scale's color channel
500
 *
501
 * Creates a new #GimpColorScale widget.
502
 *
503 504
 * Return value: a new #GimpColorScale widget
 **/
505
GtkWidget *
506
gimp_color_scale_new (GtkOrientation            orientation,
507
                      GimpColorSelectorChannel  channel)
508
{
509 510
  GimpColorScale *scale = g_object_new (GIMP_TYPE_COLOR_SCALE,
                                        "orientation", orientation,
511
                                        "channel",     channel,
512
                                        NULL);
513

514 515
  gtk_range_set_flippable (GTK_RANGE (scale),
                           orientation == GTK_ORIENTATION_HORIZONTAL);
516

Sven Neumann's avatar
Sven Neumann committed
517
  return GTK_WIDGET (scale);
518 519
}

520 521 522 523
/**
 * gimp_color_scale_set_channel:
 * @scale: a #GimpColorScale widget
 * @channel: the new color channel
524
 *
525 526
 * Changes the color channel displayed by the @scale.
 **/
527 528 529 530
void
gimp_color_scale_set_channel (GimpColorScale           *scale,
                              GimpColorSelectorChannel  channel)
{
531 532
  GimpColorScalePrivate *priv;

533 534
  g_return_if_fail (GIMP_IS_COLOR_SCALE (scale));

535 536 537
  priv = GET_PRIVATE (scale);

  if (channel != priv->channel)
538
    {
539
      priv->channel = channel;
540

541
      priv->needs_render = TRUE;
542
      gtk_widget_queue_draw (GTK_WIDGET (scale));
543 544

      g_object_notify (G_OBJECT (scale), "channel");
545 546 547
    }
}

548 549 550 551 552
/**
 * gimp_color_scale_set_color:
 * @scale: a #GimpColorScale widget
 * @rgb: the new color as #GimpRGB
 * @hsv: the new color as #GimpHSV
553
 *
554 555
 * Changes the color value of the @scale.
 **/
556 557 558 559
void
gimp_color_scale_set_color (GimpColorScale *scale,
                            const GimpRGB  *rgb,
                            const GimpHSV  *hsv)
560
{
561 562
  GimpColorScalePrivate *priv;

563 564 565
  g_return_if_fail (GIMP_IS_COLOR_SCALE (scale));
  g_return_if_fail (rgb != NULL);
  g_return_if_fail (hsv != NULL);
566

567 568 569 570
  priv = GET_PRIVATE (scale);

  priv->rgb = *rgb;
  priv->hsv = *hsv;
571

572
  priv->needs_render = TRUE;
573
  gtk_widget_queue_draw (GTK_WIDGET (scale));
574 575
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
/**
 * gimp_color_scale_set_color_config:
 * @scale:  a #GimpColorScale widget.
 * @config: a #GimpColorConfig object.
 *
 * Sets the color management configuration to use with this color scale.
 *
 * Since: 2.10
 */
void
gimp_color_scale_set_color_config (GimpColorScale  *scale,
                                   GimpColorConfig *config)
{
  GimpColorScalePrivate *priv;

  g_return_if_fail (GIMP_IS_COLOR_SCALE (scale));
  g_return_if_fail (config == NULL || GIMP_IS_COLOR_CONFIG (config));

  priv = GET_PRIVATE (scale);

596
  if (config != priv->config)
597
    {
598
      if (priv->config)
599
        {
600
          g_signal_handlers_disconnect_by_func (priv->config,
601
                                                gimp_color_scale_notify_config,
602 603
                                                scale);

604
          gimp_color_scale_destroy_transform (scale);
605 606
        }

607
      g_set_object (&priv->config, config);
608

609 610
      if (priv->config)
        {
611 612 613 614 615
          g_signal_connect (priv->config, "notify",
                            G_CALLBACK (gimp_color_scale_notify_config),
                            scale);

          gimp_color_scale_notify_config (priv->config, NULL, scale);
616
        }
617 618 619 620
    }
}


621 622 623
/* as in gtkrange.c */
static gboolean
should_invert (GtkRange *range)
624
{
625 626 627
  gboolean inverted  = gtk_range_get_inverted (range);
  gboolean flippable = gtk_range_get_flippable (range);

628 629
  if (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)) ==
      GTK_ORIENTATION_HORIZONTAL)
630 631 632 633 634 635 636 637
    {
      return
        (inverted && !flippable) ||
        (inverted && flippable &&
         gtk_widget_get_direction (GTK_WIDGET (range)) == GTK_TEXT_DIR_LTR) ||
        (!inverted && flippable &&
         gtk_widget_get_direction (GTK_WIDGET (range)) == GTK_TEXT_DIR_RTL);
    }
638
  else
639 640 641
    {
      return inverted;
    }
642 643
}

644 645 646
static void
gimp_color_scale_render (GimpColorScale *scale)
{
647 648 649 650 651 652 653 654 655 656 657 658 659
  GimpColorScalePrivate *priv  = GET_PRIVATE (scale);
  GtkRange              *range = GTK_RANGE (scale);
  GimpRGB                rgb;
  GimpHSV                hsv;
  GimpLCH                lch;
  gint                   multiplier = 1;
  guint                  x, y;
  gdouble               *channel_value = NULL; /* shut up compiler */
  gboolean               from_hsv      = FALSE;
  gboolean               from_lch      = FALSE;
  gboolean               invert;
  guchar                *buf;
  guchar                *d;
660

661
  if ((buf = priv->buf) == NULL)
662 663
    return;

664
  if (priv->channel == GIMP_COLOR_SELECTOR_ALPHA)
665 666 667 668 669
    {
      gimp_color_scale_render_alpha (scale);
      return;
    }

670 671
  rgb = priv->rgb;
  hsv = priv->hsv;
672
  babl_process (fish_rgb_to_lch, &rgb, &lch, 1);
673

674
  switch (priv->channel)
675 676 677 678
    {
    case GIMP_COLOR_SELECTOR_HUE:        channel_value = &hsv.h; break;
    case GIMP_COLOR_SELECTOR_SATURATION: channel_value = &hsv.s; break;
    case GIMP_COLOR_SELECTOR_VALUE:      channel_value = &hsv.v; break;
679

680 681 682 683
    case GIMP_COLOR_SELECTOR_RED:        channel_value = &rgb.r; break;
    case GIMP_COLOR_SELECTOR_GREEN:      channel_value = &rgb.g; break;
    case GIMP_COLOR_SELECTOR_BLUE:       channel_value = &rgb.b; break;
    case GIMP_COLOR_SELECTOR_ALPHA:      channel_value = &rgb.a; break;
684 685 686 687

    case GIMP_COLOR_SELECTOR_LCH_LIGHTNESS: channel_value = &lch.l; break;
    case GIMP_COLOR_SELECTOR_LCH_CHROMA:    channel_value = &lch.c; break;
    case GIMP_COLOR_SELECTOR_LCH_HUE:       channel_value = &lch.h; break;
688 689
    }

690
  switch (priv->channel)
691 692 693 694
    {
    case GIMP_COLOR_SELECTOR_HUE:
    case GIMP_COLOR_SELECTOR_SATURATION:
    case GIMP_COLOR_SELECTOR_VALUE:
695 696 697 698 699 700 701 702
      from_hsv = TRUE;
      break;

    case GIMP_COLOR_SELECTOR_LCH_LIGHTNESS:
      multiplier = 100;
      from_lch = TRUE;
      break;
    case GIMP_COLOR_SELECTOR_LCH_CHROMA:
703
      multiplier = 200;
704 705 706 707 708
      from_lch = TRUE;
      break;
    case GIMP_COLOR_SELECTOR_LCH_HUE:
      multiplier = 360;
      from_lch = TRUE;
709 710 711 712 713 714
      break;

    default:
      break;
    }

715 716
  invert = should_invert (range);

717
  switch (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)))
718
    {
719
    case GTK_ORIENTATION_HORIZONTAL:
720
      for (x = 0, d = buf; x < priv->width; x++, d += 4)
721
        {
722
          gdouble value = (gdouble) x * multiplier / (gdouble) (priv->width - 1);
723
          guchar  r, g, b;
724 725

          if (invert)
726
            value = multiplier - value;
727 728

          *channel_value = value;
729

730
          if (from_hsv)
731
            gimp_hsv_to_rgb (&hsv, &rgb);
732 733
          else if (from_lch)
            babl_process (fish_lch_to_rgb, &lch, &rgb, 1);
734

735 736 737 738 739 740 741 742 743 744 745 746
          if (rgb.r < 0.0 || rgb.r > 1.0 ||
              rgb.g < 0.0 || rgb.g > 1.0 ||
              rgb.b < 0.0 || rgb.b > 1.0)
            {
              r = priv->oog_color[0];
              g = priv->oog_color[1];
              b = priv->oog_color[2];
            }
          else
            {
              gimp_rgb_get_uchar (&rgb, &r, &g, &b);
            }
747 748

          GIMP_CAIRO_RGB24_SET_PIXEL (d, r, g, b);
749 750
        }

751 752
      d = buf + priv->rowstride;
      for (y = 1; y < priv->height; y++)
753
        {
754 755
          memcpy (d, buf, priv->rowstride);
          d += priv->rowstride;
756
        }
757
      break;
758

759
    case GTK_ORIENTATION_VERTICAL:
760
      for (y = 0; y < priv->height; y++)
761
        {
762
          gdouble value = (gdouble) y * multiplier / (gdouble) (priv->height - 1);
763
          guchar  r, g, b;
764 765

          if (invert)
766
            value = multiplier - value;
767 768

          *channel_value = value;
769

770
          if (from_hsv)
771
            gimp_hsv_to_rgb (&hsv, &rgb);
772 773
          else if (from_lch)
            babl_process (fish_lch_to_rgb, &lch, &rgb, 1);
774

775 776 777 778 779 780 781 782 783 784 785 786
          if (rgb.r < 0.0 || rgb.r > 1.0 ||
              rgb.g < 0.0 || rgb.g > 1.0 ||
              rgb.b < 0.0 || rgb.b > 1.0)
            {
              r = priv->oog_color[0];
              g = priv->oog_color[1];
              b = priv->oog_color[2];
            }
          else
            {
              gimp_rgb_get_uchar (&rgb, &r, &g, &b);
            }
787

788
          for (x = 0, d = buf; x < priv->width; x++, d += 4)
789
            {
790
              GIMP_CAIRO_RGB24_SET_PIXEL (d, r, g, b);
791 792
            }

793
          buf += priv->rowstride;
794
        }
795 796 797 798 799 800 801
      break;
    }
}

static void
gimp_color_scale_render_alpha (GimpColorScale *scale)
{
802 803 804 805 806 807 808 809
  GimpColorScalePrivate *priv  = GET_PRIVATE (scale);
  GtkRange              *range = GTK_RANGE (scale);
  GimpRGB                rgb;
  gboolean               invert;
  gdouble                a;
  guint                  x, y;
  guchar                *buf;
  guchar                *d, *l;
810 811

  invert = should_invert (range);
812

813 814
  buf = priv->buf;
  rgb = priv->rgb;
815

816
  switch (gtk_orientable_get_orientation (GTK_ORIENTABLE (range)))
817 818 819 820 821 822 823 824
    {
    case GTK_ORIENTATION_HORIZONTAL:
      {
        guchar  *light;
        guchar  *dark;

        light = buf;
        /* this won't work correctly for very thin scales */
825 826
        dark  = (priv->height > GIMP_CHECK_SIZE_SM ?
                 buf + GIMP_CHECK_SIZE_SM * priv->rowstride : light);
827

828
        for (x = 0, d = light, l = dark; x < priv->width; x++)
829 830 831 832
          {
            if ((x % GIMP_CHECK_SIZE_SM) == 0)
              {
                guchar *t;
833

834 835 836 837
                t = d;
                d = l;
                l = t;
              }
838

839
            a = (gdouble) x / (gdouble) (priv->width - 1);
840 841 842

            if (invert)
              a = 1.0 - a;
843

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
            GIMP_CAIRO_RGB24_SET_PIXEL (l,
                                        (GIMP_CHECK_LIGHT +
                                         (rgb.r - GIMP_CHECK_LIGHT) * a) * 255.999,
                                        (GIMP_CHECK_LIGHT +
                                         (rgb.g - GIMP_CHECK_LIGHT) * a) * 255.999,
                                        (GIMP_CHECK_LIGHT +
                                         (rgb.b - GIMP_CHECK_LIGHT) * a) * 255.999);
            l += 4;

            GIMP_CAIRO_RGB24_SET_PIXEL (d,
                                        (GIMP_CHECK_DARK +
                                         (rgb.r - GIMP_CHECK_DARK) * a) * 255.999,
                                        (GIMP_CHECK_DARK +
                                         (rgb.g - GIMP_CHECK_DARK) * a) * 255.999,
                                        (GIMP_CHECK_DARK +
                                         (rgb.b - GIMP_CHECK_DARK) * a) * 255.999);
            d += 4;
861 862
          }

863
        for (y = 0, d = buf; y < priv->height; y++, d += priv->rowstride)
864 865 866
          {
            if (y == 0 || y == GIMP_CHECK_SIZE_SM)
              continue;
867

868
            if ((y / GIMP_CHECK_SIZE_SM) & 1)
869
              memcpy (d, dark, priv->rowstride);
870
            else
871
              memcpy (d, light, priv->rowstride);
872 873 874
          }
      }
      break;
875

876 877
    case GTK_ORIENTATION_VERTICAL:
      {
878 879
        guchar  light[4] = {0xff, 0xff, 0xff, 0xff};
        guchar  dark[4] = {0xff, 0xff, 0xff, 0xff};
880

881
        for (y = 0, d = buf; y < priv->height; y++, d += priv->rowstride)
882
          {
883
            a = (gdouble) y / (gdouble) (priv->height - 1);
884

885 886 887
            if (invert)
              a = 1.0 - a;

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
            GIMP_CAIRO_RGB24_SET_PIXEL (light,
                                        (GIMP_CHECK_LIGHT +
                                         (rgb.r - GIMP_CHECK_LIGHT) * a) * 255.999,
                                        (GIMP_CHECK_LIGHT +
                                         (rgb.g - GIMP_CHECK_LIGHT) * a) * 255.999,
                                        (GIMP_CHECK_LIGHT +
                                         (rgb.b - GIMP_CHECK_LIGHT) * a) * 255.999);

            GIMP_CAIRO_RGB24_SET_PIXEL (dark,
                                        (GIMP_CHECK_DARK +
                                         (rgb.r - GIMP_CHECK_DARK) * a) * 255.999,
                                        (GIMP_CHECK_DARK +
                                         (rgb.g - GIMP_CHECK_DARK) * a) * 255.999,
                                        (GIMP_CHECK_DARK +
                                         (rgb.b - GIMP_CHECK_DARK) * a) * 255.999);

904
            for (x = 0, l = d; x < priv->width; x++, l += 4)
905 906 907 908 909 910
              {
                if (((x / GIMP_CHECK_SIZE_SM) ^ (y / GIMP_CHECK_SIZE_SM)) & 1)
                  {
                    l[0] = light[0];
                    l[1] = light[1];
                    l[2] = light[2];
911
                    l[3] = light[3];
912 913 914 915 916 917
                  }
                else
                  {
                    l[0] = dark[0];
                    l[1] = dark[1];
                    l[2] = dark[2];
918
                    l[3] = dark[3];
919 920 921 922 923
                  }
              }
          }
      }
      break;
924
    }
925
}
926

927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
static void
gimp_color_scale_create_transform (GimpColorScale *scale)
{
  GimpColorScalePrivate *priv = GET_PRIVATE (scale);

  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 (scale),
                                                         priv->config,
                                                         profile,
                                                         format,
                                                         format);
    }
}
948 949 950 951 952 953 954 955 956 957 958 959 960 961

static void
gimp_color_scale_destroy_transform (GimpColorScale *scale)
{
  GimpColorScalePrivate *priv = GET_PRIVATE (scale);

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

  gtk_widget_queue_draw (GTK_WIDGET (scale));
}
962 963 964 965 966 967 968

static void
gimp_color_scale_notify_config (GimpColorConfig  *config,
                                const GParamSpec *pspec,
                                GimpColorScale   *scale)
{
  GimpColorScalePrivate *priv = GET_PRIVATE (scale);
969
  GimpRGB                color;
970 971 972

  gimp_color_scale_destroy_transform (scale);

973 974
  gimp_color_config_get_out_of_gamut_color (config, &color);
  gimp_rgb_get_uchar (&color,
975 976 977
                      priv->oog_color,
                      priv->oog_color + 1,
                      priv->oog_color + 2);
978
  priv->needs_render = TRUE;
979
}