From b9fd064d3069024b7d9084d3303e28fede3ad4b0 Mon Sep 17 00:00:00 2001 From: Amit Prasad <17amitprasad@gmail.com> Date: Mon, 16 Dec 2019 11:45:22 -0500 Subject: Started working on matching functions --- pywal/export.py | 28 +++++++++++++++++++++++++++- pywal/util.py | 5 +++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/pywal/export.py b/pywal/export.py index 88c98a6..b175c11 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -3,6 +3,7 @@ Export colors in various formats. """ import logging import os +import re from .settings import CACHE_DIR, MODULE_DIR, CONF_DIR from . import util @@ -12,7 +13,32 @@ 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"(?<=(? Date: Tue, 17 Dec 2019 20:29:04 -0500 Subject: Added ability to modify colors using methods ( .lighten(%), .darken(%), .saturate(%) for now) --- pywal/colors.py | 3 +++ pywal/export.py | 59 +++++++++++++++++++++++++++++++-------------------------- 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"(?<=(? 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: -- cgit v1.2.3 From 0a4f2e6dae65c0cd0000465e36309b05fafca11e Mon Sep 17 00:00:00 2001 From: Amit Prasad <17amitprasad@gmail.com> Date: Wed, 18 Dec 2019 00:34:48 -0500 Subject: Update Readme --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0e14f97..5be06a9 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,12 @@ img -Pywal is a tool that generates a color palette from the dominant colors in an image. It then applies the colors system-wide and on-the-fly in all of your favourite programs. +Pywal is a tool that generates a color palette from the dominant colors in an image. It then applies the colors system-wide and on-the-fly in all of your favourite programs. + +This fork of Pywal aims to create a more versatile system, by being able to modify colors in templates. Currently supported functions include: +* `lighten` +* `darken` +* `saturate` There are currently 5 supported color generation backends, each providing a different palette of colors from each image. You're bound to find an appealing color-scheme. -- cgit v1.2.3 From fc885697c4583a924adbc8ff3b827ab62d89c806 Mon Sep 17 00:00:00 2001 From: Amit Prasad <17amitprasad@gmail.com> Date: Wed, 18 Dec 2019 00:36:05 -0500 Subject: Update Readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5be06a9..18543dc 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ Pywal is a tool that generates a color palette from the dominant colors in an im This fork of Pywal aims to create a more versatile system, by being able to modify colors in templates. Currently supported functions include: * `lighten` * `darken` -* `saturate` +* `saturate` +(More coming) There are currently 5 supported color generation backends, each providing a different palette of colors from each image. You're bound to find an appealing color-scheme. -- cgit v1.2.3 From f0692c31eb91f0c6e5f2f5c8fdf4737808784362 Mon Sep 17 00:00:00 2001 From: Amit Prasad <17amitprasad@gmail.com> Date: Wed, 18 Dec 2019 13:28:13 -0500 Subject: Commented and minimize. --- README.md | 5 ----- pywal/export.py | 47 ++++++++++++++++++++++++----------------------- pywal/templates/functest | 1 + 3 files changed, 25 insertions(+), 28 deletions(-) create mode 100644 pywal/templates/functest diff --git a/README.md b/README.md index 5be06a9..f263c2f 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,6 @@ Pywal is a tool that generates a color palette from the dominant colors in an image. It then applies the colors system-wide and on-the-fly in all of your favourite programs. -This fork of Pywal aims to create a more versatile system, by being able to modify colors in templates. Currently supported functions include: -* `lighten` -* `darken` -* `saturate` - There are currently 5 supported color generation backends, each providing a different palette of colors from each image. You're bound to find an appealing color-scheme. Pywal also supports predefined themes and has over 250 themes built-in. You can also create your own theme files to share with others. diff --git a/pywal/export.py b/pywal/export.py index ae619cd..f7b9de8 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -17,33 +17,34 @@ def template(colors, input_file, output_file=None): line = template_data[i] matches = re.finditer(r"(?<=(? 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: + args = [] + if len(func_split) > 1: args = func_split[1].split(",") + fname = func_split[0] + if fname[0] == '.': fname = fname[1:] + f = getattr(new_color, fname) + + # If the function is callable, call it + if callable(f): + new_color = f(*args) + #add to the string that will replace the function calls with the generated function. + if func[0] != '.': replace_str += "." + replace_str += func + ")" + #If the color was changed, replace the template with a unique identifier for the new color. + if not new_color is colors[color]: cname = "color" + new_color.strip - template_data[i] = line.replace(to_replace, cname) + template_data[i] = line.replace(replace_str, cname) colors[cname] = new_color try: template_data = "".join(template_data).format(**colors) diff --git a/pywal/templates/functest b/pywal/templates/functest new file mode 100644 index 0000000..b82d4fc --- /dev/null +++ b/pywal/templates/functest @@ -0,0 +1 @@ +{color0.lighten(10).darken(10).rgb} -- cgit v1.2.3 From c29151de464ee9267872c48115fc79bc7a0075b6 Mon Sep 17 00:00:00 2001 From: Amit Prasad <17amitprasad@gmail.com> Date: Fri, 20 Dec 2019 09:44:19 -0500 Subject: Beautify --- README.md | 2 +- pywal/export.py | 28 +++++++++++++++++----------- pywal/templates/functest | 1 - pywal/util.py | 19 ++++++++++--------- 4 files changed, 28 insertions(+), 22 deletions(-) delete mode 100644 pywal/templates/functest diff --git a/README.md b/README.md index f263c2f..443cca3 100644 --- a/README.md +++ b/README.md @@ -21,4 +21,4 @@ The goal of Pywal was to be as out of the way as possible. It doesn't modify any Terminal emulators and TTYs have their color-schemes updated in real-time with no delay. With minimal configuration this functionality can be extended to almost anything running on your system. -### More: \[[Installation](https://github.com/dylanaraps/pywal/wiki/Installation)\] \[[Getting Started](https://github.com/dylanaraps/pywal/wiki/Getting-Started)\] \[[Customization](https://github.com/dylanaraps/pywal/wiki/Customization)\] \[[Wiki](https://github.com/dylanaraps/pywal/wiki)\] \[[Screenshots](https://www.reddit.com/r/unixporn/search?q=wal&restrict_sr=on&sort=relevance&t=all)\] +### More: \[[Installation](https://github.com/dylanaraps/pywal/wiki/Installation)] \[[Getting Started](https://github.com/dylanaraps/pywal/wiki/Getting-Started)] \[[Customization](https://github.com/dylanaraps/pywal/wiki/Customization)] \[[Wiki](https://github.com/dylanaraps/pywal/wiki)] \[[Screenshots](https://www.reddit.com/r/unixporn/search?q=wal&restrict_sr=on&sort=relevance&t=all)] diff --git a/pywal/export.py b/pywal/export.py index f7b9de8..4c0649f 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -19,29 +19,35 @@ def template(colors, input_file, output_file=None): for match in matches: # Get the color, and the functions associated with it color, _, funcs = match.group(2).partition(".") - #Check that functions are needed for this color + # Check that functions are needed for this color if len(funcs) != 0: - #Build up a string which will be replaced when the color is done processing + # Build up a string which will be replaced when the color is done processing replace_str = color - #The modified color + # The modified color new_color = colors[color] - #Execute each function to be done - for func in filter(None,funcs.split(")")): - ### Get function name and arguments + # Execute each function to be done + for func in filter(None, funcs.split(")")): + # Get function name and arguments func_split = func.split("(") args = [] - if len(func_split) > 1: args = func_split[1].split(",") + if len(func_split) > 1: + args = func_split[1].split(",") fname = func_split[0] - if fname[0] == '.': fname = fname[1:] + if fname[0] == '.': + fname = fname[1:] + if not hasattr(new_color, fname): + logging.error( + "Syntax error in template file '%s' on line '%s'", input_file, i) f = getattr(new_color, fname) # If the function is callable, call it if callable(f): new_color = f(*args) - #add to the string that will replace the function calls with the generated function. - if func[0] != '.': replace_str += "." + # add to the string that will replace the function calls with the generated function. + if func[0] != '.': + replace_str += "." replace_str += func + ")" - #If the color was changed, replace the template with a unique identifier for the new color. + # If the color was changed, replace the template with a unique identifier for the new color. if not new_color is colors[color]: cname = "color" + new_color.strip template_data[i] = line.replace(replace_str, cname) diff --git a/pywal/templates/functest b/pywal/templates/functest deleted file mode 100644 index b82d4fc..0000000 --- a/pywal/templates/functest +++ /dev/null @@ -1 +0,0 @@ -{color0.lighten(10).darken(10).rgb} diff --git a/pywal/util.py b/pywal/util.py index c105a56..74fe1f0 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -36,7 +36,7 @@ class Color: def rgba(self): """Convert a hex color to rgba.""" return "rgba(%s,%s,%s,%s)" % (*hex_to_rgb(self.hex_color), - int(self.alpha_num)/100) + int(self.alpha_num) / 100) @property def alpha(self): @@ -58,17 +58,17 @@ class Color: """Strip '#' from color.""" return self.hex_color[1:] - def lighten(self,percent): + def lighten(self, percent): """Lighten color by percent""" - return Color(lighten_color(self.hex_color,float(re.sub(r'[\D\.]','',percent))/100)) + return Color(lighten_color(self.hex_color, float(re.sub(r'[\D\.]', '', percent)) / 100)) - def darken(self,percent): + def darken(self, percent): """Darken color by percent""" - return Color(darken_color(self.hex_color,float(re.sub(r'[\D\.]','',percent))/100)) + return Color(darken_color(self.hex_color, float(re.sub(r'[\D\.]', '', percent)) / 100)) - def saturate(self,percent): + def saturate(self, percent): """Saturate a color""" - return Color(saturate_color(self.hex_color,float(re.sub(r'[\D\.]','',percent))/100)) + return Color(saturate_color(self.hex_color, float(re.sub(r'[\D\.]', '', percent)) / 100)) def read_file(input_file): @@ -76,6 +76,7 @@ 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: @@ -168,11 +169,11 @@ def blend_color(color, color2): def saturate_color(color, amount): """Saturate a hex color.""" r, g, b = hex_to_rgb(color) - r, g, b = [x/255.0 for x in (r, g, b)] + r, g, b = [x / 255.0 for x in (r, g, b)] h, l, s = colorsys.rgb_to_hls(r, g, b) s = amount r, g, b = colorsys.hls_to_rgb(h, l, s) - r, g, b = [x*255.0 for x in (r, g, b)] + r, g, b = [x * 255.0 for x in (r, g, b)] return rgb_to_hex((int(r), int(g), int(b))) -- cgit v1.2.3 From f06edfa7a2f8aba5bac24710e21389434604c8cd Mon Sep 17 00:00:00 2001 From: Amit Prasad <17amitprasad@gmail.com> Date: Fri, 20 Dec 2019 10:00:38 -0500 Subject: Undo unneeded changes --- pywal/colors.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pywal/colors.py b/pywal/colors.py index 6dc5105..99d346d 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -48,9 +48,6 @@ def colors_to_dict(colors, img): "color13": colors[13], "color14": colors[14], "color15": colors[15] - }, - "modified": { - } } -- cgit v1.2.3 From d4bd389b438df7bc4d3f302d44e23c9bbdaba8ec Mon Sep 17 00:00:00 2001 From: Amit Prasad <17amitprasad@gmail.com> Date: Fri, 20 Dec 2019 11:34:57 -0500 Subject: Cleaned up code for pylint --- pywal/export.py | 76 ++++++++++++++++++++++++++++----------------------------- pywal/util.py | 13 ++++++---- 2 files changed, 46 insertions(+), 43 deletions(-) diff --git a/pywal/export.py b/pywal/export.py index 4c0649f..d9ff16d 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -5,53 +5,53 @@ import logging import os import re -from .settings import CACHE_DIR, MODULE_DIR, CONF_DIR from . import util +from .settings import CACHE_DIR, CONF_DIR, MODULE_DIR 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) - for i in range(len(template_data)): - line = template_data[i] - matches = re.finditer(r"(?<=(? 1: - args = func_split[1].split(",") - fname = func_split[0] - if fname[0] == '.': - fname = fname[1:] - if not hasattr(new_color, fname): - logging.error( - "Syntax error in template file '%s' on line '%s'", input_file, i) - f = getattr(new_color, fname) - - # If the function is callable, call it - if callable(f): - new_color = f(*args) - # add to the string that will replace the function calls with the generated function. - if func[0] != '.': - replace_str += "." - replace_str += func + ")" - # If the color was changed, replace the template with a unique identifier for the new color. - if not new_color is colors[color]: - cname = "color" + new_color.strip - template_data[i] = line.replace(replace_str, cname) - colors[cname] = new_color + if len(funcs) == 0: + continue + # Build up a string which will be replaced with the new color + replace_str = cname + # Color to be modified copied into new one + new_color = util.Color(colors[cname].hex_color) + # Execute each function to be done + for func in filter(None, funcs.split(")")): + # Get function name and arguments + func = func.split("(") + fname = func[0] + if fname[0] == '.': + fname = fname[1:] + if not hasattr(new_color, fname): + logging.error( + "Syntax error in template file '%s' on line '%s'", + input_file, i) + function = getattr(new_color, fname) + + # If the function is callable, call it + if callable(function): + if len(func) > 1: + new_color = function(*func[1].split(",")) + else: + new_color = function() + # string to replace generated colors + if func[0] != '.': + replace_str += "." + replace_str += "(".join(func) + ")" + # If the color was changed, replace with a unique identifier. + if new_color is not colors[cname]: + template_data[i] = l.replace( + replace_str, "color" + new_color.strip) + colors["color" + new_color.strip] = new_color try: template_data = "".join(template_data).format(**colors) except ValueError: diff --git a/pywal/util.py b/pywal/util.py index 74fe1f0..cd6628a 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -5,11 +5,11 @@ import colorsys import json import logging import os +import platform +import re import shutil import subprocess import sys -import platform -import re class Color: @@ -60,15 +60,18 @@ class Color: def lighten(self, percent): """Lighten color by percent""" - return Color(lighten_color(self.hex_color, float(re.sub(r'[\D\.]', '', percent)) / 100)) + percent = float(re.sub(r'[\D\.]', '', str(percent))) + return Color(lighten_color(self.hex_color, percent / 100)) def darken(self, percent): """Darken color by percent""" - return Color(darken_color(self.hex_color, float(re.sub(r'[\D\.]', '', percent)) / 100)) + percent = float(re.sub(r'[\D\.]', '', str(percent))) + return Color(darken_color(self.hex_color, percent / 100)) def saturate(self, percent): """Saturate a color""" - return Color(saturate_color(self.hex_color, float(re.sub(r'[\D\.]', '', percent)) / 100)) + percent = float(re.sub(r'[\D\.]', '', str(percent))) + return Color(saturate_color(self.hex_color, percent / 100)) def read_file(input_file): -- cgit v1.2.3