diff --git a/stylix/colors.nix b/stylix/colors.nix index e84a8438..ff05bea1 100644 --- a/stylix/colors.nix +++ b/stylix/colors.nix @@ -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; } diff --git a/stylix/colors.py b/stylix/colors.py index 8b1f7a3e..1ed37f5a 100644 --- a/stylix/colors.py +++ b/stylix/colors.py @@ -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