2013-07-08 40 views
4

我想要获取整个x11显示器的顶部/左侧像素(0; 0)的RGB值。如何在x11中获取屏幕像素的颜色

什么我这么远:

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"; 

,但我需要这些值是0..255(0.00)..(1.00),而他们看起来0..57825,这是没有任何格式我承认。

此外,复制整个屏幕只是为了获得一个像素是非常缓慢的。因为这将用于速度至关重要的环境中,如果有人知道更高效的方式来执行此操作,我将不胜感激。也许使用尺寸为1x1的XGetSubImage,但是我在x11开发中非常糟糕,并且不知道如何实现它。

我该怎么办?

+0

除以57825? –

+0

当然,这就是我现在正在做的事情,但它让我感到毛骨悚然,因为a)我不知道它为什么起作用,b)我不知道它有多可靠,c)它仍然很慢(时间说“'cpu 0,054 total'”为单个像素!)。 – nonchip

+0

实际上,根据[this](http://http://tronche.com/gui/x/xlib/color/structures.html),它应该只是未初始化的垃圾值。在XGetPixel返回的长整型上使用一些基本的位运算符,并且应该设置。 –

回答

7

我把你的代码,并得到它编译。打印的值(缩放到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, 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/256 << " " << c.green/256 << " " << c.blue/256 << "\n"; 

    return 0; 
} 
+0

Your代码不会为我编译,我需要在某些函数之前添加'X':'XRootWindow','XDefaultScreen'和'XDefaultColormap'。 – Rakete1111

2

XColor(3)手册页:

红色,绿色和蓝色的值总是在范围0到65535包容性的,独立的显示硬件实际使用的比特数。服务器将这些值缩小到硬件使用的范围。黑色由(0,0,0)表示,白色由(65535,65535,65535)表示。在某些函数中,flags成员控制使用红色,绿色和蓝色成员中的哪一个,并且可以是DoRed,DoGreen和DoBlue中零个或多个的包含OR。

所以你必须将这些值缩放到你想要的范围内。

+0

其实我试过了(因为它是最接近的值),但它比57825更不准确:-( – nonchip