File: gimpcolorframe.c

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (348 lines) | stat: -rw-r--r-- 10,219 bytes parent folder | download | duplicates (3)
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
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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.
 */

#include "config.h"

#include <gtk/gtk.h>

#include "libgimpcolor/gimpcolor.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "core/gimpimage.h"

#include "gimpcolorframe.h"
#include "gimpenumcombobox.h"

#include "gimp-intl.h"


/*  local function prototypes  */

static void   gimp_color_frame_class_init    (GimpColorFrameClass *klass);
static void   gimp_color_frame_init          (GimpColorFrame      *frame);

static void   gimp_color_frame_menu_callback (GtkWidget           *widget,
                                              GimpColorFrame      *frame);
static void   gimp_color_frame_update        (GimpColorFrame      *frame);


static GimpFrameClass *parent_class = NULL;


GType
gimp_color_frame_get_type (void)
{
  static GType type = 0;

  if (! type)
    {
      static const GTypeInfo frame_info =
      {
        sizeof (GimpColorFrameClass),
        NULL,           /* base_init */
        NULL,           /* base_finalize */
        (GClassInitFunc) gimp_color_frame_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GimpColorFrame),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gimp_color_frame_init,
      };

      type = g_type_register_static (GIMP_TYPE_FRAME,
                                     "GimpColorFrame",
                                     &frame_info, 0);
    }

  return type;
}

static void
gimp_color_frame_class_init (GimpColorFrameClass *klass)
{
  parent_class = g_type_class_peek_parent (klass);
}

static void
gimp_color_frame_init (GimpColorFrame *frame)
{
  GtkWidget *table;
  gint       i;

  frame->sample_valid = FALSE;
  frame->sample_type  = GIMP_RGB_IMAGE;

  gimp_rgba_set (&frame->color, 0.0, 0.0, 0.0, GIMP_OPACITY_OPAQUE);

  frame->menu = gimp_enum_combo_box_new (GIMP_TYPE_COLOR_FRAME_MODE);
  g_signal_connect (frame->menu, "changed",
                    G_CALLBACK (gimp_color_frame_menu_callback),
                    frame);
  gtk_frame_set_label_widget (GTK_FRAME (frame), frame->menu);
  gtk_widget_show (frame->menu);

  table = gtk_table_new (GIMP_COLOR_FRAME_ROWS, 2, FALSE);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
  gtk_container_add (GTK_CONTAINER (frame), table);
  gtk_widget_show (table);

  for (i = 0; i < GIMP_COLOR_FRAME_ROWS; i++)
    {
      frame->name_labels[i] = gtk_label_new (" ");
      gtk_misc_set_alignment (GTK_MISC (frame->name_labels[i]), 0.0, 0.5);
      gtk_table_attach (GTK_TABLE (table), frame->name_labels[i],
                        0, 1, i, i + 1,
                        GTK_FILL, GTK_FILL, 0, 0);
      gtk_widget_show (frame->name_labels[i]);

      frame->value_labels[i] = gtk_label_new (" ");
      gtk_label_set_selectable (GTK_LABEL (frame->value_labels[i]), TRUE);
      gtk_misc_set_alignment (GTK_MISC (frame->value_labels[i]), 1.0, 0.5);
      gtk_table_attach (GTK_TABLE (table), frame->value_labels[i],
                        1, 2, i, i + 1,
                        GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
      gtk_widget_show (frame->value_labels[i]);
    }
}

/**
 * gimp_color_frame_new:
 *
 * Creates a new #GimpColorFrame widget.
 *
 * Return value: The new #GimpColorFrame widget.
 **/
GtkWidget *
gimp_color_frame_new (void)
{
  return g_object_new (GIMP_TYPE_COLOR_FRAME, NULL);
}

/**
 * gimp_color_frame_set_mode:
 * @frame: The #GimpColorFrame.
 * @mode:  The new @mode.
 *
 * Sets the #GimpColorFrame's color @mode. Calling this function does
 * the same as selecting the @mode from the frame's #GtkOptionMenu.
 **/
void
gimp_color_frame_set_mode (GimpColorFrame     *frame,
                           GimpColorFrameMode  mode)
{
  g_return_if_fail (GIMP_IS_COLOR_FRAME (frame));

  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (frame->menu), mode);
  frame->frame_mode = mode;

  gimp_color_frame_update (frame);
}

/**
 * gimp_color_frame_set_color:
 * @frame:       The #GimpColorFrame.
 * @sample_type: The type of the #GimpDrawable or #GimpImage the @color
 *               was picked from.
 * @color:       The @color to set.
 * @color_index: The @color's index. This value is ignored unless
 *               @sample_type equals to #GIMP_INDEXED_IMAGE or
 *               #GIMP_INDEXEDA_IMAGE.
 *
 * Sets the color sample to display in the #GimpColorFrame.
 **/
void
gimp_color_frame_set_color (GimpColorFrame *frame,
                            GimpImageType   sample_type,
                            const GimpRGB  *color,
                            gint            color_index)
{
  g_return_if_fail (GIMP_IS_COLOR_FRAME (frame));
  g_return_if_fail (color != NULL);

  frame->sample_valid = TRUE;
  frame->sample_type  = sample_type;
  frame->color        = *color;
  frame->color_index  = color_index;

  gimp_color_frame_update (frame);
}

/**
 * gimp_color_frame_set_invalid:
 * @frame: The #GimpColorFrame.
 *
 * Tells the #GimpColorFrame that the current sample is invalid. All labels
 * visible for the current color space will show "n/a" (not available).
 *
 * There is no special API for setting the frame to "valid" again because
 * this happens automatically when calling gimp_color_frame_set_color().
 **/
void
gimp_color_frame_set_invalid (GimpColorFrame *frame)
{
  g_return_if_fail (GIMP_IS_COLOR_FRAME (frame));

  frame->sample_valid = FALSE;

  gimp_color_frame_update (frame);
}


/*  private functions  */

static void
gimp_color_frame_menu_callback (GtkWidget      *widget,
                                GimpColorFrame *frame)
{
  gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget),
                                 (gint *) &frame->frame_mode);
  gimp_color_frame_update (frame);
}

static void
gimp_color_frame_update (GimpColorFrame *frame)
{
  const gchar *names[GIMP_COLOR_FRAME_ROWS]  = { NULL, };
  gchar       *values[GIMP_COLOR_FRAME_ROWS] = { NULL, };
  gboolean     has_alpha;
  gint         alpha_row = 3;
  guchar       r, g, b, a;
  gint         i;

  has_alpha = GIMP_IMAGE_TYPE_HAS_ALPHA (frame->sample_type);

  gimp_rgba_get_uchar (&frame->color, &r, &g, &b, &a);

  switch (frame->frame_mode)
    {
    case GIMP_COLOR_FRAME_MODE_PIXEL:
      switch (GIMP_IMAGE_TYPE_BASE_TYPE (frame->sample_type))
        {
        case GIMP_INDEXED:
          names[4]  = _("Index:");
          values[4] = g_strdup_printf ("%d", frame->color_index);

        case GIMP_RGB:
          names[0] = _("Red:");
          names[1] = _("Green:");
          names[2] = _("Blue:");

          values[0] = g_strdup_printf ("%d", r);
          values[1] = g_strdup_printf ("%d", g);
          values[2] = g_strdup_printf ("%d", b);

          alpha_row = 3;
          break;

        case GIMP_GRAY:
          names[0]  = _("Value:");
          values[0] = g_strdup_printf ("%d", r);

          alpha_row = 1;
          break;
        }
      break;

    case GIMP_COLOR_FRAME_MODE_RGB:
      names[0] = _("Red:");
      names[1] = _("Green:");
      names[2] = _("Blue:");

      values[0] = g_strdup_printf ("%d %%", ROUND (frame->color.r * 100.0));
      values[1] = g_strdup_printf ("%d %%", ROUND (frame->color.g * 100.0));
      values[2] = g_strdup_printf ("%d %%", ROUND (frame->color.b * 100.0));

      alpha_row = 3;

      names[4]  = _("Hex:");
      values[4] = g_strdup_printf ("%.2x%.2x%.2x", r, g, b);
      break;

    case GIMP_COLOR_FRAME_MODE_HSV:
      {
        GimpHSV hsv;

        gimp_rgb_to_hsv (&frame->color, &hsv);

        names[0] = _("Hue:");
        names[1] = _("Sat.:");
        names[2] = _("Value:");

        values[0] = g_strdup_printf ("%d \302\260", ROUND (hsv.h * 360.0));
        values[1] = g_strdup_printf ("%d %%",       ROUND (hsv.s * 100.0));
        values[2] = g_strdup_printf ("%d %%",       ROUND (hsv.v * 100.0));

        alpha_row = 3;
      }
      break;

    case GIMP_COLOR_FRAME_MODE_CMYK:
      {
        GimpCMYK cmyk;

        gimp_rgb_to_cmyk (&frame->color, 1.0, &cmyk);

        names[0] = _("Cyan:");
        names[1] = _("Magenta:");
        names[2] = _("Yellow:");
        names[3] = _("Black:");

        values[0] = g_strdup_printf ("%d %%", ROUND (cmyk.c * 100.0));
        values[1] = g_strdup_printf ("%d %%", ROUND (cmyk.m * 100.0));
        values[2] = g_strdup_printf ("%d %%", ROUND (cmyk.y * 100.0));
        values[3] = g_strdup_printf ("%d %%", ROUND (cmyk.k * 100.0));

        alpha_row = 4;
      }
      break;
    }

  if (has_alpha)
    {
      names[alpha_row]  = _("Alpha:");

      if (frame->frame_mode == GIMP_COLOR_FRAME_MODE_PIXEL)
        values[alpha_row] = g_strdup_printf ("%d", a);
      else
        values[alpha_row] = g_strdup_printf ("%d %%",
                                             (gint) (frame->color.a * 100.0));
    }

  for (i = 0; i < GIMP_COLOR_FRAME_ROWS; i++)
    {
      if (names[i] && values[i])
        {
          gtk_label_set_text (GTK_LABEL (frame->name_labels[i]), names[i]);

          if (frame->sample_valid)
            gtk_label_set_text (GTK_LABEL (frame->value_labels[i]), values[i]);
          else
            gtk_label_set_text (GTK_LABEL (frame->value_labels[i]), _("n/a"));
        }
      else
        {
          gtk_label_set_text (GTK_LABEL (frame->name_labels[i]),  " ");
          gtk_label_set_text (GTK_LABEL (frame->value_labels[i]), " ");
        }

      g_free (values[i]);
    }
}