hue-saturation.c 26.1 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
18

19
#include "config.h"
20

21 22 23 24 25
#ifdef __GNUC__
#warning GTK_DISABLE_DEPRECATED
#endif
#undef GTK_DISABLE_DEPRECATED

26
#include <gtk/gtk.h>
Sven Neumann's avatar
Sven Neumann committed
27

28 29
#include "libgimpcolor/gimpcolor.h"
#include "libgimpmath/gimpmath.h"
30
#include "libgimpwidgets/gimpwidgets.h"
31

Nate Summers's avatar
Nate Summers committed
32 33 34
#include "core/core-types.h"
#include "display/display-types.h"
#include "libgimptool/gimptooltypes.h"
Michael Natterer's avatar
Michael Natterer committed
35 36 37 38 39 40 41

#include "base/pixel-region.h"

#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "core/gimpimage-mask.h"

42
#include "display/gimpdisplay.h"
43
#include "display/gimpdisplay-foreach.h"
44

Michael Natterer's avatar
Michael Natterer committed
45 46
#include "gimphuesaturationtool.h"
#include "tool_manager.h"
Sven Neumann's avatar
Sven Neumann committed
47

48
#include "app_procs.h"
49
#include "image_map.h"
Elliot Lee's avatar
Elliot Lee committed
50

51 52
#include "libgimp/gimpintl.h"

53

Elliot Lee's avatar
Elliot Lee committed
54 55 56
#define HUE_PARTITION_MASK  GDK_EXPOSURE_MASK | GDK_ENTER_NOTIFY_MASK

#define SLIDER_WIDTH  200
57 58
#define DA_WIDTH       40
#define DA_HEIGHT      20
Elliot Lee's avatar
Elliot Lee committed
59

60 61 62 63 64 65
#define HUE_PARTITION      0x0
#define HUE_SLIDER         0x1
#define LIGHTNESS_SLIDER   0x2
#define SATURATION_SLIDER  0x4
#define DRAW               0x40
#define ALL                0xFF
Elliot Lee's avatar
Elliot Lee committed
66 67


Michael Natterer's avatar
Michael Natterer committed
68
/*  local function prototypes  */
69

Michael Natterer's avatar
Michael Natterer committed
70 71
static void   gimp_hue_saturation_tool_class_init (GimpHueSaturationToolClass *klass);
static void   gimp_hue_saturation_tool_init       (GimpHueSaturationTool      *bc_tool);
Elliot Lee's avatar
Elliot Lee committed
72

73 74 75 76 77
static void   gimp_hue_saturation_tool_initialize (GimpTool       *tool,
                                                   GimpDisplay    *gdisp);
static void   gimp_hue_saturation_tool_control    (GimpTool       *tool,
                                                   GimpToolAction  action,
                                                   GimpDisplay    *gdisp);
78 79 80

static HueSaturationDialog * hue_saturation_dialog_new (void);

81
static void   hue_saturation_dialog_hide             (void);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static void   hue_saturation_update                  (HueSaturationDialog *hsd,
						      gint);
static void   hue_saturation_preview                 (HueSaturationDialog *hsd);
static void   hue_saturation_reset_callback          (GtkWidget *,
						      gpointer);
static void   hue_saturation_ok_callback             (GtkWidget *,
						      gpointer);
static void   hue_saturation_cancel_callback         (GtkWidget *,
						      gpointer);
static void   hue_saturation_partition_callback      (GtkWidget *,
						      gpointer);
static void   hue_saturation_preview_update          (GtkWidget *,
						      gpointer);
static void   hue_saturation_hue_adjustment_update        (GtkAdjustment *,
							   gpointer);
static void   hue_saturation_lightness_adjustment_update  (GtkAdjustment *,
							   gpointer);
static void   hue_saturation_saturation_adjustment_update (GtkAdjustment *,
							   gpointer);
static gint   hue_saturation_hue_partition_events    (GtkWidget *,
						      GdkEvent *,
						      HueSaturationDialog *hsd);


106
/*  Local variables  */
107 108 109 110
static gint hue_transfer[6][256];
static gint lightness_transfer[6][256];
static gint saturation_transfer[6][256];
static gint default_colors[6][3] =
111
{
112 113 114 115 116 117
  { 255,   0,   0 },
  { 255, 255,   0 },
  {   0, 255,   0 },
  {   0, 255, 255 },
  {   0,   0, 255 },
  { 255,   0, 255 }
118
};
Elliot Lee's avatar
Elliot Lee committed
119

120
static HueSaturationDialog *hue_saturation_dialog = NULL;
Elliot Lee's avatar
Elliot Lee committed
121

122 123 124 125
static GimpImageMapToolClass *parent_class = NULL;


/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
126 127

void
Nate Summers's avatar
Nate Summers committed
128 129
gimp_hue_saturation_tool_register (GimpToolRegisterCallback  callback,
                                   Gimp                     *gimp)
Michael Natterer's avatar
Michael Natterer committed
130
{
Nate Summers's avatar
Nate Summers committed
131
  (* callback) (GIMP_TYPE_HUE_SATURATION_TOOL,
132 133
                NULL,
                FALSE,
134
                "gimp-hue-saturation-tool",
135 136
                _("Hue-Saturation"),
                _("Adjust hue and saturation"),
137
                N_("/Layer/Colors/Hue-Saturation..."), NULL,
138
                NULL, "tools/hue_saturation.html",
Nate Summers's avatar
Nate Summers committed
139 140
                GIMP_STOCK_TOOL_HUE_SATURATION,
                gimp);
Michael Natterer's avatar
Michael Natterer committed
141 142
}

143
GType
Michael Natterer's avatar
Michael Natterer committed
144 145
gimp_hue_saturation_tool_get_type (void)
{
146
  static GType tool_type = 0;
Michael Natterer's avatar
Michael Natterer committed
147 148 149

  if (! tool_type)
    {
150
      static const GTypeInfo tool_info =
Michael Natterer's avatar
Michael Natterer committed
151 152
      {
        sizeof (GimpHueSaturationToolClass),
153 154 155 156 157 158 159 160
	(GBaseInitFunc) NULL,
	(GBaseFinalizeFunc) NULL,
	(GClassInitFunc) gimp_hue_saturation_tool_class_init,
	NULL,           /* class_finalize */
	NULL,           /* class_data     */
	sizeof (GimpHueSaturationTool),
	0,              /* n_preallocs    */
	(GInstanceInitFunc) gimp_hue_saturation_tool_init,
Michael Natterer's avatar
Michael Natterer committed
161 162
      };

163 164 165
      tool_type = g_type_register_static (GIMP_TYPE_IMAGE_MAP_TOOL,
					  "GimpHueSaturationTool", 
                                          &tool_info, 0);
Michael Natterer's avatar
Michael Natterer committed
166 167 168 169 170 171 172 173
    }

  return tool_type;
}

static void
gimp_hue_saturation_tool_class_init (GimpHueSaturationToolClass *klass)
{
174
  GimpToolClass *tool_class;
Michael Natterer's avatar
Michael Natterer committed
175

176
  tool_class = GIMP_TOOL_CLASS (klass);
Michael Natterer's avatar
Michael Natterer committed
177

178
  parent_class = g_type_class_peek_parent (klass);
Michael Natterer's avatar
Michael Natterer committed
179 180 181 182 183 184

  tool_class->initialize = gimp_hue_saturation_tool_initialize;
  tool_class->control    = gimp_hue_saturation_tool_control;
}

static void
Nate Summers's avatar
Nate Summers committed
185
gimp_hue_saturation_tool_init (GimpHueSaturationTool *tool)
Michael Natterer's avatar
Michael Natterer committed
186
{
Nate Summers's avatar
Nate Summers committed
187 188 189 190 191 192 193 194 195 196 197
  GIMP_TOOL(tool)->control = gimp_tool_control_new  (FALSE,                      /* scroll_lock */
                                                     TRUE,                       /* auto_snap_to */
                                                     TRUE,                       /* preserve */
                                                     FALSE,                      /* handle_empty_image */
                                                     FALSE,                      /* perfectmouse */
                                                     GIMP_MOUSE_CURSOR,          /* cursor */
                                                     GIMP_TOOL_CURSOR_NONE,      /* tool_cursor */
                                                     GIMP_CURSOR_MODIFIER_NONE,  /* cursor_modifier */
                                                     GIMP_MOUSE_CURSOR,          /* toggle_cursor */
                                                     GIMP_TOOL_CURSOR_NONE,      /* toggle_tool_cursor */
                                                     GIMP_CURSOR_MODIFIER_NONE   /* toggle_cursor_modifier */);
Michael Natterer's avatar
Michael Natterer committed
198 199 200
}

static void
Michael Natterer's avatar
Michael Natterer committed
201 202
gimp_hue_saturation_tool_initialize (GimpTool    *tool,
				     GimpDisplay *gdisp)
Michael Natterer's avatar
Michael Natterer committed
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
{
  gint i;

  if (! gdisp)
    {
      hue_saturation_dialog_hide ();
      return;
    }

  if (! gimp_drawable_is_rgb (gimp_image_active_drawable (gdisp->gimage)))
    {
      g_message (_("Hue-Saturation operates only on RGB color drawables."));
      return;
    }

  /*  The "hue-saturation" dialog  */
  if (!hue_saturation_dialog)
    hue_saturation_dialog = hue_saturation_dialog_new ();
  else
    if (!GTK_WIDGET_VISIBLE (hue_saturation_dialog->shell))
      gtk_widget_show (hue_saturation_dialog->shell);

  for (i = 0; i < 7; i++)
    {
      hue_saturation_dialog->hue[i] = 0.0;
      hue_saturation_dialog->lightness[i] = 0.0;
      hue_saturation_dialog->saturation[i] = 0.0;
    }

  hue_saturation_dialog->drawable = gimp_image_active_drawable (gdisp->gimage);
  hue_saturation_dialog->image_map =
    image_map_create (gdisp, hue_saturation_dialog->drawable);

  hue_saturation_update (hue_saturation_dialog, ALL);
}

static void
240 241 242
gimp_hue_saturation_tool_control (GimpTool       *tool,
				  GimpToolAction  action,
				  GimpDisplay    *gdisp)
Michael Natterer's avatar
Michael Natterer committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
{
  switch (action)
    {
    case PAUSE:
      break;

    case RESUME:
      break;

    case HALT:
      hue_saturation_dialog_hide ();
      break;

    default:
      break;
    }

260
  GIMP_TOOL_CLASS (parent_class)->control (tool, action, gdisp);
Michael Natterer's avatar
Michael Natterer committed
261 262 263
}


Elliot Lee's avatar
Elliot Lee committed
264 265
/*  hue saturation machinery  */

Manish Singh's avatar
Manish Singh committed
266
void
Elliot Lee's avatar
Elliot Lee committed
267 268
hue_saturation_calculate_transfers (HueSaturationDialog *hsd)
{
269 270 271
  gint value;
  gint hue;
  gint i;
Elliot Lee's avatar
Elliot Lee committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

  /*  Calculate transfers  */
  for (hue = 0; hue < 6; hue++)
    for (i = 0; i < 256; i++)
      {
	value = (hsd->hue[0] + hsd->hue[hue + 1]) * 255.0 / 360.0;
	if ((i + value) < 0)
	  hue_transfer[hue][i] = 255 + (i + value);
	else if ((i + value) > 255)
	  hue_transfer[hue][i] = i + value - 255;
	else
	  hue_transfer[hue][i] = i + value;

	/*  Lightness  */
	value = (hsd->lightness[0] + hsd->lightness[hue + 1]) * 127.0 / 100.0;
287
	value = CLAMP (value, -255, 255);
Elliot Lee's avatar
Elliot Lee committed
288 289 290 291 292 293 294
	if (value < 0)
	  lightness_transfer[hue][i] = (unsigned char) ((i * (255 + value)) / 255);
	else
	  lightness_transfer[hue][i] = (unsigned char) (i + ((255 - i) * value) / 255);

	/*  Saturation  */
	value = (hsd->saturation[0] + hsd->saturation[hue + 1]) * 255.0 / 100.0;
295
	value = CLAMP (value, -255, 255);
296 297 298 299 300 301 302 303 304

	/* This change affects the way saturation is computed. With the
	   old code (different code for value < 0), increasing the
	   saturation affected muted colors very much, and bright colors
	   less. With the new code, it affects muted colors and bright
	   colors more or less evenly. For enhancing the color in photos,
	   the new behavior is exactly what you want. It's hard for me
	   to imagine a case in which the old behavior is better.
	*/
305
	saturation_transfer[hue][i] = CLAMP ((i * (255 + value)) / 255, 0, 255);
Elliot Lee's avatar
Elliot Lee committed
306 307 308
      }
}

Manish Singh's avatar
Manish Singh committed
309
void
Elliot Lee's avatar
Elliot Lee committed
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
hue_saturation (PixelRegion *srcPR,
		PixelRegion *destPR,
		void        *user_data)
{
  HueSaturationDialog *hsd;
  unsigned char *src, *s;
  unsigned char *dest, *d;
  int alpha;
  int w, h;
  int r, g, b;
  int hue;

  hsd = (HueSaturationDialog *) user_data;

  /*  Set the transfer arrays  (for speed)  */
  h = srcPR->h;
  src = srcPR->data;
  dest = destPR->data;
  alpha = (srcPR->bytes == 4) ? TRUE : FALSE;

  while (h--)
    {
      w = srcPR->w;
      s = src;
      d = dest;
      while (w--)
	{
	  r = s[RED_PIX];
	  g = s[GREEN_PIX];
	  b = s[BLUE_PIX];

341
	  gimp_rgb_to_hls_int (&r, &g, &b);
Elliot Lee's avatar
Elliot Lee committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

	  if (r < 43)
	    hue = 0;
	  else if (r < 85)
	    hue = 1;
	  else if (r < 128)
	    hue = 2;
	  else if (r < 171)
	    hue = 3;
	  else if (r < 213)
	    hue = 4;
	  else
	    hue = 5;

	  r = hue_transfer[hue][r];
	  g = lightness_transfer[hue][g];
	  b = saturation_transfer[hue][b];

360
	  gimp_hls_to_rgb_int (&r, &g, &b);
Elliot Lee's avatar
Elliot Lee committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377

	  d[RED_PIX] = r;
	  d[GREEN_PIX] = g;
	  d[BLUE_PIX] = b;

	  if (alpha)
	    d[ALPHA_PIX] = s[ALPHA_PIX];

	  s += srcPR->bytes;
	  d += destPR->bytes;
	}

      src += srcPR->rowstride;
      dest += destPR->rowstride;
    }
}

378
/*  hue saturation action functions  */
Elliot Lee's avatar
Elliot Lee committed
379 380

void
381
hue_saturation_free (void)
Elliot Lee's avatar
Elliot Lee committed
382
{
383 384 385 386
  GimpTool *active_tool;

  active_tool = tool_manager_get_active (the_gimp);

Elliot Lee's avatar
Elliot Lee committed
387 388 389 390
  if (hue_saturation_dialog)
    {
      if (hue_saturation_dialog->image_map)
	{
Nate Summers's avatar
Nate Summers committed
391
	  gimp_tool_control_set_preserve(active_tool->control, TRUE);
Elliot Lee's avatar
Elliot Lee committed
392
	  image_map_abort (hue_saturation_dialog->image_map);
Nate Summers's avatar
Nate Summers committed
393
	  gimp_tool_control_set_preserve(active_tool->control, FALSE);
394

Elliot Lee's avatar
Elliot Lee committed
395 396 397 398 399 400
	  hue_saturation_dialog->image_map = NULL;
	}
      gtk_widget_destroy (hue_saturation_dialog->shell);
    }
}

401 402 403
/***************************/
/*  Hue-Saturation dialog  */
/***************************/
Elliot Lee's avatar
Elliot Lee committed
404

405
static HueSaturationDialog *
406
hue_saturation_dialog_new (void)
Elliot Lee's avatar
Elliot Lee committed
407 408 409 410 411 412 413 414 415
{
  HueSaturationDialog *hsd;
  GtkWidget *main_vbox;
  GtkWidget *main_hbox;
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *table;
  GtkWidget *label;
  GtkWidget *slider;
416 417
  GtkWidget *abox;
  GtkWidget *spinbutton;
Elliot Lee's avatar
Elliot Lee committed
418 419 420 421 422
  GtkWidget *toggle;
  GtkWidget *radio_button;
  GtkWidget *frame;
  GtkObject *data;
  GSList *group = NULL;
423
  gint i;
424

425
  gchar *hue_partition_names[] =
Elliot Lee's avatar
Elliot Lee committed
426
  {
427 428 429 430 431 432 433
    N_("Master"),
    N_("R"),
    N_("Y"),
    N_("G"),
    N_("C"),
    N_("B"),
    N_("M")
Elliot Lee's avatar
Elliot Lee committed
434
  };
435

436
  hsd = g_new (HueSaturationDialog, 1);
437
  hsd->hue_partition = ALL_HUES;
438
  hsd->preview       = TRUE;
Elliot Lee's avatar
Elliot Lee committed
439 440

  /*  The shell and main vbox  */
441
  hsd->shell = gimp_dialog_new (_("Hue-Saturation"), "hue_saturation",
Michael Natterer's avatar
Michael Natterer committed
442
				tool_manager_help_func, NULL,
443 444 445
				GTK_WIN_POS_NONE,
				FALSE, TRUE, FALSE,

446
				GTK_STOCK_CANCEL, hue_saturation_cancel_callback,
447
				hsd, NULL, NULL, FALSE, TRUE,
448

449 450 451
				GIMP_STOCK_RESET, hue_saturation_reset_callback,
				hsd, NULL, NULL, FALSE, FALSE,

452 453 454
				GTK_STOCK_OK, hue_saturation_ok_callback,
				hsd, NULL, NULL, TRUE, FALSE,

455
				NULL);
Elliot Lee's avatar
Elliot Lee committed
456

457 458
  main_vbox = gtk_vbox_new (FALSE, 4);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 4);
459
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (hsd->shell)->vbox), main_vbox);
Elliot Lee's avatar
Elliot Lee committed
460 461

  /*  The main hbox containing hue partitions and sliders  */
462
  main_hbox = gtk_hbox_new (FALSE, 12);
Elliot Lee's avatar
Elliot Lee committed
463 464 465 466
  gtk_box_pack_start (GTK_BOX (main_vbox), main_hbox, FALSE, FALSE, 0);

  /*  The table containing hue partitions  */
  table = gtk_table_new (7, 2, FALSE);
467 468
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
Elliot Lee's avatar
Elliot Lee committed
469 470 471 472 473
  gtk_box_pack_start (GTK_BOX (main_hbox), table, FALSE, FALSE, 0);

  /*  the radio buttons for hue partitions  */
  for (i = 0; i < 7; i++)
    {
Stanislav Brabec's avatar
Stanislav Brabec committed
474
      radio_button = gtk_radio_button_new_with_label (group, gettext (hue_partition_names[i]));
475
      group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio_button));
476
      g_object_set_data (G_OBJECT (radio_button), "hue_partition",
477
			   (gpointer) i);
Elliot Lee's avatar
Elliot Lee committed
478 479 480 481

      if (!i)
	{
	  gtk_table_attach (GTK_TABLE (table), radio_button, 0, 2, 0, 1,
482
			    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
483 484 485 486
	}
      else
	{
	  gtk_table_attach (GTK_TABLE (table), radio_button, 0, 1, i, i + 1,
487
			    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
488 489 490 491

	  frame = gtk_frame_new (NULL);
	  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
	  gtk_table_attach (GTK_TABLE (table), frame,  1, 2, i, i + 1,
492
			    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
493
	  hsd->hue_partition_da[i - 1] = gtk_preview_new (GTK_PREVIEW_COLOR);
494 495 496 497
	  gtk_preview_size (GTK_PREVIEW (hsd->hue_partition_da[i - 1]),
			    DA_WIDTH, DA_HEIGHT);
	  gtk_widget_set_events (hsd->hue_partition_da[i - 1],
				 HUE_PARTITION_MASK);
498 499 500
	  g_signal_connect (G_OBJECT (hsd->hue_partition_da[i - 1]), "event",
                            G_CALLBACK (hue_saturation_hue_partition_events),
                            hsd);
Elliot Lee's avatar
Elliot Lee committed
501 502 503 504 505 506
	  gtk_container_add (GTK_CONTAINER (frame), hsd->hue_partition_da[i - 1]);

	  gtk_widget_show (hsd->hue_partition_da[i - 1]);
	  gtk_widget_show (frame);
	}

507 508 509
      g_signal_connect (G_OBJECT (radio_button), "toggled",
                        G_CALLBACK (hue_saturation_partition_callback),
                        hsd);
510

Elliot Lee's avatar
Elliot Lee committed
511 512
      gtk_widget_show (radio_button);
    }
513

Elliot Lee's avatar
Elliot Lee committed
514 515 516
  gtk_widget_show (table);

  /*  The vbox for the table and preview toggle  */
517
  vbox = gtk_vbox_new (FALSE, 4);
Elliot Lee's avatar
Elliot Lee committed
518 519
  gtk_box_pack_start (GTK_BOX (main_hbox), vbox, FALSE, FALSE, 0);

520
  label = gtk_label_new (_("Hue / Lightness / Saturation Adjustments"));
Elliot Lee's avatar
Elliot Lee committed
521 522 523 524 525 526
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  /*  The table containing sliders  */
  table = gtk_table_new (3, 3, FALSE);
527 528
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
Elliot Lee's avatar
Elliot Lee committed
529 530 531
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);

  /*  Create the hue scale widget  */
532
  label = gtk_label_new (_("Hue:"));
533
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
Elliot Lee's avatar
Elliot Lee committed
534
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
535
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
536 537 538

  data = gtk_adjustment_new (0, -180, 180.0, 1.0, 1.0, 0.0);
  hsd->hue_data = GTK_ADJUSTMENT (data);
539 540

  slider = gtk_hscale_new (hsd->hue_data);
541
  gtk_widget_set_size_request (slider, SLIDER_WIDTH, -1);
Elliot Lee's avatar
Elliot Lee committed
542 543 544 545 546
  gtk_scale_set_digits (GTK_SCALE (slider), 0);
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  gtk_table_attach (GTK_TABLE (table), slider, 1, 2, 0, 1,
		    GTK_EXPAND | GTK_SHRINK | GTK_FILL,
547 548 549 550
		    GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);

  abox = gtk_vbox_new (FALSE, 0);
  spinbutton = gtk_spin_button_new (hsd->hue_data, 1.0, 0);
551
  gtk_widget_set_size_request (spinbutton, 74, -1);
552 553 554
  gtk_box_pack_end (GTK_BOX (abox), spinbutton, FALSE, FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), abox, 2, 3, 0, 1,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
555

556 557 558
  g_signal_connect (G_OBJECT (hsd->hue_data), "value_changed",
                    G_CALLBACK (hue_saturation_hue_adjustment_update),
                    hsd);
Elliot Lee's avatar
Elliot Lee committed
559 560 561

  gtk_widget_show (label);
  gtk_widget_show (slider);
562 563
  gtk_widget_show (spinbutton);
  gtk_widget_show (abox);
Elliot Lee's avatar
Elliot Lee committed
564 565

  /*  Create the lightness scale widget  */
566
  label = gtk_label_new (_("Lightness:"));
567
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
Elliot Lee's avatar
Elliot Lee committed
568
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
569
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
570 571 572

  data = gtk_adjustment_new (0, -100.0, 100.0, 1.0, 1.0, 0.0);
  hsd->lightness_data = GTK_ADJUSTMENT (data);
573 574

  slider = gtk_hscale_new (hsd->lightness_data);
575
  gtk_widget_set_size_request (slider, SLIDER_WIDTH, -1);
Elliot Lee's avatar
Elliot Lee committed
576 577 578 579 580
  gtk_scale_set_digits (GTK_SCALE (slider), 0);
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  gtk_table_attach (GTK_TABLE (table), slider, 1, 2, 1, 2,
		    GTK_EXPAND | GTK_SHRINK | GTK_FILL,
581 582 583 584
		    GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);

  abox = gtk_vbox_new (FALSE, 0);
  spinbutton = gtk_spin_button_new (hsd->lightness_data, 1.0, 0);
585
  gtk_widget_set_size_request (spinbutton, 75, -1);
586 587 588
  gtk_box_pack_end (GTK_BOX (abox), spinbutton, FALSE, FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), abox, 2, 3, 1, 2,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
589

590 591 592
  g_signal_connect (G_OBJECT (hsd->lightness_data), "value_changed",
                    G_CALLBACK (hue_saturation_lightness_adjustment_update),
                    hsd);
Elliot Lee's avatar
Elliot Lee committed
593 594 595

  gtk_widget_show (label);
  gtk_widget_show (slider);
596 597
  gtk_widget_show (spinbutton);
  gtk_widget_show (abox);
Elliot Lee's avatar
Elliot Lee committed
598 599

  /*  Create the saturation scale widget  */
600
  label = gtk_label_new (_("Saturation:"));
601
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
Elliot Lee's avatar
Elliot Lee committed
602
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3,
603
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
604 605 606

  data = gtk_adjustment_new (0, -100.0, 100.0, 1.0, 1.0, 0.0);
  hsd->saturation_data = GTK_ADJUSTMENT (data);
607 608

  slider = gtk_hscale_new (hsd->saturation_data);
609
  gtk_widget_set_size_request (slider, SLIDER_WIDTH, -1);
Elliot Lee's avatar
Elliot Lee committed
610 611 612 613 614
  gtk_scale_set_digits (GTK_SCALE (slider), 0);
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  gtk_table_attach (GTK_TABLE (table), slider, 1, 2, 2, 3,
		    GTK_EXPAND | GTK_SHRINK | GTK_FILL,
615
		    GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
616

617 618
  abox = gtk_vbox_new (FALSE, 0);
  spinbutton = gtk_spin_button_new (hsd->saturation_data, 1.0, 0);
619
  gtk_widget_set_size_request (spinbutton, 75, -1);
620 621 622 623
  gtk_box_pack_end (GTK_BOX (abox), spinbutton, FALSE, FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), abox, 2, 3, 2, 3,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);

624 625 626
  g_signal_connect (G_OBJECT (hsd->saturation_data), "value_changed",
                    G_CALLBACK (hue_saturation_saturation_adjustment_update),
                    hsd);
Elliot Lee's avatar
Elliot Lee committed
627 628 629

  gtk_widget_show (label);
  gtk_widget_show (slider);
630 631
  gtk_widget_show (spinbutton);
  gtk_widget_show (abox);
Elliot Lee's avatar
Elliot Lee committed
632

633
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
634

635 636 637
  /*  Horizontal box for preview toggle button  */
  hbox = gtk_hbox_new (FALSE, 4);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
Elliot Lee's avatar
Elliot Lee committed
638 639

  /*  The preview toggle  */
640
  toggle = gtk_check_button_new_with_label (_("Preview"));
641
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), hsd->preview);
642 643
  gtk_box_pack_end (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);

644 645 646
  g_signal_connect (G_OBJECT (toggle), "toggled",
                    G_CALLBACK (hue_saturation_preview_update),
                    hsd);
Elliot Lee's avatar
Elliot Lee committed
647 648 649 650 651 652 653 654 655 656 657 658

  gtk_widget_show (toggle);
  gtk_widget_show (hbox);

  gtk_widget_show (vbox);
  gtk_widget_show (main_hbox);
  gtk_widget_show (main_vbox);
  gtk_widget_show (hsd->shell);

  return hsd;
}

659 660 661 662 663 664 665
static void
hue_saturation_dialog_hide (void)
{
  if (hue_saturation_dialog)
    hue_saturation_cancel_callback (NULL, (gpointer) hue_saturation_dialog);
} 
  
Elliot Lee's avatar
Elliot Lee committed
666 667
static void
hue_saturation_update (HueSaturationDialog *hsd,
668
		       gint                 update)
Elliot Lee's avatar
Elliot Lee committed
669
{
670 671 672
  gint i, j, b;
  gint rgb[3];
  guchar buf[DA_WIDTH * 3];
Elliot Lee's avatar
Elliot Lee committed
673 674 675

  if (update & HUE_SLIDER)
    {
676 677
      gtk_adjustment_set_value (GTK_ADJUSTMENT (hsd->hue_data),
				hsd->hue[hsd->hue_partition]);
Elliot Lee's avatar
Elliot Lee committed
678 679 680
    }
  if (update & LIGHTNESS_SLIDER)
    {
681 682
      gtk_adjustment_set_value (GTK_ADJUSTMENT (hsd->lightness_data),
				hsd->lightness[hsd->hue_partition]);
Elliot Lee's avatar
Elliot Lee committed
683 684 685
    }
  if (update & SATURATION_SLIDER)
    {
686 687
      gtk_adjustment_set_value (GTK_ADJUSTMENT (hsd->saturation_data),
				hsd->saturation[hsd->hue_partition]);
Elliot Lee's avatar
Elliot Lee committed
688 689 690 691 692 693
    }

  hue_saturation_calculate_transfers (hsd);

  for (i = 0; i < 6; i++)
    {
694
      rgb[RED_PIX]   = default_colors[i][RED_PIX];
Elliot Lee's avatar
Elliot Lee committed
695
      rgb[GREEN_PIX] = default_colors[i][GREEN_PIX];
696
      rgb[BLUE_PIX]  = default_colors[i][BLUE_PIX];
Elliot Lee's avatar
Elliot Lee committed
697

698
      gimp_rgb_to_hls_int (rgb, rgb + 1, rgb + 2);
Elliot Lee's avatar
Elliot Lee committed
699

700
      rgb[RED_PIX]   = hue_transfer[i][rgb[RED_PIX]];
Elliot Lee's avatar
Elliot Lee committed
701
      rgb[GREEN_PIX] = lightness_transfer[i][rgb[GREEN_PIX]];
702
      rgb[BLUE_PIX]  = saturation_transfer[i][rgb[BLUE_PIX]];
Elliot Lee's avatar
Elliot Lee committed
703

704
      gimp_hls_to_rgb_int (rgb, rgb + 1, rgb + 2);
Elliot Lee's avatar
Elliot Lee committed
705 706 707

      for (j = 0; j < DA_WIDTH; j++)
	for (b = 0; b < 3; b++)
708
	  buf[j * 3 + b] = (guchar) rgb[b];
Elliot Lee's avatar
Elliot Lee committed
709 710

      for (j = 0; j < DA_HEIGHT; j++)
711 712
	gtk_preview_draw_row (GTK_PREVIEW (hsd->hue_partition_da[i]),
			      buf, 0, j, DA_WIDTH);
Elliot Lee's avatar
Elliot Lee committed
713 714

      if (update & DRAW)
715
	gtk_widget_queue_draw (hsd->hue_partition_da[i]);
Elliot Lee's avatar
Elliot Lee committed
716 717 718 719 720 721
    }
}

static void
hue_saturation_preview (HueSaturationDialog *hsd)
{
722 723 724 725
  GimpTool *active_tool;

  active_tool = tool_manager_get_active (the_gimp);

Elliot Lee's avatar
Elliot Lee committed
726
  if (!hsd->image_map)
727 728 729 730 731
    {
      g_warning ("hue_saturation_preview(): No image map");
      return;
    }

Nate Summers's avatar
Nate Summers committed
732
  gimp_tool_control_set_preserve(active_tool->control, TRUE);
Elliot Lee's avatar
Elliot Lee committed
733
  image_map_apply (hsd->image_map, hue_saturation, (void *) hsd);
Nate Summers's avatar
Nate Summers committed
734
  gimp_tool_control_set_preserve(active_tool->control, FALSE);
Elliot Lee's avatar
Elliot Lee committed
735 736
}

737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
static void
hue_saturation_reset_callback (GtkWidget *widget,
			       gpointer   data)
{
  HueSaturationDialog *hsd;

  hsd = (HueSaturationDialog *) data;

  hsd->hue[hsd->hue_partition] = 0.0;
  hsd->lightness[hsd->hue_partition] = 0.0;
  hsd->saturation[hsd->hue_partition] = 0.0;

  hue_saturation_update (hsd, ALL);

  if (hsd->preview)
    hue_saturation_preview (hsd);
}

Elliot Lee's avatar
Elliot Lee committed
755 756
static void
hue_saturation_ok_callback (GtkWidget *widget,
757
			    gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
758 759
{
  HueSaturationDialog *hsd;
760
  GimpTool            *active_tool;
Elliot Lee's avatar
Elliot Lee committed
761

762
  hsd = (HueSaturationDialog *) data;
Elliot Lee's avatar
Elliot Lee committed
763

764
  gtk_widget_hide (hsd->shell);
Elliot Lee's avatar
Elliot Lee committed
765

766 767
  active_tool = tool_manager_get_active (the_gimp);

Nate Summers's avatar
Nate Summers committed
768
  gimp_tool_control_set_preserve(active_tool->control, TRUE);
769

Elliot Lee's avatar
Elliot Lee committed
770
  if (!hsd->preview)
771
    image_map_apply (hsd->image_map, hue_saturation, (gpointer) hsd);
Elliot Lee's avatar
Elliot Lee committed
772 773 774 775

  if (hsd->image_map)
    image_map_commit (hsd->image_map);

Nate Summers's avatar
Nate Summers committed
776
  gimp_tool_control_set_preserve(active_tool->control, FALSE);
777

Elliot Lee's avatar
Elliot Lee committed
778
  hsd->image_map = NULL;
779

780
  active_tool->gdisp    = NULL;
781
  active_tool->drawable = NULL;
Elliot Lee's avatar
Elliot Lee committed
782 783 784 785
}

static void
hue_saturation_cancel_callback (GtkWidget *widget,
786
				gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
787 788
{
  HueSaturationDialog *hsd;
789
  GimpTool            *active_tool;
Elliot Lee's avatar
Elliot Lee committed
790

791 792
  hsd = (HueSaturationDialog *) data;

793
  gtk_widget_hide (hsd->shell);
Elliot Lee's avatar
Elliot Lee committed
794

795 796
  active_tool = tool_manager_get_active (the_gimp);

Elliot Lee's avatar
Elliot Lee committed
797 798
  if (hsd->image_map)
    {
Nate Summers's avatar
Nate Summers committed
799
      gimp_tool_control_set_preserve(active_tool->control, TRUE);
Elliot Lee's avatar
Elliot Lee committed
800
      image_map_abort (hsd->image_map);
Nate Summers's avatar
Nate Summers committed
801
      gimp_tool_control_set_preserve(active_tool->control, FALSE);
802

Elliot Lee's avatar
Elliot Lee committed
803
      gdisplays_flush ();
804
      hsd->image_map = NULL;
Elliot Lee's avatar
Elliot Lee committed
805
    }
806

807
  active_tool->gdisp    = NULL;
808
  active_tool->drawable = NULL;
Elliot Lee's avatar
Elliot Lee committed
809 810 811
}

static void
812 813
hue_saturation_partition_callback (GtkWidget *widget,
				   gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
814 815
{
  HueSaturationDialog *hsd;
816
  HueRange partition;
Elliot Lee's avatar
Elliot Lee committed
817

818
  hsd = (HueSaturationDialog *) data;
Elliot Lee's avatar
Elliot Lee committed
819

820
  partition = (HueRange) g_object_get_data (G_OBJECT (widget),
821 822
					      "hue_partition");
  hsd->hue_partition = partition;
Elliot Lee's avatar
Elliot Lee committed
823 824 825 826 827

  hue_saturation_update (hsd, ALL);
}

static void
828
hue_saturation_preview_update (GtkWidget *widget,
Elliot Lee's avatar
Elliot Lee committed
829 830 831
			       gpointer   data)
{
  HueSaturationDialog *hsd;
832
  GimpTool            *active_tool;
Elliot Lee's avatar
Elliot Lee committed
833 834 835

  hsd = (HueSaturationDialog *) data;

836
  if (GTK_TOGGLE_BUTTON (widget)->active)
Elliot Lee's avatar
Elliot Lee committed
837 838 839 840 841
    {
      hsd->preview = TRUE;
      hue_saturation_preview (hsd);
    }
  else
842 843 844 845
    {
      hsd->preview = FALSE;
      if (hsd->image_map)
	{
846 847
	  active_tool = tool_manager_get_active (the_gimp);

Nate Summers's avatar
Nate Summers committed
848
	  gimp_tool_control_set_preserve(active_tool->control, TRUE);
849
	  image_map_clear (hsd->image_map);
Nate Summers's avatar
Nate Summers committed
850
	  gimp_tool_control_set_preserve(active_tool->control, FALSE);
851 852 853
	  gdisplays_flush ();
	}
    }
Elliot Lee's avatar
Elliot Lee committed
854 855 856
}

static void
857 858
hue_saturation_hue_adjustment_update (GtkAdjustment *adjustment,
				      gpointer       data)
Elliot Lee's avatar
Elliot Lee committed
859 860 861 862 863 864 865 866
{
  HueSaturationDialog *hsd;

  hsd = (HueSaturationDialog *) data;

  if (hsd->hue[hsd->hue_partition] != adjustment->value)
    {
      hsd->hue[hsd->hue_partition] = adjustment->value;
867
      hue_saturation_update (hsd, DRAW);
Elliot Lee's avatar
Elliot Lee committed
868 869 870 871 872 873 874

      if (hsd->preview)
	hue_saturation_preview (hsd);
    }
}

static void
875 876
hue_saturation_lightness_adjustment_update (GtkAdjustment *adjustment,
					    gpointer       data)
Elliot Lee's avatar
Elliot Lee committed
877 878 879 880 881 882 883 884
{
  HueSaturationDialog *hsd;

  hsd = (HueSaturationDialog *) data;

  if (hsd->lightness[hsd->hue_partition] != adjustment->value)
    {
      hsd->lightness[hsd->hue_partition] = adjustment->value;
885
      hue_saturation_update (hsd, DRAW);
Elliot Lee's avatar
Elliot Lee committed
886 887 888 889 890 891 892

      if (hsd->preview)
	hue_saturation_preview (hsd);
    }
}

static void
893 894
hue_saturation_saturation_adjustment_update (GtkAdjustment *adjustment,
					     gpointer       data)
Elliot Lee's avatar
Elliot Lee committed
895 896 897 898 899 900 901 902
{
  HueSaturationDialog *hsd;

  hsd = (HueSaturationDialog *) data;

  if (hsd->saturation[hsd->hue_partition] != adjustment->value)
    {
      hsd->saturation[hsd->hue_partition] = adjustment->value;
903
      hue_saturation_update (hsd, DRAW);
Elliot Lee's avatar
Elliot Lee committed
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926

      if (hsd->preview)
	hue_saturation_preview (hsd);
    }
}

static gint
hue_saturation_hue_partition_events (GtkWidget           *widget,
				     GdkEvent            *event,
				     HueSaturationDialog *hsd)
{
  switch (event->type)
    {
    case GDK_EXPOSE:
      hue_saturation_update (hsd, HUE_PARTITION);
      break;

    default:
      break;
    }

  return FALSE;
}