lib: add color function collection

This commit is contained in:
ooks-io 2024-11-06 18:49:36 +11:00
parent 3773a25ef1
commit 9d5fcc0668
6 changed files with 512 additions and 1 deletions

46
outputs/lib/math.nix Normal file
View file

@ -0,0 +1,46 @@
{lib}: let
inherit (lib) min max;
inherit (builtins) floor ceil;
# basic math functions
# credits to github:xddxdd/nix-math
abs = x:
if x < 0
then 0 - x
else x;
clamp = a: b: v: min (max v (min a b)) (max a b);
round = x:
if (x - floor x) < 0.5
then floor x
else ceil x;
hasFraction = x: let
splitted = lib.splitString "." (builtins.toString x);
in
builtins.length splitted >= 2 && builtins.length (builtins.filter (ch: ch != "0") (lib.stringToCharacters (builtins.elemAt splitted 1))) > 0;
div = a: b: let
divideExactly = !(hasFraction (1.0 * a / b));
offset =
if divideExactly
then 0
else (0 - 1);
in
if b < 0
then offset - div a (0 - b)
else if a < 0
then offset - div (0 - a) b
else floor (1.0 * a / b);
mod = a: b:
if b < 0
then 0 - mod (0 - a) (0 - b)
else if a < 0
then mod (b - mod (0 - a) b) b
else a - b * (div a b);
in {
inherit round mod abs hasFraction clamp;
}