]> Freerunner's - dabruin.kak/commitdiff
fix: gawk does not support sprintf to transform hex 2 dec
authorThomas Teixeira <thomas.teixeira@startmail.com>
Mon, 2 Sep 2024 15:56:54 +0000 (17:56 +0200)
committerThomas Teixeira <thomas.teixeira@startmail.com>
Mon, 2 Sep 2024 15:56:54 +0000 (17:56 +0200)
dabruin.sh

index cfcab7d24b023d0f4789ee30479812ec4c2e51db..1003faf9e0b50f4e19dd75a36da5362acd176ada 100644 (file)
 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)
                }'