【发布时间】:2021-05-13 03:03:51
【问题描述】:
我需要获取鼠标光标下方像素的十六进制代码颜色。有很多精美的 GUI 工具可以解决此任务,但我需要一种简单 命令行方式来获取颜色,以便我可以在 shell 脚本中使用该解决方案。
可能我可以使用 ImageMagick 截取(一个像素?)屏幕截图并从中提取颜色(我可以使用 xdotool 获取位置)。也许有更简单的解决方案。
有什么建议吗?
【问题讨论】:
我需要获取鼠标光标下方像素的十六进制代码颜色。有很多精美的 GUI 工具可以解决此任务,但我需要一种简单 命令行方式来获取颜色,以便我可以在 shell 脚本中使用该解决方案。
可能我可以使用 ImageMagick 截取(一个像素?)屏幕截图并从中提取颜色(我可以使用 xdotool 获取位置)。也许有更简单的解决方案。
有什么建议吗?
【问题讨论】:
对其他解决方案不太满意,我尝试了 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
编辑:
现在使用 Gnome Shell,我遇到了上述解决方案的问题(导入不会对可见窗口进行截图,我不知道为什么。欢迎提示)。另一种方法是使用scrot 之类的(快速)屏幕截图并使用convert 而不是import:
#!/bin/sh
# Get hex rgb color under mouse cursor, put it into clipboard and create a
# notification.
scrot --overwrite /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
2020 年更新:较新版本的 scrot 需要设置“--overwrite”选项才能正常工作。
【讨论】:
当然可以。但是你需要另一个 linux 包。如果您使用的是 Ubuntu,只需发出:
sudo apt-get install xdotool grabc
然后运行grabc,但在后台运行
grabc &
然后使用 xdotool 执行鼠标点击
xdotool click 1
点击会被grabc的光标捕捉到,后台进程会输出颜色。
但也许它不适用于脚本。为此,您可能需要查看this topic on the Ubuntu forums。
或者如果你不介意,你可以do it with python as described here。
【讨论】:
另一种获取像素颜色的方法,基于@Christian 的出色回答:
eval $(xdotool getmouselocation --shell)
xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+$X+$Y" txt:- | grep -om1 '#\w\+'
xwd 在我的系统上比import 快得多。
【讨论】:
最快的解决方案:
编辑/更新:
在查看了https://github.com/muquit/grabc 的新版本之后,我需要说它胜过其他任何东西:
time ( COL=$(~/grabc/grabc -w $WINDOW_ID -l +$X+$Y) ; echo COL=$COL )
COL=#2e2f30
real 0m0,006s
user 0m0,005s
sys 0m0,000s
显然,大多数发行版都没有此功能,因此请谨慎使用。如果您需要发行版提供的东西,那么旧答案仍然有效:
time ( X=1 ; Y=1 ; xdotool mousemove --sync $X $Y sleep 0.01 click 1 \
mousemove --sync restore & COL=$( grabc 2>/dev/null ) ; \
echo COL=$COL )
COL=#ddedaa
real 0m0.046s
user 0m0.004s
sys 0m0.008s
可能存在时间问题 - 即 sleep 0.01 部分 - 但即使增加到 0.1 它仍然比其他解决方案更快,并且如果 $COL 为空,您可以检测到错误。
如果单击不是一个选项,或者时间问题太成问题,那么我可能会使用 import(如果特定的窗口 shell 出现问题,则 xwd 然后 scrot)
显然对于$X 和$Y 使用eval $(xdotool getmouselocation --shell) 或某事。类似的
比较:
scrot
time ( X=1 ; Y=1 ; scrot /tmp/copycolor.png ; \
IMAGE=$(convert /tmp/copycolor.png -depth 8 -crop 1x1+$X+$Y txt:- ) ; \
COL=$( echo $IMAGE | grep -om1 '#\w\+' ) ; \
echo COL=$COL )
COL=#DDEDAA
real 0m0.590s
user 0m0.596s
sys 0m0.024s
xwd
time ( X=1 ; Y=1 ; COL=$( xwd -root -silent | \
convert xwd:- -depth 8 -crop "1x1+$X+$Y" txt:- | grep -om1 '#\w\+' ) ; \
echo COL=$COL )
COL=#DDEDAA
real 0m0.387s
user 0m0.380s
sys 0m0.084s
导入:
time ( X=1 ; Y=1 ; IMAGE=$(import -window root -depth 8 -crop 1x1+$X+$Y txt:-) ;\
COL=$( echo $IMAGE | grep -om1 '#\w\+' ) ; \
echo COL=$COL )
COL=#DDEDAA
real 0m0.302s
user 0m0.456s
sys 0m0.044s
【讨论】:
picom 来拥有半透明的窗口,并且我发现(出乎意料但高兴地)这个grabc 的东西不受它的影响。