#!/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 "$@"