#!/usr/bin/env bash
 
# Copyright (C) 2021  Max Z. Tan

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

color=$BLOCK_INSTANCE

if [ -z "${SWAY}" ]; then
  # sleep to cater to grabc startup time
  sleep 0.5
  value=$(grabc 2>/dev/null)
  if [ "${CLIPBOARD}" ]; then
    i3-msg -q exec "xclip -sel clip < <(echo -n \\$value)"
  fi
else
  value=$(grim -g "$(slurp -p -b '#00000000')" -t ppm - \
    | convert - -format '%[pixel:s]' info:- \
    | awk -F '[(,)]' '{printf("#%02x%02x%02x",$2,$3,$4)}')
  if [ "${CLIPBOARD}" ]; then
    swaymsg -q exec wl-copy "\\$value"
  fi
fi
full_text="<span foreground=\"$value\"></span><span foreground=\"$color\">$value</span>"

echo $full_text
