Initial Commit

This commit is contained in:
sh+github@codevoid.de 2020-11-30 20:59:08 +00:00
commit ac09e065e2
3 changed files with 69 additions and 0 deletions

15
Makefile Normal file
View File

@ -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

19
README Normal file
View File

@ -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

35
xpick.c Normal file
View File

@ -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;
}