#!/bin/sh
# Grab the color of a specific point

# Required Commands:
# convert(imagemagick) - Get the color from the screenshotted point
# grim - Take a screenshot of the selected point
# slurp - Select a point
# mkdir - Create missing directories
# notify-send (optional) - Send a screenshot with the color
# wl-copy(wl-clipboard) (optional) - Copy the color into the clipboard
# xdg-open (optional) - Open the screenshot in an image viewer

usage() {
	printf '%s\n' "usage: ${0##*/} [options]" \
	              "options:" \
	              "  -c - copy to clipboard" \
	              "  -h - display usage statement" \
	              "  -n - send a notification with the color" \
	              "  -o - open the color as an image" \
	              "  -r - display the color as an rgb value" \
	              "  -x - display the color as a hexcode value (default)"
}

# Parse arguments
for flag
do
	# Make sure flag begins with '-' and are atleast two characters long
	case $flag in
		-  ) continue ;;
		-- ) break ;;
		-* ) ;;
		*  ) continue ;;
	esac

	# Split the flags into individual arguments and set variables
	flag=${flag#-}

	while [ "$flag" ]
	do
		a=${flag%"${flag#?}"}

		case $a in
			c ) copy_to_clipboard=true ;;
			h ) usage; exit 0 ;;
			n ) send_notification=true ;;
			o ) open_color_image=true ;;
			r ) color_type="rgb" ;;
			x ) color_type="hex" ;;
			* ) printf '%s\n' "${0##*/}: -$a invalid argument" 1>&2
			    usage 1>&2; exit 1 ;;
		esac

		flag=${flag#?}
	done
done

# Make sure this directory exist before continuing
[ -d "/tmp/${0##*/}" ] || { mkdir -p "/tmp/${0##*/}" || exit 1; }

# Set color_type if not already set
[ "$color_type" ] || color_type="hex"

# Get a screenshot of the pixel
grim -s 1 -g "$(slurp -b 00000000 -s 00000000 -w -1 -p)" "/tmp/${0##*/}/temp.png"

case $color_type in
	hex ) color=$(convert "/tmp/${0##*/}/temp.png" -format "%[hex:p]\n" info:) ;;
	rgb )
		color=$(convert "/tmp/${0##*/}/temp.png" -format "%[pixel:p]\n" info:)
		color=${color#*(}; color=${color%)*}
	;;
	*   ) printf '%s\n' "${0##*/}: invalid color_type: $color_type" 1>&2; exit 1 ;;
esac
printf '%s\n' "$color"

# Copy color to clipboard
[ "$copy_to_clipboard" ] && {
	wl-copy -n "$color" || printf '%s\n' "${0##*/}: failed to copy color to clipboard" 1>&2
}

# Open color image in the user's perfered image viewer
[ "$open_color_image" ] && {
	# Create the color image if it doesn't already exist
	[ -f "/tmp/${0##*/}/o$color.png" ] || {
		case $color_type in
			hex ) ocolor="#$color" ;;
			rgb ) ocolor="rgb($color)" ;;
		esac

		convert -size 150x150 xc:"$ocolor" +size -gravity center \
		        \( -background white pango:"<span font_family=\"monospace\"
		        font_weight=\"bold\"> $color </span>" \) \
		        -composite "/tmp/${0##*/}/o$color.png"
	}

	xdg-open "/tmp/${0##*/}/o$color.png" > "/tmp/${0##*/}/xdg-open.log" 2>&1 &
}

# Send a notification with an image of the color aswell as the value
[ "$send_notification" ] && {
	[ -f "/tmp/${0##*/}/n$color.png" ] || {
		case $color_type in
			hex ) ncolor="#$color"; color_prefix="hex:";;
			rgb )
				ncolor="rgb($color)"
				color_r="${color%%,*}"
				color_g="${color#*,}"; color_g="${color_g%,*}"
				color_b="${color##*,}"
				color_rgb="$color_r$color_g$color_b"
				color_prefix="rgb:";;
		esac

		convert -size 64x64 xc:"$ncolor" "/tmp/${0##*/}/n$color_rgb.png"
	}

	notify-send -a "${0##*/}" -i "/tmp/${0##*/}/n$color_rgb.png" "$color_prefix $color"
}
