-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcolors.py
240 lines (201 loc) · 7.83 KB
/
colors.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""
Generate a palette using various backends.
"""
import logging
import os
import random
import re
import sys
from . import theme
from . import util
from .settings import CACHE_DIR, MODULE_DIR, __cache_version__
def list_backends():
"""List color backends."""
return [
b.name.replace(".py", "")
for b in os.scandir(os.path.join(MODULE_DIR, "backends"))
if "__" not in b.name
]
def normalize_img_path(img: str):
"""Normalizes the image path for output."""
if os.name == "nt":
# On Windows, the JSON.dump ends up outputting un-escaped backslash breaking
# the ability to read colors.json. Windows supports forward slash, so we can
# use that for now
return img.replace("\\", "/")
return img
def colors_to_dict(colors, img):
"""Convert list of colors to pywal format."""
return {
"checksum": util.get_img_checksum(img),
"wallpaper": normalize_img_path(img),
"alpha": util.Color.alpha_num,
"special": {
"background": colors[0],
"foreground": colors[15],
"cursor": colors[15],
},
"colors": {
"color0": colors[0],
"color1": colors[1],
"color2": colors[2],
"color3": colors[3],
"color4": colors[4],
"color5": colors[5],
"color6": colors[6],
"color7": colors[7],
"color8": colors[8],
"color9": colors[9],
"color10": colors[10],
"color11": colors[11],
"color12": colors[12],
"color13": colors[13],
"color14": colors[14],
"color15": colors[15],
},
}
def generic_adjust(colors, light, cols16):
"""Generic color adjustment for themers."""
if light:
for color in colors:
color = util.saturate_color(color, 0.60)
color = util.darken_color(color, 0.5)
colors[0] = util.lighten_color(colors[0], 0.95)
if cols16:
colors[7] = util.darken_color(colors[0], 0.50)
colors[8] = util.darken_color(colors[0], 0.25)
if cols16 == "lighten":
colors[9] = util.lighten_color(colors[1], 0.25)
colors[10] = util.lighten_color(colors[2], 0.25)
colors[11] = util.lighten_color(colors[3], 0.25)
colors[12] = util.lighten_color(colors[4], 0.25)
colors[13] = util.lighten_color(colors[5], 0.25)
colors[14] = util.lighten_color(colors[6], 0.25)
colors[15] = util.darken_color(colors[0], 0.75)
else:
colors[1] = util.darken_color(colors[1], 0.25)
colors[2] = util.darken_color(colors[2], 0.25)
colors[3] = util.darken_color(colors[3], 0.25)
colors[4] = util.darken_color(colors[4], 0.25)
colors[5] = util.darken_color(colors[5], 0.25)
colors[6] = util.darken_color(colors[6], 0.25)
colors[15] = util.darken_color(colors[0], 0.75)
else:
colors[7] = util.darken_color(colors[0], 0.75)
colors[8] = util.darken_color(colors[0], 0.25)
colors[15] = colors[7]
else:
if colors[0][1] != "0": # the color may already be dark enough
colors[0] = util.darken_color(colors[0], 0.40) # just a bit darker
if cols16:
colors[7] = util.lighten_color(colors[0], 0.55)
colors[7] = util.saturate_color(colors[7], 0.05)
colors[8] = util.lighten_color(colors[0], 0.35)
colors[8] = util.saturate_color(colors[8], 0.10)
colors[15] = util.lighten_color(colors[0], 0.75)
if cols16 == "lighten":
colors[9] = util.lighten_color(colors[1], 0.25)
colors[10] = util.lighten_color(colors[2], 0.25)
colors[11] = util.lighten_color(colors[3], 0.25)
colors[12] = util.lighten_color(colors[4], 0.25)
colors[13] = util.lighten_color(colors[5], 0.25)
colors[14] = util.lighten_color(colors[6], 0.25)
for i in range(9, 15):
colors[i] = util.saturate_color(colors[i], 0.40)
else:
colors[1] = util.darken_color(colors[1], 0.25)
colors[2] = util.darken_color(colors[2], 0.25)
colors[3] = util.darken_color(colors[3], 0.25)
colors[4] = util.darken_color(colors[4], 0.25)
colors[5] = util.darken_color(colors[5], 0.25)
colors[6] = util.darken_color(colors[6], 0.25)
else:
colors[7] = util.lighten_color(colors[0], 0.75)
colors[8] = util.lighten_color(colors[0], 0.35)
colors[8] = util.saturate_color(colors[8], 0.10)
colors[15] = colors[7]
return colors
def saturate_colors(colors, amount):
"""Saturate all colors."""
if amount and float(amount) <= 1.0:
for i, _ in enumerate(colors):
if i not in [0, 7, 8, 15]:
colors[i] = util.saturate_color(colors[i], float(amount))
return colors
def cache_fname(img, backend, cols16, light, cache_dir, sat=""):
"""Create the cache file name."""
color_type = "light" if light else "dark"
color_num = "16" if cols16 else "9"
file_name = re.sub("[/|\\|.]", "_", img)
file_size = os.path.getsize(img)
if cols16:
file_parts = [
file_name,
color_num,
cols16,
color_type,
backend,
sat,
file_size,
__cache_version__,
]
return [cache_dir, "schemes", "%s_%s_%s_%s_%s_%s_%s_%s.json" % (*file_parts,)]
else:
file_parts = [
file_name,
color_num,
color_type,
backend,
sat,
file_size,
__cache_version__,
]
return [cache_dir, "schemes", "%s_%s_%s_%s_%s_%s_%s.json" % (*file_parts,)]
def get_backend(backend):
"""Figure out which backend to use."""
if backend == "random":
backends = list_backends()
random.shuffle(backends)
return backends[0]
return backend
def palette():
"""Generate a palette from the colors."""
for i in range(0, 16):
if i % 8 == 0:
print()
if i > 7:
i = "8;5;%s" % i
print("\033[4%sm%s\033[0m" % (i, " " * (80 // 20)), end="")
print("\n")
def get(img, light=False, cols16=False, backend="wal", cache_dir=CACHE_DIR, sat=""):
"""Generate a palette."""
# home_dylan_img_jpg_backend_1.2.2.json
cache_name = cache_fname(img, backend, cols16, light, cache_dir, sat)
cache_file = os.path.join(*cache_name)
# Check the wallpaper's checksum against the cache'
if os.path.isfile(cache_file) and theme.parse(cache_file)[
"checksum"
] == util.get_img_checksum(img):
colors = theme.file(cache_file)
colors["alpha"] = util.Color.alpha_num
logging.info("Found cached colorscheme.")
else:
logging.info("Generating a colorscheme.")
backend = get_backend(backend)
# Dynamically import the backend we want to use.
# This keeps the dependencies "optional".
try:
__import__("pywal.backends.%s" % backend)
except ImportError:
__import__("pywal.backends.wal")
backend = "wal"
logging.info("Using %s backend.", backend)
backend = sys.modules["pywal.backends.%s" % backend]
colors = getattr(backend, "get")(img, light, cols16)
colors = colors_to_dict(saturate_colors(colors, sat), img)
util.save_file_json(colors, cache_file)
logging.info("Generation complete.")
return colors
def file(input_file):
"""Deprecated: symbolic link to --> theme.file"""
return theme.file(input_file)