mirror of
https://github.com/dylanaraps/pywal.git
synced 2025-06-20 17:47:44 +02:00
backend: cleanup
This commit is contained in:
parent
37bc93cc03
commit
958426f167
@ -38,7 +38,7 @@ def get_args(args):
|
|||||||
|
|
||||||
arg.add_argument("--backend", metavar="backend",
|
arg.add_argument("--backend", metavar="backend",
|
||||||
help="Which color backend to use.",
|
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",
|
arg.add_argument("-c", action="store_true",
|
||||||
help="Delete all cached colorschemes.")
|
help="Delete all cached colorschemes.")
|
||||||
@ -106,6 +106,10 @@ def process_args(args):
|
|||||||
reload.colors()
|
reload.colors()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
if args.backend == "list_backends":
|
||||||
|
print("Available backends:", colors.list_backends())
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
if args.q:
|
if args.q:
|
||||||
sys.stdout = sys.stderr = open(os.devnull, "w")
|
sys.stdout = sys.stderr = open(os.devnull, "w")
|
||||||
|
|
||||||
|
@ -6,29 +6,26 @@ import sys
|
|||||||
from colorthief import ColorThief
|
from colorthief import ColorThief
|
||||||
|
|
||||||
from .. import util
|
from .. import util
|
||||||
from ..settings import COLOR_COUNT
|
|
||||||
|
|
||||||
|
|
||||||
def gen_colors(img, color_count):
|
def gen_colors(img):
|
||||||
"""Loop until 16 colors are generated."""
|
"""Loop until 16 colors are generated."""
|
||||||
color_thief = ColorThief(img)
|
color_cmd = ColorThief(img).get_palette
|
||||||
color_cmd = color_thief.get_palette
|
|
||||||
|
|
||||||
for i in range(0, 20, 1):
|
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:
|
if len(raw_colors) > 16:
|
||||||
break
|
break
|
||||||
|
|
||||||
elif i == 19:
|
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...")
|
"for the image. Exiting...")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("colors: ColorThief couldn't generate a %s color palette, "
|
print("colors: ColorThief couldn't create a suitable palette, "
|
||||||
"trying a larger palette size %s."
|
"trying a larger palette size", 16 + i)
|
||||||
% (color_count, color_count + i))
|
|
||||||
|
|
||||||
return [util.rgb_to_hex(color) for color in raw_colors]
|
return [util.rgb_to_hex(color) for color in raw_colors]
|
||||||
|
|
||||||
@ -62,7 +59,7 @@ def adjust(img, colors, light):
|
|||||||
return colors
|
return colors
|
||||||
|
|
||||||
|
|
||||||
def get(img, color_count=COLOR_COUNT, light=False):
|
def get(img, light=False):
|
||||||
"""Get colorscheme."""
|
"""Get colorscheme."""
|
||||||
colors = gen_colors(img, color_count)
|
colors = gen_colors(img)
|
||||||
return adjust(img, colors, light)
|
return adjust(img, colors, light)
|
||||||
|
@ -4,14 +4,12 @@ Generate a colorscheme using Colorz.
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from .. import util
|
from .. import util
|
||||||
from ..settings import COLOR_COUNT
|
|
||||||
|
|
||||||
|
|
||||||
def gen_colors(img, color_count):
|
def gen_colors(img):
|
||||||
"""Generate a colorscheme using Colorz."""
|
"""Generate a colorscheme using Colorz."""
|
||||||
flags = ["-n", "6", "--bold", "0", "--no-preview"]
|
colorz = ["colorz", "-n", "6", "--bold", "0", "--no-preview"]
|
||||||
|
return subprocess.check_output([*colorz, img]).splitlines()
|
||||||
return subprocess.check_output(["colorz", *flags, img]).splitlines()
|
|
||||||
|
|
||||||
|
|
||||||
def adjust(img, colors, light):
|
def adjust(img, colors, light):
|
||||||
@ -42,8 +40,8 @@ def adjust(img, colors, light):
|
|||||||
return colors
|
return colors
|
||||||
|
|
||||||
|
|
||||||
def get(img, color_count=COLOR_COUNT, light=False):
|
def get(img, light=False):
|
||||||
"""Get colorscheme."""
|
"""Get colorscheme."""
|
||||||
colors = gen_colors(img, color_count)
|
colors = gen_colors(img)
|
||||||
colors = [color.decode('UTF-8').split()[0] for color in colors]
|
colors = [color.decode('UTF-8').split()[0] for color in colors]
|
||||||
return adjust(img, colors, light)
|
return adjust(img, colors, light)
|
||||||
|
@ -7,7 +7,6 @@ import subprocess
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from .. import util
|
from .. import util
|
||||||
from ..settings import COLOR_COUNT
|
|
||||||
|
|
||||||
|
|
||||||
def imagemagick(color_count, img, magick_command):
|
def imagemagick(color_count, img, magick_command):
|
||||||
@ -32,26 +31,25 @@ def has_im():
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def gen_colors(img, color_count):
|
def gen_colors(img):
|
||||||
"""Format the output from imagemagick into a list
|
"""Format the output from imagemagick into a list
|
||||||
of hex colors."""
|
of hex colors."""
|
||||||
magick_command = has_im()
|
magick_command = has_im()
|
||||||
|
|
||||||
for i in range(0, 20, 1):
|
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:
|
if len(raw_colors) > 16:
|
||||||
break
|
break
|
||||||
|
|
||||||
elif i == 19:
|
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...")
|
"for the image. Exiting...")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("colors: Imagemagick couldn't generate a %s color palette, "
|
print("colors: Imagemagick couldn't generate a suitable palette, "
|
||||||
"trying a larger palette size %s."
|
"trying a larger palette size", 16 + i)
|
||||||
% (color_count, color_count + i))
|
|
||||||
|
|
||||||
return [re.search("#.{6}", str(col)).group(0) for col in raw_colors[1:]]
|
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
|
return colors
|
||||||
|
|
||||||
|
|
||||||
def get(img, color_count=COLOR_COUNT, light=False):
|
def get(img, light=False):
|
||||||
"""Get colorscheme."""
|
"""Get colorscheme."""
|
||||||
colors = gen_colors(img, color_count)
|
colors = gen_colors(img)
|
||||||
return adjust(img, colors, light)
|
return adjust(img, colors, light)
|
||||||
|
@ -6,7 +6,7 @@ import os
|
|||||||
|
|
||||||
from . import backends
|
from . import backends
|
||||||
from . import util
|
from . import util
|
||||||
from .settings import CACHE_DIR, COLOR_COUNT, __cache_version__
|
from .settings import CACHE_DIR, __cache_version__
|
||||||
|
|
||||||
|
|
||||||
def get(backend_type="wal"):
|
def get(backend_type="wal"):
|
||||||
@ -15,14 +15,18 @@ def get(backend_type="wal"):
|
|||||||
"colorthief": backends.colorthief.get,
|
"colorthief": backends.colorthief.get,
|
||||||
"colorz": backends.colorz.get,
|
"colorz": backends.colorz.get,
|
||||||
"wal": backends.wal.get,
|
"wal": backends.wal.get,
|
||||||
}.get(backend_type, backend_type)
|
}.get(backend_type, backends.wal.get)
|
||||||
|
|
||||||
|
|
||||||
def generate(img, cache_dir=CACHE_DIR,
|
def list_backends():
|
||||||
color_count=COLOR_COUNT, light=False,
|
"""List color backends."""
|
||||||
backend="wal"):
|
# TODO: Dynamically generate this.
|
||||||
|
return "colorthief, colorz, wal"
|
||||||
|
|
||||||
|
|
||||||
|
def generate(img, cache_dir=CACHE_DIR, light=False, backend="wal"):
|
||||||
"""Generate a palette."""
|
"""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"
|
color_type = "light" if light else "dark"
|
||||||
cache_file = re.sub("[/|\\|.]", "_", img)
|
cache_file = re.sub("[/|\\|.]", "_", img)
|
||||||
cache_file = os.path.join(cache_dir, "schemes", "%s_%s_%s_%s.json"
|
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:
|
else:
|
||||||
print("wal: Generating a colorscheme...")
|
print("wal: Generating a colorscheme...")
|
||||||
|
|
||||||
backend = get(backend)
|
colors = get(backend)(img, light)
|
||||||
colors = backend(img, color_count, light)
|
|
||||||
|
|
||||||
util.save_file_json(colors, cache_file)
|
util.save_file_json(colors, cache_file)
|
||||||
print("wal: Generation complete.")
|
print("wal: Generation complete.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user