t* A cli color picker URI git clone git://git.codevoid.de/xpick.git DIR Log DIR Files DIR Refs DIR README DIR LICENSE --- DIR commit b8390fe0fb70b6a08c1f1c541c7d2e641f0c6dfc URI Author: c0dev0id <sh+github[at]codevoid[dot]de> Date: Mon, 30 Nov 2020 21:59:08 +0100 Initial Commit Diffstat: A Makefile | 15 +++++++++++++++ A README | 19 +++++++++++++++++++ A xpick.c | 35 +++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 0 deletions(-) --- DIR diff --git a/Makefile b/Makefile t@@ -0,0 +1,15 @@ +CFLAGS=-g -Wall +INCS=-I/usr/X11R6/include +LIBS=-L/usr/X11R6/lib -lX11 + +xpick: + $(CC) $(CFLAGS) $(INCS) $(LIBS) -o ${@} xpick.c + +all: xpick + +clean: + rm -f xpick + +install: xpick + install -m755 xpick /usr/local/bin/xpick + DIR diff --git a/README b/README t@@ -0,0 +1,19 @@ +# xpick + +pick a color and show it in hex. + +## Installation + + $ make + $ sudo make install + +## Usage + + $ xpick (then click somewhere) + #001800 + +Use xclip to push the value into the clipboard instead of showing it on +the terminal + + $ xpick | xclip + DIR diff --git a/xpick.c b/xpick.c t@@ -0,0 +1,35 @@ +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/cursorfont.h> +#include <stdio.h> + + +int main() { + + Display *dpy = XOpenDisplay(NULL); + Window root = RootWindow(dpy, DefaultScreen(dpy)); + + Cursor cursor = XCreateFontCursor(dpy, XC_tcross); + XColor fgc, bgc; + fgc.red = -1; fgc.green = 0; fgc.blue = 0; + bgc.red = -1; bgc.green = -1; bgc.blue = -1; + + XRecolorCursor(dpy, cursor, &fgc, &bgc); + XGrabPointer(dpy, root, 0, ButtonReleaseMask, GrabModeAsync, GrabModeAsync, + None, cursor, CurrentTime); + + XEvent event; + XNextEvent(dpy, &event); + XUngrabPointer(dpy, CurrentTime); + XImage *ximage = XGetImage(dpy, root, event.xbutton.x_root, + event.xbutton.y_root, 1, 1, -1, ZPixmap); + + unsigned long p = XGetPixel(ximage, 0, 0); + XDestroyImage(ximage); + + XColor result; result.pixel = p; + XQueryColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), &result); + printf("#%02.2x%02.2x%02.2x\n", + result.red/256, result.green/256, result.blue/256); + return 0; +}