gimpcolorizetool.c 11.3 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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"

21 22
#include <string.h>

23
#include <gegl.h>
24 25
#include <gtk/gtk.h>

26
#include "libgimpconfig/gimpconfig.h"
27 28 29 30 31 32
#include "libgimpwidgets/gimpwidgets.h"

#include "tools-types.h"

#include "base/colorize.h"

33 34
#include "gegl/gimpcolorizeconfig.h"

35 36 37
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"

38 39
#include "widgets/gimphelp-ids.h"

40 41 42
#include "display/gimpdisplay.h"

#include "gimpcolorizetool.h"
43
#include "gimpimagemapoptions.h"
44 45 46 47

#include "gimp-intl.h"


48 49
#define SLIDER_WIDTH  200
#define SPINNER_WIDTH 4
50 51 52 53


/*  local function prototypes  */

54
static void       gimp_colorize_tool_finalize      (GObject          *object);
55

56 57 58
static gboolean   gimp_colorize_tool_initialize    (GimpTool         *tool,
                                                    GimpDisplay      *display,
                                                    GError          **error);
59

60 61
static GeglNode * gimp_colorize_tool_get_operation (GimpImageMapTool *im_tool,
                                                    GObject         **config);
62 63
static void       gimp_colorize_tool_map           (GimpImageMapTool *im_tool);
static void       gimp_colorize_tool_dialog        (GimpImageMapTool *im_tool);
64

65 66 67 68
static void       gimp_colorize_tool_config_notify (GObject          *object,
                                                    GParamSpec       *pspec,
                                                    GimpColorizeTool *col_tool);

69 70 71 72 73 74
static void       colorize_hue_changed             (GtkAdjustment    *adj,
                                                    GimpColorizeTool *col_tool);
static void       colorize_saturation_changed      (GtkAdjustment    *adj,
                                                    GimpColorizeTool *col_tool);
static void       colorize_lightness_changed       (GtkAdjustment    *adj,
                                                    GimpColorizeTool *col_tool);
75 76


77
G_DEFINE_TYPE (GimpColorizeTool, gimp_colorize_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
78

79
#define parent_class gimp_colorize_tool_parent_class
80 81 82 83 84 85 86


void
gimp_colorize_tool_register (GimpToolRegisterCallback  callback,
                             gpointer                  data)
{
  (* callback) (GIMP_TYPE_COLORIZE_TOOL,
87
                GIMP_TYPE_IMAGE_MAP_OPTIONS, NULL,
88
                0,
89 90
                "gimp-colorize-tool",
                _("Colorize"),
91
                _("Colorize Tool: Colorize the image"),
92
                N_("Colori_ze..."), NULL,
93
                NULL, GIMP_HELP_TOOL_COLORIZE,
94 95 96 97 98 99 100
                GIMP_STOCK_TOOL_COLORIZE,
                data);
}

static void
gimp_colorize_tool_class_init (GimpColorizeToolClass *klass)
{
101 102 103
  GObjectClass          *object_class  = G_OBJECT_CLASS (klass);
  GimpToolClass         *tool_class    = GIMP_TOOL_CLASS (klass);
  GimpImageMapToolClass *im_tool_class = GIMP_IMAGE_MAP_TOOL_CLASS (klass);
104

105
  object_class->finalize             = gimp_colorize_tool_finalize;
106

107
  tool_class->initialize             = gimp_colorize_tool_initialize;
108

109 110 111
  im_tool_class->shell_desc          = _("Colorize the Image");
  im_tool_class->settings_name       = "colorize";
  im_tool_class->import_dialog_title = _("Import Colorize Settings");
112
  im_tool_class->export_dialog_title = _("Export Colorize Settings");
113

114 115 116
  im_tool_class->get_operation       = gimp_colorize_tool_get_operation;
  im_tool_class->map                 = gimp_colorize_tool_map;
  im_tool_class->dialog              = gimp_colorize_tool_dialog;
117 118 119 120 121
}

static void
gimp_colorize_tool_init (GimpColorizeTool *col_tool)
{
122 123
  GimpImageMapTool *im_tool = GIMP_IMAGE_MAP_TOOL (col_tool);

124
  col_tool->colorize = g_slice_new0 (Colorize);
125 126

  colorize_init (col_tool->colorize);
127 128 129

  im_tool->apply_func = (GimpImageMapApplyFunc) colorize;
  im_tool->apply_data = col_tool->colorize;
130 131 132 133 134
}

static void
gimp_colorize_tool_finalize (GObject *object)
{
135
  GimpColorizeTool *col_tool = GIMP_COLORIZE_TOOL (object);
136

137 138
  g_slice_free (Colorize, col_tool->colorize);

139 140 141
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

142
static gboolean
143 144 145
gimp_colorize_tool_initialize (GimpTool     *tool,
                               GimpDisplay  *display,
                               GError      **error)
146
{
147
  GimpColorizeTool *col_tool = GIMP_COLORIZE_TOOL (tool);
148
  GimpDrawable     *drawable = gimp_image_get_active_drawable (display->image);
149

150
  if (! drawable)
151
    return FALSE;
152

153
  if (! gimp_drawable_is_rgb (drawable))
154
    {
155 156
      g_set_error (error, 0, 0,
                   _("Colorize operates only on RGB color layers."));
157
      return FALSE;
158 159
    }

160
  gimp_config_reset (GIMP_CONFIG (col_tool->config));
161

162
  GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error);
163 164

  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));
165

166
  return TRUE;
167 168
}

169
static GeglNode *
170 171
gimp_colorize_tool_get_operation (GimpImageMapTool  *im_tool,
                                  GObject          **config)
172
{
173
  GimpColorizeTool *col_tool = GIMP_COLORIZE_TOOL (im_tool);
174 175 176
  GeglNode         *node;

  node = g_object_new (GEGL_TYPE_NODE,
177
                       "operation", "gimp:colorize",
178
                       NULL);
179

180 181 182 183 184 185 186
  col_tool->config = g_object_new (GIMP_TYPE_COLORIZE_CONFIG, NULL);

  *config = G_OBJECT (col_tool->config);

  g_signal_connect_object (col_tool->config, "notify",
                           G_CALLBACK (gimp_colorize_tool_config_notify),
                           G_OBJECT (col_tool), 0);
187 188

  gegl_node_set (node,
189
                 "config", col_tool->config,
190 191 192
                 NULL);

  return node;
193 194
}

195 196 197
static void
gimp_colorize_tool_map (GimpImageMapTool *image_map_tool)
{
198
  GimpColorizeTool *col_tool = GIMP_COLORIZE_TOOL (image_map_tool);
199

200
  gimp_colorize_config_to_cruft (col_tool->config, col_tool->colorize);
201 202 203 204 205 206 207 208 209 210
}


/***************************/
/*  Hue-Saturation dialog  */
/***************************/

static void
gimp_colorize_tool_dialog (GimpImageMapTool *image_map_tool)
{
211
  GimpColorizeTool *col_tool = GIMP_COLORIZE_TOOL (image_map_tool);
212
  GtkWidget        *main_vbox;
213 214 215 216 217 218
  GtkWidget        *table;
  GtkWidget        *slider;
  GtkWidget        *frame;
  GtkWidget        *vbox;
  GtkObject        *data;

219 220
  main_vbox = gimp_image_map_tool_dialog_get_vbox (image_map_tool);

221
  frame = gimp_frame_new (_("Select Color"));
222
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
223 224 225
  gtk_widget_show (frame);

  /*  The table containing sliders  */
226
  vbox = gtk_vbox_new (FALSE, 6);
227 228 229 230 231 232 233 234 235 236 237
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);

  table = gtk_table_new (3, 3, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
  gtk_widget_show (table);

  /*  Create the hue scale widget  */
  data = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
238
                               _("_Hue:"), SLIDER_WIDTH, SPINNER_WIDTH,
239 240
                               col_tool->config->hue * 360.0,
                               0.0, 360.0, 1.0, 15.0, 0,
241 242 243 244
                               TRUE, 0.0, 0.0,
                               NULL, NULL);
  col_tool->hue_data = GTK_ADJUSTMENT (data);
  slider = GIMP_SCALE_ENTRY_SCALE (data);
245
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_CONTINUOUS);
246

247
  g_signal_connect (data, "value-changed",
248
                    G_CALLBACK (colorize_hue_changed),
249 250
                    col_tool);

251
  /*  Create the saturation scale widget  */
252
  data = gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
253
                               _("_Saturation:"), SLIDER_WIDTH, SPINNER_WIDTH,
254 255
                               col_tool->config->saturation * 100.0,
                               0.0, 100.0, 1.0, 10.0, 0,
256 257
                               TRUE, 0.0, 0.0,
                               NULL, NULL);
258
  col_tool->saturation_data = GTK_ADJUSTMENT (data);
259
  slider = GIMP_SCALE_ENTRY_SCALE (data);
260
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_CONTINUOUS);
261

262
  g_signal_connect (data, "value-changed",
263
                    G_CALLBACK (colorize_saturation_changed),
264 265
                    col_tool);

266
  /*  Create the lightness scale widget  */
267
  data = gimp_scale_entry_new (GTK_TABLE (table), 0, 2,
268
                               _("_Lightness:"), SLIDER_WIDTH, SPINNER_WIDTH,
269 270
                               col_tool->config->lightness * 100.0,
                               -100.0, 100.0, 1.0, 10.0, 0,
271 272
                               TRUE, 0.0, 0.0,
                               NULL, NULL);
273
  col_tool->lightness_data = GTK_ADJUSTMENT (data);
274
  slider = GIMP_SCALE_ENTRY_SCALE (data);
275
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_CONTINUOUS);
276

277
  g_signal_connect (data, "value-changed",
278
                    G_CALLBACK (colorize_lightness_changed),
279 280 281 282
                    col_tool);
}

static void
283 284 285
gimp_colorize_tool_config_notify (GObject          *object,
                                  GParamSpec       *pspec,
                                  GimpColorizeTool *col_tool)
286
{
287
  GimpColorizeConfig *config = GIMP_COLORIZE_CONFIG (object);
288

289 290
  if (! col_tool->hue_data)
    return;
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
  if (! strcmp (pspec->name, "hue"))
    {
      gtk_adjustment_set_value (col_tool->hue_data,
                                config->hue * 360.0);
    }
  else if (! strcmp (pspec->name, "saturation"))
    {
      gtk_adjustment_set_value (col_tool->saturation_data,
                                config->saturation * 100.0);
    }
  else if (! strcmp (pspec->name, "lightness"))
    {
      gtk_adjustment_set_value (col_tool->lightness_data,
                                config->lightness * 100.0);
    }
307

308
  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (col_tool));
309 310 311
}

static void
312 313
colorize_hue_changed (GtkAdjustment    *adjustment,
                      GimpColorizeTool *col_tool)
314
{
315
  gdouble value = gtk_adjustment_get_value (adjustment) / 360.0;
316 317

  if (col_tool->config->hue != value)
318
    {
319 320 321
      g_object_set (col_tool->config,
                    "hue", value,
                    NULL);
322 323 324 325
    }
}

static void
326 327
colorize_saturation_changed (GtkAdjustment    *adjustment,
                             GimpColorizeTool *col_tool)
328
{
329
  gdouble value = gtk_adjustment_get_value (adjustment) / 100.0;
330 331

  if (col_tool->config->saturation != value)
332
    {
333 334 335
      g_object_set (col_tool->config,
                    "saturation", value,
                    NULL);
336 337 338 339
    }
}

static void
340 341
colorize_lightness_changed (GtkAdjustment    *adjustment,
                            GimpColorizeTool *col_tool)
342
{
343
  gdouble value = gtk_adjustment_get_value (adjustment) / 100.0;
344 345

  if (col_tool->config->lightness != value)
346
    {
347 348 349
      g_object_set (col_tool->config,
                    "lightness", value,
                    NULL);
350 351
    }
}