Skip to content

Instantly share code, notes, and snippets.

@01micko
Created May 13, 2024 11:23
Show Gist options
  • Save 01micko/0df689bce9b4df80d839e206adfae80d to your computer and use it in GitHub Desktop.
Save 01micko/0df689bce9b4df80d839e206adfae80d to your computer and use it in GitHub Desktop.
Yad GUI to fetch the color anywhere on screen in a wayland session, at least wlroots anyway.
#!/usr/bin/env bash
# (c) Copyright 2024 Mick Amadio <01micko@gmail.com> GPL3
# requires yad, sed, grep, dc, ppmtoxpm, grim, slurp
# simple hex to decimal conversion
find_color() {
printf "%d" "0x${1}"
}
# uses pnmtoxpm to make a 1px xpm image
get_col() {
col=$(pos="$(slurp -b FFFFFF00 -p)";sleep 1; grim -g "$pos" -t ppm - | ppmtoxpm | grep -m1 '" c.*"' | sed -e 's/\" c //' -e 's/\".*$//')
echo $col
}
# shows our selected hex, rgb() and float notation colors
return_gui() {
echo -e '<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="48px" width="48px" viewBox="0 0 48 48">
<path d="m 0 0 48 0 0 48 -48 0 z" style="fill:'$1';stroke:none;"/>
</svg>' > $TEMPDIR/col.svg
BCOL=${1/\#/}
BR=${BCOL:0:2}; BG=${BCOL:2:2}; BB=${BCOL:4:2};
BHR=$(find_color $BR)
BHG=$(find_color $BG)
BHB=$(find_color $BB)
ZC='rgb('$BHR','$BHG','$BHB')'
BHR=$(dc -e'3k '$BHR' 255 / p')
BHG=$(dc -e'3k '$BHG' 255 / p')
BHB=$(dc -e'3k '$BHB' 255 / p')
X=''
[[ ${BHR:0:1} = '.' ]] && X=0
[[ ${BHG:0:1} = '.' ]] && X=0
[[ ${BHB:0:1} = '.' ]] && X=0
ZF=''$X$BHR' '$X$BHG' '$X$BHB''
yad --title="Color Picker" --name="select-color" --form \
--image="$TEMPDIR/col.svg" --text=" Copy and paste color found\n to your application:" \
--field="Color Hex" "$1" \
--field="Color rgb" "$ZC" \
--field="Color float" "$ZF" \
--button="Ok!gtk-ok":0
}
export -f find_color get_col return_gui
# this script is wayland dependant
[[ -n "$WAYLAND_DISPLAY" ]] || {
yad --title=error --window-icon="dialog-error" --name="dialog-error" \
--text="Error: This applicatiom <b>must</b> be run in a wayland session." \
--button="Close!dialog-error":1
exit 1
}
export TEMPDIR
TEMPDIR=$(mktemp -d /tmp/colpickXXXX)
# cleanup
trap "rm -rf $TEMPDIR" EXIT
# main gui
ret=$(yad --title="Color Picker" --window-icon="select-color" --name="select-color" \
--text="Click the Select button then place the cursor \n on screen where you want to fetch the color" \
--buttons-layout=center \
--button="Select!preferences-desktop-cursors!choose place on screen":'bash -c "get_col && kill -USR1 $YAD_PID"')
[[ $? -ne 0 ]] && exit
return_gui $ret
@01micko
Copy link
Author

01micko commented May 18, 2024

I've created a repo for this - https://github.com/01micko/wly_colpick so I'll just leave this here as an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment