Add stylix.colors option

Closes #1
This commit is contained in:
Daniel Thwaites 2021-03-28 17:56:24 +01:00
parent 67e4bcebb3
commit dccee9b6fd
No known key found for this signature in database
GPG key ID: D8AFC4BF05670F9D
2 changed files with 40 additions and 8 deletions

View file

@ -17,11 +17,29 @@ let
};
colorgramPython = pkgs.python3.withPackages (ps: [ colorgram ]);
# Pass the wallpaper to ./colors.py and return a JSON file containing the
# generated colorscheme
colorsJSON = pkgs.runCommand "stylix-colors" {}
"${colorgramPython}/bin/python ${./colors.py} ${cfg.image} > $out";
# Pass the wallpaper and any manually selected colors to ./colors.py and
# return a JSON file containing the generated colorscheme
colorsJSON = pkgs.runCommand
"stylix-colors"
{
colors = builtins.toJSON config.stylix.colors;
passAsFile = [ "colors" ];
}
''
${colorgramPython}/bin/python ${./colors.py} \
${cfg.image} < $colorsPath > $out
'';
in {
options.stylix.colors = genAttrs
[ "base00" "base01" "base02" "base03" "base04" "base05" "base06" "base07"
"base08" "base09" "base0A" "base0B" "base0C" "base0D" "base0E" "base0F" ]
(name: mkOption {
description = "Hexadecimal color value for ${name}.";
default = null;
defaultText = "Automatically selected from the background image.";
type = types.nullOr (types.strMatching "[0-9a-fA-F]{6}");
});
config.lib.stylix.colors = importJSON colorsJSON;
}

View file

@ -40,8 +40,8 @@ scheme = {}
for i in range(8):
scheme[int_to_base(i)] = (
dominant_color.hsl.h,
dominant_color.hsl.s,
scale(i),
dominant_color.hsl.s,
)
# base08 to base0A use the remaining 8 colors from the image,
@ -51,19 +51,33 @@ for i in range(8, 16):
color = colors[i-8]
scheme[int_to_base(i)] = (
color.hsl.h,
color.hsl.s,
clamp(color.hsl.l),
color.hsl.s,
)
# Override with any manually selected colors
manual_colors = json.load(sys.stdin)
for k, v in manual_colors.items():
if v is not None:
scheme[k] = colorsys.rgb_to_hls(
int(v[0:2], 16) / 255,
int(v[2:4], 16) / 255,
int(v[4:6], 16) / 255,
)
scheme[k] = (
scheme[k][0] * 255,
scheme[k][1] * 255,
scheme[k][2] * 255,
)
data = {}
for key, color in scheme.items():
r, g, b = colorsys.hls_to_rgb(
# Function wants HLS, stored as HSL
color[0] / 255,
color[2] / 255,
color[1] / 255,
color[2] / 255,
)
data[key + "-dec-r"] = r
data[key + "-dec-g"] = g