feat(coloryou): add flags to dynamically set tone percent

This commit is contained in:
matt1432 2023-09-27 15:30:35 -04:00
parent 1f4914e659
commit 3bc5c9ce97
2 changed files with 27 additions and 33 deletions

View file

@ -6,46 +6,40 @@ https://github.com/dharmx/vile/blob/7d486c128c7e553912673755f97b118aaab0193d/src
""" """
import argparse import argparse
import utils
import json 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( parser = argparse.ArgumentParser(
prog='coloryou', prog='coloryou',
description='Extract Material You colors from an image', description='This program extract Material You colors from an image. It returns them as a JSON object for scripting.')
epilog='coucou')
parser.add_argument("image_path", help="A full path to your image", type=str) 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() 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: img = Image.open(args.image_path)
image_path: The location of the image. 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: theme = themeFromImage(img).get("palettes").get("primary")
A dict of image accent color, button accent color and button text color eg: {'image_accent': '#292929', 'button_accent': '#BEBFC1', 'button_text': '#292929'} parsed_colors = {"imageAccent": hexFromArgb(theme.tone(args.image)),
""" "buttonAccent": hexFromArgb(theme.tone(args.button)),
"buttonText": hexFromArgb(theme.tone(args.text)),
"""if image_path == default_cover: "hoverAccent": hexFromArgb(theme.tone(args.hover))}
return {'image_accent': '#292929', 'button_accent': '#BEBFC1', 'button_text': '#292929'}"""
img = Image.open(image_path) print(json.dumps(parsed_colors))
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)))

View file

@ -16,7 +16,7 @@ let
f = { buildPythonPackage, utils, material-color-utilities }: f = { buildPythonPackage, utils, material-color-utilities }:
buildPythonPackage rec { buildPythonPackage rec {
pname = "coloryou"; pname = "coloryou";
version = "0.1.0"; version = "0.2.0";
# If you have your sources locally, you can specify a path # If you have your sources locally, you can specify a path
src = ./.; src = ./.;