From 7cc4a3443455945bf04ce5b8b5d5bc108de37939 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Wed, 8 Oct 2025 16:34:53 +0100 Subject: [PATCH] Add hex2rgb script --- home/dot_local/bin/executable_hex2rgb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 home/dot_local/bin/executable_hex2rgb diff --git a/home/dot_local/bin/executable_hex2rgb b/home/dot_local/bin/executable_hex2rgb new file mode 100644 index 0000000..f0e3cfa --- /dev/null +++ b/home/dot_local/bin/executable_hex2rgb @@ -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 "$@"