tags:

views:

545

answers:

3

Hello,

I'm looking for a possibility to get the color of a pixel with given screen coordinates (x,y) in c++ / Linux? Maybe something similarly like getPixel() in Windows. I spent the whole day to find sth but without any success.

Thanks, Stefan

A: 

See various different techniques posted at http://ubuntuforums.org/showthread.php?t=715256

Ken Bloom
thanks, i already found this topic. actually there's no c++ solution and i'm thinking now about dismissing my firefox-extension (for the xpcom component i have to write in c++) and just create a java-application instead.
Stefan
@Stefan, you can port the Python Xlib example into C/C++.
Ken Bloom
oh - that sounds great! i will take a look at this. thanks a lot!
Stefan
didn't really work for me but it gave me the right direction. thanks!
Stefan
+2  A: 

Assuming you mean using C and GTK the answer can be using:

gdk_get_default_root_window()

And

GdkPixbuf*  gdk_pixbuf_get_from_drawable    (GdkPixbuf *dest,
                                             GdkDrawable *src,
                                             GdkColormap *cmap,
                                             int src_x,
                                             int src_y,
                                             int dest_x,
                                             int dest_y,
                                             int width,
                                             int height);

EDIT: sample c++ code using Gdkmm (note that this is just a sample that assume an RGB color space, you should check the colorspace of the drawable before giving a meaning to the raw bytes).

#include <iostream>
#include <gtkmm.h>
#include <gdkmm.h>

int main(int argc, char* argv[])
{
  Gtk::Main kit(argc, argv);
  if(argc != 3) { std::cerr << argv[0] << " x y" << std::endl; return 1;}
  int x = atoi(argv[1]);
  int y = atoi(argv[2]);
  Glib::RefPtr<Gdk::Screen> screen = Gdk::Screen::get_default();
  Glib::RefPtr<Gdk::Drawable> win = screen->get_root_window();
  Glib::RefPtr<Gdk::Pixbuf> pb = Gdk::Pixbuf::create(win, x, y, 1, 1);
  unsigned char* rgb = pb->get_pixels();
  std::cerr << (int)rgb[0] << ", " << (int)rgb[1] << ", " << (int)rgb[2] << std::endl;
  return 0;
}
baol
If you run `gdk_pixbuf_get_from_drawable` on the root window, will it tell you the pixel color in the root window (even if it's obscured by another window), or it tell you the color of whatever window is on top above that pixel?
Ken Bloom
The second that you said.
baol
A: 

How to compile it?

sirHermit