2013-07-08 39 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