2 Generate a palette using various backends.
12 from .settings import CACHE_DIR, MODULE_DIR, __cache_version__
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]
22 def colors_to_dict(colors, img):
23 """Convert list of colors to pywal format."""
26 "alpha": util.Color.alpha_num,
29 "background": colors[0],
30 "foreground": colors[15],
45 "color10": colors[10],
46 "color11": colors[11],
47 "color12": colors[12],
48 "color13": colors[13],
49 "color14": colors[14],
55 def generic_adjust(colors, light):
56 """Generic color adjustment for themers."""
59 color = util.saturate_color(color, 0.60)
60 color = util.darken_color(color, 0.5)
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]
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]
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))
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)
91 file_parts = [file_name, color_type, backend, sat, __cache_version__]
92 return [cache_dir, "schemes", "%s_%s_%s_%s_%s.json" % (*file_parts,)]
95 def get_backend(backend):
96 """Figure out which backend to use."""
97 if backend == "random":
98 backends = list_backends()
99 random.shuffle(backends)
106 """Generate a palette from the colors."""
107 for i in range(0, 16):
114 print("\033[4%sm%s\033[0m" % (i, " " * (80 // 20)), end="")
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)
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.")
131 logging.info("Generating a colorscheme.")
132 backend = get_backend(backend)
134 # Dynamically import the backend we want to use.
135 # This keeps the dependencies "optional".
137 __import__("pywal.backends.%s" % backend)
139 __import__("pywal.backends.wal")
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)
147 util.save_file_json(colors, cache_file)
148 logging.info("Generation complete.")
153 def file(input_file):
154 """Deprecated: symbolic link to --> theme.file"""
155 return theme.file(input_file)