Skip to content

Commit 4c3bf20

Browse files
committed
palette: add option to allow swapping colorscheme generator logic
Signed-off-by: lucasew <[email protected]>
1 parent b1a84f8 commit 4c3bf20

File tree

1 file changed

+80
-7
lines changed

1 file changed

+80
-7
lines changed

stylix/palette.nix

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,35 @@
88
let
99
cfg = config.stylix;
1010
opts = options.stylix;
11+
12+
# Validates that a palette contains all required base16 colors
13+
validateBase16Palette =
14+
palette:
15+
let
16+
requiredColors = [
17+
"base00"
18+
"base01"
19+
"base02"
20+
"base03"
21+
"base04"
22+
"base05"
23+
"base06"
24+
"base07"
25+
"base08"
26+
"base09"
27+
"base0A"
28+
"base0B"
29+
"base0C"
30+
"base0D"
31+
"base0E"
32+
"base0F"
33+
];
34+
missingColors = lib.filter (c: !(palette ? ${c})) requiredColors;
35+
in
36+
if missingColors != [ ] then
37+
throw "Palette generator output is missing required base16 colors: ${lib.concatStringsSep ", " missingColors}"
38+
else
39+
palette;
1140
in
1241
{
1342
options.stylix = {
@@ -41,6 +70,53 @@ in
4170
default = null;
4271
};
4372

73+
paletteGeneratorFunction = lib.mkOption {
74+
description = ''
75+
Function to generate palette from image. Must return a derivation that
76+
produces a JSON file with a base16 color scheme (base00-base0F).
77+
78+
The function receives an attribute set with the following arguments:
79+
- `polarity`: The polarity setting ("either", "light", or "dark")
80+
- `image`: Path to the wallpaper image
81+
82+
Example:
83+
```nix
84+
85+
paletteGeneratorFunction = { polarity, image }:
86+
let
87+
color = "ffffff";
88+
colormap = {
89+
base00 = color;
90+
base01 = color;
91+
base02 = color;
92+
base03 = color;
93+
base04 = color;
94+
base05 = color;
95+
base06 = color;
96+
base07 = color;
97+
base08 = color;
98+
base09 = color;
99+
base0A = color;
100+
base0B = color;
101+
base0C = color;
102+
base0D = color;
103+
base0E = color;
104+
base0F = color;
105+
};
106+
in
107+
pkgs.runCommand "palette.json" {color = "ffffff"; } \'\'
108+
echo \'\$\{builtins.toJSON colormap}\' > $out
109+
\'\';
110+
```
111+
'';
112+
type = lib.types.functionTo lib.types.package;
113+
default =
114+
{ polarity, image }:
115+
pkgs.runCommand "palette.json" { } ''
116+
${lib.getExe cfg.paletteGenerator} "${polarity}" ${lib.escapeShellArg image} "$out"
117+
'';
118+
};
119+
44120
imageScalingMode = lib.mkOption {
45121
type = lib.types.enum [
46122
"stretch"
@@ -80,20 +156,17 @@ in
80156
# and not anything indirect such as filling a template, otherwise
81157
# the output of the palette generator will not be protected from
82158
# garbage collection.
83-
default = pkgs.runCommand "palette.json" { } ''
84-
${lib.getExe cfg.paletteGenerator} \
85-
"${cfg.polarity}" \
86-
${lib.escapeShellArg cfg.image} \
87-
"$out"
88-
'';
159+
default = cfg.paletteGeneratorFunction {
160+
inherit (cfg) polarity image;
161+
};
89162
};
90163

91164
palette = lib.mkOption {
92165
type = lib.types.attrs;
93166
description = "The palette generated by the palette generator.";
94167
readOnly = true;
95168
internal = true;
96-
default = (lib.importJSON cfg.generated.json) // {
169+
default = validateBase16Palette (lib.importJSON cfg.generated.json) // {
97170
author = "Stylix";
98171
scheme = "Stylix";
99172
slug = "stylix";

0 commit comments

Comments
 (0)