From a30e2a0bc1eb8fc314057696069e34a92036b504 Mon Sep 17 00:00:00 2001 From: ooks-io Date: Mon, 18 Nov 2024 11:28:33 +1100 Subject: [PATCH] lib: improve types/checks --- outputs/lib/color/check.nix | 72 ++++++++++++++++++++++++++++++------- outputs/lib/color/types.nix | 13 ++++--- outputs/lib/color/utils.nix | 5 ++- outputs/lib/default.nix | 2 +- outputs/lib/icon/utils.nix | 16 +++++++++ 5 files changed, 87 insertions(+), 21 deletions(-) create mode 100644 outputs/lib/icon/utils.nix diff --git a/outputs/lib/color/check.nix b/outputs/lib/color/check.nix index cb78c4b..d76738e 100644 --- a/outputs/lib/color/check.nix +++ b/outputs/lib/color/check.nix @@ -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;} diff --git a/outputs/lib/color/types.nix b/outputs/lib/color/types.nix index 850e647..c3b9806 100644 --- a/outputs/lib/color/types.nix +++ b/outputs/lib/color/types.nix @@ -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;} diff --git a/outputs/lib/color/utils.nix b/outputs/lib/color/utils.nix index a6abb35..dc0e160 100644 --- a/outputs/lib/color/utils.nix +++ b/outputs/lib/color/utils.nix @@ -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" diff --git a/outputs/lib/default.nix b/outputs/lib/default.nix index a6a7c1e..42ee09f 100644 --- a/outputs/lib/default.nix +++ b/outputs/lib/default.nix @@ -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; diff --git a/outputs/lib/icon/utils.nix b/outputs/lib/icon/utils.nix new file mode 100644 index 0000000..57ac62c --- /dev/null +++ b/outputs/lib/icon/utils.nix @@ -0,0 +1,16 @@ +{ + lib, + pkgs, + ... +}: let + inherit (lib) concatStrings; + + mkIcon = { + svg, + colors, {}, + height ? 24, + width ? 24 + }: let + + in + in