Вот, подумалось, что для любителей терминала может пригодиться...
Дополнил код интерактивным выводом rgb при перемещении курсора.
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/cursorfont.h>
int 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);
if (image == (XImage *) NULL)
return(1);
color->pixel = XGetPixel (image, 0, 0);
XFree (image);
XQueryColor (d, DefaultColormap(d, DefaultScreen (d)), color);
return(0);
};
Window selectWindow(Display *display,int *x,int *y, int count)
{
Cursor target_cursor;
static Cursor cross_cursor=(Cursor) NULL;
int status;
Window target_window, root_window;
XEvent event;
Colormap cmap;
XColor c;
int r, g, b;
target_window = (Window) NULL;
if (cross_cursor == (Cursor) NULL)
{
cross_cursor=XCreateFontCursor(display,XC_crosshair);
if (cross_cursor == (Cursor) NULL)
{
fprintf (stdout,"Failed to create Cross Cursor!\n");
return ((Window) NULL);
}
}
target_cursor=cross_cursor;
root_window=XRootWindow(display,XDefaultScreen(display));
status=XGrabPointer(display,root_window,False,
(unsigned int) ButtonPressMask | PointerMotionMask,GrabModeAsync,
GrabModeAsync,root_window,target_cursor,CurrentTime);
if (status == GrabSuccess)
{
XAllowEvents(display,SyncPointer,CurrentTime);
while (count)
{
XWindowEvent(display,root_window,ButtonPressMask | PointerMotionMask,&event);
count--;
if (event.type == ButtonPress || count == 0 )
{
if (event.xbutton.button == 3)
{
target_window=root_window;
if (target_window == (Window) NULL)
{
fprintf (stdout,
"Failed to get target window, getting root window!\n");
target_window=root_window;
}
XUngrabPointer(display,CurrentTime);
break;
}
else
printf ("\n");
} else
if (event.type == MotionNotify)
{
target_window=root_window;
if (target_window == (Window) NULL)
{
fprintf (stdout,
"Failed to get target window, getting root window!\n");
target_window=root_window;
}
else
{
(*x)=event.xbutton.x;
(*y)=event.xbutton.y;
cmap=DefaultColormap(display,DefaultScreen(display));
if (get_pixel_color (display, *x, *y, &c) == 0)
{
XQueryColor(display,cmap,&c);
r=(c.red >> 8);
g=(c.green >> 8);
b=(c.blue >> 8);
printf ("\033[1F\033[0K%d %d %d x=%d y=%d\n", r, g, b, *x, *y);
}
}
}
}
}
else
{
fprintf (stdout,"Failed to grab mouse!\n");
return ((Window) NULL);
}
/* free things we do not need, always a good practice */
XFreeCursor(display,cross_cursor);
(*x)=event.xbutton.x;
(*y)=event.xbutton.y;
return (target_window);
}
int main(int argc, char *argv[])
{
Display *display;
XColor c;
int status;
Colormap cmap;
Window target_window, root_window;
int i, r, g , b;
int ix, iy;
int inwin = 0, done = 0;
if (argc > 1)
{
display=XOpenDisplay((char *) NULL);
int maxwidth = DisplayWidth (display, DefaultScreen(display));
int maxheight = DisplayHeight (display, DefaultScreen(display));
if (argv[1][0] == 'w')
{
inwin = 1;
printf("\033[?25l\033[0J\n");
}
else
{
ix = atoi(argv[1]);
iy = atoi(argv[2]);
}
if (inwin)
{
target_window = selectWindow(display, &ix, &iy, 10000);
}
if (display != (Display *) NULL && ix >= 0 && ix < maxwidth && iy >= 0 && iy < maxheight)
{
cmap=DefaultColormap(display,DefaultScreen(display));
if (get_pixel_color (display, ix, iy, &c) == 0)
{
XQueryColor(display,cmap,&c);
r=(c.red >> 8);
g=(c.green >> 8);
b=(c.blue >> 8);
if (inwin)
printf ("\033[1F\033[0K");
else
printf ("%d %d %d\n", r, g, b);
done = 1;
}
}
if (done)
{
if (display != (Display *) NULL) XCloseDisplay(display);
}
else
{
printf ("-1 -1 -1\n");
return(-1);
}
}
else
{
printf ("xycolor <x> <y>\n or\nxycolor w\n lbutton - save, rbutton - exit\n");
}
if (inwin)
printf("\033[?25h");
return (0);
};