From a75698ea8171e9bc80c6faf5c5d294d6b6dec31e Mon Sep 17 00:00:00 2001 From: Noah Yoshida Date: Tue, 29 May 2018 20:13:10 -0700 Subject: [PATCH 01/17] vscode template json file added --- pywal/export.py | 1 + pywal/templates/colors-vscode.json | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 pywal/templates/colors-vscode.json diff --git a/pywal/export.py b/pywal/export.py index 993e528..c67cca0 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -44,6 +44,7 @@ def get_export_type(export_type): "shell": "colors.sh", "sway": "colors-sway", "tty": "colors-tty.sh", + "vscode": "colors-vscode.json", "xresources": "colors.Xresources", "yaml": "colors.yml", }.get(export_type, export_type) diff --git a/pywal/templates/colors-vscode.json b/pywal/templates/colors-vscode.json new file mode 100644 index 0000000..5138994 --- /dev/null +++ b/pywal/templates/colors-vscode.json @@ -0,0 +1,16 @@ +{ + "editor.tokenColorCustomizations": { + "functions": "{color1}", + "keywords": "{color2}", + "numbers": "{color3}", + "strings": "{color4}", + "types": "{color5}", + "variables": "{color6}", + "comments": "{color7}" + }, + "workbench.colorCustomizations": { + "editor.background": "{color0}" + } +} + + From 6afcb2c5a2cd5e935f75d5f34f52a6e2e04c8d33 Mon Sep 17 00:00:00 2001 From: Noah Yoshida Date: Tue, 29 May 2018 20:13:10 -0700 Subject: [PATCH 02/17] vscode template json file added --- pywal/export.py | 1 + pywal/templates/colors-vscode.json | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 pywal/templates/colors-vscode.json diff --git a/pywal/export.py b/pywal/export.py index 993e528..c67cca0 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -44,6 +44,7 @@ def get_export_type(export_type): "shell": "colors.sh", "sway": "colors-sway", "tty": "colors-tty.sh", + "vscode": "colors-vscode.json", "xresources": "colors.Xresources", "yaml": "colors.yml", }.get(export_type, export_type) diff --git a/pywal/templates/colors-vscode.json b/pywal/templates/colors-vscode.json new file mode 100644 index 0000000..75c0a2f --- /dev/null +++ b/pywal/templates/colors-vscode.json @@ -0,0 +1,16 @@ +{{ + "editor.tokenColorCustomizations": {{ + "functions": "{color1}", + "keywords": "{color2}", + "numbers": "{color3}", + "strings": "{color4}", + "types": "{color5}", + "variables": "{color6}", + "comments": "{color7}" + }}, + "workbench.colorCustomizations": {{ + "editor.background": "{color0}" + }} +}} + + From 788533e5c20132c727e2f287dce6ada64ebd5872 Mon Sep 17 00:00:00 2001 From: Austin Schey Date: Sun, 3 Mar 2019 11:13:44 -0600 Subject: [PATCH 03/17] added features for saving user themes and loading random themes --- pywal/__main__.py | 10 +++++++++- pywal/theme.py | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 2e50ca9..15b7228 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -45,7 +45,7 @@ def get_args(): arg.add_argument("--theme", "-f", metavar="/path/to/file or theme_name", help="Which colorscheme file to use. \ - Use 'wal --theme' to list builtin themes.", + Use 'wal --theme' to list builtin and user themes.", const="list_themes", nargs="?") arg.add_argument("--iterative", action="store_true", @@ -77,6 +77,11 @@ def get_args(): arg.add_argument("-o", metavar="\"script_name\"", action="append", help="External script to run after \"wal\".") + arg.add_argument("-p", metavar="\"theme_name\"", + help="permanently save theme to " + "$XDG_CONFIG_HOME/wal/colorschemes with " + "the specified name") + arg.add_argument("-q", action="store_true", help="Quiet mode, don\'t print anything.") @@ -177,6 +182,9 @@ def parse_args(parser): if not args.n: wallpaper.change(colors_plain["wallpaper"]) + if args.p: + theme.save(colors_plain, args.p, args.l) + sequences.send(colors_plain, to_send=not args.s, vte_fix=args.vte) if sys.stdout.isatty(): diff --git a/pywal/theme.py b/pywal/theme.py index fcd1dd1..62695b5 100644 --- a/pywal/theme.py +++ b/pywal/theme.py @@ -33,6 +33,7 @@ def list_out(): print(" - random (select a random dark theme)") print(" - random_dark (select a random dark theme)") print(" - random_light (select a random light theme)") + print(" - random_user (select a random user theme)") def list_themes(dark=True): @@ -88,6 +89,13 @@ def get_random_theme(dark=True): return themes[0] +def get_random_theme_user(): + """Get a random theme file from user theme directories.""" + themes = [theme.path for theme in list_themes_user()] + random.shuffle(themes) + return themes[0] + + def file(input_file, light=False): """Import colorscheme from json file.""" util.create_dir(os.path.join(CONF_DIR, "colorschemes/light/")) @@ -106,6 +114,9 @@ def file(input_file, light=False): elif input_file == "random_light": theme_file = get_random_theme(light) + elif input_file == "random_user": + theme_file = get_random_theme_user() + elif os.path.isfile(user_theme_file): theme_file = user_theme_file @@ -122,3 +133,11 @@ def file(input_file, light=False): logging.error("Try adding '-l' to set light themes.") logging.error("Try removing '-l' to set dark themes.") sys.exit(1) + + +def save(colors, theme_name, light=False): + """Save colors to a theme file.""" + theme_file = theme_name + ".json" + theme_path = os.path.join(CONF_DIR, "colorschemes", + "light" if light else "dark", theme_file) + util.save_file_json(colors, theme_path) From 3bc9f104518c4b480591c16404a4c04ca4801a7a Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Tue, 1 Oct 2019 10:37:07 +0200 Subject: [PATCH 04/17] Save used theme in CACHE_DIR --- pywal/theme.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pywal/theme.py b/pywal/theme.py index fcd1dd1..423a4c5 100644 --- a/pywal/theme.py +++ b/pywal/theme.py @@ -6,7 +6,7 @@ import os import random import sys -from .settings import CONF_DIR, MODULE_DIR +from .settings import CACHE_DIR, CONF_DIR, MODULE_DIR from . import util @@ -116,6 +116,7 @@ def file(input_file, light=False): if os.path.isfile(theme_file): logging.info("Set theme to \033[1;37m%s\033[0m.", os.path.basename(theme_file)) + util.save_file(os.path.basename(theme_file), os.path.join(CACHE_DIR, "theme")) return parse(theme_file) logging.error("No %s colorscheme file found.", bri) From 3476989e6bd7a0526ff4dc4d592c8327a4c64f50 Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Tue, 1 Oct 2019 10:58:39 +0200 Subject: [PATCH 05/17] Print (last used) next to last used theme --- pywal/theme.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pywal/theme.py b/pywal/theme.py index 423a4c5..5c973da 100644 --- a/pywal/theme.py +++ b/pywal/theme.py @@ -19,12 +19,16 @@ def list_out(): user_themes = [theme.name.replace(".json", "") for theme in list_themes_user()] + last_used_theme = util.read_file(os.path.join(CACHE_DIR, + "last_used_theme"))[0].replace(".json", "") + if user_themes: print("\033[1;32mUser Themes\033[0m:") print(" -", "\n - ".join(sorted(user_themes))) print("\033[1;32mDark Themes\033[0m:") print(" -", "\n - ".join(sorted(dark_themes))) + print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t for t in sorted(dark_themes))) print("\033[1;32mLight Themes\033[0m:") print(" -", "\n - ".join(sorted(ligh_themes))) @@ -116,7 +120,8 @@ def file(input_file, light=False): if os.path.isfile(theme_file): logging.info("Set theme to \033[1;37m%s\033[0m.", os.path.basename(theme_file)) - util.save_file(os.path.basename(theme_file), os.path.join(CACHE_DIR, "theme")) + util.save_file(os.path.basename(theme_file), os.path.join(CACHE_DIR, + "last_used_theme")) return parse(theme_file) logging.error("No %s colorscheme file found.", bri) From fe700cb77aab6df55de05704082576b19d7652b5 Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Tue, 1 Oct 2019 11:00:42 +0200 Subject: [PATCH 06/17] Do for all types of themes (dark, light, user) --- pywal/theme.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pywal/theme.py b/pywal/theme.py index 5c973da..04e4b2b 100644 --- a/pywal/theme.py +++ b/pywal/theme.py @@ -24,14 +24,16 @@ def list_out(): if user_themes: print("\033[1;32mUser Themes\033[0m:") - print(" -", "\n - ".join(sorted(user_themes))) + print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t + for t in sorted(user_themes))) print("\033[1;32mDark Themes\033[0m:") - print(" -", "\n - ".join(sorted(dark_themes))) - print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t for t in sorted(dark_themes))) + print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t + for t in sorted(dark_themes))) print("\033[1;32mLight Themes\033[0m:") - print(" -", "\n - ".join(sorted(ligh_themes))) + print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t + for t in sorted(ligh_themes))) print("\033[1;32mExtra\033[0m:") print(" - random (select a random dark theme)") From 0c32f4e9a920938120297a397c33ca3c655f697f Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Tue, 1 Oct 2019 11:18:37 +0200 Subject: [PATCH 07/17] Fix pylint --- pywal/theme.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pywal/theme.py b/pywal/theme.py index 04e4b2b..c053f18 100644 --- a/pywal/theme.py +++ b/pywal/theme.py @@ -19,21 +19,22 @@ def list_out(): user_themes = [theme.name.replace(".json", "") for theme in list_themes_user()] - last_used_theme = util.read_file(os.path.join(CACHE_DIR, - "last_used_theme"))[0].replace(".json", "") + + last_used_theme = util.read_file(os.path.join( + CACHE_DIR, "last_used_theme"))[0].replace(".json", "") if user_themes: print("\033[1;32mUser Themes\033[0m:") print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t - for t in sorted(user_themes))) + for t in sorted(user_themes))) print("\033[1;32mDark Themes\033[0m:") print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t - for t in sorted(dark_themes))) + for t in sorted(dark_themes))) print("\033[1;32mLight Themes\033[0m:") print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t - for t in sorted(ligh_themes))) + for t in sorted(ligh_themes))) print("\033[1;32mExtra\033[0m:") print(" - random (select a random dark theme)") @@ -122,8 +123,8 @@ def file(input_file, light=False): if os.path.isfile(theme_file): logging.info("Set theme to \033[1;37m%s\033[0m.", os.path.basename(theme_file)) - util.save_file(os.path.basename(theme_file), os.path.join(CACHE_DIR, - "last_used_theme")) + util.save_file(os.path.basename(theme_file), + os.path.join(CACHE_DIR, "last_used_theme")) return parse(theme_file) logging.error("No %s colorscheme file found.", bri) From b6a11f6338daabd33ffbfadc3a4f15e7080214eb Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Tue, 1 Oct 2019 11:54:42 +0200 Subject: [PATCH 08/17] Fix line length for pylint --- pywal/theme.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pywal/theme.py b/pywal/theme.py index c053f18..2b14600 100644 --- a/pywal/theme.py +++ b/pywal/theme.py @@ -19,14 +19,13 @@ def list_out(): user_themes = [theme.name.replace(".json", "") for theme in list_themes_user()] - last_used_theme = util.read_file(os.path.join( CACHE_DIR, "last_used_theme"))[0].replace(".json", "") if user_themes: print("\033[1;32mUser Themes\033[0m:") - print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t - for t in sorted(user_themes))) + print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme + else t for t in sorted(user_themes))) print("\033[1;32mDark Themes\033[0m:") print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t From 4cc2dda585a6e7f6fa8f801c2be42064204c71d8 Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Tue, 1 Oct 2019 16:54:53 +0200 Subject: [PATCH 09/17] Fix FileNotFoundError if theme has never been set --- pywal/theme.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pywal/theme.py b/pywal/theme.py index 2b14600..706ed31 100644 --- a/pywal/theme.py +++ b/pywal/theme.py @@ -19,8 +19,11 @@ def list_out(): user_themes = [theme.name.replace(".json", "") for theme in list_themes_user()] - last_used_theme = util.read_file(os.path.join( - CACHE_DIR, "last_used_theme"))[0].replace(".json", "") + try: + last_used_theme = util.read_file(os.path.join( + CACHE_DIR, "last_used_theme"))[0].replace(".json", "") + except FileNotFoundError: + last_used_theme = "" if user_themes: print("\033[1;32mUser Themes\033[0m:") 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: [PATCH 10/17] Started working on matching functions --- pywal/export.py | 26 ++++++++++++++++++++++++++ pywal/util.py | 5 +++++ 2 files changed, 31 insertions(+) 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: [PATCH 11/17] 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: 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: [PATCH 12/17] 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. 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: [PATCH 13/17] 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. 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: [PATCH 14/17] 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} 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: [PATCH 15/17] 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))) 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: [PATCH 16/17] 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": { - } } 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: [PATCH 17/17] Cleaned up code for pylint --- pywal/export.py | 74 ++++++++++++++++++++++++------------------------- pywal/util.py | 13 +++++---- 2 files changed, 45 insertions(+), 42 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 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(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 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):