A collection of scripts
at main 3.5 kB view raw
1#!/bin/sh 2# Grab the color of a specific point 3 4# Required Commands: 5# convert(imagemagick) - Get the color from the screenshotted point 6# grim - Take a screenshot of the selected point 7# slurp - Select a point 8# mkdir - Create missing directories 9# notify-send (optional) - Send a screenshot with the color 10# wl-copy(wl-clipboard) (optional) - Copy the color into the clipboard 11# xdg-open (optional) - Open the screenshot in an image viewer 12 13usage() { 14 printf '%s\n' "usage: ${0##*/} [options]" \ 15 "options:" \ 16 " -c - copy to clipboard" \ 17 " -h - display usage statement" \ 18 " -n - send a notification with the color" \ 19 " -o - open the color as an image" \ 20 " -r - display the color as an rgb value" \ 21 " -x - display the color as a hexcode value (default)" 22} 23 24# Parse arguments 25for flag 26do 27 # Make sure flag begins with '-' and are atleast two characters long 28 case $flag in 29 - ) continue ;; 30 -- ) break ;; 31 -* ) ;; 32 * ) continue ;; 33 esac 34 35 # Split the flags into individual arguments and set variables 36 flag=${flag#-} 37 38 while [ "$flag" ] 39 do 40 a=${flag%"${flag#?}"} 41 42 case $a in 43 c ) copy_to_clipboard=true ;; 44 h ) usage; exit 0 ;; 45 n ) send_notification=true ;; 46 o ) open_color_image=true ;; 47 r ) color_type="rgb" ;; 48 x ) color_type="hex" ;; 49 * ) printf '%s\n' "${0##*/}: -$a invalid argument" 1>&2 50 usage 1>&2; exit 1 ;; 51 esac 52 53 flag=${flag#?} 54 done 55done 56 57# Make sure this directory exist before continuing 58[ -d "/tmp/${0##*/}" ] || { mkdir -p "/tmp/${0##*/}" || exit 1; } 59 60# Set color_type if not already set 61[ "$color_type" ] || color_type="hex" 62 63# Get a screenshot of the pixel 64# shellcheck disable=SC2086 65grim -s 1 -g "$(slurp ${SLURP_ARGS} -b 00000000 -s 00000000 -w -1 -p)" "/tmp/${0##*/}/temp.png" 66 67case $color_type in 68 hex ) color=$(convert "/tmp/${0##*/}/temp.png" -format "%[hex:p]\n" info:) ;; 69 rgb ) 70 color=$(convert "/tmp/${0##*/}/temp.png" -format "%[pixel:p]\n" info:) 71 color=${color#*(}; color=${color%)*} 72 ;; 73 * ) printf '%s\n' "${0##*/}: invalid color_type: $color_type" 1>&2; exit 1 ;; 74esac 75printf '%s\n' "$color" 76 77# Copy color to clipboard 78[ "$copy_to_clipboard" ] && { 79 wl-copy -n "$color" || printf '%s\n' "${0##*/}: failed to copy color to clipboard" 1>&2 80} 81 82# Open color image in the user's perfered image viewer 83[ "$open_color_image" ] && { 84 # Create the color image if it doesn't already exist 85 [ -f "/tmp/${0##*/}/o$color.png" ] || { 86 case $color_type in 87 hex ) ocolor="#$color" ;; 88 rgb ) ocolor="rgb($color)" ;; 89 esac 90 91 convert -size 150x150 xc:"$ocolor" +size -gravity center \ 92 \( -background white pango:"<span font_family=\"monospace\" 93 font_weight=\"bold\"> $color </span>" \) \ 94 -composite "/tmp/${0##*/}/o$color.png" 95 } 96 97 xdg-open "/tmp/${0##*/}/o$color.png" > "/tmp/${0##*/}/xdg-open.log" 2>&1 & 98} 99 100# Send a notification with an image of the color aswell as the value 101[ "$send_notification" ] && { 102 [ -f "/tmp/${0##*/}/n$color.png" ] || { 103 case $color_type in 104 hex ) ncolor="#$color"; color_prefix="hex:";; 105 rgb ) 106 ncolor="rgb($color)" 107 color_r="${color%%,*}" 108 color_g="${color#*,}"; color_g="${color_g%,*}" 109 color_b="${color##*,}" 110 color_rgb="$color_r$color_g$color_b" 111 color_prefix="rgb:";; 112 esac 113 114 convert -size 64x64 xc:"$ncolor" "/tmp/${0##*/}/n$color_rgb.png" 115 } 116 117 notify-send -a "${0##*/}" -i "/tmp/${0##*/}/n$color_rgb.png" "$color_prefix $color" 118}