Re: Getting the color of a pixel



On Thu, 28 Nov 2002, Owen Taylor wrote:


Bryan Brown <bbrown saltmine radix net> writes:

How does one get the color of a pixel in a drawing area or a pixmap using
gtk 1.2 on a Linux box?

I'm successfully drawing lines and points in different colors, but I also
need to be able to get the color of a given point that's already been
drawn in another routine.

I'd generally try to avoid doing this, it's going to be inefficient
and not very reliable. (Certainly don't do this for a window; the
point might be obscured by another window or offscreen. It's
a little better for a pixmap.)

For GTK+-1.2 it's also not possible to do without dropping down
to Xlib. GTK+-2.0 adds gdk_colormap_query_color() which you can
use like:

====
  image = gdk_drawable_get_image (root_window, x_root, y_root, 1, 1);
  pixel = gdk_image_get_pixel (image, 0, 0);
  g_object_unref (image);

  gdk_colormap_query_color (colormap, pixel, &color);
===

i searched a solution yesterday and i tried this : 

#define IMAGE_WIDTH 256 
#define IMAGE_HEIGHT 256

typedef struct
{
  guint r,g,b;
}couleur;   

  GdkPixbuf *pix_buf;
  GdkColormap *cmap;
  guchar *pixels;         
  couleur col;
  GdkDrawable *gdk_draw;  /* the GdkDrawable */
  guint i,j;               /* the coord. of the point */

  cmap = gdk_drawable_get_colormap (gdk_draw);
  pix_buf = gdk_pixbuf_get_from_drawable (NULL, gdk_draw, cmap, 0, 0, 0, 
0, IMAGE_WIDTH, IMAGE_HEIGHT);
  pixels = gdk_pixbuf_get_pixels (pix_buf);
  col.r = pixels[(j*IMAGE_WIDTH+i)*3+0];
  col.g = pixels[(j*IMAGE_WIDTH+i)*3+1];
  col.b = pixels[(j*IMAGE_WIDTH+i)*3+2];

but when i compared the values of col and those that i put in the 
GtkDrawingArea, i do not find exactly the same values (that is, I find 
sometimes the value +1 or +3). Where am I wrong ? 

thank you

Vincent TORRI


(Code snippet from gtkcolorsel.c's color dropper ... that's about
the only case I would suggest it.)

Regards,
                                        Owen
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


-- 
TORRI Vincent
Mathematiques Appliquees Bordeaux                                                
Institut de Mathematiques                                                       
Universite Bordeaux 1                                                           
351 cours de la liberation                                                      
33 405 Talence cedex - France                                                   
                                                                                
Tel : 33 (0)5 57 96 21 42                                                       
Fax : 33 (0)5 56 84 26 26                                                       
--




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