Clean up implementation of palette options ♻️

The automatically chosen color is now the default value under `stylix.palette`,
rather than `null`.

The colors from `stylix.palette` are collected to make the default value
of `stylix.base16Scheme`.

If `stylix.base16Scheme` is overridden to an external file, the colors
from that file will NOT be written back to `stylix.palette`. For that
reason, you must still only refer to `lib.stylix.colors` within modules.
This commit is contained in:
Daniel Thwaites 2022-07-22 22:46:00 +01:00
parent 0defebf853
commit 2c8ac84830
No known key found for this signature in database
GPG key ID: D8AFC4BF05670F9D

View file

@ -11,16 +11,6 @@ let
'';
generatedPalette = importJSON paletteJSON;
paletteOverrides = filterAttrs (_: color: color != null) cfg.palette;
metadata = {
author = "Stylix";
scheme = "Stylix";
slug = "stylix";
};
stylixPalette = generatedPalette // paletteOverrides // metadata;
in {
options.stylix = {
polarity = mkOption {
@ -52,17 +42,12 @@ in {
description = ''
Hexadecimal color value for ${base}.
These options can be used to override a section of the automatically
generated palette, while keeping other parts. For example, you can
select the background and text colors by setting bases 00 to 07, while
keeping the accent colors automatic.
See <literal>stylix.base16Scheme</literal> for importing an entire
scheme.
See <literal>stylix.base16Scheme</literal> if you want to import a
base16 scheme from a file.
'';
default = null;
type = types.strMatching "[0-9a-fA-F]{6}";
default = generatedPalette.${base};
defaultText = "Automatically selected from the background image.";
type = types.nullOr (types.strMatching "[0-9a-fA-F]{6}");
});
base16Scheme = mkOption {
@ -70,19 +55,23 @@ in {
A scheme following the base16 standard.
This can be a path to a file, a string of YAML, or an attribute set.
Setting this option will completely disable the automatic palette
generator, and use these colors instead.
'';
default = null;
type = with types; nullOr (oneOf [ path lines attrs ]);
type = with types; oneOf [ path lines attrs ];
default = cfg.palette // {
author = "Stylix";
scheme = "Stylix";
slug = "stylix";
};
defaultText = ''
The colors defined in <literal>stylix.palette</literal>.
Those are automatically selected from the background image by default,
but could be overridden manually.
'';
};
};
# This attrset can be used like a function too, see
# https://github.com/SenchoPens/base16.nix#mktheme
config.lib.stylix.colors =
if cfg.base16Scheme != null
then base16.mkSchemeAttrs cfg.base16Scheme
else base16.mkSchemeAttrs stylixPalette;
config.lib.stylix.colors = base16.mkSchemeAttrs cfg.base16Scheme;
}