lib: simplify rgba function

This commit is contained in:
Anthony Rodriguez 2024-10-06 12:14:35 +02:00
parent 7e9a7d8e1e
commit f8a7ac40e6
Signed by: nezia
GPG key ID: EE3BE97C040A86CE

View file

@ -3,6 +3,13 @@
lib, lib,
... ...
}: let }: let
# convert rrggbb hex to rgba(r, g, b, a) CSS format
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)";
# Helper function to convert hex color to decimal RGB values # Helper function to convert hex color to decimal RGB values
hexToDec = v: let hexToDec = v: let
hexToInt = { hexToInt = {
@ -30,21 +37,12 @@
"F" = 15; "F" = 15;
}; };
chars = lib.strings.stringToCharacters v; chars = lib.strings.stringToCharacters v;
charsLen = builtins.length chars;
in in
lib.lists.foldl (a: v: a + v) 0 lib.foldl' (a: v: a + v) 0
(lib.lists.imap0 (k: v: hexToInt."${v}" * (pow 16 (charsLen - k - 1))) chars); (lib.imap (k: v: hexToInt."${v}" * (pow 16 (builtins.length chars - k - 1))) chars);
# Power function for exponentiation # Power function for exponentiation
pow = let pow = base: exponent: lib.foldl' (acc: _: acc * base) 1 (lib.range 1 exponent);
pow' = base: exponent: value:
if exponent == 0
then 1
else if exponent <= 1
then value
else pow' base (exponent - 1) (value * base);
in
base: exponent: pow' base exponent base;
# Converts hex color to KDE color format # Converts hex color to KDE color format
colorToKde = name: hexColor: let colorToKde = name: hexColor: let
@ -74,25 +72,22 @@
base0E = ["Color5" "Color5Intense"]; base0E = ["Color5" "Color5Intense"];
}; };
# Generate KDE color sections using the mapped colors
colorSections = lib.concatStringsSep "\n" (lib.attrsets.mapAttrsToList colorSections = lib.concatStringsSep "\n" (lib.attrsets.mapAttrsToList
( (name: value:
name: value: lib.concatMapStrings (slot: colorToKde slot value) (colorMap.${name} or []))
lib.concatMapStrings (slot: colorToKde slot value) (colorMap.${name} or [])
)
palette); palette);
in in
lib.strings.concatStringsSep "\n" ( lib.concatStringsSep "\n" [
[
"[General]" "[General]"
"\nDescription=${scheme.name}" "Description=${scheme.name}"
"\nOpacity=1" "Opacity=1"
"\nWallpaper=" "Wallpaper="
] colorSections
++ [colorSections] ];
);
# Main function to create the KDE colorscheme file # Create a KDE konsole color scheme from base16 colors
mkKonsoleColorScheme = scheme: mkKonsoleColorScheme = scheme:
pkgs.writeText "${scheme.name}.colorscheme" (schemeToKonsole scheme); pkgs.writeText "${scheme.name}.colorscheme" (schemeToKonsole scheme);
in {inherit mkKonsoleColorScheme;} in {
inherit mkKonsoleColorScheme rgba;
}