color rework, flake bump

This commit is contained in:
Lennart J. Kurzweg (Nx2)
2024-05-12 21:55:03 +02:00
parent 2db49fe4c6
commit d97010da0c
14 changed files with 424 additions and 278 deletions

View File

@@ -1,14 +1,15 @@
{ lib, ... }:
rec
{
nohash = s: builtins.substring 1 7 s;
let
# takes in "ff0044" (no hash!) and returns { r = "ff", g = "00", b = "44" }
slice-hex = hex: with builtins; { r = substring 0 2 hex; g = substring 2 2 hex; b = substring 4 2 hex; };
slice-hex = hex: { r = builtins.substring 0 2 hex; g = builtins.substring 2 2 hex; b = builtins.substring 4 5 hex; };
# https://github.com/bertof/nix-rice
rune-to-num = c:
# takes in "44" and returns 64
drune-to-255 = drune: with builtins; (rune-to-num (substring 0 1 drune)) * 16 + (rune-to-num (substring 1 1 drune));
num-to-drune = num: "${num-to-rune (num / 16)}${num-to-rune (num - ((num / 16) * 16))}";
# takes in "D" and returns 13
rune-to-num = rune: # inspiration from https://github.com/bertof/nix-rice
let
k = lib.strings.toUpper c;
dict = {
"0" = 0;
"1" = 1;
@@ -28,16 +29,13 @@ rec
"F" = 15;
};
in
assert(builtins.hasAttr k dict);
builtins.getAttr k dict;
assert(builtins.hasAttr (lib.strings.toUpper rune) dict);
builtins.getAttr (lib.strings.toUpper rune) dict;
drune-to-255 = hex: (rune-to-num (builtins.substring 0 1 hex)) * 15 + (rune-to-num (builtins.substring 1 2 hex));
hex-to-rgb-comma-string = hex: let color = (slice-hex (nohash hex)); in "${builtins.toString (drune-to-255 color.r)},${builtins.toString (drune-to-255 color.g)},${builtins.toString (drune-to-255 color.b)}";
num-to-rune = n:
# takes in 15 and returns "F"
num-to-rune = num:
let
ns = builtins.toString n;
num-string = builtins.toString num;
dict = {
"0" = "0";
"1" = "1";
@@ -53,12 +51,46 @@ rec
"11" = "B";
"12" = "C";
"13" = "D";
"14" = "E";
"14" = "E";
"15" = "F";
};
in
assert(builtins.hasAttr ns dict);
builtins.getAttr ns dict;
assert(builtins.hasAttr num-string dict);
builtins.getAttr num-string dict;
float-to-drune = f: "${num-to-rune (builtins.floor((255*f) / 16))}${num-to-rune (builtins.floor(255*f) - (builtins.floor((255 * f) / 16) * 16))}";
# Keeps num between 0 and 255
# Make sure to pass in an int not a float
cap-255 = num: (if (num>255) then 255 else if (num<0) then 0 else num);
nohash = hex: with builtins; assert((stringLength hex) == 7); substring 1 6 hex;
in
{
## USEFUL FUNCTIONS
# --------------------------------------------------------------------------------
# takes in a string like "#ff0044" and returns "ff0044" symbol
inherit nohash;
# --------------------------------------------------------------------------------
# This takes in something like "#ff0044" and returns "255,0,64"
hex-to-rgb-comma-string = hex:
with (slice-hex (nohash hex));
with builtins;
assert(isString hex);
"${toString (drune-to-255 r)},${toString (drune-to-255 g)},${toString (drune-to-255 b)}";
# --------------------------------------------------------------------------------
# This is useful if you have a float (like a transparency value) and want a drune representation of it
# So 0.0 -> "00" and 1.0 -> "FF"
float-to-drune = f: with builtins; assert(isFloat f); "${num-to-rune (floor((255*f) / 16))}${num-to-rune (floor(255*f) - (floor((255*f) / 16) * 16))}";
# --------------------------------------------------------------------------------
# Takes in hex and a float. 0.5 is +50% brightness and (-0.5) is -50% brightness.
# So "#ff0044": 0.3 -> "#ff0055"
alter-luminace-hex = hex: amount:
let
color-num = with (slice-hex (nohash hex)); { r = drune-to-255 r; g = drune-to-255 g; b = drune-to-255 b; };
alter = num: (num-to-drune (cap-255 (builtins.floor (num * (1 + amount)))));
in
with color-num; "#${alter r}${alter g}${alter b}";
}