How to get screen pixel color in x11

I want to get the RGB value of the top / left pixel (0; 0) of the whole x11 display.

what I still have:

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

but I need these values ​​to be 0..255 or (0.00)..(1.00) , while they look like 0..57825 , which do not match the format.

also, copying the entire screen to get one pixel is very slow. since it will be used in mission-critical environments, I would appreciate it if someone knows a more efficient way to do this. Perhaps using XGetSubImage size of 1x1, but I am very poorly versed in x11 development and do not know how to implement this.

what should I do?

+4
source share
2 answers

I took your code and got it for compilation. Values ​​printed (scaled to 0-255) give me the same values ​​as for the desktop background image.

 #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; } 
+7
source

From the XColor (3) man page:

Red, green, and blue values ​​are always in the range 0 through 65535 inclusive, regardless of the number of bits actually used in the display hardware. The server scales these values ​​to the range used by the hardware. Black is (0,0,0) and white is (65535 65535 65535). In some functions, the flags element controls which of the red, green, and blue elements is used and can be enabled OR from zero or more from DoRed, DoGreen, and DoBlue.

Therefore, you must scale these values ​​in any range that you want.

+2
source

Source: https://habr.com/ru/post/1490202/


All Articles