Shell command to get color under mouse cursor (xorg)

I need to get the color in hexadecimal pixel code under my mouse cursor. There are many useful GUI tools to solve this problem, but I need a simple command line method to get the color so that I can use this solution in a shell script.

Perhaps I can use ImageMagick to take (one pixel?) A screenshot and extract color from it (I can get the position using xdotool)). There may be a simpler solution.

Any suggestions?

+4
source share
3 answers

, ImageMagick. ! ( xclip, ImageMagick, xdotool, notify-send)

#!/bin/sh
# Get hex rgb color under mouse cursor, put it into clipboard and create a
# notification.

eval $(xdotool getmouselocation --shell)
IMAGE=`import -window root -depth 8 -crop 1x1+$X+$Y txt:-`
COLOR=`echo $IMAGE | grep -om1 '#\w\+'`
echo -n $COLOR | xclip -i -selection CLIPBOARD
notify-send "Color under mouse cursor: " $COLOR

EDIT:

, Gnome Shell, ( , , . ). () , scrot, convert import:

#!/bin/sh
# Get hex rgb color under mouse cursor, put it into clipboard and create a
# notification.

scrot /tmp/copycolor.png
eval $(xdotool getmouselocation --shell)
IMAGE=`convert /tmp/copycolor.png -depth 8 -crop 1x1+$X+$Y txt:-`
COLOR=`echo $IMAGE | grep -om1 '#\w\+'`
echo -n $COLOR | xclip -i -selection CLIPBOARD
notify-send "Color under mouse cursor: " $COLOR
+4

. linux. Ubuntu :

sudo apt-get install xdotool grabc

grabc,

grabc &

mouseclick, xdotool

xdotool click 1

grabc, .

, , script. Ubuntu.

, , python, .

+4

Another way to get pixel color based on @Christian's excellent answer:

eval $(xdotool getmouselocation --shell)
xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+$X+$Y" txt:- | grep -om1 '#\w\+'

xwdsignificantly faster than importon my system.

+2
source

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


All Articles