From 20db7224d77d774bc34eb93a23c5463cec3240ce Mon Sep 17 00:00:00 2001 From: rankshank Date: Fri, 1 Mar 2024 15:48:49 +1100 Subject: [PATCH 1/7] Init branch First working example push --- stylix/nixos/default.nix | 1 + stylix/swatches.nix | 206 ++++++++++++++++++++++++++++++ swatches/tab-active-hovered.nix | 1 + swatches/tab-active-unhovered.nix | 1 + 4 files changed, 209 insertions(+) create mode 100644 stylix/swatches.nix create mode 100644 swatches/tab-active-hovered.nix create mode 100644 swatches/tab-active-unhovered.nix diff --git a/stylix/nixos/default.nix b/stylix/nixos/default.nix index 6317b8ace..5d6241e73 100644 --- a/stylix/nixos/default.nix +++ b/stylix/nixos/default.nix @@ -10,6 +10,7 @@ in { ../pixel.nix ../target.nix ../opacity.nix + ../swatches.nix ./cursor.nix ./fonts.nix (import ./palette.nix { inherit palette-generator base16; }) diff --git a/stylix/swatches.nix b/stylix/swatches.nix new file mode 100644 index 000000000..b9503218d --- /dev/null +++ b/stylix/swatches.nix @@ -0,0 +1,206 @@ +{ config, lib, ... }: + +let + inherit (lib) findFirst foldl' genAttrs hasSuffix last mkOption pipe removeSuffix toUpper splitString stringToCharacters; + inherit (lib.types) submodule attrsOf; + inherit (lib.types.ints) u8; + inherit (builtins) attrNames elem elemAt filter hasAttr head listToAttrs mapAttrs substring stringLength; + hex01 = { + "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; + "A" = 10; + "B" = 11; + "C" = 12; + "D" = 13; + "E" = 14; + "F" = 15; + }; + hex10 = { + "0" = 0; + "1" = 16; + "2" = 32; + "3" = 48; + "4" = 64; + "5" = 80; + "6" = 96; + "7" = 112; + "8" = 128; + "9" = 144; + "a" = 160; + "b" = 176; + "c" = 192; + "d" = 208; + "e" = 224; + "f" = 240; + "A" = 160; + "B" = 176; + "C" = 192; + "D" = 208; + "E" = 224; + "F" = 240; + }; + + hexToRgb = str: pipe str [ + (str: stringToCharacters str) + (chars: { + r = hex10.${elemAt chars 0} + hex01.${elemAt chars 1}; + g = hex10.${elemAt chars 2} + hex01.${elemAt chars 3}; + b = hex10.${elemAt chars 4} + hex01.${elemAt chars 5}; + }) + ]; + + getSwatchName = path: + pipe path [ + (toString) + (removeSuffix ".nix") + (splitString "/") + (last) + (splitString "-") + (strs: foldl' (acc: str: + acc + (if str != (head strs) then + (toUpper (substring 0 1 str)) + (substring 1 (stringLength str) str) + else str)) + "" strs) + ]; + + findSwatchFiles = (filter (hasSuffix ".nix") (lib.filesystem.listFilesRecursive ../swatches)); + + colorSubmodule = { + options = { + r = mkOption { + type = u8; + }; + g = mkOption { + type = u8; + }; + b = mkOption { + type = u8; + }; + }; + }; + + swatchSubmodule = { + options = { + fg = mkOption { + type = submodule colorSubmodule; + }; + bg = mkOption { + type = submodule colorSubmodule; + }; + ol = mkOption { + type = submodule colorSubmodule; + }; + }; + }; + + wrappedColor = color: rec { + inherit (color) r g b; + asRgbDec = "rgb(${toString r}, ${toString g}, ${toString b})"; + asRgbaDec = alphaDec: "rgba(${toString r}, ${toString g}, ${toString b}, ${toString alphaDec})"; + asHex = "${lib.toHexString r}${lib.toHexString g}${lib.toHexString b}"; + asHexWithHash = "#${asHex}"; + asHexAlpha = alphaHex: "${asHex}${alphaHex}"; + asHexAlphaWithHash = alphaHex: "#${asHex}${alphaHex}"; + asDecInt = r * 256 + g * 16 + b; + asDecIntAlpha = decAlpha: asDecInt * 16 + decAlpha; + newFactored = factor: wrappedColor { + r = r * factor; + g = g * factor; + b = b * factor; + }; + newBlended = toBlend: wrappedColor { + r = ((r + toBlend.r) / 2); + g = ((g + toBlend.g) / 2); + b = ((b + toBlend.b) / 2); + }; + }; + + wrappedSwatch = swatch: let + wrapped = hasAttr "asHex" swatch.fg; + in rec { + fg = if wrapped then swatch.fg else wrappedColor swatch.fg; + bg = if wrapped then swatch.bg else wrappedColor swatch.bg; + ol = if wrapped then swatch.ol else wrappedColor swatch.ol; + newFactored = factor: wrappedSwatch { + fg = fg.newFactored factor; + bg = bg.newFactored factor; + ol = ol.newFactored factor; + }; + newBlended = color: wrappedSwatch { + fg = fg.newBlended color; + bg = bg.newBlended color; + ol = ol.newBlended color; + }; + }; + + swatchGenerators = listToAttrs (map (swatchFile: { + name = getSwatchName swatchFile; + value = colors: import swatchFile { + inherit colors lib config; + inherit (config.stylix) polarity; + mkSwatch = fg: bg: ol: { inherit fg bg ol; }; + }; + } ) findSwatchFiles); + + getSwatches = targetOrder: let + foundTarget = findFirst (check: elem check (attrNames config.stylix.swatches)) "default" targetOrder; + in genAttrs (attrNames swatchGenerators) (name: + wrappedSwatch (config.stylix.swatches.${foundTarget}.${name}) + ); + + convertBase16ToWrappedColors = base16Attrs: { + base00 = hexToRgb base16Attrs.base00; + base01 = hexToRgb base16Attrs.base01; + base02 = hexToRgb base16Attrs.base02; + base03 = hexToRgb base16Attrs.base03; + base04 = hexToRgb base16Attrs.base04; + base05 = hexToRgb base16Attrs.base05; + base06 = hexToRgb base16Attrs.base06; + base07 = hexToRgb base16Attrs.base07; + base08 = hexToRgb base16Attrs.base08; + base09 = hexToRgb base16Attrs.base09; + base0A = hexToRgb base16Attrs.base0A; + base0B = hexToRgb base16Attrs.base0B; + base0C = hexToRgb base16Attrs.base0C; + base0D = hexToRgb base16Attrs.base0D; + base0E = hexToRgb base16Attrs.base0E; + base0F = hexToRgb base16Attrs.base0F; + }; + + genSwatches = colors: mapAttrs (name: generator: + generator colors + ) swatchGenerators; + + genSwatchesFromBase16Attrs = colors: genSwatches (convertBase16ToWrappedColors colors); +in { + config = { + lib.stylix = { + inherit swatchGenerators hexToRgb getSwatches genSwatches genSwatchesFromBase16Attrs; + }; + stylix.swatches.default = (genSwatchesFromBase16Attrs (config.lib.stylix.colors)); + }; + + options.stylix = { + swatches = mkOption { + type = attrsOf (submodule { + options = genAttrs (attrNames swatchGenerators) (name: mkOption { + type = submodule swatchSubmodule; + }); + }); + }; + }; +} diff --git a/swatches/tab-active-hovered.nix b/swatches/tab-active-hovered.nix new file mode 100644 index 000000000..41c781e20 --- /dev/null +++ b/swatches/tab-active-hovered.nix @@ -0,0 +1 @@ +{ colors, mkSwatch, ... }: with colors; mkSwatch base00 base05 base03 diff --git a/swatches/tab-active-unhovered.nix b/swatches/tab-active-unhovered.nix new file mode 100644 index 000000000..464bfa0bf --- /dev/null +++ b/swatches/tab-active-unhovered.nix @@ -0,0 +1 @@ +{ colors, mkSwatch, ... }: with colors; mkSwatch base05 base03 base02 From e496e09bc6232a48607e5272a140344f27eb5b5e Mon Sep 17 00:00:00 2001 From: rankshank Date: Sun, 3 Mar 2024 03:00:56 +1100 Subject: [PATCH 2/7] Added descriptions to options Added blend factor --- stylix/swatches.nix | 62 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/stylix/swatches.nix b/stylix/swatches.nix index b9503218d..c96c17063 100644 --- a/stylix/swatches.nix +++ b/stylix/swatches.nix @@ -1,10 +1,12 @@ { config, lib, ... }: let - inherit (lib) findFirst foldl' genAttrs hasSuffix last mkOption pipe removeSuffix toUpper splitString stringToCharacters; - inherit (lib.types) submodule attrsOf; + inherit (lib) findFirst foldl' genAttrs hasSuffix last max mdDoc min mkOption pipe removeSuffix toUpper splitString stringToCharacters; + inherit (lib.types) attrsOf submodule; + inherit (lib.types.numbers) between; inherit (lib.types.ints) u8; inherit (builtins) attrNames elem elemAt filter hasAttr head listToAttrs mapAttrs substring stringLength; + constrainU8 = num: min (max num 0) 255; hex01 = { "0" = 0; "1" = 1; @@ -83,12 +85,15 @@ let options = { r = mkOption { type = u8; + description = mdDoc "Red component int with value between 0 to 255"; }; g = mkOption { type = u8; + description = mdDoc "Green component int with value between 0 to 255"; }; b = mkOption { type = u8; + description = mdDoc "Blue component int with value between 0 to 255"; }; }; }; @@ -97,17 +102,22 @@ let options = { fg = mkOption { type = submodule colorSubmodule; + description = mdDoc "Foreground swatch component with r g and b sub components"; }; bg = mkOption { type = submodule colorSubmodule; + description = mdDoc "Background swatch component with r g and b sub components"; }; ol = mkOption { type = submodule colorSubmodule; + description = mdDoc "Outline swatch component with r g and b sub components"; }; }; }; - wrappedColor = color: rec { + wrappedColor = color: let + factor = config.stylix.blendFactor; + in rec { inherit (color) r g b; asRgbDec = "rgb(${toString r}, ${toString g}, ${toString b})"; asRgbaDec = alphaDec: "rgba(${toString r}, ${toString g}, ${toString b}, ${toString alphaDec})"; @@ -118,14 +128,26 @@ let asDecInt = r * 256 + g * 16 + b; asDecIntAlpha = decAlpha: asDecInt * 16 + decAlpha; newFactored = factor: wrappedColor { - r = r * factor; - g = g * factor; - b = b * factor; + r = constrainU8 (r * factor); + g = constrainU8 (g * factor); + b = constrainU8 (b * factor); }; newBlended = toBlend: wrappedColor { - r = ((r + toBlend.r) / 2); - g = ((g + toBlend.g) / 2); - b = ((b + toBlend.b) / 2); + r = constrainU8 ((r + toBlend.r) / 2); + g = constrainU8 ((g + toBlend.g) / 2); + b = constrainU8 ((b + toBlend.b) / 2); + }; + newBrighter = let + brightFactor = factor + 1.0; + in wrappedColor { + r = constrainU8 (r * brightFactor); + g = constrainU8 (g * brightFactor); + b = constrainU8 (b * brightFactor); + }; + newDarker = wrappedColor { + r = constrainU8 (r * factor); + g = constrainU8 (g * factor); + b = constrainU8 (b * factor); }; }; @@ -145,6 +167,16 @@ let bg = bg.newBlended color; ol = ol.newBlended color; }; + newBrigter = color: wrappedSwatch { + fg = fg.newBrighter color; + bg = bg.newBrighter color; + ol = ol.newBrighter color; + }; + newDarker = color: wrappedSwatch { + fg = fg.newDarker color; + bg = bg.newDarker color; + ol = ol.newDarker color; + }; }; swatchGenerators = listToAttrs (map (swatchFile: { @@ -180,27 +212,33 @@ let base0E = hexToRgb base16Attrs.base0E; base0F = hexToRgb base16Attrs.base0F; }; - + genSwatches = colors: mapAttrs (name: generator: generator colors ) swatchGenerators; - + genSwatchesFromBase16Attrs = colors: genSwatches (convertBase16ToWrappedColors colors); in { config = { lib.stylix = { inherit swatchGenerators hexToRgb getSwatches genSwatches genSwatchesFromBase16Attrs; }; - stylix.swatches.default = (genSwatchesFromBase16Attrs (config.lib.stylix.colors)); + stylix.swatches.default = lib.mkDefault (genSwatchesFromBase16Attrs (config.lib.stylix.colors)); }; options.stylix = { swatches = mkOption { type = attrsOf (submodule { options = genAttrs (attrNames swatchGenerators) (name: mkOption { + description = mdDoc "Swatch for ${name}. Use this to override it and config.lib.stylix.getSwatches to use it."; type = submodule swatchSubmodule; }); }); }; + colors.blendFactor = lib.mkOption { + type = between 0 1; + description = mdDoc "Default amount to blend incremental highlights/color changes by. Range of 0.0 - 1.0."; + default = 0.2; + }; }; } From baa717dad139460821922431057176d7cd7df8e9 Mon Sep 17 00:00:00 2001 From: rankshank Date: Sun, 3 Mar 2024 14:15:00 +1100 Subject: [PATCH 3/7] Added swatch generation functions for base16 schemes Added swatch loading to hm and darwin modules --- stylix/darwin/default.nix | 1 + stylix/hm/default.nix | 1 + stylix/nixos/default.nix | 2 +- stylix/swatches.nix | 46 ++++++++++++++++----------------------- 4 files changed, 22 insertions(+), 28 deletions(-) diff --git a/stylix/darwin/default.nix b/stylix/darwin/default.nix index 31e495a04..29d8e9704 100644 --- a/stylix/darwin/default.nix +++ b/stylix/darwin/default.nix @@ -11,6 +11,7 @@ in { ../target.nix ./fonts.nix (import ./palette.nix { inherit palette-generator base16; }) + (import ../swatches.nix { inherit base16; }) (import ../templates.nix inputs) ] ++ autoload; diff --git a/stylix/hm/default.nix b/stylix/hm/default.nix index c27dec24a..fd59ec10f 100644 --- a/stylix/hm/default.nix +++ b/stylix/hm/default.nix @@ -12,6 +12,7 @@ in { ./cursor.nix ./fonts.nix (import ./palette.nix { inherit palette-generator base16; }) + (import ../swatches.nix { inherit base16; }) (import ../templates.nix inputs) ] ++ autoload; } diff --git a/stylix/nixos/default.nix b/stylix/nixos/default.nix index 5d6241e73..ec11492f4 100644 --- a/stylix/nixos/default.nix +++ b/stylix/nixos/default.nix @@ -10,10 +10,10 @@ in { ../pixel.nix ../target.nix ../opacity.nix - ../swatches.nix ./cursor.nix ./fonts.nix (import ./palette.nix { inherit palette-generator base16; }) + (import ../swatches.nix { inherit base16; }) (import ../templates.nix inputs) ] ++ autoload; diff --git a/stylix/swatches.nix b/stylix/swatches.nix index c96c17063..7943b96c6 100644 --- a/stylix/swatches.nix +++ b/stylix/swatches.nix @@ -1,3 +1,4 @@ +{ base16, ... }: { config, lib, ... }: let @@ -114,10 +115,9 @@ let }; }; }; + blendFactor = config.stylix.blendFactor; - wrappedColor = color: let - factor = config.stylix.blendFactor; - in rec { + wrappedColor = color: rec { inherit (color) r g b; asRgbDec = "rgb(${toString r}, ${toString g}, ${toString b})"; asRgbaDec = alphaDec: "rgba(${toString r}, ${toString g}, ${toString b}, ${toString alphaDec})"; @@ -137,18 +137,8 @@ let g = constrainU8 ((g + toBlend.g) / 2); b = constrainU8 ((b + toBlend.b) / 2); }; - newBrighter = let - brightFactor = factor + 1.0; - in wrappedColor { - r = constrainU8 (r * brightFactor); - g = constrainU8 (g * brightFactor); - b = constrainU8 (b * brightFactor); - }; - newDarker = wrappedColor { - r = constrainU8 (r * factor); - g = constrainU8 (g * factor); - b = constrainU8 (b * factor); - }; + newBrighter = newFactored (blendFactor + 1.0); + newDarker = newFactored blendFactor; }; wrappedSwatch = swatch: let @@ -167,16 +157,9 @@ let bg = bg.newBlended color; ol = ol.newBlended color; }; - newBrigter = color: wrappedSwatch { - fg = fg.newBrighter color; - bg = bg.newBrighter color; - ol = ol.newBrighter color; - }; - newDarker = color: wrappedSwatch { - fg = fg.newDarker color; - bg = bg.newDarker color; - ol = ol.newDarker color; - }; + newBrighter = color: newFactored (blendFactor + 1.0); + newDarker = color: newFactored blendFactor; + }; swatchGenerators = listToAttrs (map (swatchFile: { @@ -193,7 +176,7 @@ let in genAttrs (attrNames swatchGenerators) (name: wrappedSwatch (config.stylix.swatches.${foundTarget}.${name}) ); - + convertBase16ToWrappedColors = base16Attrs: { base00 = hexToRgb base16Attrs.base00; base01 = hexToRgb base16Attrs.base01; @@ -218,10 +201,19 @@ let ) swatchGenerators; genSwatchesFromBase16Attrs = colors: genSwatches (convertBase16ToWrappedColors colors); + + genSwatchesFromBase16SchemeOverriden = overrides: scheme: (base16.mkSchemeAttrs scheme).override overrides; + genSwatchesFromBase16Scheme = scheme: genSwatchesFromBase16SchemeOverriden {}; in { config = { lib.stylix = { - inherit swatchGenerators hexToRgb getSwatches genSwatches genSwatchesFromBase16Attrs; + inherit swatchGenerators + hexToRgb + getSwatches + genSwatches + genSwatchesFromBase16Attrs + genSwatchesFromBase16Scheme + genSwatchesFromBase16SchemeOverriden; }; stylix.swatches.default = lib.mkDefault (genSwatchesFromBase16Attrs (config.lib.stylix.colors)); }; From 4588468f2f65c00f65fc2e8b78761363563a410e Mon Sep 17 00:00:00 2001 From: rankshank Date: Sun, 3 Mar 2024 18:57:59 +1100 Subject: [PATCH 4/7] Fixed asHash for values < 10 Removed swatch options from swatches Added base16scheme option to swatches Added override option to swatches --- stylix/swatches.nix | 191 +++++++++++++++++++++++--------------------- 1 file changed, 101 insertions(+), 90 deletions(-) diff --git a/stylix/swatches.nix b/stylix/swatches.nix index 7943b96c6..dfd7a7fa8 100644 --- a/stylix/swatches.nix +++ b/stylix/swatches.nix @@ -2,12 +2,17 @@ { config, lib, ... }: let - inherit (lib) findFirst foldl' genAttrs hasSuffix last max mdDoc min mkOption pipe removeSuffix toUpper splitString stringToCharacters; - inherit (lib.types) attrsOf submodule; + + inherit (lib) findFirst foldl' hasSuffix last literalMD max mdDoc min mkOption pipe removeSuffix splitString stringToCharacters toHexString toUpper; + + inherit (lib.types) attrs attrsOf lines nullOr oneOf path submodule; + inherit (lib.types.numbers) between; - inherit (lib.types.ints) u8; - inherit (builtins) attrNames elem elemAt filter hasAttr head listToAttrs mapAttrs substring stringLength; + + inherit (builtins) attrNames elem elemAt filter hasAttr head listToAttrs mapAttrs substring stringLength typeOf; + constrainU8 = num: min (max num 0) 255; + hex01 = { "0" = 0; "1" = 1; @@ -32,6 +37,7 @@ let "E" = 14; "F" = 15; }; + hex10 = { "0" = 0; "1" = 16; @@ -57,6 +63,19 @@ let "F" = 240; }; + to2HexStr = num: pipe num [ + toHexString + (str: if num < 10 then "0" + str else str) + ]; + + genBaseAttrs = fn: listToAttrs (map (base: rec { + name = "base${base}"; + value = fn name; + }) [ + "00" "01" "02" "03" "04" "05" "06" "07" + "08" "09" "0A" "0B" "0C" "0D" "0E" "0F" + ]); + hexToRgb = str: pipe str [ (str: stringToCharacters str) (chars: { @@ -82,46 +101,18 @@ let findSwatchFiles = (filter (hasSuffix ".nix") (lib.filesystem.listFilesRecursive ../swatches)); - colorSubmodule = { - options = { - r = mkOption { - type = u8; - description = mdDoc "Red component int with value between 0 to 255"; - }; - g = mkOption { - type = u8; - description = mdDoc "Green component int with value between 0 to 255"; - }; - b = mkOption { - type = u8; - description = mdDoc "Blue component int with value between 0 to 255"; - }; - }; - }; + swatchGenerators = listToAttrs (map (swatchFile: { + name = getSwatchName swatchFile; + value = swatchFile; + }) findSwatchFiles); - swatchSubmodule = { - options = { - fg = mkOption { - type = submodule colorSubmodule; - description = mdDoc "Foreground swatch component with r g and b sub components"; - }; - bg = mkOption { - type = submodule colorSubmodule; - description = mdDoc "Background swatch component with r g and b sub components"; - }; - ol = mkOption { - type = submodule colorSubmodule; - description = mdDoc "Outline swatch component with r g and b sub components"; - }; - }; - }; - blendFactor = config.stylix.blendFactor; + blendFactor = config.stylix.blendFactor; wrappedColor = color: rec { inherit (color) r g b; asRgbDec = "rgb(${toString r}, ${toString g}, ${toString b})"; asRgbaDec = alphaDec: "rgba(${toString r}, ${toString g}, ${toString b}, ${toString alphaDec})"; - asHex = "${lib.toHexString r}${lib.toHexString g}${lib.toHexString b}"; + asHex = "${to2HexStr r}${to2HexStr g}${to2HexStr b}"; asHexWithHash = "#${asHex}"; asHexAlpha = alphaHex: "${asHex}${alphaHex}"; asHexAlphaWithHash = alphaHex: "#${asHex}${alphaHex}"; @@ -159,73 +150,93 @@ let }; newBrighter = color: newFactored (blendFactor + 1.0); newDarker = color: newFactored blendFactor; - }; - swatchGenerators = listToAttrs (map (swatchFile: { - name = getSwatchName swatchFile; - value = colors: import swatchFile { - inherit colors lib config; + getSwatches = targetOrder: let + type = typeOf targetOrder; + target = if type == "list" then + findFirst (check: elem check (attrNames config.stylix.swatches)) "default" targetOrder + else if type == "string" then + if hasAttr targetOrder config.stylix.swatches then + targetOrder + else + "default" + else + "default"; + in + getSwatchesFromOpts target; + + getSwatchesFromOpts = target: let + opts = config.stylix.swatches.${target}; + override = opts.override; + scheme = if opts.base16Scheme == null then + config.stylix.base16Scheme + else + opts.base16Scheme; + colors = if (findFirst (hasSuffix "base") null (attrNames override)) != null then + if scheme == config.stylix.base16Scheme then + config.lib.stylix.colors + else + base16.mkSchemeAttrs scheme + else + (base16.mkSchemeAttrs scheme).override override; + args = { + inherit lib config; inherit (config.stylix) polarity; - mkSwatch = fg: bg: ol: { inherit fg bg ol; }; + colors = genBaseAttrs (base: hexToRgb colors.${base}); + mkSwatch = fg: bg: ol: wrappedSwatch { inherit fg bg ol; }; }; - } ) findSwatchFiles); - - getSwatches = targetOrder: let - foundTarget = findFirst (check: elem check (attrNames config.stylix.swatches)) "default" targetOrder; - in genAttrs (attrNames swatchGenerators) (name: - wrappedSwatch (config.stylix.swatches.${foundTarget}.${name}) - ); - - convertBase16ToWrappedColors = base16Attrs: { - base00 = hexToRgb base16Attrs.base00; - base01 = hexToRgb base16Attrs.base01; - base02 = hexToRgb base16Attrs.base02; - base03 = hexToRgb base16Attrs.base03; - base04 = hexToRgb base16Attrs.base04; - base05 = hexToRgb base16Attrs.base05; - base06 = hexToRgb base16Attrs.base06; - base07 = hexToRgb base16Attrs.base07; - base08 = hexToRgb base16Attrs.base08; - base09 = hexToRgb base16Attrs.base09; - base0A = hexToRgb base16Attrs.base0A; - base0B = hexToRgb base16Attrs.base0B; - base0C = hexToRgb base16Attrs.base0C; - base0D = hexToRgb base16Attrs.base0D; - base0E = hexToRgb base16Attrs.base0E; - base0F = hexToRgb base16Attrs.base0F; - }; - - genSwatches = colors: mapAttrs (name: generator: - generator colors - ) swatchGenerators; - - genSwatchesFromBase16Attrs = colors: genSwatches (convertBase16ToWrappedColors colors); + in (mapAttrs (name: genFile: + if (elem name (attrNames override)) then + override.${name} args + else + import genFile args + ) swatchGenerators) // colors; - genSwatchesFromBase16SchemeOverriden = overrides: scheme: (base16.mkSchemeAttrs scheme).override overrides; - genSwatchesFromBase16Scheme = scheme: genSwatchesFromBase16SchemeOverriden {}; in { config = { lib.stylix = { - inherit swatchGenerators - hexToRgb - getSwatches - genSwatches - genSwatchesFromBase16Attrs - genSwatchesFromBase16Scheme - genSwatchesFromBase16SchemeOverriden; + inherit + swatchGenerators + hexToRgb + getSwatches; }; - stylix.swatches.default = lib.mkDefault (genSwatchesFromBase16Attrs (config.lib.stylix.colors)); + stylix.swatches.default = {}; }; options.stylix = { swatches = mkOption { type = attrsOf (submodule { - options = genAttrs (attrNames swatchGenerators) (name: mkOption { - description = mdDoc "Swatch for ${name}. Use this to override it and config.lib.stylix.getSwatches to use it."; - type = submodule swatchSubmodule; - }); + options = { + base16Scheme = mkOption { + description = mdDoc '' + The scheme for the swatches. + + This can be a path to a file, a string of YAML, or an attribute set. + ''; + type = nullOr (oneOf [ path lines attrs ]); + default = null; + defaultText = literalMD '' + The colors used in the theming. + + Those are automatically selected from the background image by default, + but could be overridden manually. + ''; + }; + override = mkOption { + description = mdDoc '' + An override that will be both be applied to the swatches' base16Scheme when generating + the $'{swatch}.colors, + + Takes anything that a scheme generated by base16nix can take as argument + to override. + ''; + type = attrs; + default = {}; + }; + }; }); + description = "Attr set containing base16Schemes and overrides to build swatches from"; }; colors.blendFactor = lib.mkOption { type = between 0 1; From aca69cbb9a4eff44ccc9dd29341962dde7715b2e Mon Sep 17 00:00:00 2001 From: rankshank Date: Sun, 3 Mar 2024 23:47:29 +1100 Subject: [PATCH 5/7] Fixed colors being applied to the top level of getSwatches --- stylix/swatches.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stylix/swatches.nix b/stylix/swatches.nix index dfd7a7fa8..cded6b2c7 100644 --- a/stylix/swatches.nix +++ b/stylix/swatches.nix @@ -191,7 +191,7 @@ let override.${name} args else import genFile args - ) swatchGenerators) // colors; + ) swatchGenerators) // { inherit colors; }; in { config = { From 0a574a6cc579089566391370a00a65b53dd4d9bd Mon Sep 17 00:00:00 2001 From: rankshank Date: Tue, 5 Mar 2024 18:54:22 +1100 Subject: [PATCH 6/7] Changed swatch/color to more verbose naming --- stylix/swatches.nix | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/stylix/swatches.nix b/stylix/swatches.nix index cded6b2c7..4cf17a7f4 100644 --- a/stylix/swatches.nix +++ b/stylix/swatches.nix @@ -109,44 +109,44 @@ let blendFactor = config.stylix.blendFactor; wrappedColor = color: rec { - inherit (color) r g b; - asRgbDec = "rgb(${toString r}, ${toString g}, ${toString b})"; - asRgbaDec = alphaDec: "rgba(${toString r}, ${toString g}, ${toString b}, ${toString alphaDec})"; - asHex = "${to2HexStr r}${to2HexStr g}${to2HexStr b}"; + inherit (color) red blue green; + asRgbDec = "rgb(${toString red}, ${toString green}, ${toString blue})"; + asRgbaDec = alphaDec: "rgba(${toString red}, ${toString green}, ${toString blue}, ${toString alphaDec})"; + asHex = "${to2HexStr red}${to2HexStr green}${to2HexStr blue}"; asHexWithHash = "#${asHex}"; asHexAlpha = alphaHex: "${asHex}${alphaHex}"; asHexAlphaWithHash = alphaHex: "#${asHex}${alphaHex}"; - asDecInt = r * 256 + g * 16 + b; + asDecInt = red * 256 + green * 16 + blue; asDecIntAlpha = decAlpha: asDecInt * 16 + decAlpha; newFactored = factor: wrappedColor { - r = constrainU8 (r * factor); - g = constrainU8 (g * factor); - b = constrainU8 (b * factor); + red = constrainU8 (red * factor); + green = constrainU8 (green * factor); + blue = constrainU8 (blue * factor); }; newBlended = toBlend: wrappedColor { - r = constrainU8 ((r + toBlend.r) / 2); - g = constrainU8 ((g + toBlend.g) / 2); - b = constrainU8 ((b + toBlend.b) / 2); + red = constrainU8 ((red + toBlend.r) / 2); + green = constrainU8 ((green + toBlend.g) / 2); + blue = constrainU8 ((blue + toBlend.b) / 2); }; newBrighter = newFactored (blendFactor + 1.0); newDarker = newFactored blendFactor; }; wrappedSwatch = swatch: let - wrapped = hasAttr "asHex" swatch.fg; + wrapped = hasAttr "asHex" swatch.foreground; in rec { - fg = if wrapped then swatch.fg else wrappedColor swatch.fg; - bg = if wrapped then swatch.bg else wrappedColor swatch.bg; - ol = if wrapped then swatch.ol else wrappedColor swatch.ol; + foreground = if wrapped then swatch.foreground else wrappedColor swatch.foreground; + background = if wrapped then swatch.background else wrappedColor swatch.background; + outline = if wrapped then swatch.outline else wrappedColor swatch.outline; newFactored = factor: wrappedSwatch { - fg = fg.newFactored factor; - bg = bg.newFactored factor; - ol = ol.newFactored factor; + foreground = foreground.newFactored factor; + background = background.newFactored factor; + outline = outline.newFactored factor; }; newBlended = color: wrappedSwatch { - fg = fg.newBlended color; - bg = bg.newBlended color; - ol = ol.newBlended color; + foreground = foreground.newBlended color; + background = background.newBlended color; + outline = outline.newBlended color; }; newBrighter = color: newFactored (blendFactor + 1.0); newDarker = color: newFactored blendFactor; From 1e1f30a5d837697ac018b8890530cfc19af3a84c Mon Sep 17 00:00:00 2001 From: rankshank Date: Tue, 5 Mar 2024 22:52:11 +1100 Subject: [PATCH 7/7] Nested swatches instead of camel casing them Fixed missed renames from previous commit --- stylix/swatches.nix | 46 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/stylix/swatches.nix b/stylix/swatches.nix index 4cf17a7f4..9e75bd590 100644 --- a/stylix/swatches.nix +++ b/stylix/swatches.nix @@ -1,15 +1,14 @@ -{ base16, ... }: -{ config, lib, ... }: +{ base16, ... }: { config, lib, ... }: let - inherit (lib) findFirst foldl' hasSuffix last literalMD max mdDoc min mkOption pipe removeSuffix splitString stringToCharacters toHexString toUpper; + inherit (lib) findFirst foldl' getAttrFromPath hasAttrByPath hasSuffix literalMD mapAttrsRecursive max mdDoc min mkOption pipe removeSuffix setAttrByPath splitString stringToCharacters toHexString ; inherit (lib.types) attrs attrsOf lines nullOr oneOf path submodule; inherit (lib.types.numbers) between; - inherit (builtins) attrNames elem elemAt filter hasAttr head listToAttrs mapAttrs substring stringLength typeOf; + inherit (builtins) attrNames elem elemAt filter hasAttr listToAttrs typeOf; constrainU8 = num: min (max num 0) 255; @@ -79,32 +78,25 @@ let hexToRgb = str: pipe str [ (str: stringToCharacters str) (chars: { - r = hex10.${elemAt chars 0} + hex01.${elemAt chars 1}; - g = hex10.${elemAt chars 2} + hex01.${elemAt chars 3}; - b = hex10.${elemAt chars 4} + hex01.${elemAt chars 5}; + red = hex10.${elemAt chars 0} + hex01.${elemAt chars 1}; + green = hex10.${elemAt chars 2} + hex01.${elemAt chars 3}; + blue = hex10.${elemAt chars 4} + hex01.${elemAt chars 5}; }) ]; - getSwatchName = path: + nestSwatchAttrs = path: pipe path [ (toString) - (removeSuffix ".nix") (splitString "/") - (last) + (lib.last) + (removeSuffix ".nix") (splitString "-") - (strs: foldl' (acc: str: - acc + (if str != (head strs) then - (toUpper (substring 0 1 str)) + (substring 1 (stringLength str) str) - else str)) - "" strs) + (strs: setAttrByPath strs path) ]; findSwatchFiles = (filter (hasSuffix ".nix") (lib.filesystem.listFilesRecursive ../swatches)); - swatchGenerators = listToAttrs (map (swatchFile: { - name = getSwatchName swatchFile; - value = swatchFile; - }) findSwatchFiles); + swatchGenerators = { swatches = foldl' (acc: path: lib.recursiveUpdate acc (nestSwatchAttrs path)) {} findSwatchFiles; }; blendFactor = config.stylix.blendFactor; @@ -124,9 +116,9 @@ let blue = constrainU8 (blue * factor); }; newBlended = toBlend: wrappedColor { - red = constrainU8 ((red + toBlend.r) / 2); - green = constrainU8 ((green + toBlend.g) / 2); - blue = constrainU8 ((blue + toBlend.b) / 2); + red = constrainU8 ((red + toBlend.red) / 2); + green = constrainU8 ((green + toBlend.green) / 2); + blue = constrainU8 ((blue + toBlend.blue) / 2); }; newBrighter = newFactored (blendFactor + 1.0); newDarker = newFactored blendFactor; @@ -184,13 +176,13 @@ let inherit lib config; inherit (config.stylix) polarity; colors = genBaseAttrs (base: hexToRgb colors.${base}); - mkSwatch = fg: bg: ol: wrappedSwatch { inherit fg bg ol; }; + mkSwatch = foreground: background: outline: wrappedSwatch { inherit foreground background outline; }; }; - in (mapAttrs (name: genFile: - if (elem name (attrNames override)) then - override.${name} args + in (mapAttrsRecursive (path: value: + if (hasAttrByPath path override) then + (getAttrFromPath path override) args else - import genFile args + import value args ) swatchGenerators) // { inherit colors; }; in {