From d0855de1b809656fdc35992c1533cfdebad078e6 Mon Sep 17 00:00:00 2001 From: Todd Sonjiku Date: Thu, 20 May 2021 18:47:20 +0300 Subject: [PATCH] Added command line option --nine, which reverts to generating 9 colors, and alacritty output templates. --- pywal/__main__.py | 7 ++- pywal/backends/colorthief.py | 8 +-- pywal/backends/colorz.py | 8 +-- pywal/backends/haishoku.py | 8 +-- pywal/backends/schemer2.py | 8 +-- pywal/backends/wal.py | 8 +-- pywal/colors.py | 69 +++++++++++++-------- pywal/templates/colors--nodim-alacritty.yml | 53 ++++++++++++++++ pywal/templates/colors-alacritty.yml | 63 +++++++++++++++++++ pywal/theme.py | 36 ++++++++--- 10 files changed, 209 insertions(+), 59 deletions(-) create mode 100644 pywal/templates/colors--nodim-alacritty.yml create mode 100644 pywal/templates/colors-alacritty.yml diff --git a/pywal/__main__.py b/pywal/__main__.py index 91ead11..2686416 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -53,6 +53,9 @@ def get_args(): "flag is used: Go through the images in order " "instead of shuffled.") + arg.add_argument("--nine", action="store_true", + help="Use 9 color output. ") + arg.add_argument("--recursive", action="store_true", help="When pywal is given a directory as input and this " "flag is used: Search for images recursively in " @@ -175,7 +178,7 @@ def parse_args(parser): if args.i: image_file = image.get(args.i, iterative=args.iterative, recursive=args.recursive) - colors_plain = colors.get(image_file, args.l, args.backend, + colors_plain = colors.get(image_file, args.l, args.nine, args.backend, sat=args.saturate) if args.theme: @@ -186,7 +189,7 @@ def parse_args(parser): if args.w: cached_wallpaper = util.read_file(os.path.join(CACHE_DIR, "wal")) - colors_plain = colors.get(cached_wallpaper[0], args.l, args.backend, + colors_plain = colors.get(cached_wallpaper[0], args.l, args.nine, args.backend, sat=args.saturate) if args.b: diff --git a/pywal/backends/colorthief.py b/pywal/backends/colorthief.py index 1189b51..9675564 100644 --- a/pywal/backends/colorthief.py +++ b/pywal/backends/colorthief.py @@ -37,15 +37,15 @@ def gen_colors(img): return [util.rgb_to_hex(color) for color in raw_colors] -def adjust(cols, light): +def adjust(cols, light, nine): """Create palette.""" cols.sort(key=util.rgb_to_yiq) raw_colors = [*cols, *cols] - return colors.generic_adjust(raw_colors, light) + return colors.generic_adjust(raw_colors, light, nine) -def get(img, light=False): +def get(img, light=False, nine=False): """Get colorscheme.""" cols = gen_colors(img) - return adjust(cols, light) + return adjust(cols, light, nine) diff --git a/pywal/backends/colorz.py b/pywal/backends/colorz.py index b789a3a..3ffec36 100644 --- a/pywal/backends/colorz.py +++ b/pywal/backends/colorz.py @@ -23,15 +23,15 @@ def gen_colors(img): return [util.rgb_to_hex([*color[0]]) for color in raw_colors] -def adjust(cols, light): +def adjust(cols, light, nine): """Create palette.""" raw_colors = [cols[0], *cols, "#FFFFFF", "#000000", *cols, "#FFFFFF"] - return colors.generic_adjust(raw_colors, light) + return colors.generic_adjust(raw_colors, light, nine) -def get(img, light=False): +def get(img, light=False, nine=False): """Get colorscheme.""" cols = gen_colors(img) @@ -40,4 +40,4 @@ def get(img, light=False): logging.error("Try another backend or another image. (wal --backend)") sys.exit(1) - return adjust(cols, light) + return adjust(cols, light, nine) diff --git a/pywal/backends/haishoku.py b/pywal/backends/haishoku.py index 00025b2..ae8dd18 100644 --- a/pywal/backends/haishoku.py +++ b/pywal/backends/haishoku.py @@ -22,16 +22,16 @@ def gen_colors(img): return [util.rgb_to_hex(col[1]) for col in palette] -def adjust(cols, light): +def adjust(cols, light, nine): """Create palette.""" cols.sort(key=util.rgb_to_yiq) raw_colors = [*cols, *cols] raw_colors[0] = util.lighten_color(cols[0], 0.40) - return colors.generic_adjust(raw_colors, light) + return colors.generic_adjust(raw_colors, light, nine) -def get(img, light=False): +def get(img, light=False, nine=False): """Get colorscheme.""" cols = gen_colors(img) - return adjust(cols, light) + return adjust(cols, light, nine) diff --git a/pywal/backends/schemer2.py b/pywal/backends/schemer2.py index adae650..6fdd89f 100644 --- a/pywal/backends/schemer2.py +++ b/pywal/backends/schemer2.py @@ -16,15 +16,15 @@ def gen_colors(img): return subprocess.check_output([*cmd, img]).splitlines() -def adjust(cols, light): +def adjust(cols, light, nine): """Create palette.""" cols.sort(key=util.rgb_to_yiq) raw_colors = [*cols[8:], *cols[8:]] - return colors.generic_adjust(raw_colors, light) + return colors.generic_adjust(raw_colors, light, nine) -def get(img, light=False): +def get(img, light=False, nine=False): """Get colorscheme.""" if not shutil.which("schemer2"): logging.error("Schemer2 wasn't found on your system.") @@ -32,4 +32,4 @@ def get(img, light=False): sys.exit(1) cols = [col.decode('UTF-8') for col in gen_colors(img)] - return adjust(cols, light) + return adjust(cols, light, nine) diff --git a/pywal/backends/wal.py b/pywal/backends/wal.py index 12b2158..fb1e039 100644 --- a/pywal/backends/wal.py +++ b/pywal/backends/wal.py @@ -55,15 +55,15 @@ def gen_colors(img): return [re.search("#.{6}", str(col)).group(0) for col in raw_colors[1:]] -def adjust(cols, light): +def adjust(cols, light, nine): """Adjust the generated colors and store them in a dict that we will later save in json format.""" raw_colors = cols[:1] + cols[8:16] + cols[8:-1] - return colors.generic_adjust(raw_colors, light) + return colors.generic_adjust(raw_colors, light, nine) -def get(img, light=False): +def get(img, light=False, nine=False): """Get colorscheme.""" colors = gen_colors(img) - return adjust(colors, light) + return adjust(colors, light, nine) diff --git a/pywal/colors.py b/pywal/colors.py index 7d6ab3e..8688b4b 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -60,37 +60,51 @@ def colors_to_dict(colors, img): } -def generic_adjust(colors, light): +def generic_adjust(colors, light, nine): """Generic color adjustment for themers.""" if light: for color in colors: color = util.saturate_color(color, 0.60) color = util.darken_color(color, 0.5) + + if nine: + colors[0] = util.lighten_color(colors[0], 0.95) + colors[7] = util.darken_color(colors[0], 0.75) + colors[8] = util.darken_color(colors[0], 0.25) + colors[15] = colors[7] + else: - colors[0] = util.lighten_color(colors[0], 0.75) - colors[7] = util.darken_color(colors[0], 0.50) - colors[8] = util.darken_color(colors[0], 0.25) - colors[1] = util.darken_color(colors[1], 0.25) - colors[2] = util.darken_color(colors[2], 0.25) - colors[3] = util.darken_color(colors[3], 0.25) - colors[4] = util.darken_color(colors[4], 0.25) - colors[5] = util.darken_color(colors[5], 0.25) - colors[6] = util.darken_color(colors[6], 0.25) - colors[15] = util.darken_color(colors[0], 0.75) + colors[0] = util.lighten_color(colors[0], 0.75) + colors[7] = util.darken_color(colors[0], 0.50) + colors[8] = util.darken_color(colors[0], 0.25) + colors[1] = util.darken_color(colors[1], 0.25) + colors[2] = util.darken_color(colors[2], 0.25) + colors[3] = util.darken_color(colors[3], 0.25) + colors[4] = util.darken_color(colors[4], 0.25) + colors[5] = util.darken_color(colors[5], 0.25) + colors[6] = util.darken_color(colors[6], 0.25) + colors[15] = util.darken_color(colors[0], 0.75) else: + if nine: + colors[0] = util.darken_color(colors[0], 0.80) + colors[7] = util.lighten_color(colors[0], 0.75) + colors[8] = util.lighten_color(colors[0], 0.25) + colors[15] = colors[7] - colors[0] = util.darken_color(colors[0], 0.75) - colors[7] = util.lighten_color(colors[0], 0.50) - colors[8] = util.lighten_color(colors[0], 0.25) - colors[1] = util.darken_color(colors[1], 0.25) - colors[2] = util.darken_color(colors[2], 0.25) - colors[3] = util.darken_color(colors[3], 0.25) - colors[4] = util.darken_color(colors[4], 0.25) - colors[5] = util.darken_color(colors[5], 0.25) - colors[6] = util.darken_color(colors[6], 0.25) - colors[15] = util.lighten_color(colors[0], 0.75) + else: + + colors[0] = util.darken_color(colors[0], 0.75) + colors[7] = util.lighten_color(colors[0], 0.50) + colors[8] = util.lighten_color(colors[0], 0.25) + colors[1] = util.darken_color(colors[1], 0.25) + colors[2] = util.darken_color(colors[2], 0.25) + colors[3] = util.darken_color(colors[3], 0.25) + colors[4] = util.darken_color(colors[4], 0.25) + colors[5] = util.darken_color(colors[5], 0.25) + colors[6] = util.darken_color(colors[6], 0.25) + colors[15] = util.lighten_color(colors[0], 0.75) return colors @@ -106,15 +120,16 @@ def saturate_colors(colors, amount): return colors -def cache_fname(img, backend, light, cache_dir, sat=""): +def cache_fname(img, backend, nine, light, cache_dir, sat=""): """Create the cache file name.""" color_type = "light" if light else "dark" + color_num = "9" if nine else "16" file_name = re.sub("[/|\\|.]", "_", img) file_size = os.path.getsize(img) - file_parts = [file_name, color_type, backend, + file_parts = [file_name, color_num, color_type, backend, sat, file_size, __cache_version__] - return [cache_dir, "schemes", "%s_%s_%s_%s_%s_%s.json" % (*file_parts,)] + return [cache_dir, "schemes", "%s_%s_%s_%s_%s_%s_%s.json" % (*file_parts,)] def get_backend(backend): @@ -141,10 +156,10 @@ def palette(): print("\n") -def get(img, light=False, backend="wal", cache_dir=CACHE_DIR, sat=""): +def get(img, light=False, nine=False, backend="wal", cache_dir=CACHE_DIR, sat=""): """Generate a palette.""" # home_dylan_img_jpg_backend_1.2.2.json - cache_name = cache_fname(img, backend, light, cache_dir, sat) + cache_name = cache_fname(img, backend, nine, light, cache_dir, sat) cache_file = os.path.join(*cache_name) if os.path.isfile(cache_file): @@ -166,7 +181,7 @@ def get(img, light=False, backend="wal", cache_dir=CACHE_DIR, sat=""): logging.info("Using %s backend.", backend) backend = sys.modules["pywal.backends.%s" % backend] - colors = getattr(backend, "get")(img, light) + colors = getattr(backend, "get")(img, light, nine) colors = colors_to_dict(saturate_colors(colors, sat), img) util.save_file_json(colors, cache_file) diff --git a/pywal/templates/colors--nodim-alacritty.yml b/pywal/templates/colors--nodim-alacritty.yml new file mode 100644 index 0000000..a6990e1 --- /dev/null +++ b/pywal/templates/colors--nodim-alacritty.yml @@ -0,0 +1,53 @@ +colors: + + primary: + background: "{background}" + foreground: "{foreground}" + + cursor: + text: CellBackground + cursor: CellForeground + + vi_mode_cursor: + text: CellBackground + cursor: CellForeground + + search: + matches: + foreground: '{color0}' + background: '{color15}' + focused_match: + foreground: CellBackground + background: CellForeground + + bar: + foreground: '{color8}' + background: '{color7}' + + line_indicator: + foreground: None + background: None + + selection: + text: CellBackground + background: CellForeground + + normal: + black: "{color0}" + red: "{color1}" + green: "{color2}" + yellow: "{color3}" + blue: "{color4}" + magenta: "{color5}" + cyan: "{color6}" + white: "{color7}" + + bright: + black: "{color8}" + red: "{color9}" + green: "{color10}" + yellow: "{color11}" + blue: "{color12}" + magenta: "{color13}" + cyan: "{color14}" + white: "{color15}" diff --git a/pywal/templates/colors-alacritty.yml b/pywal/templates/colors-alacritty.yml new file mode 100644 index 0000000..febe33d --- /dev/null +++ b/pywal/templates/colors-alacritty.yml @@ -0,0 +1,63 @@ +colors: + + primary: + background: "{background}" + foreground: "{foreground}" + + cursor: + text: CellBackground + cursor: CellForeground + + vi_mode_cursor: + text: CellBackground + cursor: CellForeground + + search: + matches: + foreground: '{color0}' + background: '{color15}' + focused_match: + foreground: CellBackground + background: CellForeground + + bar: + foreground: '{color8}' + background: '{color7}' + + line_indicator: + foreground: None + background: None + + selection: + text: CellBackground + background: CellForeground + + normal: + black: "{color0}" + red: "{color1}" + green: "{color2}" + yellow: "{color3}" + blue: "{color4}" + magenta: "{color5}" + cyan: "{color6}" + white: "{color7}" + + bright: + black: "{color8}" + red: "{color9}" + green: "{color10}" + yellow: "{color11}" + blue: "{color12}" + magenta: "{color13}" + cyan: "{color14}" + white: "{color15}" + + dim: + black: "{color15}" + red: "{color9}" + green: "{color13}" + yellow: "{color11}" + blue: "{color12}" + magenta: "{color10}" + cyan: "{color14}" + white: "{color8}" diff --git a/pywal/theme.py b/pywal/theme.py index 9dc4f13..feddf4b 100644 --- a/pywal/theme.py +++ b/pywal/theme.py @@ -16,6 +16,10 @@ def list_out(): for theme in list_themes()] ligh_themes = [theme.name.replace(".json", "") for theme in list_themes(dark=False)] + dark9_themes = [theme.name.replace(".json", "") + for theme in list_themes(nine=True)] + ligh9_themes = [theme.name.replace(".json", "") + for theme in list_themes(dark=False,nine=True)] user_themes = [theme.name.replace(".json", "") for theme in list_themes_user()] @@ -33,8 +37,14 @@ def list_out(): 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))) + print("\033[1;32mDark9 Themes\033[0m:") + print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t + for t in sorted(dark9_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))) + print("\033[1;32mLight9 Themes\033[0m:") print(" -", "\n - ".join(t + " (last used)" if t == last_used_theme else t for t in sorted(ligh_themes))) @@ -45,17 +55,20 @@ def list_out(): print(" - random_user (select a random user theme)") -def list_themes(dark=True): +def list_themes(dark=True, nine=False): """List all installed theme files.""" dark = "dark" if dark else "light" - themes = os.scandir(os.path.join(MODULE_DIR, "colorschemes", dark)) + nine = "9" if nine else "16" + themes = os.scandir(os.path.join(MODULE_DIR, "colorschemes", dark, nine)) return [t for t in themes if os.path.isfile(t.path)] def list_themes_user(): """List user theme files.""" - themes = [*os.scandir(os.path.join(CONF_DIR, "colorschemes/dark/")), - *os.scandir(os.path.join(CONF_DIR, "colorschemes/light/"))] + themes = [*os.scandir(os.path.join(CONF_DIR, "colorschemes/dark/9")), + *os.scandir(os.path.join(CONF_DIR, "colorschemes/dark/16")), + *os.scandir(os.path.join(CONF_DIR, "colorschemes/light/9")), + *os.scandir(os.path.join(CONF_DIR, "colorschemes/light/16"))] return [t for t in themes if os.path.isfile(t.path)] @@ -91,7 +104,7 @@ def parse(theme_file): return data -def get_random_theme(dark=True): +def get_random_theme(dark=True, nine=False): """Get a random theme file.""" themes = [theme.path for theme in list_themes(dark)] random.shuffle(themes) @@ -105,16 +118,17 @@ def get_random_theme_user(): return themes[0] -def file(input_file, light=False): +def file(input_file, light=False, nine=False): """Import colorscheme from json file.""" util.create_dir(os.path.join(CONF_DIR, "colorschemes/light/")) util.create_dir(os.path.join(CONF_DIR, "colorschemes/dark/")) theme_name = ".".join((input_file, "json")) bri = "light" if light else "dark" + full16 = "9" if nine else "16" - user_theme_file = os.path.join(CONF_DIR, "colorschemes", bri, theme_name) - theme_file = os.path.join(MODULE_DIR, "colorschemes", bri, theme_name) + user_theme_file = os.path.join(CONF_DIR, "colorschemes", full16, bri, theme_name) + theme_file = os.path.join(MODULE_DIR, "colorschemes", full16, bri, theme_name) # Find the theme file. if input_file in ("random", "random_dark"): @@ -146,9 +160,11 @@ def file(input_file, light=False): sys.exit(1) -def save(colors, theme_name, light=False): +def save(colors, theme_name, light=False, nine=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) + "9" if nine else "16", + "light" if light else "dark", + theme_file) util.save_file_json(colors, theme_path)