summaryrefslogtreecommitdiff
path: root/pywal
diff options
context:
space:
mode:
authorDylan Araps <dylan.araps@gmail.com>2018-03-30 12:05:54 +1100
committerDylan Araps <dylan.araps@gmail.com>2018-03-30 12:05:54 +1100
commit4066d082f12b0989f06b32efc86e8cff582b9a10 (patch)
tree80fcf2ad33586cabf224884c43add9a5a7938324 /pywal
parent42338ea59d4456d632077e33ec5aaa6199d79dba (diff)
backend: initial work to add more backends
Diffstat (limited to 'pywal')
-rw-r--r--pywal/__main__.py2
-rw-r--r--pywal/backends/__init__.py16
-rw-r--r--pywal/backends/wal.py106
-rw-r--r--pywal/colors.py114
4 files changed, 138 insertions, 100 deletions
diff --git a/pywal/__main__.py b/pywal/__main__.py
index a47deb3..430cde2 100644
--- a/pywal/__main__.py
+++ b/pywal/__main__.py
@@ -120,7 +120,7 @@ def process_args(args):
if args.i:
image_file = image.get(args.i)
- colors_plain = colors.get(image_file, light=args.l)
+ colors_plain = colors.generate(image_file, light=args.l)
if args.f:
colors_plain = colors.file(args.f)
diff --git a/pywal/backends/__init__.py b/pywal/backends/__init__.py
new file mode 100644
index 0000000..d1fed5d
--- /dev/null
+++ b/pywal/backends/__init__.py
@@ -0,0 +1,16 @@
+"""
+ '||
+... ... .... ... ... ... ... .... ||
+ ||' || '|. | || || | '' .|| ||
+ || | '|.| ||| ||| .|' || ||
+ ||...' '| | | '|..'|' .||.
+ || .. |
+'''' ''
+Created by Dylan Araps.
+"""
+
+from . import wal
+
+__all__ = [
+ "wal",
+]
diff --git a/pywal/backends/wal.py b/pywal/backends/wal.py
new file mode 100644
index 0000000..14c4bff
--- /dev/null
+++ b/pywal/backends/wal.py
@@ -0,0 +1,106 @@
+"""
+Generate a colorscheme using imagemagick.
+"""
+import re
+import shutil
+import subprocess
+import sys
+
+from .. import util
+from ..settings import COLOR_COUNT
+
+
+def imagemagick(color_count, img, magick_command):
+ """Call Imagemagick to generate a scheme."""
+ flags = ["-resize", "25%", "-colors", str(color_count),
+ "-unique-colors", "txt:-"]
+ img += "[0]"
+
+ return subprocess.check_output([*magick_command, img, *flags]).splitlines()
+
+
+def has_im():
+ """Check to see if the user has im installed."""
+ if shutil.which("magick"):
+ return ["magick", "convert"]
+
+ elif shutil.which("convert"):
+ return ["convert"]
+
+ print("error: imagemagick not found, exiting...\n"
+ "error: wal requires imagemagick to function.")
+ sys.exit(1)
+
+
+def gen_colors(img, color_count):
+ """Format the output from imagemagick into a list
+ of hex colors."""
+ magick_command = has_im()
+
+ for i in range(0, 20, 1):
+ raw_colors = imagemagick(color_count + i, img, magick_command)
+
+ if len(raw_colors) > 16:
+ break
+
+ elif i == 19:
+ print("colors: Imagemagick couldn't generate a suitable scheme",
+ "for the image. Exiting...")
+ sys.exit(1)
+
+ else:
+ print("colors: Imagemagick couldn't generate a %s color palette, "
+ "trying a larger palette size %s."
+ % (color_count, color_count + i))
+
+ return [re.search("#.{6}", str(col)).group(0) for col in raw_colors[1:]]
+
+
+def create_palette(img, colors, light):
+ """Sort 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]
+
+ 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)
+
+ 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
+
+
+def get(img, color_count=COLOR_COUNT, light=False):
+ """Get colorscheme."""
+ colors = gen_colors(img, color_count)
+ return create_palette(img, colors, light)
diff --git a/pywal/colors.py b/pywal/colors.py
index f47ac33..b7e274d 100644
--- a/pywal/colors.py
+++ b/pywal/colors.py
@@ -1,109 +1,25 @@
"""
-Generate a colorscheme using imagemagick.
+Generate a palette using various backends.
"""
-import os
import re
-import shutil
-import subprocess
-import sys
+import os
-from .settings import CACHE_DIR, COLOR_COUNT, __cache_version__
+from . import backends
from . import util
+from .settings import CACHE_DIR, COLOR_COUNT, __cache_version__
-def imagemagick(color_count, img, magick_command):
- """Call Imagemagick to generate a scheme."""
- flags = ["-resize", "25%", "-colors", str(color_count),
- "-unique-colors", "txt:-"]
- img += "[0]"
-
- return subprocess.check_output([*magick_command, img, *flags]).splitlines()
-
-
-def has_im():
- """Check to see if the user has im installed."""
- if shutil.which("magick"):
- return ["magick", "convert"]
-
- elif shutil.which("convert"):
- return ["convert"]
-
- print("error: imagemagick not found, exiting...\n"
- "error: wal requires imagemagick to function.")
- sys.exit(1)
-
-
-def gen_colors(img, color_count):
- """Format the output from imagemagick into a list
- of hex colors."""
- magick_command = has_im()
-
- for i in range(0, 20, 1):
- raw_colors = imagemagick(color_count + i, img, magick_command)
-
- if len(raw_colors) > 16:
- break
-
- elif i == 19:
- print("colors: Imagemagick couldn't generate a suitable scheme",
- "for the image. Exiting...")
- sys.exit(1)
-
- else:
- print("colors: Imagemagick couldn't generate a %s color palette, "
- "trying a larger palette size %s."
- % (color_count, color_count + i))
-
- return [re.search("#.{6}", str(col)).group(0) for col in raw_colors[1:]]
-
-
-def create_palette(img, colors, light):
- """Sort 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]
-
- 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)
-
- 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
+def get(backend_type="wal"):
+ """Get backend function name from name."""
+ return {
+ "wal": backends.wal.get,
+ }.get(backend_type, backend_type)
-def get(img, cache_dir=CACHE_DIR,
- color_count=COLOR_COUNT, light=False):
- """Get the colorscheme."""
+def generate(img, cache_dir=CACHE_DIR,
+ color_count=COLOR_COUNT, light=False,
+ backend="wal"):
+ """Generate a palette."""
# home_dylan_img_jpg_1.2.2.json
color_type = "light" if light else "dark"
cache_file = re.sub("[/|\\|.]", "_", img)
@@ -118,8 +34,8 @@ def get(img, cache_dir=CACHE_DIR,
else:
print("wal: Generating a colorscheme...")
- colors = gen_colors(img, color_count)
- colors = create_palette(img, colors, light)
+ backend = get(backend)
+ colors = backend(img, color_count, light)
util.save_file_json(colors, cache_file)
print("wal: Generation complete.")