With only minimum knowledge of C, I have put together the following code, which I compile on Linux Mint 19 with:
gcc-7 -o getPixelColor getPixelColor.c -L/usr/X11/lib -lX11
and execute without argument:
./getPixelColor
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
void get_pixel_color(Display *d, int x, int y, XColor *color)
{
XImage *image;
image = XGetImage(d, RootWindow(d, DefaultScreen(d)), x, y, 1, 1, AllPlanes, XYPixmap);
color->pixel = XGetPixel(image, 0, 0);
XFree(image);
XQueryColor(d, DefaultColormap(d, DefaultScreen(d)), color);
}
int main(int argc, char const *argv[])
{
Display *disp = XOpenDisplay(":0");
XColor pixel_color;
get_pixel_color(disp, 0, 0, &pixel_color);
printf("%d %d %d\n", pixel_color.red, pixel_color.green, pixel_color.blue);
return 0;
}
Question:
I have no idea what the why the output numbers are not in standard RGB range: 0-255?
Current output example (taken from the desktop picture, [0,0]):
7683 8484 4097
Expected output (taken with a Windows color picker ran by Wine):
29 32 16
I get that I need to convert the values to standard range, could you advise on the formula then?