home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / hue_saturation.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  23.1 KB  |  838 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18. #include "config.h"
  19.  
  20. #include <glib.h>
  21.  
  22. #include "apptypes.h"
  23.  
  24. #include "appenv.h"
  25. #include "drawable.h"
  26. #include "gimage_mask.h"
  27. #include "gdisplay.h"
  28. #include "gimpui.h"
  29. #include "hue_saturation.h"
  30.  
  31. #include "libgimp/gimpcolorspace.h"
  32. #include "libgimp/gimpmath.h"
  33.  
  34. #include "libgimp/gimpintl.h"
  35.  
  36.  
  37. #define HUE_PARTITION_MASK  GDK_EXPOSURE_MASK | GDK_ENTER_NOTIFY_MASK
  38.  
  39. #define SLIDER_WIDTH  200
  40. #define DA_WIDTH  40
  41. #define DA_HEIGHT 20
  42.  
  43. #define HUE_PARTITION      0x0
  44. #define HUE_SLIDER         0x1
  45. #define LIGHTNESS_SLIDER   0x2
  46. #define SATURATION_SLIDER  0x4
  47. #define DRAW               0x40
  48. #define ALL                0xFF
  49.  
  50. /*  the hue-saturation structures  */
  51.  
  52. typedef struct _HueSaturation HueSaturation;
  53.  
  54. struct _HueSaturation
  55. {
  56.   gint x, y;    /*  coords for last mouse click  */
  57. };
  58.  
  59. /*  the hue-saturation tool options  */
  60. static ToolOptions *hue_saturation_options = NULL;
  61.  
  62. /*  the hue-saturation tool dialog  */
  63. static HueSaturationDialog *hue_saturation_dialog = NULL;
  64.  
  65. /*  Local variables  */
  66. static gint hue_transfer[6][256];
  67. static gint lightness_transfer[6][256];
  68. static gint saturation_transfer[6][256];
  69. static gint default_colors[6][3] =
  70. {
  71.   { 255,   0,   0 },
  72.   { 255, 255,   0 },
  73.   {   0, 255,   0 },
  74.   {   0, 255, 255 },
  75.   {   0,   0, 255 },
  76.   { 255,   0, 255 }
  77. };
  78.  
  79. /*  hue saturation action functions  */
  80. static void   hue_saturation_control (Tool *, ToolAction, gpointer);
  81.  
  82. static HueSaturationDialog * hue_saturation_dialog_new (void);
  83.  
  84. static void   hue_saturation_update                  (HueSaturationDialog *,
  85.                               gint);
  86. static void   hue_saturation_preview                 (HueSaturationDialog *);
  87. static void   hue_saturation_reset_callback          (GtkWidget *, gpointer);
  88. static void   hue_saturation_ok_callback             (GtkWidget *, gpointer);
  89. static void   hue_saturation_cancel_callback         (GtkWidget *, gpointer);
  90. static void   hue_saturation_partition_callback      (GtkWidget *, gpointer);
  91. static void   hue_saturation_preview_update          (GtkWidget *, gpointer);
  92. static void   hue_saturation_hue_adjustment_update        (GtkAdjustment *,
  93.                                gpointer);
  94. static void   hue_saturation_lightness_adjustment_update  (GtkAdjustment *,
  95.                                gpointer);
  96. static void   hue_saturation_saturation_adjustment_update (GtkAdjustment *,
  97.                                gpointer);
  98. static gint   hue_saturation_hue_partition_events    (GtkWidget *, GdkEvent *,
  99.                               HueSaturationDialog *);
  100.  
  101. /*  hue saturation machinery  */
  102.  
  103. void
  104. hue_saturation_calculate_transfers (HueSaturationDialog *hsd)
  105. {
  106.   gint value;
  107.   gint hue;
  108.   gint i;
  109.  
  110.   /*  Calculate transfers  */
  111.   for (hue = 0; hue < 6; hue++)
  112.     for (i = 0; i < 256; i++)
  113.       {
  114.     value = (hsd->hue[0] + hsd->hue[hue + 1]) * 255.0 / 360.0;
  115.     if ((i + value) < 0)
  116.       hue_transfer[hue][i] = 255 + (i + value);
  117.     else if ((i + value) > 255)
  118.       hue_transfer[hue][i] = i + value - 255;
  119.     else
  120.       hue_transfer[hue][i] = i + value;
  121.  
  122.     /*  Lightness  */
  123.     value = (hsd->lightness[0] + hsd->lightness[hue + 1]) * 127.0 / 100.0;
  124.     value = CLAMP (value, -255, 255);
  125.     if (value < 0)
  126.       lightness_transfer[hue][i] = (unsigned char) ((i * (255 + value)) / 255);
  127.     else
  128.       lightness_transfer[hue][i] = (unsigned char) (i + ((255 - i) * value) / 255);
  129.  
  130.     /*  Saturation  */
  131.     value = (hsd->saturation[0] + hsd->saturation[hue + 1]) * 255.0 / 100.0;
  132.     value = CLAMP (value, -255, 255);
  133.  
  134.     /* This change affects the way saturation is computed. With the
  135.        old code (different code for value < 0), increasing the
  136.        saturation affected muted colors very much, and bright colors
  137.        less. With the new code, it affects muted colors and bright
  138.        colors more or less evenly. For enhancing the color in photos,
  139.        the new behavior is exactly what you want. It's hard for me
  140.        to imagine a case in which the old behavior is better.
  141.     */
  142.     saturation_transfer[hue][i] = CLAMP ((i * (255 + value)) / 255, 0, 255);
  143.       }
  144. }
  145.  
  146. void
  147. hue_saturation (PixelRegion *srcPR,
  148.         PixelRegion *destPR,
  149.         void        *user_data)
  150. {
  151.   HueSaturationDialog *hsd;
  152.   unsigned char *src, *s;
  153.   unsigned char *dest, *d;
  154.   int alpha;
  155.   int w, h;
  156.   int r, g, b;
  157.   int hue;
  158.  
  159.   hsd = (HueSaturationDialog *) user_data;
  160.  
  161.   /*  Set the transfer arrays  (for speed)  */
  162.   h = srcPR->h;
  163.   src = srcPR->data;
  164.   dest = destPR->data;
  165.   alpha = (srcPR->bytes == 4) ? TRUE : FALSE;
  166.  
  167.   while (h--)
  168.     {
  169.       w = srcPR->w;
  170.       s = src;
  171.       d = dest;
  172.       while (w--)
  173.     {
  174.       r = s[RED_PIX];
  175.       g = s[GREEN_PIX];
  176.       b = s[BLUE_PIX];
  177.  
  178.       gimp_rgb_to_hls (&r, &g, &b);
  179.  
  180.       if (r < 43)
  181.         hue = 0;
  182.       else if (r < 85)
  183.         hue = 1;
  184.       else if (r < 128)
  185.         hue = 2;
  186.       else if (r < 171)
  187.         hue = 3;
  188.       else if (r < 213)
  189.         hue = 4;
  190.       else
  191.         hue = 5;
  192.  
  193.       r = hue_transfer[hue][r];
  194.       g = lightness_transfer[hue][g];
  195.       b = saturation_transfer[hue][b];
  196.  
  197.       gimp_hls_to_rgb (&r, &g, &b);
  198.  
  199.       d[RED_PIX] = r;
  200.       d[GREEN_PIX] = g;
  201.       d[BLUE_PIX] = b;
  202.  
  203.       if (alpha)
  204.         d[ALPHA_PIX] = s[ALPHA_PIX];
  205.  
  206.       s += srcPR->bytes;
  207.       d += destPR->bytes;
  208.     }
  209.  
  210.       src += srcPR->rowstride;
  211.       dest += destPR->rowstride;
  212.     }
  213. }
  214.  
  215. /*  hue saturation action functions  */
  216.  
  217. static void
  218. hue_saturation_control (Tool       *tool,
  219.             ToolAction  action,
  220.             gpointer    gdisp_ptr)
  221. {
  222.   switch (action)
  223.     {
  224.     case PAUSE:
  225.       break;
  226.  
  227.     case RESUME:
  228.       break;
  229.  
  230.     case HALT:
  231.       hue_saturation_dialog_hide ();
  232.       break;
  233.  
  234.     default:
  235.       break;
  236.     }
  237. }
  238.  
  239. Tool *
  240. tools_new_hue_saturation (void)
  241. {
  242.   Tool * tool;
  243.   HueSaturation * private;
  244.  
  245.   /*  The tool options  */
  246.   if (!hue_saturation_options)
  247.     {
  248.       hue_saturation_options = tool_options_new (_("Hue-Saturation"));
  249.       tools_register (HUE_SATURATION, hue_saturation_options);
  250.     }
  251.  
  252.   tool = tools_new_tool (HUE_SATURATION);
  253.   private = g_new0 (HueSaturation, 1);
  254.  
  255.   tool->scroll_lock = TRUE;   /*  Disallow scrolling  */
  256.   tool->preserve    = FALSE;  /*  Don't preserve on drawable change  */
  257.  
  258.   tool->private = (void *) private;
  259.  
  260.   tool->control_func = hue_saturation_control;
  261.  
  262.   return tool;
  263. }
  264.  
  265. void
  266. hue_saturation_dialog_hide (void)
  267. {
  268.   if (hue_saturation_dialog)
  269.     hue_saturation_cancel_callback (NULL, (gpointer) hue_saturation_dialog);
  270.   
  271. void
  272. tools_free_hue_saturation (Tool *tool)
  273. {
  274.   HueSaturation * color_bal;
  275.  
  276.   color_bal = (HueSaturation *) tool->private;
  277.  
  278.   /*  Close the hue saturation dialog  */
  279.   hue_saturation_dialog_hide ();
  280.  
  281.   g_free (color_bal);
  282. }
  283.  
  284. void
  285. hue_saturation_initialize (GDisplay *gdisp)
  286. {
  287.   gint i;
  288.  
  289.   if (! drawable_color (gimage_active_drawable (gdisp->gimage)))
  290.     {
  291.       g_message (_("Hue-Saturation operates only on RGB color drawables."));
  292.       return;
  293.     }
  294.  
  295.   /*  The "hue-saturation" dialog  */
  296.   if (!hue_saturation_dialog)
  297.     hue_saturation_dialog = hue_saturation_dialog_new ();
  298.   else
  299.     if (!GTK_WIDGET_VISIBLE (hue_saturation_dialog->shell))
  300.       gtk_widget_show (hue_saturation_dialog->shell);
  301.  
  302.   for (i = 0; i < 7; i++)
  303.     {
  304.       hue_saturation_dialog->hue[i] = 0.0;
  305.       hue_saturation_dialog->lightness[i] = 0.0;
  306.       hue_saturation_dialog->saturation[i] = 0.0;
  307.     }
  308.  
  309.   hue_saturation_dialog->drawable = gimage_active_drawable (gdisp->gimage);
  310.   hue_saturation_dialog->image_map =
  311.     image_map_create (gdisp, hue_saturation_dialog->drawable);
  312.  
  313.   hue_saturation_update (hue_saturation_dialog, ALL);
  314. }
  315.  
  316. void
  317. hue_saturation_free (void)
  318. {
  319.   if (hue_saturation_dialog)
  320.     {
  321.       if (hue_saturation_dialog->image_map)
  322.     {
  323.       active_tool->preserve = TRUE;
  324.       image_map_abort (hue_saturation_dialog->image_map);
  325.       active_tool->preserve = FALSE;
  326.  
  327.       hue_saturation_dialog->image_map = NULL;
  328.     }
  329.       gtk_widget_destroy (hue_saturation_dialog->shell);
  330.     }
  331. }
  332.  
  333. /***************************/
  334. /*  Hue-Saturation dialog  */
  335. /***************************/
  336.  
  337. static HueSaturationDialog *
  338. hue_saturation_dialog_new (void)
  339. {
  340.   HueSaturationDialog *hsd;
  341.   GtkWidget *main_vbox;
  342.   GtkWidget *main_hbox;
  343.   GtkWidget *vbox;
  344.   GtkWidget *hbox;
  345.   GtkWidget *table;
  346.   GtkWidget *label;
  347.   GtkWidget *slider;
  348.   GtkWidget *abox;
  349.   GtkWidget *spinbutton;
  350.   GtkWidget *toggle;
  351.   GtkWidget *radio_button;
  352.   GtkWidget *frame;
  353.   GtkObject *data;
  354.   GSList *group = NULL;
  355.   gint i;
  356.  
  357.   gchar *hue_partition_names[] =
  358.   {
  359.     N_("Master"),
  360.     N_("R"),
  361.     N_("Y"),
  362.     N_("G"),
  363.     N_("C"),
  364.     N_("B"),
  365.     N_("M")
  366.   };
  367.  
  368.   hsd = g_new (HueSaturationDialog, 1);
  369.   hsd->hue_partition = ALL_HUES;
  370.   hsd->preview       = TRUE;
  371.  
  372.   /*  The shell and main vbox  */
  373.   hsd->shell = gimp_dialog_new (_("Hue-Saturation"), "hue_saturation",
  374.                 tools_help_func, NULL,
  375.                 GTK_WIN_POS_NONE,
  376.                 FALSE, TRUE, FALSE,
  377.  
  378.                 _("OK"), hue_saturation_ok_callback,
  379.                 hsd, NULL, NULL, TRUE, FALSE,
  380.                 _("Reset"), hue_saturation_reset_callback,
  381.                 hsd, NULL, NULL, FALSE, FALSE,
  382.                 _("Cancel"), hue_saturation_cancel_callback,
  383.                 hsd, NULL, NULL, FALSE, TRUE,
  384.  
  385.                 NULL);
  386.  
  387.   main_vbox = gtk_vbox_new (FALSE, 4);
  388.   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 4);
  389.   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (hsd->shell)->vbox), main_vbox);
  390.  
  391.   /*  The main hbox containing hue partitions and sliders  */
  392.   main_hbox = gtk_hbox_new (FALSE, 12);
  393.   gtk_box_pack_start (GTK_BOX (main_vbox), main_hbox, FALSE, FALSE, 0);
  394.  
  395.   /*  The table containing hue partitions  */
  396.   table = gtk_table_new (7, 2, FALSE);
  397.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  398.   gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  399.   gtk_box_pack_start (GTK_BOX (main_hbox), table, FALSE, FALSE, 0);
  400.  
  401.   /*  the radio buttons for hue partitions  */
  402.   for (i = 0; i < 7; i++)
  403.     {
  404.       radio_button = gtk_radio_button_new_with_label (group, gettext (hue_partition_names[i]));
  405.       group = gtk_radio_button_group (GTK_RADIO_BUTTON (radio_button));
  406.       gtk_object_set_data (GTK_OBJECT (radio_button), "hue_partition",
  407.                (gpointer) i);
  408.  
  409.       if (!i)
  410.     {
  411.       gtk_table_attach (GTK_TABLE (table), radio_button, 0, 2, 0, 1,
  412.                 GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  413.     }
  414.       else
  415.     {
  416.       gtk_table_attach (GTK_TABLE (table), radio_button, 0, 1, i, i + 1,
  417.                 GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  418.  
  419.       frame = gtk_frame_new (NULL);
  420.       gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  421.       gtk_table_attach (GTK_TABLE (table), frame,  1, 2, i, i + 1,
  422.                 GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  423.       hsd->hue_partition_da[i - 1] = gtk_preview_new (GTK_PREVIEW_COLOR);
  424.       gtk_preview_size (GTK_PREVIEW (hsd->hue_partition_da[i - 1]),
  425.                 DA_WIDTH, DA_HEIGHT);
  426.       gtk_widget_set_events (hsd->hue_partition_da[i - 1],
  427.                  HUE_PARTITION_MASK);
  428.       gtk_signal_connect (GTK_OBJECT (hsd->hue_partition_da[i - 1]), "event",
  429.                   GTK_SIGNAL_FUNC (hue_saturation_hue_partition_events),
  430.                   hsd);
  431.       gtk_container_add (GTK_CONTAINER (frame), hsd->hue_partition_da[i - 1]);
  432.  
  433.       gtk_widget_show (hsd->hue_partition_da[i - 1]);
  434.       gtk_widget_show (frame);
  435.     }
  436.  
  437.       gtk_signal_connect (GTK_OBJECT (radio_button), "toggled",
  438.               GTK_SIGNAL_FUNC (hue_saturation_partition_callback),
  439.               hsd);
  440.  
  441.       gtk_widget_show (radio_button);
  442.     }
  443.  
  444.   gtk_widget_show (table);
  445.  
  446.   /*  The vbox for the table and preview toggle  */
  447.   vbox = gtk_vbox_new (FALSE, 4);
  448.   gtk_box_pack_start (GTK_BOX (main_hbox), vbox, FALSE, FALSE, 0);
  449.  
  450.   label = gtk_label_new (_("Hue / Lightness / Saturation Adjustments"));
  451.   gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  452.   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  453.   gtk_widget_show (label);
  454.  
  455.   /*  The table containing sliders  */
  456.   table = gtk_table_new (3, 3, FALSE);
  457.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  458.   gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  459.   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
  460.  
  461.   /*  Create the hue scale widget  */
  462.   label = gtk_label_new (_("Hue:"));
  463.   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  464.   gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
  465.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  466.  
  467.   data = gtk_adjustment_new (0, -180, 180.0, 1.0, 1.0, 0.0);
  468.   hsd->hue_data = GTK_ADJUSTMENT (data);
  469.  
  470.   slider = gtk_hscale_new (hsd->hue_data);
  471.   gtk_widget_set_usize (slider, SLIDER_WIDTH, -1);
  472.   gtk_scale_set_digits (GTK_SCALE (slider), 0);
  473.   gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  474.   gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  475.   gtk_table_attach (GTK_TABLE (table), slider, 1, 2, 0, 1,
  476.             GTK_EXPAND | GTK_SHRINK | GTK_FILL,
  477.             GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
  478.  
  479.   abox = gtk_vbox_new (FALSE, 0);
  480.   spinbutton = gtk_spin_button_new (hsd->hue_data, 1.0, 0);
  481.   gtk_widget_set_usize (spinbutton, 74, -1);
  482.   gtk_box_pack_end (GTK_BOX (abox), spinbutton, FALSE, FALSE, 0);
  483.   gtk_table_attach (GTK_TABLE (table), abox, 2, 3, 0, 1,
  484.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  485.  
  486.   gtk_signal_connect (GTK_OBJECT (hsd->hue_data), "value_changed",
  487.               GTK_SIGNAL_FUNC (hue_saturation_hue_adjustment_update),
  488.               hsd);
  489.  
  490.   gtk_widget_show (label);
  491.   gtk_widget_show (slider);
  492.   gtk_widget_show (spinbutton);
  493.   gtk_widget_show (abox);
  494.  
  495.   /*  Create the lightness scale widget  */
  496.   label = gtk_label_new (_("Lightness:"));
  497.   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  498.   gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
  499.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  500.  
  501.   data = gtk_adjustment_new (0, -100.0, 100.0, 1.0, 1.0, 0.0);
  502.   hsd->lightness_data = GTK_ADJUSTMENT (data);
  503.  
  504.   slider = gtk_hscale_new (hsd->lightness_data);
  505.   gtk_widget_set_usize (slider, SLIDER_WIDTH, -1);
  506.   gtk_scale_set_digits (GTK_SCALE (slider), 0);
  507.   gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  508.   gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  509.   gtk_table_attach (GTK_TABLE (table), slider, 1, 2, 1, 2,
  510.             GTK_EXPAND | GTK_SHRINK | GTK_FILL,
  511.             GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
  512.  
  513.   abox = gtk_vbox_new (FALSE, 0);
  514.   spinbutton = gtk_spin_button_new (hsd->lightness_data, 1.0, 0);
  515.   gtk_widget_set_usize (spinbutton, 75, -1);
  516.   gtk_box_pack_end (GTK_BOX (abox), spinbutton, FALSE, FALSE, 0);
  517.   gtk_table_attach (GTK_TABLE (table), abox, 2, 3, 1, 2,
  518.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  519.  
  520.   gtk_signal_connect (GTK_OBJECT (hsd->lightness_data), "value_changed",
  521.               GTK_SIGNAL_FUNC (hue_saturation_lightness_adjustment_update),
  522.               hsd);
  523.  
  524.   gtk_widget_show (label);
  525.   gtk_widget_show (slider);
  526.   gtk_widget_show (spinbutton);
  527.   gtk_widget_show (abox);
  528.  
  529.   /*  Create the saturation scale widget  */
  530.   label = gtk_label_new (_("Saturation:"));
  531.   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  532.   gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3,
  533.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  534.  
  535.   data = gtk_adjustment_new (0, -100.0, 100.0, 1.0, 1.0, 0.0);
  536.   hsd->saturation_data = GTK_ADJUSTMENT (data);
  537.  
  538.   slider = gtk_hscale_new (hsd->saturation_data);
  539.   gtk_widget_set_usize (slider, SLIDER_WIDTH, -1);
  540.   gtk_scale_set_digits (GTK_SCALE (slider), 0);
  541.   gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  542.   gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  543.   gtk_table_attach (GTK_TABLE (table), slider, 1, 2, 2, 3,
  544.             GTK_EXPAND | GTK_SHRINK | GTK_FILL,
  545.             GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
  546.  
  547.   abox = gtk_vbox_new (FALSE, 0);
  548.   spinbutton = gtk_spin_button_new (hsd->saturation_data, 1.0, 0);
  549.   gtk_widget_set_usize (spinbutton, 75, -1);
  550.   gtk_box_pack_end (GTK_BOX (abox), spinbutton, FALSE, FALSE, 0);
  551.   gtk_table_attach (GTK_TABLE (table), abox, 2, 3, 2, 3,
  552.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  553.  
  554.   gtk_signal_connect (GTK_OBJECT (hsd->saturation_data), "value_changed",
  555.               GTK_SIGNAL_FUNC (hue_saturation_saturation_adjustment_update),
  556.               hsd);
  557.  
  558.   gtk_widget_show (label);
  559.   gtk_widget_show (slider);
  560.   gtk_widget_show (spinbutton);
  561.   gtk_widget_show (abox);
  562.  
  563.   gtk_widget_show (table);
  564.  
  565.   /*  Horizontal box for preview toggle button  */
  566.   hbox = gtk_hbox_new (FALSE, 4);
  567.   gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  568.  
  569.   /*  The preview toggle  */
  570.   toggle = gtk_check_button_new_with_label (_("Preview"));
  571.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), hsd->preview);
  572.   gtk_box_pack_end (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
  573.  
  574.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  575.               GTK_SIGNAL_FUNC (hue_saturation_preview_update),
  576.               hsd);
  577.  
  578.   gtk_widget_show (toggle);
  579.   gtk_widget_show (hbox);
  580.  
  581.   gtk_widget_show (vbox);
  582.   gtk_widget_show (main_hbox);
  583.   gtk_widget_show (main_vbox);
  584.   gtk_widget_show (hsd->shell);
  585.  
  586.   return hsd;
  587. }
  588.  
  589. static void
  590. hue_saturation_update (HueSaturationDialog *hsd,
  591.                gint                 update)
  592. {
  593.   gint i, j, b;
  594.   gint rgb[3];
  595.   guchar buf[DA_WIDTH * 3];
  596.  
  597.   if (update & HUE_SLIDER)
  598.     {
  599.       gtk_adjustment_set_value (GTK_ADJUSTMENT (hsd->hue_data),
  600.                 hsd->hue[hsd->hue_partition]);
  601.     }
  602.   if (update & LIGHTNESS_SLIDER)
  603.     {
  604.       gtk_adjustment_set_value (GTK_ADJUSTMENT (hsd->lightness_data),
  605.                 hsd->lightness[hsd->hue_partition]);
  606.     }
  607.   if (update & SATURATION_SLIDER)
  608.     {
  609.       gtk_adjustment_set_value (GTK_ADJUSTMENT (hsd->saturation_data),
  610.                 hsd->saturation[hsd->hue_partition]);
  611.     }
  612.  
  613.   hue_saturation_calculate_transfers (hsd);
  614.  
  615.   for (i = 0; i < 6; i++)
  616.     {
  617.       rgb[RED_PIX]   = default_colors[i][RED_PIX];
  618.       rgb[GREEN_PIX] = default_colors[i][GREEN_PIX];
  619.       rgb[BLUE_PIX]  = default_colors[i][BLUE_PIX];
  620.  
  621.       gimp_rgb_to_hls (rgb, rgb + 1, rgb + 2);
  622.  
  623.       rgb[RED_PIX]   = hue_transfer[i][rgb[RED_PIX]];
  624.       rgb[GREEN_PIX] = lightness_transfer[i][rgb[GREEN_PIX]];
  625.       rgb[BLUE_PIX]  = saturation_transfer[i][rgb[BLUE_PIX]];
  626.  
  627.       gimp_hls_to_rgb (rgb, rgb + 1, rgb + 2);
  628.  
  629.       for (j = 0; j < DA_WIDTH; j++)
  630.     for (b = 0; b < 3; b++)
  631.       buf[j * 3 + b] = (guchar) rgb[b];
  632.  
  633.       for (j = 0; j < DA_HEIGHT; j++)
  634.     gtk_preview_draw_row (GTK_PREVIEW (hsd->hue_partition_da[i]),
  635.                   buf, 0, j, DA_WIDTH);
  636.  
  637.       if (update & DRAW)
  638.     gtk_widget_draw (hsd->hue_partition_da[i], NULL);
  639.     }
  640. }
  641.  
  642. static void
  643. hue_saturation_preview (HueSaturationDialog *hsd)
  644. {
  645.   if (!hsd->image_map)
  646.     {
  647.       g_warning ("hue_saturation_preview(): No image map");
  648.       return;
  649.     }
  650.  
  651.   active_tool->preserve = TRUE;
  652.   image_map_apply (hsd->image_map, hue_saturation, (void *) hsd);
  653.   active_tool->preserve = FALSE;
  654. }
  655.  
  656. static void
  657. hue_saturation_reset_callback (GtkWidget *widget,
  658.                    gpointer   data)
  659. {
  660.   HueSaturationDialog *hsd;
  661.  
  662.   hsd = (HueSaturationDialog *) data;
  663.  
  664.   hsd->hue[hsd->hue_partition] = 0.0;
  665.   hsd->lightness[hsd->hue_partition] = 0.0;
  666.   hsd->saturation[hsd->hue_partition] = 0.0;
  667.  
  668.   hue_saturation_update (hsd, ALL);
  669.  
  670.   if (hsd->preview)
  671.     hue_saturation_preview (hsd);
  672. }
  673.  
  674. static void
  675. hue_saturation_ok_callback (GtkWidget *widget,
  676.                 gpointer   data)
  677. {
  678.   HueSaturationDialog *hsd;
  679.  
  680.   hsd = (HueSaturationDialog *) data;
  681.  
  682.   gimp_dialog_hide (hsd->shell);
  683.  
  684.   active_tool->preserve = TRUE;
  685.  
  686.   if (!hsd->preview)
  687.     image_map_apply (hsd->image_map, hue_saturation, (void *) hsd);
  688.  
  689.   if (hsd->image_map)
  690.     image_map_commit (hsd->image_map);
  691.  
  692.   active_tool->preserve = FALSE;
  693.  
  694.   hsd->image_map = NULL;
  695.  
  696.   active_tool->gdisp_ptr = NULL;
  697.   active_tool->drawable = NULL;
  698. }
  699.  
  700. static void
  701. hue_saturation_cancel_callback (GtkWidget *widget,
  702.                 gpointer   data)
  703. {
  704.   HueSaturationDialog *hsd;
  705.  
  706.   hsd = (HueSaturationDialog *) data;
  707.  
  708.   gimp_dialog_hide (hsd->shell);
  709.  
  710.   if (hsd->image_map)
  711.     {
  712.       active_tool->preserve = TRUE;
  713.       image_map_abort (hsd->image_map);
  714.       active_tool->preserve = FALSE;
  715.  
  716.       gdisplays_flush ();
  717.       hsd->image_map = NULL;
  718.     }
  719.  
  720.   active_tool->gdisp_ptr = NULL;
  721.   active_tool->drawable = NULL;
  722. }
  723.  
  724. static void
  725. hue_saturation_partition_callback (GtkWidget *widget,
  726.                    gpointer   data)
  727. {
  728.   HueSaturationDialog *hsd;
  729.   HueRange partition;
  730.  
  731.   hsd = (HueSaturationDialog *) data;
  732.  
  733.   partition = (HueRange) gtk_object_get_data (GTK_OBJECT (widget),
  734.                           "hue_partition");
  735.   hsd->hue_partition = partition;
  736.  
  737.   hue_saturation_update (hsd, ALL);
  738. }
  739.  
  740. static void
  741. hue_saturation_preview_update (GtkWidget *widget,
  742.                    gpointer   data)
  743. {
  744.   HueSaturationDialog *hsd;
  745.  
  746.   hsd = (HueSaturationDialog *) data;
  747.  
  748.   if (GTK_TOGGLE_BUTTON (widget)->active)
  749.     {
  750.       hsd->preview = TRUE;
  751.       hue_saturation_preview (hsd);
  752.     }
  753.   else
  754.     {
  755.       hsd->preview = FALSE;
  756.       if (hsd->image_map)
  757.     {
  758.       active_tool->preserve = TRUE;
  759.       image_map_clear (hsd->image_map);
  760.       active_tool->preserve = FALSE;
  761.       gdisplays_flush ();
  762.     }
  763.     }
  764. }
  765.  
  766. static void
  767. hue_saturation_hue_adjustment_update (GtkAdjustment *adjustment,
  768.                       gpointer       data)
  769. {
  770.   HueSaturationDialog *hsd;
  771.  
  772.   hsd = (HueSaturationDialog *) data;
  773.  
  774.   if (hsd->hue[hsd->hue_partition] != adjustment->value)
  775.     {
  776.       hsd->hue[hsd->hue_partition] = adjustment->value;
  777.       hue_saturation_update (hsd, DRAW);
  778.  
  779.       if (hsd->preview)
  780.     hue_saturation_preview (hsd);
  781.     }
  782. }
  783.  
  784. static void
  785. hue_saturation_lightness_adjustment_update (GtkAdjustment *adjustment,
  786.                         gpointer       data)
  787. {
  788.   HueSaturationDialog *hsd;
  789.  
  790.   hsd = (HueSaturationDialog *) data;
  791.  
  792.   if (hsd->lightness[hsd->hue_partition] != adjustment->value)
  793.     {
  794.       hsd->lightness[hsd->hue_partition] = adjustment->value;
  795.       hue_saturation_update (hsd, DRAW);
  796.  
  797.       if (hsd->preview)
  798.     hue_saturation_preview (hsd);
  799.     }
  800. }
  801.  
  802. static void
  803. hue_saturation_saturation_adjustment_update (GtkAdjustment *adjustment,
  804.                          gpointer       data)
  805. {
  806.   HueSaturationDialog *hsd;
  807.  
  808.   hsd = (HueSaturationDialog *) data;
  809.  
  810.   if (hsd->saturation[hsd->hue_partition] != adjustment->value)
  811.     {
  812.       hsd->saturation[hsd->hue_partition] = adjustment->value;
  813.       hue_saturation_update (hsd, DRAW);
  814.  
  815.       if (hsd->preview)
  816.     hue_saturation_preview (hsd);
  817.     }
  818. }
  819.  
  820. static gint
  821. hue_saturation_hue_partition_events (GtkWidget           *widget,
  822.                      GdkEvent            *event,
  823.                      HueSaturationDialog *hsd)
  824. {
  825.   switch (event->type)
  826.     {
  827.     case GDK_EXPOSE:
  828.       hue_saturation_update (hsd, HUE_PARTITION);
  829.       break;
  830.  
  831.     default:
  832.       break;
  833.     }
  834.  
  835.   return FALSE;
  836. }
  837.