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;}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
{
|
||||
check,
|
||||
math,
|
||||
...
|
||||
}: let
|
||||
inherit (math) round;
|
||||
hex = {
|
||||
|
|
@ -12,19 +11,19 @@
|
|||
}: let
|
||||
attrs = {inherit r g b;};
|
||||
in
|
||||
assert check.hex.set attrs || throw "Invalid Hex values: r=${toString r}, g=${toString g}, b=${toString b}"; attrs;
|
||||
check.hex.set attrs;
|
||||
|
||||
string = r: g: b: let
|
||||
str = "${r}${g}${b}";
|
||||
in
|
||||
assert check.hex.string str || throw "Invalid Hex value: ${str}"; str;
|
||||
check.hex.string str;
|
||||
};
|
||||
|
||||
rgb = {
|
||||
string = r: g: b: let
|
||||
str = "${toString r},${toString g},${toString b}";
|
||||
in
|
||||
assert check.rgb.string str || throw "Invalid RBG string format: ${str}"; str;
|
||||
check.rgb.string str;
|
||||
set = {
|
||||
r,
|
||||
g,
|
||||
|
|
@ -32,13 +31,13 @@
|
|||
}: let
|
||||
attrs = {inherit r g b;};
|
||||
in
|
||||
assert check.rgb.set attrs || throw "Invalid RGB values: r=${toString r}, g=${toString g}, b=${toString b}"; attrs;
|
||||
check.rgb.set attrs;
|
||||
};
|
||||
hsl = {
|
||||
string = h: s: l: let
|
||||
str = "${toString (round h)}, ${toString (round (s * 100))}%, ${toString (round (l * 100))}%";
|
||||
in
|
||||
assert check.hsl.string str || throw "Invalid HSL values: ${str}"; str;
|
||||
check.hsl.string str;
|
||||
set = {
|
||||
h,
|
||||
s,
|
||||
|
|
@ -46,6 +45,6 @@
|
|||
}: let
|
||||
attrs = {inherit h s l;};
|
||||
in
|
||||
assert check.hsl.set attrs || throw "Invalid HSL values: h=${toString h}, s=${toString s}, l=${toString l}"; attrs;
|
||||
check.hsl.set attrs;
|
||||
};
|
||||
in {inherit hex hsl rgb;}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
math,
|
||||
types,
|
||||
translate,
|
||||
check,
|
||||
}: let
|
||||
# Base modification functions
|
||||
modifyHSL = hexStr: modifications: let
|
||||
|
|
@ -94,6 +95,8 @@
|
|||
then mkDarkColorScale
|
||||
else mkLightColorScale;
|
||||
|
||||
validNeutrals = check.neutrals args;
|
||||
|
||||
# Generate color scales
|
||||
colors = {
|
||||
red = mkColorScale args.red;
|
||||
|
|
@ -201,7 +204,7 @@
|
|||
# Common structure for both themes
|
||||
neutrals = {
|
||||
inherit
|
||||
(args.neutrals)
|
||||
(validNeutrals)
|
||||
"50"
|
||||
"100"
|
||||
"150"
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@
|
|||
};
|
||||
utils = import ./color/utils.nix {
|
||||
inherit (ook-lib) math;
|
||||
inherit types translate;
|
||||
inherit check types translate;
|
||||
};
|
||||
in {
|
||||
inherit check types translate utils;
|
||||
|
|
|
|||
16
outputs/lib/icon/utils.nix
Normal file
16
outputs/lib/icon/utils.nix
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}: let
|
||||
inherit (lib) concatStrings;
|
||||
|
||||
mkIcon = {
|
||||
svg,
|
||||
colors, {},
|
||||
height ? 24,
|
||||
width ? 24
|
||||
}: let
|
||||
|
||||
in
|
||||
in
|
||||
Loading…
Add table
Add a link
Reference in a new issue