Skip to content

Instantly share code, notes, and snippets.

@usrshare
Created June 7, 2017 09:11
Show Gist options
  • Save usrshare/5ef88f7416ab9a7e15a7ab678e85fbf4 to your computer and use it in GitHub Desktop.
Save usrshare/5ef88f7416ab9a7e15a7ab678e85fbf4 to your computer and use it in GitHub Desktop.
#!/bin/bash
function rxvt_set_bg { #arg: filename
FILE="$(realpath "$1")"
BGDATA="$(convert "$FILE" -crop '1x1+0+0' txt:-)" #get color of top-left pixel for background
RGB="$(echo "$BGDATA" | grep -P -o '(?<=\().*(?=\))')"
R="$(echo "$RGB" | awk -F ',' '{print $1}')" #get value of each
G="$(echo "$RGB" | awk -F ',' '{print $2}')" #color to determine
B="$(echo "$RGB" | awk -F ',' '{print $3}')" #if background is dark or bright
RGBSUM="$(( $R + $G + $B ))"
BGPIXEL="$(echo "$BGDATA" | grep -o '#.*')"
#echo "File is $FILE"
#echo "RGB is $RGB -- $RGBSUM"
#echo "BG pixel is $BGPIXEL"
echo -ne "\e]11;$BGPIXEL\a\e]20;$FILE;100x100+100+100:op=keep-aspect\a"
if (("$RGBSUM" < 384)); then
echo -ne "\e]10;#ffffff\a\e]705;#ffffff\a" #dark, use white text
else
echo -ne "\e]10;#000000\a\e]705;#000000\a" #bright, use black text
fi
}
function load_by_number { #arg: number of pokemon
PADNUM="$(printf "%03d" "$1")"
IMG="$(find . -name "$PADNUM".png)"
if [ -z "$IMG" ]; then
echo "Couldn't find an image for #$NUMBER."
exit 1
fi
rxvt_set_bg "$IMG"
}
function load_by_name { #arg: name of pokemon
NUMBER="$(grep -i $INPUT Data/pokemon.txt | awk '{print $1}')"
if [ "$NUMBER" == 0 ] ; then
echo "Couldn't find an image for $INPUT."
exit 1
fi
load_by_number "$NUMBER"
}
function load_random {
RNDNUM="$(shuf -n 1 Data/pokemon.txt | awk '{print $1}')"
load_by_number "$RNDNUM"
}
if [[ $# -lt 1 ]]; then
echo "This program requires an argument -- either a Pokémon's name, its number, or the word 'random'."
fi
INPUT=$1
if [[ $INPUT == "random" ]]; then
load_random
elif [[ $INPUT =~ ^[0-9]+$ ]] ; then
load_by_number $1
else
load_by_name $1
fi
./setbg.sh "$IMG"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment