Add hex2rgb script
This commit is contained in:
parent
9f4f2ed7df
commit
7cc4a34434
1 changed files with 27 additions and 0 deletions
27
home/dot_local/bin/executable_hex2rgb
Normal file
27
home/dot_local/bin/executable_hex2rgb
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#!/bin/sh
|
||||
##? ~/bin/hex2rgb - convert color hex to rgb
|
||||
# '#C0FFEE' => 192 255 238
|
||||
# 'DEADBEEF' => 222 173 190 239
|
||||
# 'fab' => 255 170 187
|
||||
# Shamelessly stolen from https://stackoverflow.com/questions/7253235/convert-hexadecimal-color-to-decimal-rgb-values-in-unix-shell-script/75643474#75643474
|
||||
|
||||
|
||||
__hex2rgb() {
|
||||
# reset $1 to scrubbed hex: '#01efa9' becomes '01EFA9'
|
||||
set -- "$(echo "$1" | tr -d '#' | tr '[:lower:]' '[:upper:]')"
|
||||
START=0
|
||||
STR=
|
||||
while (( START < ${#1} )); do
|
||||
# double each char under len 6 : FAB => FFAABB
|
||||
if (( ${#1} < 6 )); then
|
||||
STR="$(printf "${1:${START}:1}%.0s" 1 2)"
|
||||
(( START += 1 ))
|
||||
else
|
||||
STR="${1:${START}:2}"
|
||||
(( START += 2 ))
|
||||
fi
|
||||
echo "ibase=16; ${STR}" | bc
|
||||
done
|
||||
unset START STR
|
||||
}
|
||||
__hex2rgb "$@"
|
||||
Loading…
Add table
Reference in a new issue