backend: cleanup

This commit is contained in:
Dylan Araps 2018-03-31 10:51:23 +11:00
parent 37bc93cc03
commit 958426f167
5 changed files with 36 additions and 36 deletions

View File

@ -38,7 +38,7 @@ def get_args(args):
arg.add_argument("--backend", metavar="backend",
help="Which color backend to use.",
const="wal", type=str, nargs="?", default="wal")
const="list_backends", type=str, nargs="?", default="wal")
arg.add_argument("-c", action="store_true",
help="Delete all cached colorschemes.")
@ -106,6 +106,10 @@ def process_args(args):
reload.colors()
sys.exit(0)
if args.backend == "list_backends":
print("Available backends:", colors.list_backends())
sys.exit(0)
if args.q:
sys.stdout = sys.stderr = open(os.devnull, "w")

View File

@ -6,29 +6,26 @@ import sys
from colorthief import ColorThief
from .. import util
from ..settings import COLOR_COUNT
def gen_colors(img, color_count):
def gen_colors(img):
"""Loop until 16 colors are generated."""
color_thief = ColorThief(img)
color_cmd = color_thief.get_palette
color_cmd = ColorThief(img).get_palette
for i in range(0, 20, 1):
raw_colors = color_cmd(color_count=color_count + i)
raw_colors = color_cmd(color_count=16 + i)
if len(raw_colors) > 16:
break
elif i == 19:
print("colors: ColorThief couldn't generate a suitable scheme",
print("colors: ColorThief couldn't generate a suitable palette",
"for the image. Exiting...")
sys.exit(1)
else:
print("colors: ColorThief couldn't generate a %s color palette, "
"trying a larger palette size %s."
% (color_count, color_count + i))
print("colors: ColorThief couldn't create a suitable palette, "
"trying a larger palette size", 16 + i)
return [util.rgb_to_hex(color) for color in raw_colors]
@ -62,7 +59,7 @@ def adjust(img, colors, light):
return colors
def get(img, color_count=COLOR_COUNT, light=False):
def get(img, light=False):
"""Get colorscheme."""
colors = gen_colors(img, color_count)
colors = gen_colors(img)
return adjust(img, colors, light)

View File

@ -4,14 +4,12 @@ Generate a colorscheme using Colorz.
import subprocess
from .. import util
from ..settings import COLOR_COUNT
def gen_colors(img, color_count):
def gen_colors(img):
"""Generate a colorscheme using Colorz."""
flags = ["-n", "6", "--bold", "0", "--no-preview"]
return subprocess.check_output(["colorz", *flags, img]).splitlines()
colorz = ["colorz", "-n", "6", "--bold", "0", "--no-preview"]
return subprocess.check_output([*colorz, img]).splitlines()
def adjust(img, colors, light):
@ -42,8 +40,8 @@ def adjust(img, colors, light):
return colors
def get(img, color_count=COLOR_COUNT, light=False):
def get(img, light=False):
"""Get colorscheme."""
colors = gen_colors(img, color_count)
colors = gen_colors(img)
colors = [color.decode('UTF-8').split()[0] for color in colors]
return adjust(img, colors, light)

View File

@ -7,7 +7,6 @@ import subprocess
import sys
from .. import util
from ..settings import COLOR_COUNT
def imagemagick(color_count, img, magick_command):
@ -32,26 +31,25 @@ def has_im():
sys.exit(1)
def gen_colors(img, color_count):
def gen_colors(img):
"""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)
raw_colors = imagemagick(16 + i, img, magick_command)
if len(raw_colors) > 16:
break
elif i == 19:
print("colors: Imagemagick couldn't generate a suitable scheme",
print("colors: Imagemagick couldn't generate a suitable palette",
"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))
print("colors: Imagemagick couldn't generate a suitable palette, "
"trying a larger palette size", 16 + i)
return [re.search("#.{6}", str(col)).group(0) for col in raw_colors[1:]]
@ -103,7 +101,7 @@ def adjust(img, colors, light):
return colors
def get(img, color_count=COLOR_COUNT, light=False):
def get(img, light=False):
"""Get colorscheme."""
colors = gen_colors(img, color_count)
colors = gen_colors(img)
return adjust(img, colors, light)

View File

@ -6,7 +6,7 @@ import os
from . import backends
from . import util
from .settings import CACHE_DIR, COLOR_COUNT, __cache_version__
from .settings import CACHE_DIR, __cache_version__
def get(backend_type="wal"):
@ -15,14 +15,18 @@ def get(backend_type="wal"):
"colorthief": backends.colorthief.get,
"colorz": backends.colorz.get,
"wal": backends.wal.get,
}.get(backend_type, backend_type)
}.get(backend_type, backends.wal.get)
def generate(img, cache_dir=CACHE_DIR,
color_count=COLOR_COUNT, light=False,
backend="wal"):
def list_backends():
"""List color backends."""
# TODO: Dynamically generate this.
return "colorthief, colorz, wal"
def generate(img, cache_dir=CACHE_DIR, light=False, backend="wal"):
"""Generate a palette."""
# home_dylan_img_jpg_1.2.2.json
# home_dylan_img_jpg_backend_1.2.2.json
color_type = "light" if light else "dark"
cache_file = re.sub("[/|\\|.]", "_", img)
cache_file = os.path.join(cache_dir, "schemes", "%s_%s_%s_%s.json"
@ -37,8 +41,7 @@ def generate(img, cache_dir=CACHE_DIR,
else:
print("wal: Generating a colorscheme...")
backend = get(backend)
colors = backend(img, color_count, light)
colors = get(backend)(img, light)
util.save_file_json(colors, cache_file)
print("wal: Generation complete.")