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
|
"""
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 colors_to_dict(colors, img):
"""Convert list of colors to pywal format."""
return {
"wallpaper": 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):
"""Generic color adjustment for themers."""
if light:
for color in colors:
color = util.saturate_color(color, 0.50)
color = util.darken_color(color, 0.4)
colors[0] = util.lighten_color(colors[0], 0.9)
colors[7] = util.darken_color(colors[0], 0.75)
colors[8] = util.darken_color(colors[0], 0.25)
colors[15] = colors[7]
else:
colors[0] = util.darken_color(colors[0], 0.75)
colors[7] = util.lighten_color(colors[0], 0.75)
colors[8] = util.lighten_color(colors[0], 0.25)
colors[15] = colors[7]
return colors
def cache_fname(img, backend, light, cache_dir):
"""Create the cache file name."""
color_type = "light" if light else "dark"
file_name = re.sub("[/|\\|.]", "_", img)
file_parts = [file_name, color_type, backend, __cache_version__]
return [cache_dir, "schemes", "%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, backend="wal", cache_dir=CACHE_DIR):
"""Generate a palette."""
# home_dylan_img_jpg_backend_1.2.2.json
cache_name = cache_fname(img, backend, light, cache_dir)
cache_file = os.path.join(*cache_name)
if os.path.isfile(cache_file):
# Disable logging in theme.file().
logger = logging.getLogger()
logger.disabled = True
colors = theme.file(cache_file)
logger.disabled = False
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 = colors_to_dict(getattr(backend, "get")(img, light), 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)
|