From f0fd043ec31affa7d38f835c470b4c6f5cb85266 Mon Sep 17 00:00:00 2001 From: Anthony Rodriguez Date: Fri, 22 Nov 2024 19:36:29 +0100 Subject: [PATCH] lib: fix rgba (inject lib) --- lib/default.nix | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index 1d1f1fc..3f17696 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,13 +1,13 @@ let # convert rrggbb hex to rgba(r, g, b, a) - rgba = c: let - r = toString (hexToDec (builtins.substring 0 2 c)); - g = toString (hexToDec (builtins.substring 2 2 c)); - b = toString (hexToDec (builtins.substring 4 2 c)); - in "rgba(${r}, ${g}, ${b}, .5)"; + rgba = lib: color: opacity: let + r = toString (hexToDec lib (builtins.substring 0 2 color)); + g = toString (hexToDec lib (builtins.substring 2 2 color)); + b = toString (hexToDec lib (builtins.substring 4 2 color)); + in "rgba(${r}, ${g}, ${b}, ${opacity})"; - # Helper function to convert hex color to decimal RGB values hexToDec = lib: v: let + # Map of hex characters to their decimal values hexToInt = { "0" = 0; "1" = 1; @@ -32,13 +32,16 @@ let "E" = 14; "F" = 15; }; - chars = lib.strings.stringToCharacters v; + # Remove any leading `#` from the input + cleanHex = + if lib.strings.substring 0 1 v == "#" + then lib.strings.substring 1 (builtins.stringLength v - 1) v + else v; + # Convert the cleaned string into characters + chars = lib.strings.stringToCharacters cleanHex; in - lib.foldl' (a: v: a + v) 0 - (lib.imap (k: v: hexToInt."${v}" * (pow 16 (builtins.length chars - k - 1))) chars); - - # Power function for exponentiation - pow = lib: base: exponent: lib.foldl' (acc: _: acc * base) 1 (lib.range 1 exponent); + # Fold over the characters to calculate the decimal value + builtins.foldl' (acc: char: acc * 16 + hexToInt."${char}") 0 chars; blurImage = pkgs: path: pkgs.runCommand "${builtins.baseNameOf path}-blurred" {