98 lines
2.6 KiB
Nix
98 lines
2.6 KiB
Nix
{ lib ? (import <nixpkgs> {}).lib, ... }:
|
|
with lib;
|
|
with builtins;
|
|
{
|
|
lib = {
|
|
floats = rec {
|
|
_floatComponents = f: lib.strings.splitString "." (toString f);
|
|
|
|
ceil = f:
|
|
let
|
|
comp = _floatComponents f;
|
|
int = strings.toInt (head comp);
|
|
inc = if match "[1-9][[:digit:]]*" (head (tail comp)) != null then 1 else 0;
|
|
in
|
|
assert(isFloat f);
|
|
int + inc;
|
|
|
|
floor = f:
|
|
let
|
|
int = strings.toInt (head (_floatComponents f));
|
|
in
|
|
assert(isFloat f);
|
|
int;
|
|
|
|
toInt = f:
|
|
let
|
|
comp = _floatComponents f;
|
|
int = strings.toInt (head comp);
|
|
inc = if match "[5-9][[:digit:]]*" (head (tail comp)) != null then 1 else 0;
|
|
in
|
|
assert(isFloat f);
|
|
int + inc;
|
|
};
|
|
|
|
hex = rec {
|
|
parseDigit = c:
|
|
let
|
|
v = strings.toUpper c;
|
|
in
|
|
assert(match "[0-9A-F]" c != null);
|
|
{
|
|
"0" = 0;
|
|
"1" = 1;
|
|
"2" = 2;
|
|
"3" = 3;
|
|
"4" = 4;
|
|
"5" = 5;
|
|
"6" = 6;
|
|
"7" = 7;
|
|
"8" = 8;
|
|
"9" = 9;
|
|
"A" = 10;
|
|
"B" = 11;
|
|
"C" = 12;
|
|
"D" = 13;
|
|
"E" = 14;
|
|
"F" = 15;
|
|
}."${v}";
|
|
|
|
toDec = s:
|
|
let characters = stringToCharacters s;
|
|
values = map parseDigit characters;
|
|
value = foldl (acc: n: acc * 16 + n) 0 values;
|
|
in value;
|
|
|
|
fromDec = trivial.toHexString;
|
|
|
|
};
|
|
|
|
# colors = rec {
|
|
|
|
|
|
|
|
# colorFromHex = s:
|
|
# let rgba = match "#([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})" s;
|
|
# rgb = match "#([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})" s;
|
|
# values = map hexToDec (if isNull rgb then rgba else rgb ++ [ "FF" ]);
|
|
# in {
|
|
# r = (head values) / 255.0;
|
|
# g = (head (tail values)) / 255.0;
|
|
# b = (head (drop 2 values)) / 255.0;
|
|
# a = (last values) / 255.0;
|
|
# };
|
|
|
|
# red = { r = 255.0; g = 0.0; b = 0.0; };
|
|
# green = { r = 0.0; g = 255.0; b = 0.0; };
|
|
# blue = { r = 0.0; g = 0.0; b = 255.0; };
|
|
|
|
# toRGBHex = { r, g, b, ... }:
|
|
# ''#${concatMapStrings (v: fixedWidthString 2 "0" (toHexString (v * 255))) [ r g b ]}'';
|
|
# toRGBAHex = { r, g, b, a }:
|
|
# ''#${concatMapStrings (v: fixedWidthString 2 "0" (toHexString v)) [ r g b a ]}'';
|
|
|
|
# toRgbHex = c: strings.toLower (toRGBHex c);
|
|
# toRgbaHex = c: strings.toLower (toRGBAHex c);
|
|
# };
|
|
};
|
|
}
|