summaryrefslogtreecommitdiff
path: root/pywal/colors.py
blob: b0c6f493f99180e89d111e06b93b973fc8cea01a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Generate a palette using various backends.
"""
import re
import os

from . import backends
from . import util
from .settings import CACHE_DIR, __cache_version__


def get(backend_type="wal"):
    """Get backend function name from name."""
    return {
        "colorthief": backends.colorthief.get,
        "colorz": backends.colorz.get,
        "wal": backends.wal.get,
    }.get(backend_type, backends.wal.get)


def list_backends():
    """List color backends."""
    # TODO: Dynamically generate this.
    return "colorthief, colorz, wal"


def gen(img, light=False, backend="wal", cache_dir=CACHE_DIR):
    """Generate a palette."""
    # 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"
                              % (cache_file, color_type,
                                 backend, __cache_version__))

    if os.path.isfile(cache_file):
        colors = file(cache_file)
        util.Color.alpha_num = colors["alpha"]
        print("colors: Found cached colorscheme.")

    else:
        print("wal: Generating a colorscheme...")

        colors = get(backend)(img, light)

        util.save_file_json(colors, cache_file)
        print("wal: Generation complete.")

    return colors


def file(input_file):
    """Import colorscheme from json file."""
    data = util.read_file_json(input_file)

    if "wallpaper" not in data:
        data["wallpaper"] = "None"

    if "alpha" not in data:
        data["alpha"] = "100"

    return data