shared/lib: update rgba colors

This commit is contained in:
Anthony Rodriguez 2024-12-19 15:49:04 +01:00
parent e2bcffa79e
commit 1f1183fa4d
Signed by: nezia
GPG key ID: EE3BE97C040A86CE
2 changed files with 43 additions and 27 deletions

View file

@ -1,5 +1,5 @@
{inputs, ...}: let {inputs, ...}: let
lib' = import ../shared/lib; lib' = import ../shared/lib inputs.nixpkgs.lib;
mkSystem = args: mkSystem = args:
inputs.nixpkgs.lib.nixosSystem { inputs.nixpkgs.lib.nixosSystem {
specialArgs = {inherit inputs lib';}; specialArgs = {inherit inputs lib';};

View file

@ -1,13 +1,27 @@
let lib:
# convert rrggbb hex to rgba(r, g, b, a) with lib; let
rgba = lib: color: opacity: let # thanks fufexan https://github.com/fufexan/dotfiles/blob/2947f27791e97ea33c48af4ee2d0188fe03f80dd/lib/colors/default.nix#L8-L66
r = toString (hexToDec lib (builtins.substring 0 2 color)); # convert rrggbb hex to rgba(r, g, b, a) css
g = toString (hexToDec lib (builtins.substring 2 2 color)); rgba = c: alpha: let
b = toString (hexToDec lib (builtins.substring 4 2 color)); color = removePrefix "#" c;
in "rgba(${r}, ${g}, ${b}, ${opacity})"; r = toString (hexToDec (__substring 0 2 color));
g = toString (hexToDec (__substring 2 2 color));
b = toString (hexToDec (__substring 4 2 color));
a = toString alpha;
res = "rgba(${r}, ${g}, ${b}, ${a})";
in
res;
hexToDec = lib: v: let blurImage = pkgs: path:
# Map of hex characters to their decimal values pkgs.runCommand "${builtins.baseNameOf path}-blurred" {
buildInputs = [pkgs.imagemagick];
}
''
magick ${path} -gaussian-blur 0x12 "$out"
'';
# functions copied from https://gist.github.com/corpix/f761c82c9d6fdbc1b3846b37e1020e11
# convert a hex value to an integer
hexToDec = v: let
hexToInt = { hexToInt = {
"0" = 0; "0" = 0;
"1" = 1; "1" = 1;
@ -32,25 +46,27 @@ let
"E" = 14; "E" = 14;
"F" = 15; "F" = 15;
}; };
# Remove any leading `#` from the input chars = stringToCharacters v;
cleanHex = charsLen = length chars;
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 in
# Fold over the characters to calculate the decimal value foldl
builtins.foldl' (acc: char: acc * 16 + hexToInt."${char}") 0 chars; (a: v: a + v)
0
blurImage = pkgs: path: (imap0
pkgs.runCommand "${builtins.baseNameOf path}-blurred" { (k: v: hexToInt."${v}" * (pow 16 (charsLen - k - 1)))
buildInputs = [pkgs.imagemagick]; chars);
}
''
magick ${path} -gaussian-blur 0x12 "$out"
'';
pow = let
pow' = base: exponent: value:
# FIXME: It will silently overflow on values > 2**62 :(
# The value will become negative or zero in this case
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;
generateGtkColors = lib: palette: (lib.concatLines generateGtkColors = lib: palette: (lib.concatLines
(lib.mapAttrsToList (lib.mapAttrsToList
(name: color: "@define-color ${name} ${color};") (name: color: "@define-color ${name} ${color};")