如何在x11中获得屏幕像素的颜色

non*_*hip 8 c++ linux x11 xlib

我想获得整个x11显示器的顶部/左侧像素(0; 0)的RGB值.

到目前为止我得到了什么:

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, 0, 0);
XFree (image);
XQueryColor (d, DefaultColormap(d, DefaultScreen (d)), c);
cout << c.red << " " << c.green << " " << c.blue << "\n";
Run Code Online (Sandbox Code Playgroud)

但我需要这些价值观,0..255或者(0.00)..(1.00),虽然它们看起来0..57825,但我认识的格式不是.

另外,复制整个屏幕只是为了得到一个像素是非常慢的.因为这将用于速度关键的环境,如果有人知道更高效的方式,我会很感激.也许使用XGetSubImage1x1大小,但我在x11开发时非常糟糕,并且不知道如何实现它.

我该怎么办?

par*_*ydr 12

我拿了你的代码并让它编译.打印的值(缩放到0-255)给出了与我设置到桌面背景图像相同的值.

#include <iostream>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

using namespace std;

int main(int, char**)
{
    XColor c;
    Display *d = XOpenDisplay((char *) NULL);

    int x=0;  // Pixel x 
    int y=0;  // Pixel y

    XImage *image;
    image = XGetImage (d, XRootWindow (d, XDefaultScreen (d)), x, y, 1, 1, AllPlanes, XYPixmap);
    c.pixel = XGetPixel (image, 0, 0);
    XFree (image);
    XQueryColor (d, XDefaultColormap(d, XDefaultScreen (d)), &c);
    cout << c.red/256 << " " << c.green/256 << " " << c.blue/256 << "\n";

    return 0;
}
Run Code Online (Sandbox Code Playgroud)