~ubuntu-branches/ubuntu/hoary/gimp/hoary

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
/*
 * colorsel_triangle module (C) 1999 Simon Budig <Simon.Budig@unix-ag.org>
 *    http://www.home.unix-ag.org/simon/gimp/colorsel.html
 *
 * 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Ported to loadable colour selector interface by Austin Donnelly
 * <austin@gimp.org>
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>

#include <gtk/gtk.h>

#include "libgimpcolor/gimpcolor.h"
#include "libgimpmath/gimpmath.h"
#include "libgimpmodule/gimpmodule.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "libgimp/libgimp-intl.h"


#define BGCOLOR       180
#define MINIMUM_SIZE  48
#define PREVIEW_MASK  (GDK_BUTTON_PRESS_MASK   | \
                       GDK_BUTTON_RELEASE_MASK | \
                       GDK_BUTTON_MOTION_MASK )


#define COLORSEL_TYPE_TRIANGLE            (colorsel_triangle_type)
#define COLORSEL_TRIANGLE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), COLORSEL_TYPE_TRIANGLE, ColorselTriangle))
#define COLORSEL_TRIANGLE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), COLORSEL_TYPE_TRIANGLE, ColorselTriangleClass))
#define COLORSEL_IS_TRIANGLE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), COLORSEL_TYPE_TRIANGLE))
#define COLORSEL_IS_TRIANGLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), COLORSEL_TYPE_TRIANGLE))


typedef struct _ColorselTriangle      ColorselTriangle;
typedef struct _ColorselTriangleClass ColorselTriangleClass;

struct _ColorselTriangle
{
  GimpColorSelector  parent_instance;

  gdouble            oldsat;
  gdouble            oldval;
  gint               mode;
  GtkWidget         *preview;
  gint               wheelradius;
  gint               triangleradius;
};

struct _ColorselTriangleClass
{
  GimpColorSelectorClass  parent_class;
};


static GType      colorsel_triangle_get_type   (GTypeModule           *module);
static void       colorsel_triangle_class_init (ColorselTriangleClass *klass);
static void       colorsel_triangle_init       (ColorselTriangle      *triangle);

static void       colorsel_triangle_set_color       (GimpColorSelector *selector,
                                                     const GimpRGB     *rgb,
                                                     const GimpHSV     *hsv);

static void       colorsel_xy_to_triangle_buf       (gint              x,
                                                     gint              y,
                                                     gdouble           hue,
                                                     guchar           *buf,
                                                     gint              sx,
                                                     gint              sy,
                                                     gint              vx,
                                                     gint              vy,
                                                     gint              hx,
                                                     gint              hy);

static GtkWidget *colorsel_triangle_create_preview  (ColorselTriangle *triangle);
static void       colorsel_triangle_update_preview  (ColorselTriangle *triangle);
static void       colorsel_triangle_size_allocate   (GtkWidget        *widget,
                                                     GtkAllocation    *allocation,
                                                     ColorselTriangle *triangle);
static gboolean   colorsel_triangle_event           (GtkWidget        *widget,
                                                     GdkEvent         *event,
                                                     ColorselTriangle *triangle);

static const GimpModuleInfo colorsel_triangle_info =
{
  GIMP_MODULE_ABI_VERSION,
  N_("Painter-style triangle color selector"),
  "Simon Budig <Simon.Budig@unix-ag.org>",
  "v0.03",
  "(c) 1999, released under the GPL",
  "17 Jan 1999"
};

static const GtkTargetEntry targets[] =
{
  { "application/x-color", 0 }
};


static GType                   colorsel_triangle_type = 0;
static GimpColorSelectorClass *parent_class           = NULL;


G_MODULE_EXPORT const GimpModuleInfo *
gimp_module_query (GTypeModule *module)
{
  return &colorsel_triangle_info;
}

G_MODULE_EXPORT gboolean
gimp_module_register (GTypeModule *module)
{
  colorsel_triangle_get_type (module);

  return TRUE;
}

static GType
colorsel_triangle_get_type (GTypeModule *module)
{
  if (! colorsel_triangle_type)
    {
      static const GTypeInfo select_info =
      {
        sizeof (ColorselTriangleClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) colorsel_triangle_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (ColorselTriangle),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) colorsel_triangle_init,
      };

      colorsel_triangle_type =
        g_type_module_register_type (module,
                                     GIMP_TYPE_COLOR_SELECTOR,
                                     "ColorselTriangle",
                                     &select_info, 0);
    }

  return colorsel_triangle_type;
}

static void
colorsel_triangle_class_init (ColorselTriangleClass *klass)
{
  GimpColorSelectorClass *selector_class = GIMP_COLOR_SELECTOR_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);

  selector_class->name      = _("Triangle");
  selector_class->help_id   = "gimp-colorselector-triangle";
  selector_class->stock_id  = GIMP_STOCK_COLOR_TRIANGLE;
  selector_class->set_color = colorsel_triangle_set_color;
}

static void
colorsel_triangle_init (ColorselTriangle *triangle)
{
  GtkWidget *frame;

  triangle->oldsat = 0;
  triangle->oldval = 0;
  triangle->mode   = 0;

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (triangle), frame, TRUE, TRUE, 0);
  gtk_widget_show (frame);

  triangle->preview = colorsel_triangle_create_preview (triangle);
  gtk_container_add (GTK_CONTAINER (frame), triangle->preview);
  gtk_widget_set_size_request (triangle->preview, MINIMUM_SIZE, MINIMUM_SIZE);
  gtk_widget_show (triangle->preview);
}

static void
colorsel_triangle_set_color (GimpColorSelector *selector,
                             const GimpRGB     *rgb,
                             const GimpHSV     *hsv)
{
  ColorselTriangle *triangle = COLORSEL_TRIANGLE (selector);

  colorsel_triangle_update_preview (triangle);
}

static GtkWidget *
colorsel_triangle_create_preview (ColorselTriangle *triangle)
{
  GtkWidget *preview = gimp_preview_area_new ();

  gtk_widget_add_events (preview, PREVIEW_MASK);

  g_signal_connect (preview, "motion_notify_event",
                    G_CALLBACK (colorsel_triangle_event),
                    triangle);
  g_signal_connect (preview, "button_press_event",
                    G_CALLBACK (colorsel_triangle_event),
                    triangle);
  g_signal_connect (preview, "button_release_event",
                    G_CALLBACK (colorsel_triangle_event),
                    triangle);

  g_signal_connect (preview, "size_allocate",
                    G_CALLBACK (colorsel_triangle_size_allocate),
                    triangle);

  return preview;
}

static void
colorsel_triangle_update_preview (ColorselTriangle *triangle)
{
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (triangle);
  guchar            *preview_buf, *buf;
  gint               x, y, k, r2, dx, col;
  gint               x0, y0;
  gdouble            hue, sat, val, atn;
  gint               hx, hy;
  gint               sx, sy;
  gint               vx, vy;
  gint               width, height, size;
  gint               d;

  width  = GIMP_PREVIEW_AREA (triangle->preview)->width;
  height = GIMP_PREVIEW_AREA (triangle->preview)->height;

  /* return gracefully if the widget is not yet configured or too small */
  if (width < MINIMUM_SIZE || height < MINIMUM_SIZE)
    return;

  triangle->wheelradius    = MIN (width - 1, height - 1) / 2;
  triangle->triangleradius = RINT (0.8 * triangle->wheelradius);

  size = triangle->wheelradius * 2 + 1;

  preview_buf = g_new (guchar, 3 * size * size);
  buf = g_new (guchar, 3 * size);

  memset (preview_buf, BGCOLOR, 3 * size * size);

  hue = (gdouble) selector->hsv.h * 2 * G_PI;

  /* Colored point (value = 1, saturation = 1) */
  hx = RINT (sin (hue) * triangle->triangleradius);
  hy = RINT (cos (hue) * triangle->triangleradius);

  /* Black point (value = 0, saturation not important) */
  sx = RINT (sin (hue - 2 * G_PI / 3) * triangle->triangleradius);
  sy = RINT (cos (hue - 2 * G_PI / 3) * triangle->triangleradius);

  /* White point (value = 1, saturation = 0) */
  vx = RINT (sin (hue + 2 * G_PI / 3) * triangle->triangleradius);
  vy = RINT (cos (hue + 2 * G_PI / 3) * triangle->triangleradius);

  hue = selector->hsv.h * 360.0;

  for (y = triangle->wheelradius; y >= -triangle->wheelradius; y--)
    {
      dx = RINT (sqrt (fabs (SQR (triangle->wheelradius) - SQR (y))));
      for (x = -dx, k = 0; x <= dx; x++)
        {
          buf[k] = buf[k+1] = buf[k+2] = BGCOLOR;
          r2 = SQR (x) + SQR (y);

          if (r2 <= SQR (triangle->wheelradius))
            {
              if (r2 > SQR (triangle->triangleradius))
                {
                  atn = atan2 (x, y);
                  if (atn < 0)
                    atn = atn + 2 * G_PI;

                  gimp_hsv_to_rgb4 (buf + k, atn / (2 * G_PI), 1, 1);
                }
              else
                {
                  colorsel_xy_to_triangle_buf (x, y, hue, buf + k,
                                               hx, hy, sx, sy, vx, vy);
                }
            }

          k += 3;
        }

      memcpy (preview_buf + ((triangle->wheelradius - y) * size +
                             triangle->wheelradius - dx ) * 3,
              buf,
              3 * (2 * dx + 1));
    }

  /* marker in outer ring */

  x0 = RINT (sin (hue * G_PI / 180) *
             ((gdouble) (triangle->wheelradius -
                         triangle->triangleradius + 1) / 2 +
              triangle->triangleradius));
  y0 = RINT (cos (hue * G_PI / 180) *
             ((gdouble) (triangle->wheelradius -
                         triangle->triangleradius + 1) / 2 +
              triangle->triangleradius));

  atn = atan2 (x0, y0);
  if (atn < 0)
    atn = atn + 2 * G_PI;

  gimp_hsv_to_rgb4 (buf, atn / (2 * G_PI), 1, 1);

  col = GIMP_RGB_INTENSITY (buf[0], buf[1], buf[2]) > 127 ? 0 : 255;

  d = CLAMP (triangle->wheelradius / 16, 2, 4);

  for (y = y0 - d ; y <= y0 + d ; y++)
    {
      for (x = x0 - d, k = 0; x <= x0 + d ; x++)
        {
          r2 = SQR (x - x0) + SQR (y - y0);

          if ((r2 <= d * 5) && (r2 >= d + 2))
            {
              buf[k] = buf[k+1] = buf[k+2] = col;
            }
          else
            {
              atn = atan2 (x, y);
              if (atn < 0)
                atn = atn + 2 * G_PI;

              gimp_hsv_to_rgb4 (buf + k, atn / (2 * G_PI), 1, 1);
            }

          k += 3;
        }

      memcpy (preview_buf + ((triangle->wheelradius - y) * size +
                             triangle->wheelradius + x0 - d) * 3,
              buf,
              (2 * d + 1) * 3);
    }

  /* marker in triangle */

  col = gimp_rgb_intensity (&selector->rgb) > 0.5 ? 0 : 255;

  sat = triangle->oldsat = selector->hsv.s;
  val = triangle->oldval = selector->hsv.v;

  x0 = RINT (sx + (vx - sx) * val + (hx - vx) * sat * val);
  y0 = RINT (sy + (vy - sy) * val + (hy - vy) * sat * val);

  for (y = y0 - 4 ; y <= y0 + 4 ; y++)
    {
      for (x = x0 - 4, k=0 ; x <= x0 + 4 ; x++)
        {
          buf[k] = buf[k+1] = buf[k+2] = BGCOLOR;
          r2 = SQR (x - x0) + SQR (y - y0);

          if (r2 <= 20 && r2 >= 6)
            {
              buf[k] = buf[k+1] = buf[k+2] = col;
            }
          else
            {
              if (SQR (x) + SQR (y) > SQR (triangle->triangleradius))
                {
                  atn = atan2 (x, y);
                  if (atn < 0)
                    atn = atn + 2 * G_PI;

                  gimp_hsv_to_rgb4 (buf + k, atn / (2 * G_PI), 1, 1);
                }
              else
                {
                  colorsel_xy_to_triangle_buf (x, y, hue, buf + k,
                                               hx, hy, sx, sy, vx, vy);
                }
            }

          k += 3;
        }

      memcpy (preview_buf + ((triangle->wheelradius - y) * size +
                             triangle->wheelradius + x0 - 4) * 3,
              buf,
              9 * 3);
    }

  gimp_preview_area_draw (GIMP_PREVIEW_AREA (triangle->preview),
                          (width  - size) / 2,
                          (height - size) / 2,
                          size,
                          size,
                          GIMP_RGB_IMAGE,
                          preview_buf,
                          3 * size);
  g_free (buf);
  g_free (preview_buf);
}

static void
colorsel_xy_to_triangle_buf (gint     x,
                             gint     y,
                             gdouble  hue,
                             guchar  *buf,
                             gint     hx,
                             gint     hy, /* colored point */
                             gint     sx,
                             gint     sy, /* black point */
                             gint     vx,
                             gint     vy) /* white point */
{
  gdouble sat, val;

  /*
   * The value is 1 - (the distance from the H->V line).
   * I forgot the linear algebra behind it...
   */
  val = (gdouble) ( (x - sx) * (hy - vy) -  (y - sy) * (hx - vx)) /
        (gdouble) ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx));

  if (val >= 0 && val<= 1)
    {
      if (abs (hy - vy) < abs (hx - vx))
        {
          sat = (val == 0 ? 0: ((gdouble) (x - sx - val * (vx - sx)) /
                                          (val * (hx - vx))));
        }
      else
        {
          sat = (val == 0 ? 0: ((gdouble) (y - sy - val * (vy - sy)) /
                                          (val * (hy - vy))));
        }

      /* Yes, this ugly 1.00*01 fixes some subtle rounding errors... */
      if (sat >= 0 && sat <= 1.000000000000001)
        gimp_hsv_to_rgb4 (buf, hue / 360, sat, val);
    }
}

static void
colorsel_triangle_size_allocate (GtkWidget        *widget,
                                 GtkAllocation    *allocation,
                                 ColorselTriangle *triangle)
{
  gimp_preview_area_fill (GIMP_PREVIEW_AREA (widget),
                          0, 0, allocation->width, allocation->height,
                          BGCOLOR, BGCOLOR, BGCOLOR);

  colorsel_triangle_update_preview (triangle);
}

static gboolean
colorsel_triangle_event (GtkWidget        *widget,
                         GdkEvent         *event,
                         ColorselTriangle *triangle)
{
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (triangle);
  gint               x, y, angle;
  gdouble            r;
  gdouble            hue, sat, val;
  gint               hx, hy;
  gint               sx, sy;
  gint               vx, vy;
  gint               width, height;

  width  = GIMP_PREVIEW_AREA (triangle->preview)->width;
  height = GIMP_PREVIEW_AREA (triangle->preview)->height;

  /* return gracefully if the widget is not yet configured or too small */
  if (width < MINIMUM_SIZE || height < MINIMUM_SIZE)
    return FALSE;

  switch (event->type)
    {
    case GDK_BUTTON_PRESS:
      gtk_grab_add (widget);

      x = event->button.x - (width - 1) / 2 - 1;
      y = - event->button.y + (height - 1) / 2 + 1;
      r = sqrt ((gdouble) (SQR (x) + SQR (y)));
      angle = ((gint) RINT (atan2 (x, y) / G_PI * 180) + 360 ) % 360;

      if ( /* r <= triangle->wheelradius  && */ r > triangle->triangleradius)
        triangle->mode = 1;  /* Dragging in the Ring */
      else
        triangle->mode = 2;  /* Dragging in the Triangle */
      break;

    case GDK_MOTION_NOTIFY:
      gtk_widget_get_pointer (widget, &x, &y);
      if (x != event->motion.x || y != event->motion.y)
        return FALSE;

      x = x - (width - 1) / 2 - 1;
      y = - y + (height - 1) / 2 + 1;
      r = sqrt ((gdouble) (SQR (x) + SQR (y)));
      angle = ((gint) RINT (atan2 (x, y) / G_PI * 180) + 360 ) % 360;
      break;

    case GDK_BUTTON_RELEASE:
      triangle->mode = 0;
      gtk_grab_remove (widget);

      /* callback the user */
      gimp_color_selector_color_changed (GIMP_COLOR_SELECTOR (triangle));

      return FALSE;

    default:
      return FALSE;
    }

  if (triangle->mode == 1 ||
      (r > triangle->wheelradius &&
       (abs (angle - selector->hsv.h * 360.0) < 30 ||
        abs (abs (angle - selector->hsv.h * 360.0) - 360) < 30)))
    {
      selector->hsv.h = angle / 360.0;
      gimp_hsv_to_rgb (&selector->hsv, &selector->rgb);
      colorsel_triangle_update_preview (triangle);
    }
  else
    {
      hue = selector->hsv.h * 2 * G_PI;

      hx = sin (hue) * triangle->triangleradius;
      hy = cos (hue) * triangle->triangleradius;
      sx = sin (hue - 2 * G_PI / 3) * triangle->triangleradius;
      sy = cos (hue - 2 * G_PI / 3) * triangle->triangleradius;
      vx = sin (hue + 2 * G_PI / 3) * triangle->triangleradius;
      vy = cos (hue + 2 * G_PI / 3) * triangle->triangleradius;

      hue = selector->hsv.h * 360.0;

      if ((x - sx) * vx + (y - sy) * vy < 0)
        {
          sat = 1;
          val = ((gdouble) ( (x - sx) * (hx - sx) +  (y - sy) * (hy - sy)))
                         / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy));
          if (val < 0)
            val = 0;
          else if (val > 1)
            val = 1;
        }
      else if ((x - sx) * hx + (y - sy) * hy < 0)
        {
          sat = 0;
          val = ((gdouble) ( (x - sx) * (vx - sx) +  (y - sy) * (vy - sy)))
                         / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy));
          if (val < 0)
            val = 0;
          else if (val > 1)
            val = 1;
        }
      else if ((x - hx) * sx + (y - hy) * sy < 0)
        {
          val = 1;
          sat = ((gdouble) ( (x - vx) * (hx - vx) +  (y - vy) * (hy - vy)))
                         / ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy));
          if (sat < 0)
            sat = 0;
          else if (sat > 1)
            sat = 1;
        }
      else
        {
          val =   (gdouble) ( (x - sx) * (hy - vy) -  (y - sy) * (hx - vx))
                / (gdouble) ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx));
          if (val <= 0)
            {
              val = 0;
              sat = 0;
            }
          else
            {
              if (val > 1)
                val = 1;
              if (hy == vy)
                sat = (gdouble) (x - sx - val * (vx - sx)) / (val * (gdouble) (hx - vx));
              else
                sat = (gdouble) (y - sy - val * (vy - sy)) / (val * (gdouble) (hy - vy));

              if (sat < 0)
                sat = 0;
              else if (sat > 1)
                sat = 1;
            }
        }

      selector->hsv.s = sat;
      selector->hsv.v = val;
      gimp_hsv_to_rgb (&selector->hsv, &selector->rgb);
      colorsel_triangle_update_preview (triangle);
    }

  /* callback the user */
  gimp_color_selector_color_changed (GIMP_COLOR_SELECTOR (triangle));

  return FALSE;
}