Re: GtkColorSelection



On 27 Feb 2001, Havoc Pennington wrote:

>
> Hi,
>
> Suggest revamping the API, as appended. The API being revamped was
> added post-1.2 so there are no backward compat issues, except for
> gtk_color_selection_set_color() which I've marked deprecated.

Here is a patch that does the revamp and fixes some bugs.

Is it ok to apply?

/ Alex

Index: gtk/gtkcolorsel.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkcolorsel.c,v
retrieving revision 1.63
diff -u -p -r1.63 gtkcolorsel.c
--- gtk/gtkcolorsel.c	2001/03/24 06:10:39	1.63
+++ gtk/gtkcolorsel.c	2001/04/19 15:24:14
@@ -82,8 +82,8 @@ typedef struct _ColorSelectionPrivate Co

 struct _ColorSelectionPrivate
 {
-  guint use_opacity : 1;
-  guint use_palette : 1;
+  guint has_opacity : 1;
+  guint has_palette : 1;
   guint changing : 1;
   guint default_set : 1;

@@ -236,7 +236,7 @@ color_sample_drag_begin (GtkWidget
       colors[i++] = colsrc[n];
     }

-  if (priv->use_opacity)
+  if (priv->has_opacity)
     {
       colors[i] = colsrc[COLORSEL_OPACITY];
     }
@@ -328,7 +328,7 @@ color_sample_drag_handle (GtkWidget
   vals[0] = colsrc[COLORSEL_RED] * 0xffff;
   vals[1] = colsrc[COLORSEL_GREEN] * 0xffff;
   vals[2] = colsrc[COLORSEL_BLUE] * 0xffff;
-  vals[3] = priv->use_opacity ? colsrc[COLORSEL_OPACITY] * 0xffff : 0xffff;
+  vals[3] = priv->has_opacity ? colsrc[COLORSEL_OPACITY] * 0xffff : 0xffff;

   gtk_selection_data_set (selection_data,
 			  gdk_atom_intern ("application/x-color", FALSE),
@@ -382,7 +382,7 @@ color_sample_draw_sample (GtkColorSelect
     }
 #endif

-  if (priv->use_opacity)
+  if (priv->has_opacity)
     {
       o = (which) ? priv->color[COLORSEL_OPACITY] : priv->old_color[COLORSEL_OPACITY];

@@ -399,7 +399,7 @@ color_sample_draw_sample (GtkColorSelect
     {
       for (x = 0; x < wid; x++)
 	{
-	  if (priv->use_opacity)
+	  if (priv->has_opacity)
 	    f = 3 * ((((goff + x) % 32) < 16) ^ ((y % 32) < 16));
 	  else
 	    f = 0;
@@ -1123,7 +1123,7 @@ opacity_entry_changed (GtkWidget *opacit
   g_free (text);
 }

-static void
+static gboolean
 widget_focus_in (GtkWidget     *drawing_area,
 		 GdkEventFocus *event,
 		 gpointer       data)
@@ -1136,6 +1136,8 @@ widget_focus_in (GtkWidget     *drawing_
    */

   priv->last_palette = NULL;
+
+  return FALSE;
 }


@@ -1208,6 +1210,17 @@ set_selected_palette (GtkColorSelection
   gtk_widget_queue_clear (priv->last_palette);
 }

+#include <math.h>
+
+static double
+scale_round (double val, double factor)
+{
+  val = floor (val * factor + 0.5);
+  val = MAX (val, 0);
+  val = MIN (val, factor);
+  return val;
+}
+
 static void
 update_color (GtkColorSelection *colorsel)
 {
@@ -1225,33 +1238,33 @@ update_color (GtkColorSelection *colorse
 		     priv->color[COLORSEL_VALUE]);
   gtk_adjustment_set_value (gtk_spin_button_get_adjustment
 			    (GTK_SPIN_BUTTON (priv->hue_spinbutton)),
-			    priv->color[COLORSEL_HUE] * 360);
+			    scale_round (priv->color[COLORSEL_HUE], 360));
   gtk_adjustment_set_value (gtk_spin_button_get_adjustment
 			    (GTK_SPIN_BUTTON (priv->sat_spinbutton)),
-			    priv->color[COLORSEL_SATURATION] * 255);
+			    scale_round (priv->color[COLORSEL_SATURATION], 255));
   gtk_adjustment_set_value (gtk_spin_button_get_adjustment
 			    (GTK_SPIN_BUTTON (priv->val_spinbutton)),
-			    priv->color[COLORSEL_VALUE] * 255);
+			    scale_round (priv->color[COLORSEL_VALUE], 255));
   gtk_adjustment_set_value (gtk_spin_button_get_adjustment
 			    (GTK_SPIN_BUTTON (priv->red_spinbutton)),
-			    priv->color[COLORSEL_RED] * 255);
+			    scale_round (priv->color[COLORSEL_RED], 255));
   gtk_adjustment_set_value (gtk_spin_button_get_adjustment
 			    (GTK_SPIN_BUTTON (priv->green_spinbutton)),
-			    priv->color[COLORSEL_GREEN] * 255);
+			    scale_round (priv->color[COLORSEL_GREEN], 255));
   gtk_adjustment_set_value (gtk_spin_button_get_adjustment
 			    (GTK_SPIN_BUTTON (priv->blue_spinbutton)),
-			    priv->color[COLORSEL_BLUE] * 255);
+			    scale_round (priv->color[COLORSEL_BLUE], 255));
   gtk_adjustment_set_value (gtk_range_get_adjustment
 			    (GTK_RANGE (priv->opacity_slider)),
-			    priv->color[COLORSEL_OPACITY] * 255);
+			    scale_round (priv->color[COLORSEL_OPACITY], 255));

   g_snprintf (opacity_text, 32, "%.0f", priv->color[COLORSEL_OPACITY] * 255);
   gtk_entry_set_text (GTK_ENTRY (priv->opacity_entry), opacity_text);

   g_snprintf (entryval, 11, "#%2X%2X%2X",
-	      (guint) (255 * priv->color[COLORSEL_RED]),
-	      (guint) (255 * priv->color[COLORSEL_GREEN]),
-	      (guint) (255 * priv->color[COLORSEL_BLUE]));
+	      (guint) (scale_round (priv->color[COLORSEL_RED], 255)),
+	      (guint) (scale_round (priv->color[COLORSEL_GREEN], 255)),
+	      (guint) (scale_round (priv->color[COLORSEL_BLUE], 255)));

   for (ptr = entryval; *ptr; ptr++)
     if (*ptr == ' ')
@@ -1481,14 +1494,14 @@ gtk_color_selection_init (GtkColorSelect

   gtk_widget_show_all (top_hbox);

-  if (priv->use_opacity == FALSE)
+  if (priv->has_opacity == FALSE)
     {
       gtk_widget_hide (priv->opacity_label);
       gtk_widget_hide (priv->opacity_slider);
       gtk_widget_hide (priv->opacity_entry);
     }

-  if (priv->use_palette == FALSE)
+  if (priv->has_palette == FALSE)
     {
       gtk_widget_hide (priv->palette_frame);
     }
@@ -1531,7 +1544,7 @@ gtk_color_selection_new (void)
   colorsel = gtk_type_new (GTK_TYPE_COLOR_SELECTION);
   priv = colorsel->private_data;
   gtk_color_selection_set_color (colorsel, color);
-  gtk_color_selection_set_use_opacity (colorsel, FALSE);
+  gtk_color_selection_set_has_opacity_control (colorsel, TRUE);

   /* We want to make sure that default_set is FALSE */
   /* This way the user can still set it */
@@ -1552,15 +1565,15 @@ gtk_color_selection_set_update_policy (G
 }

 /**
- * gtk_color_selection_get_use_opacity:
+ * gtk_color_selection_get_has_opacity_control:
  * @colorsel: A GtkColorSelection.
  *
- * Determines whether the colorsel can use opacity.
+ * Determines whether the colorsel has an opacity control.
  *
- * Return value: TRUE if the @colorsel uses opacity.  FALSE if it does't.
+ * Return value: TRUE if the @colorsel has an opacity control.  FALSE if it does't.
  **/
 gboolean
-gtk_color_selection_get_use_opacity (GtkColorSelection *colorsel)
+gtk_color_selection_get_has_opacity_control (GtkColorSelection *colorsel)
 {
   ColorSelectionPrivate *priv;

@@ -1569,20 +1582,20 @@ gtk_color_selection_get_use_opacity (Gtk

   priv = colorsel->private_data;

-  return priv->use_opacity;
+  return priv->has_opacity;
 }

 /**
- * gtk_color_selection_set_use_opacity:
+ * gtk_color_selection_set_has_opacity_control:
  * @colorsel: A GtkColorSelection.
- * @use_opacity: TRUE if @colorsel can set the opacity, FALSE otherwise.
+ * @has_opacity: TRUE if @colorsel can set the opacity, FALSE otherwise.
  *
  * Sets the @colorsel to use or not use opacity.
  *
  **/
 void
-gtk_color_selection_set_use_opacity (GtkColorSelection *colorsel,
-				     gboolean           use_opacity)
+gtk_color_selection_set_has_opacity_control (GtkColorSelection *colorsel,
+					     gboolean           has_opacity)
 {
   ColorSelectionPrivate *priv;

@@ -1590,12 +1603,12 @@ gtk_color_selection_set_use_opacity (Gtk
   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));

   priv = colorsel->private_data;
-  use_opacity = use_opacity != FALSE;
+  has_opacity = has_opacity != FALSE;

-  if (priv->use_opacity != use_opacity)
+  if (priv->has_opacity != has_opacity)
     {
-      priv->use_opacity = use_opacity;
-      if (use_opacity)
+      priv->has_opacity = has_opacity;
+      if (has_opacity)
 	{
 	  gtk_widget_show (priv->opacity_slider);
 	  gtk_widget_show (priv->opacity_label);
@@ -1612,15 +1625,15 @@ gtk_color_selection_set_use_opacity (Gtk
 }

 /**
- * gtk_color_selection_get_use_palette:
+ * gtk_color_selection_get_has_palette:
  * @colorsel: A GtkColorSelection.
  *
- * Determines whether the palette is used.
+ * Determines whether the color selector has a color palette.
  *
- * Return value: TRUE if the palette is used.  FALSE if it isn't.
+ * Return value: TRUE if the selector has a palette.  FALSE if it hasn't.
  **/
 gboolean
-gtk_color_selection_get_use_palette (GtkColorSelection *colorsel)
+gtk_color_selection_get_has_palette (GtkColorSelection *colorsel)
 {
   ColorSelectionPrivate *priv;

@@ -1628,32 +1641,32 @@ gtk_color_selection_get_use_palette (Gtk

   priv = colorsel->private_data;

-  return priv->use_palette;
+  return priv->has_palette;
 }

 /**
- * gtk_color_selection_set_use_palette:
+ * gtk_color_selection_set_has_palette:
  * @colorsel: A GtkColorSelection.
- * @use_palette: TRUE if palette is to be visible, FALSE otherwise.
+ * @has_palette: TRUE if palette is to be visible, FALSE otherwise.
  *
- * Shows and hides the palette based upon the value of @use_palette.
+ * Shows and hides the palette based upon the value of @has_palette.
  *
  **/
 void
-gtk_color_selection_set_use_palette (GtkColorSelection *colorsel,
-				     gboolean           use_palette)
+gtk_color_selection_set_has_palette (GtkColorSelection *colorsel,
+				     gboolean           has_palette)
 {
   ColorSelectionPrivate *priv;
   g_return_if_fail (colorsel != NULL);
   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));

   priv = colorsel->private_data;
-  use_palette = use_palette != FALSE;
+  has_palette = has_palette != FALSE;

-  if (priv->use_palette != use_palette)
+  if (priv->has_palette != has_palette)
     {
-      priv->use_palette = use_palette;
-      if (use_palette)
+      priv->has_palette = has_palette;
+      if (has_palette)
 	gtk_widget_show (priv->palette_frame);
       else
 	gtk_widget_hide (priv->palette_frame);
@@ -1661,15 +1674,87 @@ gtk_color_selection_set_use_palette (Gtk
 }

 /**
- * gtk_color_selection_set_color:
+ * gtk_color_selection_set_current_color:
  * @colorsel: A GtkColorSelection.
- * @color: A color to set the current color with.
+ * @color: A GdkColor to set the current color with.
  *
  * Sets the current color to be @color.  The first time this is called, it will
  * also set the original color to be @color too.
  *
  **/
 void
+gtk_color_selection_set_current_color (GtkColorSelection *colorsel,
+				       GdkColor          *color)
+{
+  ColorSelectionPrivate *priv;
+  gint i;
+
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+
+  priv = colorsel->private_data;
+  priv->changing = TRUE;
+  priv->color[COLORSEL_RED] = color->red / 65535.0;
+  priv->color[COLORSEL_GREEN] = color->green / 65535.0;
+  priv->color[COLORSEL_BLUE] = color->blue / 65535.0;
+  gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
+		  priv->color[COLORSEL_GREEN],
+		  priv->color[COLORSEL_BLUE],
+		  &priv->color[COLORSEL_HUE],
+		  &priv->color[COLORSEL_SATURATION],
+		  &priv->color[COLORSEL_VALUE]);
+  if (priv->default_set == FALSE)
+    {
+      for (i = 0; i < COLORSEL_NUM_CHANNELS; i++)
+	priv->old_color[i] = priv->color[i];
+    }
+  update_color (colorsel);
+  priv->default_set = TRUE;
+}
+
+/**
+ * gtk_color_selection_set_current_alpha:
+ * @colorsel: A GtkColorSelection.
+ * @alpha: an integer between 0 and 65535
+ *
+ * Sets the current opacity to be @alpha.  The first time this is called, it will
+ * also set the original opacity to be @alpha too.
+ *
+ **/
+void
+gtk_color_selection_set_current_alpha (GtkColorSelection *colorsel,
+				       guint16            alpha)
+{
+  ColorSelectionPrivate *priv;
+  gint i;
+
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+
+  priv = colorsel->private_data;
+  priv->changing = TRUE;
+  priv->color[COLORSEL_OPACITY] = alpha / 65535.0;
+  if (priv->default_set == FALSE)
+    {
+      for (i = 0; i < COLORSEL_NUM_CHANNELS; i++)
+	priv->old_color[i] = priv->color[i];
+    }
+  update_color (colorsel);
+  priv->default_set = TRUE;
+}
+
+/**
+ * gtk_color_selection_set_color:
+ * @colorsel: A GtkColorSelection.
+ * @color: A array of doubles that specifies the color to set the current color with.
+ *
+ * Sets the current color to be @color.  The first time this is called, it will
+ * also set the original color to be @color too.
+ *
+ * This function is deprecated, use gtk_color_selection_set_current_color() instead.
+ *
+ **/
+void
 gtk_color_selection_set_color (GtkColorSelection    *colorsel,
 			       gdouble              *color)
 {
@@ -1701,6 +1786,50 @@ gtk_color_selection_set_color (GtkColorS
 }

 /**
+ * gtk_color_selection_get_current_color:
+ * @colorsel: A GtkColorSelection.
+ * @color: A GdkColor to fill in with the current color.
+ *
+ * Sets @color to be the current color in the GtkColorSelection widget.
+ *
+ * This function is deprecated, use gtk_color_selection_get_current_color() instead.
+ **/
+void
+gtk_color_selection_get_current_color (GtkColorSelection *colorsel,
+				       GdkColor          *color)
+{
+  ColorSelectionPrivate *priv;
+
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+
+  priv = colorsel->private_data;
+  color->red = priv->color[COLORSEL_RED] * 65535;
+  color->green = priv->color[COLORSEL_GREEN] * 65535;
+  color->blue = priv->color[COLORSEL_BLUE] * 65535;
+}
+
+/**
+ * gtk_color_selection_get_current_alpha:
+ * @colorsel: A GtkColorSelection.
+ *
+ * Returns the current alpha value
+ *
+ * Return value: an integer between 0 and 65535
+ **/
+guint16
+gtk_color_selection_get_current_alpha (GtkColorSelection *colorsel)
+{
+  ColorSelectionPrivate *priv;
+
+  g_return_val_if_fail (colorsel != NULL, 0);
+  g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), 0);
+
+  priv = colorsel->private_data;
+  return priv->has_opacity ? priv->color[COLORSEL_OPACITY] * 65535 : 1.0;
+}
+
+/**
  * gtk_color_selection_get_color:
  * @colorsel: A GtkColorSelection.
  * @color: A color to fill in with the current color.
@@ -1721,23 +1850,23 @@ gtk_color_selection_get_color (GtkColorS
   color[0] = priv->color[COLORSEL_RED];
   color[1] = priv->color[COLORSEL_GREEN];
   color[2] = priv->color[COLORSEL_BLUE];
-  color[3] = priv->use_opacity ? priv->color[COLORSEL_OPACITY] : 1.0;
+  color[3] = priv->has_opacity ? priv->color[COLORSEL_OPACITY] : 1.0;
 }

 /**
- * gtk_color_selection_get_old_color:
+ * gtk_color_selection_set_previous_color:
  * @colorsel: A GtkColorSelection.
- * @color: A color to set the original color with.
+ * @color: A color to set the previous color with.
  *
- * Sets the 'original' color to be @color.  This function should be called with
+ * Sets the 'previous' color to be @color.  This function should be called with
  * some hesitations, as it might seem confusing to have that color change.
- * Calling gtk_color_selection_set_color will also set this color the first
+ * Calling gtk_color_selection_set_current_color will also set this color the first
  * time it is called.
  *
  **/
 void
-gtk_color_selection_set_old_color (GtkColorSelection *colorsel,
-				   gdouble          *color)
+gtk_color_selection_set_previous_color (GtkColorSelection *colorsel,
+					GdkColor          *color)
 {
   ColorSelectionPrivate *priv;

@@ -1746,10 +1875,9 @@ gtk_color_selection_set_old_color (GtkCo

   priv = colorsel->private_data;
   priv->changing = TRUE;
-  priv->old_color[COLORSEL_RED] = color[0];
-  priv->old_color[COLORSEL_GREEN] = color[1];
-  priv->old_color[COLORSEL_BLUE] = color[2];
-  priv->old_color[COLORSEL_OPACITY] = color[3];
+  priv->old_color[COLORSEL_RED] = color->red / 65535.0;
+  priv->old_color[COLORSEL_GREEN] = color->green / 65535.0;
+  priv->old_color[COLORSEL_BLUE] = color->blue / 65535.0;
   gtk_rgb_to_hsv (priv->old_color[COLORSEL_RED],
 		  priv->old_color[COLORSEL_GREEN],
 		  priv->old_color[COLORSEL_BLUE],
@@ -1760,113 +1888,190 @@ gtk_color_selection_set_old_color (GtkCo
   priv->default_set = TRUE;
 }

+/**
+ * gtk_color_selection_set_previous_alpha:
+ * @colorsel: A GtkColorSelection.
+ * @alpha: an integer between 0 and 65535
+ *
+ * Sets the 'previous' alpha to be @alpha.  This function should be called with
+ * some hesitations, as it might seem confusing to have that color change.
+ *
+ **/
+void
+gtk_color_selection_set_previous_alpha (GtkColorSelection *colorsel,
+					guint16            alpha)
+{
+  ColorSelectionPrivate *priv;
+
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+
+  priv = colorsel->private_data;
+  priv->changing = TRUE;
+  priv->old_color[COLORSEL_OPACITY] = alpha / 65535.0;
+  color_sample_draw_samples (colorsel);
+  priv->default_set = TRUE;
+}
+
+
 /**
- * gtk_color_selection_get_old_color:
+ * gtk_color_selection_get_previous_color:
  * @colorsel: A GtkColorSelection.
- * @color: A color to fill in with the original color value.
+ * @color: A GdkColor to fill in with the original color value.
  *
  * Fills @color in with the original color value.
  *
  **/
 void
-gtk_color_selection_get_old_color (GtkColorSelection *colorsel,
-				   gdouble           *color)
+gtk_color_selection_get_previous_color (GtkColorSelection *colorsel,
+					GdkColor           *color)
 {
   ColorSelectionPrivate *priv;

   g_return_if_fail (colorsel != NULL);
   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));

+  priv = colorsel->private_data;
+  color->red = priv->old_color[COLORSEL_RED] * 65535;
+  color->green = priv->old_color[COLORSEL_GREEN] * 65535;
+  color->blue = priv->old_color[COLORSEL_BLUE] * 65535;
+}
+
+/**
+ * gtk_color_selection_get_previous_alpha:
+ * @colorsel: A GtkColorSelection.
+ *
+ * Returns the previous alpha value
+ *
+ * Return value: an integer between 0 and 65535
+ **/
+guint16
+gtk_color_selection_get_previous_alpha (GtkColorSelection *colorsel)
+{
+  ColorSelectionPrivate *priv;
+
+  g_return_val_if_fail (colorsel != NULL, 0);
+  g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), 0);
+
   priv = colorsel->private_data;
-  color[0] = priv->old_color[COLORSEL_RED];
-  color[1] = priv->old_color[COLORSEL_GREEN];
-  color[2] = priv->old_color[COLORSEL_BLUE];
-  color[3] = priv->use_opacity ? priv->old_color[COLORSEL_OPACITY] : 1.0;
+  return priv->has_opacity ? priv->old_color[COLORSEL_OPACITY] * 65535 : 1.0;
 }

 /**
  * gtk_color_selection_set_palette_color:
  * @colorsel: A GtkColorSelection.
- * @x: The x coordinate of the palette.
- * @y: The y coordinate of the palette.
- * @color: A color to set the palette with.
+ * @index: The color index of the palette.
+ * @color: A GdkColor to set the palette with.
  *
- * Set the palette located at (@x, @y) to have @color set as its color.
+ * Set the palette located at at @index to have @color set as its color.
  *
  **/
 void
 gtk_color_selection_set_palette_color (GtkColorSelection   *colorsel,
-				       gint                 x,
-				       gint                 y,
-				       gdouble             *color)
+				       gint                 index,
+				       GdkColor            *color)
 {
   ColorSelectionPrivate *priv;
+  gint x, y;
+  gdouble col[3];

   g_return_if_fail (colorsel != NULL);
   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
-  g_return_if_fail (x >= 0 && y >= 0 && x < GTK_CUSTOM_PALETTE_WIDTH && y < GTK_CUSTOM_PALETTE_HEIGHT);
+  g_return_if_fail (index >= 0  && index < GTK_CUSTOM_PALETTE_WIDTH*GTK_CUSTOM_PALETTE_HEIGHT);
+
+  x = index % GTK_CUSTOM_PALETTE_WIDTH;
+  y = index / GTK_CUSTOM_PALETTE_WIDTH;

   priv = colorsel->private_data;
-  palette_set_color (priv->custom_palette[x][y], colorsel, color);
+  col[0] = color->red / 65535.0;
+  col[1] = color->green / 65535.0;
+  col[2] = color->blue / 65535.0;
+
+  palette_set_color (priv->custom_palette[x][y], colorsel, col);
 }

 /**
  * gtk_color_selection_get_palette_color:
  * @colorsel: A GtkColorSelection.
- * @x: The x coordinate of the palette.
- * @y: The y coordinate of the palette.
+ * @index: The color index of the palette.
  * @color: A color to fill in with the color value.
  *
- * Set @color to have the color found in the palette located at (@x, @y).  If
+ * Set @color to have the color found in the palette at @index.  If
  * the palette is unset, it will leave the color unset.
  *
- * Return value: TRUE if the palette located at (@x, @y) has a color set.  FALSE
+ * Return value: TRUE if the palette located at @index has a color set.  FALSE
  * if it doesn't.
  **/
 gboolean
 gtk_color_selection_get_palette_color (GtkColorSelection   *colorsel,
-				       gint                 x,
-				       gint                 y,
-				       gdouble             *color)
+				       gint                 index,
+				       GdkColor            *color)
 {
   ColorSelectionPrivate *priv;
+  gint x, y;
+  gdouble col[4];

   g_return_val_if_fail (colorsel != NULL, FALSE);
   g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), FALSE);
-  g_return_val_if_fail (x >= 0 && y >= 0 && x < GTK_CUSTOM_PALETTE_WIDTH && y < GTK_CUSTOM_PALETTE_HEIGHT, FALSE);
+  g_return_val_if_fail (index >= 0  && index < GTK_CUSTOM_PALETTE_WIDTH*GTK_CUSTOM_PALETTE_HEIGHT, FALSE);

   priv = colorsel->private_data;
-
+
+  x = index % GTK_CUSTOM_PALETTE_WIDTH;
+  y = index / GTK_CUSTOM_PALETTE_WIDTH;
+
   if (GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (priv->custom_palette[x][y]), "color_set")) == 0)
     return FALSE;
+
+  palette_get_color (priv->custom_palette[x][y], col);
+
+  color->red = col[0] * 65535;
+  color->green = col[1] * 65535;
+  color->blue = col[2] * 65535;

-  palette_get_color (priv->custom_palette[x][y], color);
   return TRUE;
 }

 /**
  * gtk_color_selection_unset_palette_color:
  * @colorsel: A GtkColorSelection.
- * @x: The x coordinate of the palette.
- * @y: The y coordinate of the palette.
+ * @index: The color index in the palette.
  *
- * Change the palette located at (@x, @y) to have no color set.
+ * Change the palette located @index to have no color set.
  *
  **/
 void
 gtk_color_selection_unset_palette_color (GtkColorSelection   *colorsel,
-					 gint                 x,
-					 gint                 y)
+					 gint                 index)
 {
   ColorSelectionPrivate *priv;
+  gint x, y;

   g_return_if_fail (colorsel != NULL);
   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
-  g_return_if_fail (x >= 0 && y >= 0 && x < GTK_CUSTOM_PALETTE_WIDTH && y < GTK_CUSTOM_PALETTE_HEIGHT);
+  g_return_if_fail (index >= 0  && index < GTK_CUSTOM_PALETTE_WIDTH*GTK_CUSTOM_PALETTE_HEIGHT);
+
+  x = index % GTK_CUSTOM_PALETTE_WIDTH;
+  y = index / GTK_CUSTOM_PALETTE_WIDTH;

   priv = colorsel->private_data;
   palette_unset_color (priv->custom_palette[x][y]);
 }
+
+/**
+ * gtk_color_selection_get_current_alpha:
+ * @colorsel: A GtkColorSelection.
+ *
+ * Returns the maximum number of palette colors.
+ *
+ * Return value: the maximum number of palette indexes
+ **/
+gint
+gtk_color_selection_get_palette_size (GtkColorSelection *colorsel)
+{
+  return GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT;
+}
+

 /**
  * gtk_color_selection_is_adjusting:
Index: gtk/gtkcolorsel.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkcolorsel.h,v
retrieving revision 1.17
diff -u -p -r1.17 gtkcolorsel.h
--- gtk/gtkcolorsel.h	2000/12/08 20:08:51	1.17
+++ gtk/gtkcolorsel.h	2001/04/19 15:24:14
@@ -65,44 +65,49 @@ struct _GtkColorSelectionClass

 /* ColorSelection */

-GtkType     gtk_color_selection_get_type          (void) G_GNUC_CONST;
-GtkWidget * gtk_color_selection_new               (void);
-void        gtk_color_selection_set_update_policy (GtkColorSelection *colorsel,
-						   GtkUpdateType      policy);
-gboolean    gtk_color_selection_get_use_opacity   (GtkColorSelection *colorsel);
-void        gtk_color_selection_set_use_opacity   (GtkColorSelection *colorsel,
-						   gboolean           use_opacity);
-gboolean    gtk_color_selection_get_use_palette   (GtkColorSelection *colorsel);
-void        gtk_color_selection_set_use_palette   (GtkColorSelection *colorsel,
-						   gboolean           use_palette);
+GtkType    gtk_color_selection_get_type                (void) G_GNUC_CONST;
+GtkWidget *gtk_color_selection_new                     (void);
+void       gtk_color_selection_set_update_policy       (GtkColorSelection *colorsel,
+							GtkUpdateType      policy);
+gboolean   gtk_color_selection_get_has_opacity_control (GtkColorSelection *colorsel);
+void       gtk_color_selection_set_has_opacity_control (GtkColorSelection *colorsel,
+							gboolean           use_opacity);
+gboolean   gtk_color_selection_get_has_palette         (GtkColorSelection *colorsel);
+void       gtk_color_selection_set_has_palette         (GtkColorSelection *colorsel,
+							gboolean           use_palette);

-/* The Color set is an array of doubles, of the following format:
- * color[0] = red_channel;
- * color[1] = green_channel;
- * color[2] = blue_channel;
- * color[3] = alpha_channel;
- */
-void       gtk_color_selection_set_color           (GtkColorSelection    *colorsel,
-						    gdouble               *color);
-void       gtk_color_selection_get_color           (GtkColorSelection    *colorsel,
-						    gdouble               *color);
-void       gtk_color_selection_set_old_color       (GtkColorSelection    *colorsel,
-						    gdouble               *color);
-void       gtk_color_selection_get_old_color       (GtkColorSelection    *colorsel,
-						    gdouble               *color);
-void       gtk_color_selection_set_palette_color   (GtkColorSelection   *colorsel,
-						    gint                  x,
-						    gint                  y,
-						    gdouble              *color);
-gboolean   gtk_color_selection_get_palette_color   (GtkColorSelection   *colorsel,
-						    gint                  x,
-						    gint                  y,
-						    gdouble              *color);
-void       gtk_color_selection_unset_palette_color (GtkColorSelection   *colorsel,
-						    gint                  x,
-						    gint                  y);
-gboolean   gtk_color_selection_is_adjusting        (GtkColorSelection    *colorsel);

+void     gtk_color_selection_set_current_color   (GtkColorSelection *colorsel,
+						  GdkColor          *color);
+void     gtk_color_selection_set_current_alpha   (GtkColorSelection *colorsel,
+						  guint16            alpha);
+void     gtk_color_selection_get_current_color   (GtkColorSelection *colorsel,
+						  GdkColor          *color);
+guint16  gtk_color_selection_get_current_alpha   (GtkColorSelection *colorsel);
+void     gtk_color_selection_set_previous_color  (GtkColorSelection *colorsel,
+						  GdkColor          *color);
+void     gtk_color_selection_set_previous_alpha  (GtkColorSelection *colorsel,
+						  guint16            alpha);
+void     gtk_color_selection_get_previous_color  (GtkColorSelection *colorsel,
+						  GdkColor          *color);
+guint16  gtk_color_selection_get_previous_alpha  (GtkColorSelection *colorsel);
+gint     gtk_color_selection_get_palette_size    (GtkColorSelection *colorsel);
+gboolean gtk_color_selection_get_palette_color   (GtkColorSelection *colorsel,
+						  gint               index,
+						  GdkColor          *color);
+void     gtk_color_selection_set_palette_color   (GtkColorSelection *colorsel,
+						  gint               index,
+						  GdkColor          *color);
+void     gtk_color_selection_unset_palette_color (GtkColorSelection *colorsel,
+						  gint               index);
+gboolean gtk_color_selection_is_adjusting        (GtkColorSelection *colorsel);
+
+
+/* Deprecated calls: */
+void       gtk_color_selection_set_color        (GtkColorSelection    *colorsel,
+						 gdouble               *color);
+void       gtk_color_selection_get_color        (GtkColorSelection    *colorsel,
+						 gdouble              *color);

 #ifdef __cplusplus
 }
Index: gtk/gtkcolorseldialog.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkcolorseldialog.c,v
retrieving revision 1.6
diff -u -p -r1.6 gtkcolorseldialog.c
--- gtk/gtkcolorseldialog.c	2000/11/06 16:44:00	1.6
+++ gtk/gtkcolorseldialog.c	2001/04/19 15:24:14
@@ -89,8 +89,8 @@ gtk_color_selection_dialog_init (GtkColo
   gtk_widget_show (frame);

   colorseldiag->colorsel = gtk_color_selection_new ();
-  gtk_color_selection_set_use_palette (GTK_COLOR_SELECTION(colorseldiag->colorsel), FALSE);
-  gtk_color_selection_set_use_opacity (GTK_COLOR_SELECTION(colorseldiag->colorsel), FALSE);
+  gtk_color_selection_set_has_palette (GTK_COLOR_SELECTION(colorseldiag->colorsel), FALSE);
+  gtk_color_selection_set_has_opacity_control (GTK_COLOR_SELECTION(colorseldiag->colorsel), FALSE);
   gtk_container_add (GTK_CONTAINER (frame), colorseldiag->colorsel);
   gtk_widget_show (colorseldiag->colorsel);

Index: gtk/gtkhsv.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkhsv.c,v
retrieving revision 1.8
diff -u -p -r1.8 gtkhsv.c
--- gtk/gtkhsv.c	2001/03/24 06:41:27	1.8
+++ gtk/gtkhsv.c	2001/04/19 15:24:15
@@ -686,8 +686,12 @@ compute_sv (GtkHSV  *hsv,
 	{
 	  if (*v > 1.0)
 	    *v = 1.0;
-
-	  *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
+
+	  if (fabs (hy - vy) < fabs (hx - vx))
+	    *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
+	  else
+	    *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
+
 	  if (*s < 0.0)
 	    *s = 0.0;
 	  else if (*s > 1.0)
@@ -949,7 +953,7 @@ paint_ring (GtkHSV      *hsv,
 	  dx = xx + x - center;

 	  dist = dx * dx + dy * dy;
-	  if (dist < (inner * inner) || dist > (outer * outer))
+	  if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
 	    {
 	      *p++ = 0;
 	      *p++ = 0;
Index: tests/testgtk.c
===================================================================
RCS file: /cvs/gnome/gtk+/tests/testgtk.c,v
retrieving revision 1.244
diff -u -p -r1.244 testgtk.c
--- tests/testgtk.c	2001/04/18 19:56:48	1.244
+++ tests/testgtk.c	2001/04/19 15:24:17
@@ -6066,8 +6066,8 @@ opacity_toggled_cb (GtkWidget *w,
   GtkColorSelection *colorsel;

   colorsel = GTK_COLOR_SELECTION (cs->colorsel);
-  gtk_color_selection_set_use_opacity (colorsel,
-				       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w)));
+  gtk_color_selection_set_has_opacity_control (colorsel,
+					       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w)));
 }

 static void
@@ -6077,7 +6077,7 @@ palette_toggled_cb (GtkWidget *w,
   GtkColorSelection *colorsel;

   colorsel = GTK_COLOR_SELECTION (cs->colorsel);
-  gtk_color_selection_set_use_palette (colorsel,
+  gtk_color_selection_set_has_palette (colorsel,
 				       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w)));
 }






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]