From 3bc5c9ce97c2667d177af4c696b3509a706baf85 Mon Sep 17 00:00:00 2001 From: matt1432 Date: Wed, 27 Sep 2023 15:30:35 -0400 Subject: [PATCH] feat(coloryou): add flags to dynamically set tone percent --- nixos/overlays/pkgs/coloryou/coloryou.py | 58 +++++++++++------------- nixos/overlays/pkgs/coloryou/default.nix | 2 +- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/nixos/overlays/pkgs/coloryou/coloryou.py b/nixos/overlays/pkgs/coloryou/coloryou.py index f87bd055..66c8efbb 100755 --- a/nixos/overlays/pkgs/coloryou/coloryou.py +++ b/nixos/overlays/pkgs/coloryou/coloryou.py @@ -6,46 +6,40 @@ https://github.com/dharmx/vile/blob/7d486c128c7e553912673755f97b118aaab0193d/src """ import argparse -import utils import json -from material_color_utilities_python import * +from material_color_utilities_python import themeFromImage, hexFromArgb, Image + +def range_type(value_string): + value = int(value_string) + if value not in range(0, 101): + raise argparse.ArgumentTypeError("%s is out of range, choose in [0-100]" % value) + return value parser = argparse.ArgumentParser( prog='coloryou', - description='Extract Material You colors from an image', - epilog='coucou') + description='This program extract Material You colors from an image. It returns them as a JSON object for scripting.') parser.add_argument("image_path", help="A full path to your image", type=str) +parser.add_argument('-i', '--image', dest='image', default=40, type=range_type, metavar='i', choices=range(0,101), help="Value should be within [0, 100] (default: %(default)s). Set the tone for the main image accent.") + +parser.add_argument('-b', '--button', dest='button', default=90, type=range_type, metavar='i', choices=range(0,101), help="Value should be within [0, 100] (default: %(default)s). Set the tone for the button accent.") + +parser.add_argument('-t', '--text', dest='text', default=10, type=range_type, metavar='i', choices=range(0,101), help="Value should be within [0, 100] (default: %(default)s). Set the tone for the button text accent.") + +parser.add_argument('-o', '--hover', dest='hover', default=80, type=range_type, metavar='i', choices=range(0,101), help="Value should be within [0, 100] (default: %(default)s). Set the tone for the hovering effect accent.") args = parser.parse_args() -def get_material_you_colors(image_path: str) -> dict: - """Get the material you pallete colors from an image and save them to a - JSON file if it isn't already. Then return the path to that JSON file. - Arguments: - image_path: The location of the image. +img = Image.open(args.image_path) +basewidth = 64 +wpercent = (basewidth/float(img.size[0])) +hsize = int((float(img.size[1])*float(wpercent))) +img = img.resize((basewidth,hsize),Image.Resampling.LANCZOS) - Returns: - A dict of image accent color, button accent color and button text color eg: {'image_accent': '#292929', 'button_accent': '#BEBFC1', 'button_text': '#292929'} - """ - - """if image_path == default_cover: - return {'image_accent': '#292929', 'button_accent': '#BEBFC1', 'button_text': '#292929'}""" +theme = themeFromImage(img).get("palettes").get("primary") +parsed_colors = {"imageAccent": hexFromArgb(theme.tone(args.image)), + "buttonAccent": hexFromArgb(theme.tone(args.button)), + "buttonText": hexFromArgb(theme.tone(args.text)), + "hoverAccent": hexFromArgb(theme.tone(args.hover))} - img = Image.open(image_path) - basewidth = 64 - wpercent = (basewidth/float(img.size[0])) - hsize = int((float(img.size[1])*float(wpercent))) - img = img.resize((basewidth,hsize),Image.Resampling.LANCZOS) - - theme = themeFromImage(img) - themePalette = theme.get("palettes") - themePalettePrimary = themePalette.get("primary") - parsed_colors = {"imageAccent": hexFromArgb(themePalettePrimary.tone(40)), - "buttonAccent": hexFromArgb(themePalettePrimary.tone(90)), - "buttonText": hexFromArgb(themePalettePrimary.tone(10)), - "hoverAccent": hexFromArgb(themePalettePrimary.tone(85))} - - return parsed_colors - -print(json.dumps(get_material_you_colors(args.image_path))) +print(json.dumps(parsed_colors)) diff --git a/nixos/overlays/pkgs/coloryou/default.nix b/nixos/overlays/pkgs/coloryou/default.nix index 53f60386..ff82bb9f 100644 --- a/nixos/overlays/pkgs/coloryou/default.nix +++ b/nixos/overlays/pkgs/coloryou/default.nix @@ -16,7 +16,7 @@ let f = { buildPythonPackage, utils, material-color-utilities }: buildPythonPackage rec { pname = "coloryou"; - version = "0.1.0"; + version = "0.2.0"; # If you have your sources locally, you can specify a path src = ./.;