A collection of scripts
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 in "$@" 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 while [ "$flag" ] 38 do 39 a=${flag%${flag#?}} 40 case $a in 41 c ) copy_to_clipboard=true ;; 42 h ) usage; exit 0 ;; 43 n ) send_notification=true ;; 44 o ) open_color_image=true ;; 45 r ) color_type="rgb" ;; 46 x ) color_type="hex" ;; 47 * ) printf '%s\n' "${0##*/}: -$a invalid argument" 1>&2 48 usage 1>&2; exit 1 ;; 49 esac 50 flag=${flag#?} 51 done 52done 53 54# Make sure this directory exist before continuing 55[ -d "/tmp/${0##*/}" ] || { 56 mkdir -p "/tmp/${0##*/}" || \ 57 printf '%s\n' "${0##*/}: failed to create directory: /tmp/${0##*/}" 1>&2 \ 58 exit 1 59} 60 61# Set color_type if not already set 62[ $color_type ] || color_type="hex" 63 64# Get a screenshot of the pixel 65grim -s 1 -g "$(slurp -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 ) color=$(convert "/tmp/${0##*/}/temp.png" -format "%[pixel:p]\n" info:) 70 color=${color#*(}; color=${color%)*} ;; 71 * ) printf '%s\n' "${0##*/}: invalid color_type: $color_type" 1>&2; exit 1 ;; 72esac 73echo "$color" 74 75# Copy color to clipboard 76[ $copy_to_clipboard ] && { 77 wl-copy -n "$color" || printf '%s\n' "${0##*/}: failed to copy color to clipboard" 1>&2 78} 79 80# Open color image in the user's perfered image viewer 81[ $open_color_image ] && { 82 # Create the color image if it doesn't already exist 83 [ -f "/tmp/${0##*/}/o$color.png" ] || { 84 case $color_type in 85 hex ) ocolor="#$color" ;; 86 rgb ) ocolor="rgb($color)";; 87 esac 88 convert -size 150x150 xc:"$ocolor" +size -gravity center \ 89 \( -background white pango:"<span font_family=\"monospace\" 90 font_weight=\"bold\"> $color </span>" \) \ 91 -composite "/tmp/${0##*/}/o$color.png" 92 } 93 xdg-open "/tmp/${0##*/}/o$color.png" > "/tmp/${0##*/}/xdg-open.log" 2>&1 & 94} 95 96# Send a notification with an image of the color aswell as the value 97[ $send_notification ] && { 98 [ -f "/tmp/${0##*/}/n$color.png" ] || { 99 case $color_type in 100 hex ) ncolor="#$color"; color_prefix="hex:";; 101 rgb ) ncolor="rgb($color)" 102 color_r="${color%%,*}" 103 color_g="${color#*,}"; color_g="${color_g%,*}" 104 color_b="${color##*,}" 105 color_rgb="$color_r$color_g$color_b" 106 color_prefix="rgb:";; 107 esac 108 convert -size 64x64 xc:"$ncolor" "/tmp/${0##*/}/n$color_rgb.png" 109 } 110 notify-send -a "${0##*/}" -i "/tmp/${0##*/}/n$color_rgb.png" "$color_prefix $color" 111}