diff --git a/pywal/__main__.py b/pywal/__main__.py index 430cde2..0aaef25 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -36,6 +36,10 @@ def get_args(args): arg.add_argument("-b", metavar="background", help="Custom background color to use.") + arg.add_argument("--backend", metavar="backend", + help="Which color backend to use.", + const="wal", type=str, nargs="?", default="wal") + arg.add_argument("-c", action="store_true", help="Delete all cached colorschemes.") @@ -120,7 +124,8 @@ def process_args(args): if args.i: image_file = image.get(args.i) - colors_plain = colors.generate(image_file, light=args.l) + colors_plain = colors.generate(img=image_file, light=args.l, + backend=args.backend) if args.f: colors_plain = colors.file(args.f) diff --git a/pywal/backends/__init__.py b/pywal/backends/__init__.py index d1fed5d..90cae0c 100644 --- a/pywal/backends/__init__.py +++ b/pywal/backends/__init__.py @@ -9,8 +9,10 @@ Created by Dylan Araps. """ +from . import colorthief from . import wal __all__ = [ + "colorthief", "wal", ] diff --git a/pywal/backends/colorthief.py b/pywal/backends/colorthief.py new file mode 100644 index 0000000..349822c --- /dev/null +++ b/pywal/backends/colorthief.py @@ -0,0 +1,64 @@ +""" +Generate a colorscheme using ColorThief. +""" +import sys + +from colorthief import ColorThief + +from .. import util +from ..settings import COLOR_COUNT + + +def create_palette(img, colors): + """Create palette.""" + raw_colors = colors[:1] + colors[8:16] + colors[8:-1] + + # Modify colors to make a better scheme. + 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] + + 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 + + +def gen_colors(img, color_count): + """Loop until 16 colors are generated.""" + color_thief = ColorThief(img) + color_cmd = color_thief.get_palette + + for i in range(0, 20, 1): + raw_colors = color_cmd(color_count=color_count + i) + + if len(raw_colors) > 16: + break + + elif i == 19: + print("colors: ColorThief couldn't generate a suitable scheme", + "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)) + + return [util.rgb_to_hex(color) for color in raw_colors] + + +def get(img, color_count=COLOR_COUNT, light=False): + """Get colorscheme.""" + colors = gen_colors(img, color_count) + return create_palette(img, colors) diff --git a/pywal/colors.py b/pywal/colors.py index b7e274d..6123690 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -12,6 +12,7 @@ from .settings import CACHE_DIR, COLOR_COUNT, __cache_version__ def get(backend_type="wal"): """Get backend function name from name.""" return { + "colorthief": backends.colorthief.get, "wal": backends.wal.get, }.get(backend_type, backend_type)