-
Notifications
You must be signed in to change notification settings - Fork 12
/
wl-color-picker.sh
executable file
·68 lines (60 loc) · 1.94 KB
/
wl-color-picker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
#
# License: MIT
#
# A script to easily pick a color on a wayland session by using:
# slurp to select the location, grim to get the pixel, convert
# to make the pixel a hex number and zenity to display a nice color
# selector dialog where the picked color can be tweaked further.
#
# The script was possible thanks to the useful information on:
# https://www.trst.co/simple-colour-picker-in-sway-wayland.html
# https://unix.stackexchange.com/questions/320070/is-there-a-colour-picker-that-works-with-wayland-or-xwayland/523805#523805
#
# Check if running under wayland.
if [ "$WAYLAND_DISPLAY" = "" ]; then
zenity --error --width 400 \
--title "No wayland session found." \
--text "This color picker must be run under a valid wayland session."
exit 1
fi
# Get color position
position=$(slurp -b 00000000 -p)
# Sleep at least for a second to prevet issues with grim always
# returning improper color.
sleep 1
# Store the hex color value using graphicsmagick or imagemagick.
if command -v /usr/bin/gm &> /dev/null; then
color=$(grim -g "$position" -t png - \
| /usr/bin/gm convert - -format '%[pixel:p{0,0}]' txt:- \
| tail -n 1 \
| rev \
| cut -d ' ' -f 1 \
| rev
)
else
color=$(grim -g "$position" -t png - \
| convert - -format '%[pixel:p{0,0}]' txt:- \
| tail -n 1 \
| cut -d ' ' -f 4
)
fi
if [ "$1" == "clipboard" ]; then
echo $color | wl-copy -n
else
# Display a color picker and store the returned rgb color
rgb_color=$(zenity --color-selection \
--title="Copy color to Clipboard" \
--color="${color}"
)
# Execute if user didn't click cancel
if [ "$rgb_color" != "" ]; then
# Convert rgb color to hex
hex_color="#"
for value in $(echo "${rgb_color}" | grep -E -o -m1 '[0-9]+'); do
hex_color="$hex_color$(printf "%.2x" $value)"
done
# Copy user selection to clipboard
echo $hex_color | wl-copy -n
fi
fi