Skip to content

Instantly share code, notes, and snippets.

@kristm
Created August 3, 2021 06:23
Show Gist options
  • Save kristm/2dda58ff3ba15e5d4fce1ee046aba97f to your computer and use it in GitHub Desktop.
Save kristm/2dda58ff3ba15e5d4fce1ee046aba97f to your computer and use it in GitHub Desktop.
show hex color on terminal (or at least how the terminal wants to show it ^_^)
#!/bin/bash
usage="hexcat\nUsage $0 <hexcolor>"
#https://gist.github.com/mhulse/b11e568260fb8c3aa2a8
function fromhex() {
hex=$1
if [[ $hex == "#"* ]]; then
hex=$(echo $1 | awk '{print substr($0,2)}')
fi
r=$(printf '0x%0.2s' "$hex")
g=$(printf '0x%0.2s' ${hex#??})
b=$(printf '0x%0.2s' ${hex#????})
echo -e `printf "%03d" "$(((r<75?0:(r-35)/40)*6*6+(g<75?0:(g-35)/40)*6+(b<75?0:(b-35)/40)+16))"`
}
if [ $# -lt 1 ]; then
echo -e $usage
exit
fi
tcolor=$(fromhex $1)
echo -e $(tput setab $tcolor) . . $(tput sgr 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment