lib: improve types/checks
This commit is contained in:
parent
10c1ed7ecb
commit
a30e2a0bc1
5 changed files with 87 additions and 21 deletions
|
|
@ -1,6 +1,6 @@
|
|||
{lib, ...}: let
|
||||
inherit (lib) toInt all min max;
|
||||
inherit (builtins) isInt isFloat match getAttr hasAttr;
|
||||
inherit (builtins) mapAttrs elemAt isInt isFloat match getAttr hasAttr;
|
||||
|
||||
# basic checks
|
||||
range = a: b: v: (v <= max a b) && (v >= min a b);
|
||||
|
|
@ -15,8 +15,10 @@
|
|||
hexPatternNoHash = match "[[:xdigit:]]{6}" color;
|
||||
isValid = hexPatternWithHash != null || hexPatternNoHash != null;
|
||||
in
|
||||
assert isValid || throw "Invalid hex color format: ${color}";
|
||||
hexPatternWithHash != null || hexPatternNoHash != null;
|
||||
if !isValid
|
||||
then abort "Invalid hex color format: ${color}"
|
||||
else color;
|
||||
|
||||
set = color: let
|
||||
hasAttributes = all (k: hasAttr k color) ["r" "g" "b"];
|
||||
validPattern = all (k: let
|
||||
|
|
@ -24,7 +26,9 @@
|
|||
in
|
||||
match "[[:xdigit:]]{2}" v != null) ["r" "g" "b"];
|
||||
in
|
||||
hasAttributes && validPattern;
|
||||
if !(hasAttributes && validPattern)
|
||||
then abort "Invalid Hex values: r=${toString color.r}, g=${toString color.g}, b=${toString color.b}"
|
||||
else color;
|
||||
};
|
||||
|
||||
rgb = {
|
||||
|
|
@ -36,7 +40,10 @@
|
|||
num != null && range 0 255 num;
|
||||
isValid = rgbPattern != null && all toNum rgbPattern;
|
||||
in
|
||||
isValid;
|
||||
if !isValid
|
||||
then abort "Invalid RGB string: ${color}"
|
||||
else color;
|
||||
|
||||
set = color: let
|
||||
hasAttributes = all (k: hasAttr k color) ["r" "g" "b"];
|
||||
validRanges = all (
|
||||
|
|
@ -46,20 +53,61 @@
|
|||
number v && range 0 255 v
|
||||
) ["r" "g" "b"];
|
||||
in
|
||||
hasAttributes && validRanges;
|
||||
if !(hasAttributes && validRanges)
|
||||
then abort "Invalid RGB set: r=${toString color.r}, g=${toString color.g}, b=${toString color.b}"
|
||||
else color;
|
||||
};
|
||||
|
||||
hsl = {
|
||||
# TODO: add range checks
|
||||
string = color: let
|
||||
hslPattern = match "([0-9]{1,3}),[ ]*([0-9]{1,3})%,[ ]*([0-9]{1,3})%" color;
|
||||
# Convert matched values to numbers and check ranges
|
||||
validateHSL = groups: let
|
||||
h = toInt (elemAt groups 0);
|
||||
s = toInt (elemAt groups 1);
|
||||
l = toInt (elemAt groups 2);
|
||||
in
|
||||
h
|
||||
!= null
|
||||
&& h >= 0
|
||||
&& h <= 360
|
||||
&& s != null
|
||||
&& s >= 0
|
||||
&& s <= 100
|
||||
&& l != null
|
||||
&& l >= 0
|
||||
&& l <= 100;
|
||||
isValid = hslPattern != null && validateHSL hslPattern;
|
||||
in
|
||||
hslPattern != null;
|
||||
if !isValid
|
||||
then abort "Invalid HSL string: ${color} (expected format: h(0-360),s(0-100%),l(0-100%))"
|
||||
else color;
|
||||
|
||||
set = color: let
|
||||
hasAttributes = all (k: hasAttr k color) ["h" "s" "l"];
|
||||
validRanges = hue color.h && all (k: unary (getAttr k color)) ["s" "l"];
|
||||
validRanges =
|
||||
color.h
|
||||
>= 0
|
||||
&& color.h <= 360
|
||||
&& color.s >= 0
|
||||
&& color.s <= 1.0
|
||||
&& color.l >= 0
|
||||
&& color.l <= 1.0;
|
||||
in
|
||||
hasAttributes && validRanges;
|
||||
if !(hasAttributes && validRanges)
|
||||
then
|
||||
abort ''
|
||||
Invalid HSL set: h=${toString color.h}, s=${toString color.s}, l=${toString color.l}
|
||||
Expected:
|
||||
h: 0-360
|
||||
s: 0.0-1.0
|
||||
l: 0.0-1.0
|
||||
''
|
||||
else color;
|
||||
};
|
||||
in {inherit range number unary hue hex rgb hsl;}
|
||||
|
||||
# validate neutral hex values
|
||||
neutrals = {neutrals, ...}:
|
||||
mapAttrs (_: value:
|
||||
hex.string value)
|
||||
neutrals;
|
||||
in {inherit neutrals range number unary hue hex rgb hsl;}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue