Re: getting color from pixmap



Hi,

Here is "eyedropper" code from Gtk 1.3.x color selector. Note that
doing this for e.g. all the pixels in a pixmap would be super-slow. To
get a whole pixmap you would need to get a larger image and then
gdk_image_get_pixel() the pixels in the image.

Havoc

static void
grab_color_at_mouse (GtkWidget *button,
                     gint       x_root,
                     gint       y_root,
                     gpointer   data)
{
  GdkImage *image;
  guint32 pixel;
  GdkVisual *visual;
  GtkColorSelection *colorsel = data;
  ColorSelectionPrivate *priv;
  GdkColormap *colormap = gdk_colormap_get_system ();
#if defined (GDK_WINDOWING_X11)
  XColor xcolor;
#endif
  
  priv = colorsel->private_data;
  
  image = gdk_image_get (GDK_ROOT_PARENT (), x_root, y_root, 1, 1);
  pixel = gdk_image_get_pixel (image, 0, 0);
  visual = gdk_colormap_get_visual (colormap);
  
  switch (visual->type) {
  case GDK_VISUAL_DIRECT_COLOR:
  case GDK_VISUAL_TRUE_COLOR:
    priv->color[COLORSEL_RED] = (double)((pixel & 
visual->red_mask)>>visual->red_shift)/((1<<visual->red_prec) - 1);
    priv->color[COLORSEL_GREEN] = (double)((pixel & 
visual->green_mask)>>visual->green_shift)/((1<<visual->green_prec) - 1);
    priv->color[COLORSEL_BLUE] = (double)((pixel & 
visual->blue_mask)>>visual->blue_shift)/((1<<visual->blue_prec) - 1);
    break;
  case GDK_VISUAL_STATIC_GRAY:
  case GDK_VISUAL_GRAYSCALE:
    priv->color[COLORSEL_RED] = (double)pixel/((1<<visual->depth) - 1);
    priv->color[COLORSEL_GREEN] = (double)pixel/((1<<visual->depth) - 1);
    priv->color[COLORSEL_BLUE] = (double)pixel/((1<<visual->depth) - 1);
    break;
#if defined (GDK_WINDOWING_X11)
  case GDK_VISUAL_STATIC_COLOR:
    xcolor.pixel = pixel;
    XQueryColor (GDK_DISPLAY (), GDK_COLORMAP_XCOLORMAP (colormap), &xcolor);
    priv->color[COLORSEL_RED] = xcolor.red/65535.0;
    priv->color[COLORSEL_GREEN] = xcolor.green/65535.0;
    priv->color[COLORSEL_BLUE] = xcolor.blue/65535.0;
    break;
#endif
  case GDK_VISUAL_PSEUDO_COLOR:
    priv->color[COLORSEL_RED] = colormap->colors[pixel].red/(double)0xffffff;
    priv->color[COLORSEL_GREEN] = colormap->colors[pixel].green/(double)0xffffff;
    priv->color[COLORSEL_BLUE] = colormap->colors[pixel].blue/(double)0xffffff;
    break;
  default:
    g_assert_not_reached ();
    break;
  }
  
  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]);
  update_color (colorsel);
}

static void
mouse_motion (GtkWidget      *button,
              GdkEventMotion *event,
              gpointer        data)
{
  grab_color_at_mouse (button, event->x_root, event->y_root, data); 
}

static void
mouse_release (GtkWidget      *button,
               GdkEventButton *event,
               gpointer        data)
{
  GtkColorSelection *colorsel = data;
  ColorSelectionPrivate *priv;
  priv = colorsel->private_data;
  
  gtk_signal_disconnect_by_func (GTK_OBJECT (button), mouse_motion, data);
  gtk_signal_disconnect_by_func (GTK_OBJECT (button), mouse_release, data);
  
  grab_color_at_mouse (button, event->x_root, event->y_root, data);
  gdk_pointer_ungrab (0);
}

/* Helper Functions */
static void
mouse_press (GtkWidget      *button,
             GdkEventButton *event,
             gpointer        data)
{
  GtkColorSelection *colorsel = data;
  ColorSelectionPrivate *priv;
  priv = colorsel->private_data;
  
  gtk_signal_connect (GTK_OBJECT (button), "motion_notify_event", mouse_motion, data);
  gtk_signal_connect (GTK_OBJECT (button), "button_release_event", mouse_release, data);
  gtk_signal_disconnect_by_func (GTK_OBJECT (button), mouse_press, data); 
}




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