From 7c0f64bd12b84f02e780aa3d0c439bb71580dfa4 Mon Sep 17 00:00:00 2001 From: Thomas Teixeira Date: Mon, 2 Sep 2024 17:56:54 +0200 Subject: [PATCH] fix: gawk does not support sprintf to transform hex 2 dec --- dabruin.sh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/dabruin.sh b/dabruin.sh index cfcab7d..1003faf 100644 --- a/dabruin.sh +++ b/dabruin.sh @@ -10,6 +10,21 @@ ratio_calculate() { printf "${1##rgb:}" | fold -w2 | awk -v ratio="$2" ' + function char2num(char) { + if (char == "f") { return 15; } + if (char == "e") { return 14; } + if (char == "d") { return 13; } + if (char == "c") { return 12; } + if (char == "b") { return 11; } + if (char == "a") { return 10; } + return char + } + function hex2dec(hex) { + left = tolower(substr(hex, 1, 1)) + right = tolower(substr(hex, 2, 1)) + return char2num(left) * 16 + char2num(right) + } + BEGIN { printf "rgb:" if (ratio ~ /[0-9]+\/[0-9]+/) { @@ -18,7 +33,7 @@ ratio_calculate() { } } { - decimal = sprintf("%d", "0x" $0) + decimal = hex2dec($0) ratioed = int(decimal * ratio) printf("%02x", ratioed > 255 ? 255 : ratioed) }' -- 2.51.2