summaryrefslogtreecommitdiffstats
path: root/libgimpcolor/gimprgb.c
blob: b9ab4bea740170c6e4a1e109c39feeb1c39f5754 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
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
356
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
392
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
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <babl/babl.h>
#include <glib-object.h>

#define GIMP_DISABLE_DEPRECATION_WARNINGS /*  for GIMP_RGB_INTENSITY()  */
#include "libgimpmath/gimpmath.h"

#include "gimpcolortypes.h"

#undef GIMP_DISABLE_DEPRECATED  /*  for GIMP_RGB_INTENSITY()  */
#include "gimprgb.h"


/**
 * SECTION: gimprgb
 * @title: GimpRGB
 * @short_description: Definitions and Functions relating to RGB colors.
 *
 * Definitions and Functions relating to RGB colors.
 **/


/*
 * GIMP_TYPE_RGB
 */

static GimpRGB * gimp_rgb_copy (const GimpRGB *rgb);


GType
gimp_rgb_get_type (void)
{
  static GType rgb_type = 0;

  if (!rgb_type)
    rgb_type = g_boxed_type_register_static ("GimpRGB",
                                             (GBoxedCopyFunc) gimp_rgb_copy,
                                             (GBoxedFreeFunc) g_free);

  return rgb_type;
}

void
gimp_value_get_rgb (const GValue *value,
                    GimpRGB      *rgb)
{
  g_return_if_fail (GIMP_VALUE_HOLDS_RGB (value));
  g_return_if_fail (rgb != NULL);

  if (value->data[0].v_pointer)
    *rgb = *((GimpRGB *) value->data[0].v_pointer);
  else
    gimp_rgba_set (rgb, 0.0, 0.0, 0.0, 1.0);
}

void
gimp_value_set_rgb (GValue        *value,
                    const GimpRGB *rgb)
{
  g_return_if_fail (GIMP_VALUE_HOLDS_RGB (value));
  g_return_if_fail (rgb != NULL);

  g_value_set_boxed (value, rgb);
}

static GimpRGB *
gimp_rgb_copy (const GimpRGB *rgb)
{
  return g_memdup (rgb, sizeof (GimpRGB));
}


/*  RGB functions  */

/**
 * gimp_rgb_set:
 * @rgb:   a #GimpRGB struct
 * @red:   the red component
 * @green: the green component
 * @blue:  the blue component
 *
 * Sets the red, green and blue components of @rgb and leaves the
 * alpha component unchanged. The color values should be between 0.0
 * and 1.0 but there is no check to enforce this and the values are
 * set exactly as they are passed in.
 **/
void
gimp_rgb_set (GimpRGB *rgb,
              gdouble  r,
              gdouble  g,
              gdouble  b)
{
  g_return_if_fail (rgb != NULL);

  rgb->r = r;
  rgb->g = g;
  rgb->b = b;
}

/**
 * gimp_rgb_set_alpha:
 * @rgb:   a #GimpRGB struct
 * @alpha: the alpha component
 *
 * Sets the alpha component of @rgb and leaves the RGB components unchanged.
 **/
void
gimp_rgb_set_alpha (GimpRGB *rgb,
                    gdouble  a)
{
  g_return_if_fail (rgb != NULL);

  rgb->a = a;
}

/**
 * gimp_rgb_set_pixel:
 * @rgb:    a #GimpRGB struct
 * @format: a Babl format
 * @pixel:  pointer to the source pixel
 *
 * Sets the red, green and blue components of @rgb from the color
 * stored in @pixel. The pixel format of @pixel is determined by
 * @format.
 *
 * Since: 2.10
 **/
void
gimp_rgb_set_pixel (GimpRGB       *rgb,
                    const Babl    *format,
                    gconstpointer  pixel)
{
  g_return_if_fail (rgb != NULL);
  g_return_if_fail (format != NULL);
  g_return_if_fail (pixel != NULL);

  babl_process (babl_fish (format,
                           babl_format ("R'G'B' double")),
                pixel, rgb, 1);
}

/**
 * gimp_rgb_get_pixel:
 * @rgb:    a #GimpRGB struct
 * @format: a Babl format
 * @pixel:  pointer to the destination pixel
 *
 * Writes the red, green, blue and alpha components of @rgb to the
 * color stored in @pixel. The pixel format of @pixel is determined by
 * @format.
 *
 * Since: 2.10
 **/
void
gimp_rgb_get_pixel (const GimpRGB *rgb,
                    const Babl    *format,
                    gpointer       pixel)
{
  g_return_if_fail (rgb != NULL);
  g_return_if_fail (format != NULL);
  g_return_if_fail (pixel != NULL);

  babl_process (babl_fish (babl_format ("R'G'B' double"),
                           format),
                rgb, pixel, 1);
}

/**
 * gimp_rgb_set_uchar:
 * @rgb:   a #GimpRGB struct
 * @red:   the red component
 * @green: the green component
 * @blue:  the blue component
 *
 * Sets the red, green and blue components of @rgb from 8bit values
 * (0 to 255) and leaves the alpha component unchanged.
 **/
void
gimp_rgb_set_uchar (GimpRGB *rgb,
                    guchar   r,
                    guchar   g,
                    guchar   b)
{
  g_return_if_fail (rgb != NULL);

  rgb->r = (gdouble) r / 255.0;
  rgb->g = (gdouble) g / 255.0;
  rgb->b = (gdouble) b / 255.0;
}

void
gimp_rgb_get_uchar (const GimpRGB *rgb,
                    guchar        *r,
                    guchar        *g,
                    guchar        *b)
{
  g_return_if_fail (rgb != NULL);

  if (r) *r = ROUND (CLAMP (rgb->r, 0.0, 1.0) * 255.0);
  if (g) *g = ROUND (CLAMP (rgb->g, 0.0, 1.0) * 255.0);
  if (b) *b = ROUND (CLAMP (rgb->b, 0.0, 1.0) * 255.0);
}

void
gimp_rgb_add (GimpRGB       *rgb1,
              const GimpRGB *rgb2)
{
  g_return_if_fail (rgb1 != NULL);
  g_return_if_fail (rgb2 != NULL);

  rgb1->r += rgb2->r;
  rgb1->g += rgb2->g;
  rgb1->b += rgb2->b;
}

void
gimp_rgb_subtract (GimpRGB       *rgb1,
                   const GimpRGB *rgb2)
{
  g_return_if_fail (rgb1 != NULL);
  g_return_if_fail (rgb2 != NULL);

  rgb1->r -= rgb2->r;
  rgb1->g -= rgb2->g;
  rgb1->b -= rgb2->b;
}

void
gimp_rgb_multiply (GimpRGB *rgb,
                   gdouble  factor)
{
  g_return_if_fail (rgb != NULL);

  rgb->r *= factor;
  rgb->g *= factor;
  rgb->b *= factor;
}

gdouble
gimp_rgb_distance (const GimpRGB *rgb1,
                   const GimpRGB *rgb2)
{
  g_return_val_if_fail (rgb1 != NULL, 0.0);
  g_return_val_if_fail (rgb2 != NULL, 0.0);

  return (fabs (rgb1->r - rgb2->r) +
          fabs (rgb1->g - rgb2->g) +
          fabs (rgb1->b - rgb2->b));
}

gdouble
gimp_rgb_max (const GimpRGB *rgb)
{
  g_return_val_if_fail (rgb != NULL, 0.0);

  if (rgb->r > rgb->g)
    return (rgb->r > rgb->b) ? rgb->r : rgb->b;
  else
    return (rgb->g > rgb->b) ? rgb->g : rgb->b;
}

gdouble
gimp_rgb_min (const GimpRGB *rgb)
{
  g_return_val_if_fail (rgb != NULL, 0.0);

  if (rgb->r < rgb->g)
    return (rgb->r < rgb->b) ? rgb->r : rgb->b;
  else
    return (rgb->g < rgb->b) ? rgb->g : rgb->b;
}

void
gimp_rgb_clamp (GimpRGB *rgb)
{
  g_return_if_fail (rgb != NULL);

  rgb->r = CLAMP (rgb->r, 0.0, 1.0);
  rgb->g = CLAMP (rgb->g, 0.0, 1.0);
  rgb->b = CLAMP (rgb->b, 0.0, 1.0);
  rgb->a = CLAMP (rgb->a, 0.0, 1.0);
}

void
gimp_rgb_gamma (GimpRGB *rgb,
                gdouble  gamma)
{
  gdouble ig;

  g_return_if_fail (rgb != NULL);

  if (gamma != 0.0)
    ig = 1.0 / gamma;
  else
    ig = 0.0;

  rgb->r = pow (rgb->r, ig);
  rgb->g = pow (rgb->g, ig);
  rgb->b = pow (rgb->b, ig);
}

/**
 * gimp_rgb_luminance:
 * @rgb: a #GimpRGB struct
 *
 * Return value: the luminous intensity of the range from 0.0 to 1.0.
 *
 * Since: 2.4
 **/
gdouble
gimp_rgb_luminance (const GimpRGB *rgb)
{
  gdouble luminance;

  g_return_val_if_fail (rgb != NULL, 0.0);

  luminance = GIMP_RGB_LUMINANCE (rgb->r, rgb->g, rgb->b);

  return CLAMP (luminance, 0.0, 1.0);
}

/**
 * gimp_rgb_luminance_uchar:
 * @rgb: a #GimpRGB struct
 *
 * Return value: the luminous intensity in the range from 0 to 255.
 *
 * Since: 2.4
 **/
guchar
gimp_rgb_luminance_uchar (const GimpRGB *rgb)
{
  g_return_val_if_fail (rgb != NULL, 0);

  return ROUND (gimp_rgb_luminance (rgb) * 255.0);
}

/**
 * gimp_rgb_intensity:
 * @rgb: a #GimpRGB struct
 *
 * This function is deprecated! Use gimp_rgb_luminance() instead.
 *
 * Return value: the intensity in the range from 0.0 to 1.0.
 **/
gdouble
gimp_rgb_intensity (const GimpRGB *rgb)
{
  gdouble intensity;

  g_return_val_if_fail (rgb != NULL, 0.0);

  intensity = GIMP_RGB_INTENSITY (rgb->r, rgb->g, rgb->b);

  return CLAMP (intensity, 0.0, 1.0);
}

/**
 * gimp_rgb_intensity_uchar:
 * @rgb: a #GimpRGB struct
 *
 * This function is deprecated! Use gimp_rgb_luminance_uchar() instead.
 *
 * Return value: the intensity in the range from 0 to 255.
 **/
guchar
gimp_rgb_intensity_uchar (const GimpRGB *rgb)
{
  g_return_val_if_fail (rgb != NULL, 0);

  return ROUND (gimp_rgb_intensity (rgb) * 255.0);
}

void
gimp_rgb_composite (GimpRGB              *color1,
                    const GimpRGB        *color2,
                    GimpRGBCompositeMode  mode)
{
  g_return_if_fail (color1 != NULL);
  g_return_if_fail (color2 != NULL);

  switch (mode)
    {
    case GIMP_RGB_COMPOSITE_NONE:
      break;

    case GIMP_RGB_COMPOSITE_NORMAL:
      /*  put color2 on top of color1  */
      if (color2->a == 1.0)
        {
          *color1 = *color2;
        }
      else
        {
          gdouble factor = color1->a * (1.0 - color2->a);

          color1->r = color1->r * factor + color2->r * color2->a;
          color1->g = color1->g * factor + color2->g * color2->a;
          color1->b = color1->b * factor + color2->b * color2->a;
          color1->a = factor + color2->a;
        }
      break;

    case GIMP_RGB_COMPOSITE_BEHIND:
      /*  put color2 below color1  */
      if (color1->a < 1.0)
        {
          gdouble factor = color2->a * (1.0 - color1->a);

          color1->r = color2->r * factor + color1->r * color1->a;
          color1->g = color2->g * factor + color1->g * color1->a;
          color1->b = color2->b * factor + color1->b * color1->a;
          color1->a = factor + color1->a;
        }
      break;
    }
}

/*  RGBA functions  */

/**
 * gimp_rgba_set_pixel:
 * @rgba:   a #GimpRGB struct
 * @format: a Babl format
 * @pixel:  pointer to the source pixel
 *
 * Sets the red, green, blue and alpha components of @rgba from the
 * color stored in @pixel. The pixel format of @pixel is determined
 * by @format.
 *
 * Since: 2.10
 **/
void
gimp_rgba_set_pixel (GimpRGB       *rgba,
                     const Babl    *format,
                     gconstpointer  pixel)
{
  g_return_if_fail (rgba != NULL);
  g_return_if_fail (format != NULL);
  g_return_if_fail (pixel != NULL);

  babl_process (babl_fish (format,
                           babl_format ("R'G'B'A double")),
                pixel, rgba, 1);
}

/**
 * gimp_rgba_get_pixel:
 * @rgba:   a #GimpRGB struct
 * @format: a Babl format
 * @pixel:  pointer to the destination pixel
 *
 * Writes the red, green, blue and alpha components of @rgba to the
 * color stored in @pixel. The pixel format of @pixel is determined by
 * @format.
 *
 * Since: 2.10
 **/
void
gimp_rgba_get_pixel (const GimpRGB *rgba,
                     const Babl    *format,
                     gpointer       pixel)
{
  g_return_if_fail (rgba != NULL);
  g_return_if_fail (format != NULL);
  g_return_if_fail (pixel != NULL);

  babl_process (babl_fish (babl_format ("R'G'B'A double"),
                           format),
                rgba, pixel, 1);
}

/**
 * gimp_rgba_set:
 * @rgba:  a #GimpRGB struct
 * @red:   the red component
 * @green: the green component
 * @blue:  the blue component
 * @alpha: the alpha component
 *
 * Sets the red, green, blue and alpha components of @rgb. The values
 * should be between 0.0 and 1.0 but there is no check to enforce this
 * and the values are set exactly as they are passed in.
 **/
void
gimp_rgba_set (GimpRGB *rgba,
               gdouble  r,
               gdouble  g,
               gdouble  b,
               gdouble  a)
{
  g_return_if_fail (rgba != NULL);

  rgba->r = r;
  rgba->g = g;
  rgba->b = b;
  rgba->a = a;
}

/**
 * gimp_rgba_set_uchar:
 * @rgba:  a #GimpRGB struct
 * @red:   the red component
 * @green: the green component
 * @blue:  the blue component
 * @alpha: the alpha component
 *
 * Sets the red, green, blue and alpha components of @rgb from 8bit
 * values (0 to 255).
 **/
void
gimp_rgba_set_uchar (GimpRGB *rgba,
                     guchar   r,
                     guchar   g,
                     guchar   b,
                     guchar   a)
{
  g_return_if_fail (rgba != NULL);

  rgba->r = (gdouble) r / 255.0;
  rgba->g = (gdouble) g / 255.0;
  rgba->b = (gdouble) b / 255.0;
  rgba->a = (gdouble) a / 255.0;
}

void
gimp_rgba_get_uchar (const GimpRGB *rgba,
                     guchar        *r,
                     guchar        *g,
                     guchar        *b,
                     guchar        *a)
{
  g_return_if_fail (rgba != NULL);

  if (r) *r = ROUND (CLAMP (rgba->r, 0.0, 1.0) * 255.0);
  if (g) *g = ROUND (CLAMP (rgba->g, 0.0, 1.0) * 255.0);
  if (b) *b = ROUND (CLAMP (rgba->b, 0.0, 1.0) * 255.0);
  if (a) *a = ROUND (CLAMP (rgba->a, 0.0, 1.0) * 255.0);
}

void
gimp_rgba_add (GimpRGB       *rgba1,
               const GimpRGB *rgba2)
{
  g_return_if_fail (rgba1 != NULL);
  g_return_if_fail (rgba2 != NULL);

  rgba1->r += rgba2->r;
  rgba1->g += rgba2->g;
  rgba1->b += rgba2->b;
  rgba1->a += rgba2->a;
}

void
gimp_rgba_subtract (GimpRGB       *rgba1,
                    const GimpRGB *rgba2)
{
  g_return_if_fail (rgba1 != NULL);
  g_return_if_fail (rgba2 != NULL);

  rgba1->r -= rgba2->r;
  rgba1->g -= rgba2->g;
  rgba1->b -= rgba2->b;
  rgba1->a -= rgba2->a;
}

void
gimp_rgba_multiply (GimpRGB *rgba,
                    gdouble  factor)
{
  g_return_if_fail (rgba != NULL);

  rgba->r *= factor;
  rgba->g *= factor;
  rgba->b *= factor;
  rgba->a *= factor;
}

gdouble
gimp_rgba_distance (const GimpRGB *rgba1,
                    const GimpRGB *rgba2)
{
  g_return_val_if_fail (rgba1 != NULL, 0.0);
  g_return_val_if_fail (rgba2 != NULL, 0.0);

  return (fabs (rgba1->r - rgba2->r) +
          fabs (rgba1->g - rgba2->g) +
          fabs (rgba1->b - rgba2->b) +
          fabs (rgba1->a - rgba2->a));
}


/*
 * GIMP_TYPE_PARAM_RGB
 */

#define GIMP_PARAM_SPEC_RGB(pspec)    (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GIMP_TYPE_PARAM_RGB, GimpParamSpecRGB))

typedef struct _GimpParamSpecRGB GimpParamSpecRGB;

struct _GimpParamSpecRGB
{
  GParamSpecBoxed  parent_instance;

  gboolean         has_alpha;
  gboolean         validate; /* change this to enable [0.0...1.0] */
  GimpRGB          default_value;
};

static void       gimp_param_rgb_class_init  (GParamSpecClass *class);
static void       gimp_param_rgb_init        (GParamSpec      *pspec);
static void       gimp_param_rgb_set_default (GParamSpec      *pspec,
                                              GValue          *value);
static gboolean   gimp_param_rgb_validate    (GParamSpec      *pspec,
                                              GValue          *value);
static gint       gimp_param_rgb_values_cmp  (GParamSpec      *pspec,
                                              const GValue    *value1,
                                              const GValue    *value2);

/**
 * gimp_param_rgb_get_type:
 *
 * Reveals the object type
 *
 * Returns: the #GType for a GimpParamRGB object
 *
 * Since: 2.4
 **/
GType
gimp_param_rgb_get_type (void)
{
  static GType spec_type = 0;

  if (! spec_type)
    {
      const GTypeInfo type_info =
      {
        sizeof (GParamSpecClass),
        NULL, NULL,
        (GClassInitFunc) gimp_param_rgb_class_init,
        NULL, NULL,
        sizeof (GimpParamSpecRGB),
        0,
        (GInstanceInitFunc) gimp_param_rgb_init
      };

      spec_type = g_type_register_static (G_TYPE_PARAM_BOXED,
                                          "GimpParamRGB",
                                          &type_info, 0);
    }

  return spec_type;
}

static void
gimp_param_rgb_class_init (GParamSpecClass *class)
{
  class->value_type        = GIMP_TYPE_RGB;
  class->value_set_default = gimp_param_rgb_set_default;
  class->value_validate    = gimp_param_rgb_validate;
  class->values_cmp        = gimp_param_rgb_values_cmp;
}

static void
gimp_param_rgb_init (GParamSpec *pspec)
{
  GimpParamSpecRGB *cspec = GIMP_PARAM_SPEC_RGB (pspec);

  gimp_rgba_set (&cspec->default_value, 0.0, 0.0, 0.0, 1.0);
}

static void
gimp_param_rgb_set_default (GParamSpec *pspec,
                            GValue     *value)
{
  GimpParamSpecRGB *cspec = GIMP_PARAM_SPEC_RGB (pspec);

  g_value_set_static_boxed (value, &cspec->default_value);
}

static gboolean
gimp_param_rgb_validate (GParamSpec *pspec,
                         GValue     *value)
{
  GimpParamSpecRGB *rgb_spec = GIMP_PARAM_SPEC_RGB (pspec);
  GimpRGB          *rgb      = value->data[0].v_pointer;

  if (rgb_spec->validate && rgb)
    {
      GimpRGB oval = *rgb;

      gimp_rgb_clamp (rgb);

      return (oval.r != rgb->r ||
              oval.g != rgb->g ||
              oval.b != rgb->b ||
              (rgb_spec->has_alpha && oval.a != rgb->a));
    }

  return FALSE;
}

static gint
gimp_param_rgb_values_cmp (GParamSpec   *pspec,
                           const GValue *value1,
                           const GValue *value2)
{
  GimpRGB *rgb1 = value1->data[0].v_pointer;
  GimpRGB *rgb2 = value2->data[0].v_pointer;

  /*  try to return at least *something*, it's useless anyway...  */

  if (! rgb1)
    {
      return rgb2 != NULL ? -1 : 0;
    }
  else if (! rgb2)
    {
      return rgb1 != NULL;
    }
  else
    {
      guint32 int1 = 0;
      guint32 int2 = 0;

      if (GIMP_PARAM_SPEC_RGB (pspec)->has_alpha)
        {
          gimp_rgba_get_uchar (rgb1,
                               ((guchar *) &int1) + 0,
                               ((guchar *) &int1) + 1,
                               ((guchar *) &int1) + 2,
                               ((guchar *) &int1) + 3);
          gimp_rgba_get_uchar (rgb2,
                               ((guchar *) &int2) + 0,
                               ((guchar *) &int2) + 1,
                               ((guchar *) &int2) + 2,
                               ((guchar *) &int2) + 3);
        }
      else
        {
          gimp_rgb_get_uchar (rgb1,
                              ((guchar *) &int1) + 0,
                              ((guchar *) &int1) + 1,
                              ((guchar *) &int1) + 2);
          gimp_rgb_get_uchar (rgb2,
                              ((guchar *) &int2) + 0,
                              ((guchar *) &int2) + 1,
                              ((guchar *) &int2) + 2);
        }

      return int1 - int2;
    }
}

/**
 * gimp_param_spec_rgb:
 * @name:          Canonical name of the param
 * @nick:          Nickname of the param
 * @blurb:         Brief description of param.
 * @has_alpha:     %TRUE if the alpha channel has relevance.
 * @default_value: Value to use if none is assigned.
 * @flags:         a combination of #GParamFlags
 *
 * Creates a param spec to hold an #GimpRGB value.
 * See g_param_spec_internal() for more information.
 *
 * Returns: a newly allocated #GParamSpec instance
 *
 * Since: 2.4
 **/
GParamSpec *
gimp_param_spec_rgb (const gchar   *name,
                     const gchar   *nick,
                     const gchar   *blurb,
                     gboolean       has_alpha,
                     const GimpRGB *default_value,
                     GParamFlags    flags)
{
  GimpParamSpecRGB *cspec;

  cspec = g_param_spec_internal (GIMP_TYPE_PARAM_RGB,
                                 name, nick, blurb, flags);

  cspec->has_alpha = has_alpha;

  if (default_value)
    cspec->default_value = *default_value;
  else
    gimp_rgba_set (&cspec->default_value, 0.0, 0.0, 0.0, 1.0);

  return G_PARAM_SPEC (cspec);
}

/**
 * gimp_param_spec_rgb_get_default:
 * @pspec:         a #GimpParamSpecRGB.
 * @default_value: return location for @pspec's default value
 *
 * Returns the @pspec's default color value.
 *
 * Since: 2.10.14
 **/
void
gimp_param_spec_rgb_get_default (GParamSpec *pspec,
                                 GimpRGB    *default_value)
{
  g_return_if_fail (GIMP_IS_PARAM_SPEC_RGB (pspec));
  g_return_if_fail (default_value != NULL);

  *default_value = GIMP_PARAM_SPEC_RGB (pspec)->default_value;
}

/**
 * gimp_param_spec_rgb_has_alpha:
 * @pspec: a #GParamSpec to hold an #GimpRGB value.
 *
 * Returns: %TRUE if the alpha channel is relevant.
 *
 * Since: 2.4
 **/
gboolean
gimp_param_spec_rgb_has_alpha (GParamSpec *pspec)
{
  g_return_val_if_fail (GIMP_IS_PARAM_SPEC_RGB (pspec), FALSE);

  return GIMP_PARAM_SPEC_RGB (pspec)->has_alpha;
}