diff options
| author | Amit Prasad <17amitprasad@gmail.com> | 2019-12-17 20:29:04 -0500 |
|---|---|---|
| committer | Amit Prasad <17amitprasad@gmail.com> | 2019-12-17 20:29:04 -0500 |
| commit | 02acd28f0641667ff658a4e5a19061ef8657e697 (patch) | |
| tree | 78474e734954809d73d5f4e4d435295b2c1806b5 /pywal | |
| parent | b9fd064d3069024b7d9084d3303e28fede3ad4b0 (diff) | |
Added ability to modify colors using methods ( .lighten(%), .darken(%), .saturate(%) for now)
Diffstat (limited to 'pywal')
| -rw-r--r-- | pywal/colors.py | 3 | ||||
| -rw-r--r-- | pywal/export.py | 59 | ||||
| -rw-r--r-- | pywal/util.py | 13 |
3 files changed, 45 insertions, 30 deletions
diff --git a/pywal/colors.py b/pywal/colors.py index 99d346d..6dc5105 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -48,6 +48,9 @@ def colors_to_dict(colors, img): "color13": colors[13], "color14": colors[14], "color15": colors[15] + }, + "modified": { + } } diff --git a/pywal/export.py b/pywal/export.py index b175c11..ae619cd 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -13,38 +13,43 @@ def template(colors, input_file, output_file=None): """Read template file, substitute markers and save the file elsewhere.""" template_data = util.read_file_raw(input_file) - matches = re.finditer(r"(?<=(?<!\{))(\{([^{}]+)\})(?=(?!\}))", "".join(template_data), re.MULTILINE) - print(colors) - for match in matches: - # Check that this color doesn't already exist - match_str = match.group(2) - color, _, funcs = match_str.partition(".") - #if len(funcs) != 0: - #print(funcs) - #print(colors[color].hex_color,input_file) - - '''if match_str not in colors: - # Extract original color and functions - attr, _, funcs = match_str.partition(".") - original_color = colors[attr] - funcs = funcs.split(".") - # Apply every function to the original color - for func in funcs: - # Check if this sub-color has already been generated - if not hasattr(original_color, func): - # Generate new color using function from util.py - func, arg = func.strip(")").split("(") - arg = arg.split(",") - new_color = util.Color( - getattr(util, func)(original_color.hex_color, *arg)) - setattr(original_color, func, new_color) - original_color = getattr(original_color, func)''' + for i in range(len(template_data)): + line = template_data[i] + matches = re.finditer(r"(?<=(?<!\{))(\{([^{}]+)\})(?=(?!\}))", line) + for match in matches: + # Check that this color doesn't already exist + color, _, funcs = match.group(2).partition(".") + if len(funcs) != 0: + to_replace = color + new_color = None + for func in funcs.split(")"): + if len(func) == 0: + continue + func_split = func.split("(") + if len(func_split) > 1: + args = func_split[1].split(",") + else: + args = [] + name = func_split[0] + if name[0] == '.': + name = name[1:] + x = getattr(colors[color], name) + if callable(x): + new_color = x(*args) + if func[0] != '.': + to_replace += "." + 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: template_data = "".join(template_data).format(**colors) except ValueError: logging.error("Syntax error in template file '%s'.", input_file) return - util.save_file(template_data, output_file) diff --git a/pywal/util.py b/pywal/util.py index 2fe0762..c105a56 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -9,6 +9,7 @@ import shutil import subprocess import sys import platform +import re class Color: @@ -57,10 +58,17 @@ class Color: """Strip '#' from color.""" return self.hex_color[1:] - @property def lighten(self,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): @@ -68,7 +76,6 @@ def read_file(input_file): with open(input_file, "r") as file: return file.read().splitlines() - def read_file_json(input_file): """Read data from a json file.""" with open(input_file, "r") as json_file: |
