diff options
| author | Dylan Araps <dylanaraps@users.noreply.github.com> | 2017-06-29 22:52:21 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-06-29 22:52:21 +1000 |
| commit | 27e73e5bba414e30caea0dace5fc5073538359d7 (patch) | |
| tree | d574432dc7a64b50b5e146fec5ff7db526b020f9 /pywal | |
| parent | 06d726a66cec0c5626c31975397ab340349ff39b (diff) | |
| parent | 3283d38e07d4b20ad3a0646d28284fe205fcdc57 (diff) | |
Merge pull request #16 from dylanaraps/json
colors: Store colors in a json format.
Diffstat (limited to 'pywal')
| -rwxr-xr-x | pywal/__main__.py | 3 | ||||
| -rwxr-xr-x | pywal/format_colors.py | 68 | ||||
| -rwxr-xr-x | pywal/gen_colors.py | 32 | ||||
| -rwxr-xr-x | pywal/set_colors.py | 23 | ||||
| -rwxr-xr-x | pywal/util.py | 14 |
5 files changed, 99 insertions, 41 deletions
diff --git a/pywal/__main__.py b/pywal/__main__.py index f9341a5..c5a4bb8 100755 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -90,7 +90,6 @@ def process_args(args): # Create a list of hex colors. colors_plain = gen_colors.get_colors(image, args.q) - colors_plain[8] = set_colors.set_grey(colors_plain) if not args.n: wallpaper.set_wallpaper(image) @@ -101,7 +100,7 @@ def process_args(args): # -f elif args.f: - colors_plain = util.read_file(args.f) + colors_plain = util.read_file_json(args.f) set_colors.send_sequences(colors_plain, args.t) export_colors.export_colors(colors_plain) diff --git a/pywal/format_colors.py b/pywal/format_colors.py index 5d70ba0..1d0439b 100755 --- a/pywal/format_colors.py +++ b/pywal/format_colors.py @@ -6,20 +6,21 @@ from pywal import util def plain(colors): """Convert colors to plain hex.""" - return [f"{color}\n" for color in colors] + return [f"{color}\n" for color in colors["colors"].values()] def shell(colors): """Convert colors to shell variables.""" return [f"color{index}='{color}'\n" - for index, color in enumerate(colors)] + for index, color in enumerate(colors["colors"].values())] def css(colors): """Convert colors to css variables.""" css_colors = [":root {\n"] css_colors.extend([f"\t--color{index}: {color};\n" - for index, color in enumerate(colors)]) + for index, color in + enumerate(colors["colors"].values())]) css_colors.append("}\n") return css_colors @@ -27,7 +28,7 @@ def css(colors): def scss(colors): """Convert colors to scss variables.""" return [f"$color{index}: {color};\n" - for index, color in enumerate(colors)] + for index, color in enumerate(colors["colors"].values())] def putty(colors): @@ -38,7 +39,8 @@ def putty(colors): "[HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions\\Wal]\n", ] putty_colors.extend([f"\"colour{index}\"=\"{rgb(color)}\"\n" - for index, color in enumerate(colors)]) + for index, color in + enumerate(colors["colors"].values())]) return putty_colors @@ -46,33 +48,45 @@ def putty(colors): def xrdb(colors): """Convert colors to xrdb format.""" x_colors = [] - x_colors.append(f"URxvt*foreground: {colors[15]}\n") - x_colors.append(f"XTerm*foreground: {colors[15]}\n") - x_colors.append(f"URxvt*background: {colors[0]}\n") - x_colors.append(f"XTerm*background: {colors[0]}\n") - x_colors.append(f"URxvt*cursorColor: {colors[15]}\n") - x_colors.append(f"XTerm*cursorColor: {colors[15]}\n") + x_colors.append(f"URxvt*foreground: {colors['special']['foreground']}\n") + x_colors.append(f"XTerm*foreground: {colors['special']['foreground']}\n") + x_colors.append(f"URxvt*background: {colors['special']['background']}\n") + x_colors.append(f"XTerm*background: {colors['special']['background']}\n") + x_colors.append(f"URxvt*cursorColor: {colors['special']['cursor']}\n") + x_colors.append(f"XTerm*cursorColor: {colors['special']['cursor']}\n") # Colors 0-15. - x_colors.extend([f"*.color{index}: {color}\n*color{index}: {color}\n" - for index, color in enumerate(colors)]) + x_colors.extend([f"*.color{index}: {color}\n*color{index}: {color}\n" + for index, color in enumerate(colors["colors"].values())]) - x_colors.append(f"*.color66: {colors[0]}\n*color66: {colors[0]}\n") + x_colors.append(f"*.color66: {colors['special']['background']}\n" + f"*color66: {colors['special']['background']}\n") # Rofi colors. - x_colors.append(f"rofi.color-window: {colors[0]}, " - f"{colors[0]}, {colors[10]}\n") - x_colors.append(f"rofi.color-normal: {colors[0]}, " - f"{colors[15]}, {colors[0]}, " - f"{colors[10]}, {colors[0]}\n") - x_colors.append(f"rofi.color-active: {colors[0]}, " - f"{colors[15]}, {colors[0]}, " - f"{colors[10]}, {colors[0]}\n") - x_colors.append(f"rofi.color-urgent: {colors[0]}, " - f"{colors[9]}, {colors[0]}, " - f"{colors[9]}, {colors[15]}\n") + x_colors.append(f"rofi.color-window: " + f"{colors['special']['background']}, " + f"{colors['special']['background']}, " + f"{colors['colors']['color10']}\n") + x_colors.append(f"rofi.color-normal: " + f"{colors['special']['background']}, " + f"{colors['special']['foreground']}, " + f"{colors['special']['background']}, " + f"{colors['colors']['color10']}, " + f"{colors['special']['background']}\n") + x_colors.append(f"rofi.color-active: " + f"{colors['special']['background']}, " + f"{colors['special']['foreground']}, " + f"{colors['special']['background']}, " + f"{colors['colors']['color10']}, " + f"{colors['special']['background']}\n") + x_colors.append(f"rofi.color-urgent: " + f"{colors['special']['background']}, " + f"{colors['colors']['color9']}, " + f"{colors['special']['background']}, " + f"{colors['colors']['color9']}, " + f"{colors['special']['foreground']}\n") # Emacs colors. - x_colors.append(f"emacs*background: {colors[0]}\n") - x_colors.append(f"emacs*foreground: {colors[15]}\n") + x_colors.append(f"emacs*background: {colors['special']['background']}\n") + x_colors.append(f"emacs*foreground: {colors['special']['foreground']}\n") return x_colors diff --git a/pywal/gen_colors.py b/pywal/gen_colors.py index 3f5e16e..bd044c2 100755 --- a/pywal/gen_colors.py +++ b/pywal/gen_colors.py @@ -9,6 +9,7 @@ import shutil import subprocess from pywal.settings import CACHE_DIR, COLOR_COUNT +from pywal import set_colors from pywal import util @@ -96,9 +97,10 @@ def get_colors(img, quiet): # Cache the sequences file. cache_file = pathlib.Path(CACHE_DIR / "schemes" / img.replace("/", "_")) + cache_file = pathlib.Path(cache_file.with_suffix(".json")) if cache_file.is_file(): - colors = util.read_file(cache_file) + colors = util.read_file_json(cache_file) print("colors: Found cached colorscheme.") else: @@ -111,7 +113,7 @@ def get_colors(img, quiet): colors = sort_colors(colors) # Cache the colorscheme. - util.save_file("\n".join(colors), cache_file) + util.save_file_json(colors, cache_file) print("colors: Generated colorscheme") if not quiet: @@ -121,5 +123,27 @@ def get_colors(img, quiet): def sort_colors(colors): - """Sort the generated colors.""" - return colors[:1] + colors[9:] + colors[8:] + """Sort the generated colors and store them in a dict that + we will later save in json format.""" + raw_colors = colors[:1] + colors[9:] + colors[8:] + + # Special colors. + colors_special = {} + colors_special.update({"background": raw_colors[0]}) + colors_special.update({"foreground": raw_colors[15]}) + colors_special.update({"cursor": raw_colors[15]}) + + # Colors 0-15 + colors_hex = {} + [colors_hex.update({f"color{index}": color}) # pylint: disable=W0106 + for index, color in enumerate(raw_colors)] + + # Color 8 + colors_hex["color8"] = set_colors.set_grey(raw_colors) + + # Add the colors to a dict. + colors = {} + colors["special"] = colors_special + colors["colors"] = colors_hex + + return colors diff --git a/pywal/set_colors.py b/pywal/set_colors.py index 05b217c..6ac53e7 100755 --- a/pywal/set_colors.py +++ b/pywal/set_colors.py @@ -37,19 +37,26 @@ def set_grey(colors): def send_sequences(colors, vte): """Send colors to all open terminals.""" - sequences = [set_color(num, color) for num, color in enumerate(colors)] - sequences.append(set_special(10, colors[15])) - sequences.append(set_special(11, colors[0])) - sequences.append(set_special(12, colors[15])) - sequences.append(set_special(13, colors[15])) - sequences.append(set_special(14, colors[0])) + + # Colors 0-15. + sequences = [set_color(num, color) + for num, color in enumerate(colors["colors"].values())] + + # Special colors. + # http://pod.tst.eu/http://cvs.schmorp.de/rxvt-unicode/doc/rxvt.7.pod#XTerm_Operating_System_Commands + # 10 = foreground, 11 = background, 12 = cursor foregound + # 13 = mouse foreground + sequences.append(set_special(10, colors["special"]["foreground"])) + sequences.append(set_special(11, colors["special"]["background"])) + sequences.append(set_special(12, colors["special"]["cursor"])) + sequences.append(set_special(13, colors["special"]["cursor"])) # Set a blank color that isn"t affected by bold highlighting. - sequences.append(set_color(66, colors[0])) + sequences.append(set_color(66, colors["special"]["background"])) # This escape sequence doesn"t work in VTE terminals. if not vte: - sequences.append(set_special(708, colors[0])) + sequences.append(set_special(708, colors["special"]["background"])) # Get a list of terminals. terminals = [f"/dev/pts/{term}" for term in os.listdir("/dev/pts/") diff --git a/pywal/util.py b/pywal/util.py index 67baa6a..69a9d9d 100755 --- a/pywal/util.py +++ b/pywal/util.py @@ -1,6 +1,7 @@ """ Misc helper functions. """ +import json import os import pathlib import subprocess @@ -13,12 +14,25 @@ def read_file(input_file): return colors +def read_file_json(input_file): + """Read colors from a json file.""" + with open(input_file) as json_file: + colors = json.load(json_file) + return colors + + def save_file(colors, export_file): """Write the colors to the file.""" with open(export_file, "w") as file: file.write(colors) +def save_file_json(colors, export_file): + """Write the colors to a json file.""" + with open(export_file, "w") as file: + json.dump(colors, file, indent=4) + + def create_dir(directory): """Alias to create the cache dir.""" pathlib.Path(directory).mkdir(parents=True, exist_ok=True) |
