efficient vim config
[dotfiles/.git] / .local / lib / python3.9 / site-packages / pywal / colors.py
1 """
2 Generate a palette using various backends.
3 """
4 import logging
5 import os
6 import random
7 import re
8 import sys
9
10 from . import theme
11 from . import util
12 from .settings import CACHE_DIR, MODULE_DIR, __cache_version__
13
14
15 def list_backends():
16     """List color backends."""
17     return [b.name.replace(".py", "") for b in
18             os.scandir(os.path.join(MODULE_DIR, "backends"))
19             if "__" not in b.name]
20
21
22 def colors_to_dict(colors, img):
23     """Convert list of colors to pywal format."""
24     return {
25         "wallpaper": img,
26         "alpha": util.Color.alpha_num,
27
28         "special": {
29             "background": colors[0],
30             "foreground": colors[15],
31             "cursor": colors[15]
32         },
33
34         "colors": {
35             "color0": colors[0],
36             "color1": colors[1],
37             "color2": colors[2],
38             "color3": colors[3],
39             "color4": colors[4],
40             "color5": colors[5],
41             "color6": colors[6],
42             "color7": colors[7],
43             "color8": colors[8],
44             "color9": colors[9],
45             "color10": colors[10],
46             "color11": colors[11],
47             "color12": colors[12],
48             "color13": colors[13],
49             "color14": colors[14],
50             "color15": colors[15]
51         }
52     }
53
54
55 def generic_adjust(colors, light):
56     """Generic color adjustment for themers."""
57     if light:
58         for color in colors:
59             color = util.saturate_color(color, 0.60)
60             color = util.darken_color(color, 0.5)
61
62         colors[0] = util.lighten_color(colors[0], 0.95)
63         colors[7] = util.darken_color(colors[0], 0.75)
64         colors[8] = util.darken_color(colors[0], 0.25)
65         colors[15] = colors[7]
66
67     else:
68         colors[0] = util.darken_color(colors[0], 0.80)
69         colors[7] = util.lighten_color(colors[0], 0.75)
70         colors[8] = util.lighten_color(colors[0], 0.25)
71         colors[15] = colors[7]
72
73     return colors
74
75
76 def saturate_colors(colors, amount):
77     """Saturate all colors."""
78     if amount and float(amount) <= 1.0:
79         for i, _ in enumerate(colors):
80             if i not in [0, 7, 8, 15]:
81                 colors[i] = util.saturate_color(colors[i], float(amount))
82
83     return colors
84
85
86 def cache_fname(img, backend, light, cache_dir, sat=""):
87     """Create the cache file name."""
88     color_type = "light" if light else "dark"
89     file_name = re.sub("[/|\\|.]", "_", img)
90
91     file_parts = [file_name, color_type, backend, sat, __cache_version__]
92     return [cache_dir, "schemes", "%s_%s_%s_%s_%s.json" % (*file_parts,)]
93
94
95 def get_backend(backend):
96     """Figure out which backend to use."""
97     if backend == "random":
98         backends = list_backends()
99         random.shuffle(backends)
100         return backends[0]
101
102     return backend
103
104
105 def palette():
106     """Generate a palette from the colors."""
107     for i in range(0, 16):
108         if i % 8 == 0:
109             print()
110
111         if i > 7:
112             i = "8;5;%s" % i
113
114         print("\033[4%sm%s\033[0m" % (i, " " * (80 // 20)), end="")
115
116     print("\n")
117
118
119 def get(img, light=False, backend="wal", cache_dir=CACHE_DIR, sat=""):
120     """Generate a palette."""
121     # home_dylan_img_jpg_backend_1.2.2.json
122     cache_name = cache_fname(img, backend, light, cache_dir, sat)
123     cache_file = os.path.join(*cache_name)
124
125     if os.path.isfile(cache_file):
126         colors = theme.file(cache_file)
127         colors["alpha"] = util.Color.alpha_num
128         logging.info("Found cached colorscheme.")
129
130     else:
131         logging.info("Generating a colorscheme.")
132         backend = get_backend(backend)
133
134         # Dynamically import the backend we want to use.
135         # This keeps the dependencies "optional".
136         try:
137             __import__("pywal.backends.%s" % backend)
138         except ImportError:
139             __import__("pywal.backends.wal")
140             backend = "wal"
141
142         logging.info("Using %s backend.", backend)
143         backend = sys.modules["pywal.backends.%s" % backend]
144         colors = getattr(backend, "get")(img, light)
145         colors = colors_to_dict(saturate_colors(colors, sat), img)
146
147         util.save_file_json(colors, cache_file)
148         logging.info("Generation complete.")
149
150     return colors
151
152
153 def file(input_file):
154     """Deprecated: symbolic link to --> theme.file"""
155     return theme.file(input_file)