[英]Get pixel RGB color with XGetPixel on focused window in C++
When i click on my window i get correct xy values from another function. 当我单击我的窗口时,我从另一个函数获得正确的xy值。 With those values I wan't to get the RGB color value on the window that is currently active and not the whole screen.
使用这些值,我将无法在当前处于活动状态的窗口而不是整个屏幕上获得RGB颜色值。 Right now I think it's reading the whole screen.
现在,我认为它正在读取整个屏幕。 How can I edit this code to get it work?
如何编辑此代码以使其正常工作? (the window itself is a Fl_Double_Window using FLTK library).
(窗口本身是使用FLTK库的Fl_Double_Window)。
Image to explain the problem: 解释问题的图片:
int getRGB(int x, int y)
{
XColor c;
Display *d = XOpenDisplay((char *) NULL);
XImage *image;
image = XGetImage(d, RootWindow (d, DefaultScreen(d)), x, y, 1, 1, AllPlanes, XYPixmap);
c.pixel = XGetPixel (image, x, y);
XFree (image);
XQueryColor (d, DefaultColormap(d, DefaultScreen (d)), &c);
cout << c.red/256 << " " << c.green/256 << " " << c.blue/256 << "\n" ;
}
Use the Window handle that is used to render the map in stead of RootWindow(); 使用用于渲染地图的Window句柄代替RootWindow(); since you already get the correct x/y coordinates you must have that handle available somewhere.
由于您已经获得了正确的x / y坐标,因此必须在某处使用该句柄。
I found a way using Fl_BMP_image (gInterface.carte): 我找到了使用Fl_BMP_image(gInterface.carte)的方法:
void GetPixelColor(int x, int y,char *r,char *g, char *b) //get RGB color from clicked pixel x,y and returns rgb
{
const char* buf = gInterface.carte->data()[0];
long index = (y * gInterface.carte->w() * gInterface.carte->d()) + (x * gInterface.carte->d()); // X/Y -> buf index
if (gInterface.carte->count()==1) { // RGB
*r = *(buf+index+0);
*g = *(buf+index+1);
*b = *(buf+index+2);
printf("%d, %d : %2d, %d, %d \n", x,y, // hex dump r/g/b
(unsigned char) *r,
(unsigned char) *g ,
(unsigned char) *b );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.