#!/usr/bin/env fish

# Defaults to hyprpicker if available, otherwise uses a rewrite
# of https://github.com/jgmdev/wl-color-picker/blob/main/wl-color-picker.sh in fish.

if test -z "$WAYLAND_DISPLAY"
    notify-send "Not in a wayland session"
    exit 1
end

function notify-color -a color
    set swatch (mktemp -u --suffix=.png)
    magick -size 32x32 xc:$color $swatch
    notify-send -i $swatch "Color copied to clipboard." $color
    rm $swatch
end

if command -v hyprpicker
    set color (hyprpicker --autocopy --format=hex --lowercase-hex)
    notify-color $color
    exit 0
end

set pos (slurp -b 00000000 -p)
if test -z "$pos"
    exit 1
end

set color (grim -g "$pos" -t ppm - \
    | magick - -format '%[pixel:p{0,0}]' txt:- \
    | tail -n 1 \
    | string match -r '#[0-9A-Fa-f]{6}' \
    | string lower)

echo $color | wl-copy -n
notify-color $color
