Added ability to modify colors using methods ( .lighten(%), .darken(%), .saturate(%) for now)

This commit is contained in:
Amit Prasad 2019-12-17 20:29:04 -05:00
parent b9fd064d30
commit 02acd28f06
3 changed files with 45 additions and 30 deletions

View File

@ -48,6 +48,9 @@ def colors_to_dict(colors, img):
"color13": colors[13], "color13": colors[13],
"color14": colors[14], "color14": colors[14],
"color15": colors[15] "color15": colors[15]
},
"modified": {
} }
} }

View File

@ -13,38 +13,43 @@ def template(colors, input_file, output_file=None):
"""Read template file, substitute markers and """Read template file, substitute markers and
save the file elsewhere.""" save the file elsewhere."""
template_data = util.read_file_raw(input_file) template_data = util.read_file_raw(input_file)
matches = re.finditer(r"(?<=(?<!\{))(\{([^{}]+)\})(?=(?!\}))", "".join(template_data), re.MULTILINE) for i in range(len(template_data)):
print(colors) line = template_data[i]
for match in matches: matches = re.finditer(r"(?<=(?<!\{))(\{([^{}]+)\})(?=(?!\}))", line)
# Check that this color doesn't already exist for match in matches:
match_str = match.group(2) # Check that this color doesn't already exist
color, _, funcs = match_str.partition(".") color, _, funcs = match.group(2).partition(".")
#if len(funcs) != 0: if len(funcs) != 0:
#print(funcs) to_replace = color
#print(colors[color].hex_color,input_file) new_color = None
for func in funcs.split(")"):
'''if match_str not in colors: if len(func) == 0:
# Extract original color and functions continue
attr, _, funcs = match_str.partition(".") func_split = func.split("(")
original_color = colors[attr] if len(func_split) > 1:
funcs = funcs.split(".") args = func_split[1].split(",")
# Apply every function to the original color else:
for func in funcs: args = []
# Check if this sub-color has already been generated name = func_split[0]
if not hasattr(original_color, func): if name[0] == '.':
# Generate new color using function from util.py name = name[1:]
func, arg = func.strip(")").split("(") x = getattr(colors[color], name)
arg = arg.split(",") if callable(x):
new_color = util.Color( new_color = x(*args)
getattr(util, func)(original_color.hex_color, *arg)) if func[0] != '.':
setattr(original_color, func, new_color) to_replace += "."
original_color = getattr(original_color, func)''' to_replace += func + ")"
else:
pass
if not new_color is None:
cname = "color" + new_color.strip
template_data[i] = line.replace(to_replace, cname)
colors[cname] = new_color
try: try:
template_data = "".join(template_data).format(**colors) template_data = "".join(template_data).format(**colors)
except ValueError: except ValueError:
logging.error("Syntax error in template file '%s'.", input_file) logging.error("Syntax error in template file '%s'.", input_file)
return return
util.save_file(template_data, output_file) util.save_file(template_data, output_file)

View File

@ -9,6 +9,7 @@ import shutil
import subprocess import subprocess
import sys import sys
import platform import platform
import re
class Color: class Color:
@ -57,10 +58,17 @@ class Color:
"""Strip '#' from color.""" """Strip '#' from color."""
return self.hex_color[1:] return self.hex_color[1:]
@property
def lighten(self,percent): def lighten(self,percent):
"""Lighten color by percent""" """Lighten color by percent"""
return lighten_color(self.hex_color,percent/100) return Color(lighten_color(self.hex_color,float(re.sub(r'[\D\.]','',percent))/100))
def darken(self,percent):
"""Darken color by percent"""
return Color(darken_color(self.hex_color,float(re.sub(r'[\D\.]','',percent))/100))
def saturate(self,percent):
"""Saturate a color"""
return Color(saturate_color(self.hex_color,float(re.sub(r'[\D\.]','',percent))/100))
def read_file(input_file): def read_file(input_file):
@ -68,7 +76,6 @@ def read_file(input_file):
with open(input_file, "r") as file: with open(input_file, "r") as file:
return file.read().splitlines() return file.read().splitlines()
def read_file_json(input_file): def read_file_json(input_file):
"""Read data from a json file.""" """Read data from a json file."""
with open(input_file, "r") as json_file: with open(input_file, "r") as json_file: