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
|
{lib, ...}: let
|
||||||
inherit (lib) toInt all min max;
|
inherit (lib) toInt all min max;
|
||||||
inherit (builtins) isInt isFloat match getAttr hasAttr;
|
inherit (builtins) mapAttrs elemAt isInt isFloat match getAttr hasAttr;
|
||||||
|
|
||||||
# basic checks
|
# basic checks
|
||||||
range = a: b: v: (v <= max a b) && (v >= min a b);
|
range = a: b: v: (v <= max a b) && (v >= min a b);
|
||||||
|
|
@ -15,8 +15,10 @@
|
||||||
hexPatternNoHash = match "[[:xdigit:]]{6}" color;
|
hexPatternNoHash = match "[[:xdigit:]]{6}" color;
|
||||||
isValid = hexPatternWithHash != null || hexPatternNoHash != null;
|
isValid = hexPatternWithHash != null || hexPatternNoHash != null;
|
||||||
in
|
in
|
||||||
assert isValid || throw "Invalid hex color format: ${color}";
|
if !isValid
|
||||||
hexPatternWithHash != null || hexPatternNoHash != null;
|
then abort "Invalid hex color format: ${color}"
|
||||||
|
else color;
|
||||||
|
|
||||||
set = color: let
|
set = color: let
|
||||||
hasAttributes = all (k: hasAttr k color) ["r" "g" "b"];
|
hasAttributes = all (k: hasAttr k color) ["r" "g" "b"];
|
||||||
validPattern = all (k: let
|
validPattern = all (k: let
|
||||||
|
|
@ -24,7 +26,9 @@
|
||||||
in
|
in
|
||||||
match "[[:xdigit:]]{2}" v != null) ["r" "g" "b"];
|
match "[[:xdigit:]]{2}" v != null) ["r" "g" "b"];
|
||||||
in
|
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 = {
|
rgb = {
|
||||||
|
|
@ -36,7 +40,10 @@
|
||||||
num != null && range 0 255 num;
|
num != null && range 0 255 num;
|
||||||
isValid = rgbPattern != null && all toNum rgbPattern;
|
isValid = rgbPattern != null && all toNum rgbPattern;
|
||||||
in
|
in
|
||||||
isValid;
|
if !isValid
|
||||||
|
then abort "Invalid RGB string: ${color}"
|
||||||
|
else color;
|
||||||
|
|
||||||
set = color: let
|
set = color: let
|
||||||
hasAttributes = all (k: hasAttr k color) ["r" "g" "b"];
|
hasAttributes = all (k: hasAttr k color) ["r" "g" "b"];
|
||||||
validRanges = all (
|
validRanges = all (
|
||||||
|
|
@ -46,20 +53,61 @@
|
||||||
number v && range 0 255 v
|
number v && range 0 255 v
|
||||||
) ["r" "g" "b"];
|
) ["r" "g" "b"];
|
||||||
in
|
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 = {
|
hsl = {
|
||||||
# TODO: add range checks
|
|
||||||
string = color: let
|
string = color: let
|
||||||
hslPattern = match "([0-9]{1,3}),[ ]*([0-9]{1,3})%,[ ]*([0-9]{1,3})%" color;
|
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
|
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
|
set = color: let
|
||||||
hasAttributes = all (k: hasAttr k color) ["h" "s" "l"];
|
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
|
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,
|
check,
|
||||||
math,
|
math,
|
||||||
...
|
|
||||||
}: let
|
}: let
|
||||||
inherit (math) round;
|
inherit (math) round;
|
||||||
hex = {
|
hex = {
|
||||||
|
|
@ -12,19 +11,19 @@
|
||||||
}: let
|
}: let
|
||||||
attrs = {inherit r g b;};
|
attrs = {inherit r g b;};
|
||||||
in
|
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
|
string = r: g: b: let
|
||||||
str = "${r}${g}${b}";
|
str = "${r}${g}${b}";
|
||||||
in
|
in
|
||||||
assert check.hex.string str || throw "Invalid Hex value: ${str}"; str;
|
check.hex.string str;
|
||||||
};
|
};
|
||||||
|
|
||||||
rgb = {
|
rgb = {
|
||||||
string = r: g: b: let
|
string = r: g: b: let
|
||||||
str = "${toString r},${toString g},${toString b}";
|
str = "${toString r},${toString g},${toString b}";
|
||||||
in
|
in
|
||||||
assert check.rgb.string str || throw "Invalid RBG string format: ${str}"; str;
|
check.rgb.string str;
|
||||||
set = {
|
set = {
|
||||||
r,
|
r,
|
||||||
g,
|
g,
|
||||||
|
|
@ -32,13 +31,13 @@
|
||||||
}: let
|
}: let
|
||||||
attrs = {inherit r g b;};
|
attrs = {inherit r g b;};
|
||||||
in
|
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 = {
|
hsl = {
|
||||||
string = h: s: l: let
|
string = h: s: l: let
|
||||||
str = "${toString (round h)}, ${toString (round (s * 100))}%, ${toString (round (l * 100))}%";
|
str = "${toString (round h)}, ${toString (round (s * 100))}%, ${toString (round (l * 100))}%";
|
||||||
in
|
in
|
||||||
assert check.hsl.string str || throw "Invalid HSL values: ${str}"; str;
|
check.hsl.string str;
|
||||||
set = {
|
set = {
|
||||||
h,
|
h,
|
||||||
s,
|
s,
|
||||||
|
|
@ -46,6 +45,6 @@
|
||||||
}: let
|
}: let
|
||||||
attrs = {inherit h s l;};
|
attrs = {inherit h s l;};
|
||||||
in
|
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;}
|
in {inherit hex hsl rgb;}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
math,
|
math,
|
||||||
types,
|
types,
|
||||||
translate,
|
translate,
|
||||||
|
check,
|
||||||
}: let
|
}: let
|
||||||
# Base modification functions
|
# Base modification functions
|
||||||
modifyHSL = hexStr: modifications: let
|
modifyHSL = hexStr: modifications: let
|
||||||
|
|
@ -94,6 +95,8 @@
|
||||||
then mkDarkColorScale
|
then mkDarkColorScale
|
||||||
else mkLightColorScale;
|
else mkLightColorScale;
|
||||||
|
|
||||||
|
validNeutrals = check.neutrals args;
|
||||||
|
|
||||||
# Generate color scales
|
# Generate color scales
|
||||||
colors = {
|
colors = {
|
||||||
red = mkColorScale args.red;
|
red = mkColorScale args.red;
|
||||||
|
|
@ -201,7 +204,7 @@
|
||||||
# Common structure for both themes
|
# Common structure for both themes
|
||||||
neutrals = {
|
neutrals = {
|
||||||
inherit
|
inherit
|
||||||
(args.neutrals)
|
(validNeutrals)
|
||||||
"50"
|
"50"
|
||||||
"100"
|
"100"
|
||||||
"150"
|
"150"
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@
|
||||||
};
|
};
|
||||||
utils = import ./color/utils.nix {
|
utils = import ./color/utils.nix {
|
||||||
inherit (ook-lib) math;
|
inherit (ook-lib) math;
|
||||||
inherit types translate;
|
inherit check types translate;
|
||||||
};
|
};
|
||||||
in {
|
in {
|
||||||
inherit check types translate utils;
|
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