nix

configuration files that power my machines
Log | Files | Refs | README | LICENSE

wl-color-picker (2472B)


      1 #!/bin/bash
      2 #
      3 # License: MIT
      4 #
      5 # A script to easily pick a color on a wayland session by using:
      6 # slurp to select the location, grim to get the pixel, convert
      7 # to make the pixel a hex number and zenity to display a nice color
      8 # selector dialog where the picked color can be tweaked further.
      9 #
     10 # The script was possible thanks to the useful information on:
     11 # https://www.trst.co/simple-colour-picker-in-sway-wayland.html
     12 # https://unix.stackexchange.com/questions/320070/is-there-a-colour-picker-that-works-with-wayland-or-xwayland/523805#523805
     13 #
     14 
     15 showhelp() {
     16 	echo "A basic wlroots compatible color picker script."
     17 	echo ""
     18 	echo "Usage:"
     19 	echo "  wl-color-picker [command] [options]"
     20 	echo ""
     21 	echo "Commands:"
     22 	echo "  clipboard       Copy color to clipboard without dialog"
     23 	echo "    --no-notify   Don't show a system notification of copied color"
     24 }
     25 
     26 CLIPBOARD=0
     27 NO_NOTIFY=0
     28 
     29 while [ "$1" ]; do
     30 	case $1 in
     31 	'-h' | '--help' | 'help' | '?')
     32 		showhelp
     33 		exit
     34 		;;
     35 	'clipboard')
     36 		CLIPBOARD=1
     37 		;;
     38 	'--no-notify')
     39 		NO_NOTIFY=1
     40 		;;
     41 	esac
     42 
     43 	shift
     44 done
     45 
     46 # Check if running under wayland.
     47 if [ "$WAYLAND_DISPLAY" = "" ]; then
     48 	zenity --error --width 400 \
     49 		--title "No wayland session found." \
     50 		--text "This color picker must be run under a valid wayland session."
     51 
     52 	exit 1
     53 fi
     54 
     55 # Get color position
     56 position=$(slurp -b 00000000 -p)
     57 
     58 # Sleep at least for a second to prevet issues with grim always
     59 # returning improper color.
     60 sleep 1
     61 
     62 # Store the hex color value using graphicsmagick or imagemagick.
     63 if command -v /usr/bin/gm &>/dev/null; then
     64 	color=$(
     65 		grim -g "$position" -t png - |
     66 			/usr/bin/gm convert - -format '%[pixel:p{0,0}]' txt:- |
     67 			tail -n 1 |
     68 			rev |
     69 			cut -d ' ' -f 1 |
     70 			rev
     71 	)
     72 else
     73 	color=$(
     74 		grim -g "$position" -t png - |
     75 			convert - -format '%[pixel:p{0,0}]' txt:- |
     76 			tail -n 1 |
     77 			cut -d ' ' -f 4
     78 	)
     79 fi
     80 
     81 if [ $CLIPBOARD -eq 1 ]; then
     82 	echo $color | wl-copy -n
     83 	if [ $NO_NOTIFY -ne 1 ]; then
     84 		notify-send "Color copied to clipboard." $color
     85 	fi
     86 else
     87 	# Display a color picker and store the returned rgb color
     88 	rgb_color=$(
     89 		zenity --color-selection \
     90 			--title="Copy color to Clipboard" \
     91 			--color="${color}"
     92 	)
     93 
     94 	# Execute if user didn't click cancel
     95 	if [ "$rgb_color" != "" ]; then
     96 		# Convert rgb color to hex
     97 		hex_color="#"
     98 		for value in $(echo "${rgb_color}" | grep -E -o -m1 '[0-9]+'); do
     99 			hex_color="$hex_color$(printf "%.2x" $value)"
    100 		done
    101 
    102 		# Copy user selection to clipboard
    103 		echo $hex_color | wl-copy -n
    104 	fi
    105 fi