From 520b065a30152fb9639418ce0557d45c6a07d9a5 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 31 Mar 2018 11:28:28 +1100 Subject: [PATCH] backend: cleanup --- pywal/backends/colorthief.py | 29 ++++++++----------------- pywal/backends/colorz.py | 20 +++++------------ pywal/backends/wal.py | 42 ++++++++++-------------------------- pywal/colors.py | 18 ++++++++++++++++ 4 files changed, 43 insertions(+), 66 deletions(-) diff --git a/pywal/backends/colorthief.py b/pywal/backends/colorthief.py index a35b91e..837fc66 100644 --- a/pywal/backends/colorthief.py +++ b/pywal/backends/colorthief.py @@ -30,36 +30,25 @@ def gen_colors(img): return [util.rgb_to_hex(color) for color in raw_colors] -def adjust(img, colors, light): +def adjust(colors, light): """Create palette.""" if light: print("colors: Colortheif backend doesn't support light themes.") raw_colors = colors[:1] + colors[8:16] + colors[8:-1] + for color in raw_colors: + color = util.lighten_color(color, 0.25) + raw_colors[0] = util.darken_color(colors[0], 0.80) - raw_colors[15] = util.lighten_color(colors[15], 0.80) - raw_colors[7] = raw_colors[15] + raw_colors[7] = util.lighten_color(colors[15], 0.60) + raw_colors[8] = util.blend_color(colors[0], colors[15]) + raw_colors[15] = raw_colors[7] - colors = {"wallpaper": img, - "alpha": util.Color.alpha_num, - "special": {}, - "colors": {}} - - for i, color in enumerate(raw_colors): - colors["colors"]["color%s" % i] = util.lighten_color(color, 0.25) - - raw_colors[8] = util.blend_color(raw_colors[0], raw_colors[15]) - - colors["colors"]["color0"] = raw_colors[0] - colors["special"]["background"] = raw_colors[0] - colors["special"]["foreground"] = raw_colors[15] - colors["special"]["cursor"] = raw_colors[1] - - return colors + return raw_colors def get(img, light=False): """Get colorscheme.""" colors = gen_colors(img) - return adjust(img, colors, light) + return adjust(colors, light) diff --git a/pywal/backends/colorz.py b/pywal/backends/colorz.py index 39a0d17..44a07d0 100644 --- a/pywal/backends/colorz.py +++ b/pywal/backends/colorz.py @@ -12,36 +12,26 @@ def gen_colors(img): return subprocess.check_output([*colorz, img]).splitlines() -def adjust(img, colors, light): +def adjust(colors, light): """Create palette.""" if light: print("colors: Colorz backend doesn't support light themes.") + # Create list with placeholder values. raw_colors = ["#000000", *colors, "#FFFFFF", "#333333", *colors, "#FFFFFF"] + # Update placeholder values. raw_colors[0] = util.darken_color(colors[0], 0.75) raw_colors[8] = util.darken_color(colors[0], 0.25) raw_colors[7] = util.lighten_color(colors[0], 0.75) raw_colors[15] = raw_colors[7] - colors = {"wallpaper": img, - "alpha": util.Color.alpha_num, - "special": {}, - "colors": {}} - - for i, color in enumerate(raw_colors): - colors["colors"]["color%s" % i] = color - - colors["special"]["background"] = raw_colors[0] - colors["special"]["foreground"] = raw_colors[15] - colors["special"]["cursor"] = raw_colors[1] - - return colors + return raw_colors def get(img, light=False): """Get colorscheme.""" colors = gen_colors(img) colors = [color.decode('UTF-8').split()[0] for color in colors] - return adjust(img, colors, light) + return adjust(colors, light) diff --git a/pywal/backends/wal.py b/pywal/backends/wal.py index d74ed82..af955a4 100644 --- a/pywal/backends/wal.py +++ b/pywal/backends/wal.py @@ -54,54 +54,34 @@ def gen_colors(img): return [re.search("#.{6}", str(col)).group(0) for col in raw_colors[1:]] -def adjust(img, colors, light): +def adjust(colors, light): """Adjust the generated colors and store them in a dict that we will later save in json format.""" raw_colors = colors[:1] + colors[8:16] + colors[8:-1] + # Manually adjust colors. if light: - # Manually adjust colors. - raw_colors[7] = raw_colors[0] - raw_colors[0] = util.lighten_color(raw_colors[15], 0.85) - raw_colors[15] = raw_colors[7] - raw_colors[8] = util.lighten_color(raw_colors[7], 0.25) + for color in raw_colors: + color = util.saturate_color(color, 0.5) + + raw_colors[0] = util.lighten_color(colors[-1], 0.85) + raw_colors[7] = colors[0] + raw_colors[8] = util.darken_color(colors[-1], 0.4) + raw_colors[15] = colors[0] else: # Darken the background color slightly. if raw_colors[0][1] != "0": raw_colors[0] = util.darken_color(raw_colors[0], 0.25) - # Manually adjust colors. raw_colors[7] = util.blend_color(raw_colors[7], "#EEEEEE") raw_colors[8] = util.darken_color(raw_colors[7], 0.30) raw_colors[15] = util.blend_color(raw_colors[15], "#EEEEEE") - colors = {"wallpaper": img, - "alpha": util.Color.alpha_num, - "special": {}, - "colors": {}} - - colors["special"]["background"] = raw_colors[0] - colors["special"]["foreground"] = raw_colors[15] - colors["special"]["cursor"] = raw_colors[15] - - if light: - for i, color in enumerate(raw_colors): - colors["colors"]["color%s" % i] = util.saturate_color(color, 0.5) - - colors["colors"]["color0"] = raw_colors[0] - colors["colors"]["color7"] = raw_colors[15] - colors["colors"]["color8"] = util.darken_color(raw_colors[0], 0.5) - colors["colors"]["color15"] = raw_colors[15] - - else: - for i, color in enumerate(raw_colors): - colors["colors"]["color%s" % i] = color - - return colors + return raw_colors def get(img, light=False): """Get colorscheme.""" colors = gen_colors(img) - return adjust(img, colors, light) + return adjust(colors, light) diff --git a/pywal/colors.py b/pywal/colors.py index b0c6f49..a03dd26 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -24,6 +24,23 @@ def list_backends(): return "colorthief, colorz, wal" +def colors_to_dict(colors, img): + """Convert list of colors to pywal format.""" + scheme = {"wallpaper": img, + "alpha": util.Color.alpha_num, + "special": {}, + "colors": {}} + + for i, color in enumerate(colors): + scheme["colors"]["color%s" % i] = color + + scheme["special"]["background"] = colors[0] + scheme["special"]["foreground"] = colors[15] + scheme["special"]["cursor"] = colors[1] + + return scheme + + def gen(img, light=False, backend="wal", cache_dir=CACHE_DIR): """Generate a palette.""" # home_dylan_img_jpg_backend_1.2.2.json @@ -42,6 +59,7 @@ def gen(img, light=False, backend="wal", cache_dir=CACHE_DIR): print("wal: Generating a colorscheme...") colors = get(backend)(img, light) + colors = colors_to_dict(colors, img) util.save_file_json(colors, cache_file) print("wal: Generation complete.")