efficient vim config
[dotfiles/.git] / .local / lib / python3.9 / site-packages / pywal / backends / wal.py
1 """
2 Generate a colorscheme using imagemagick.
3 """
4 import logging
5 import re
6 import shutil
7 import subprocess
8 import sys
9
10 from .. import util
11
12
13 def imagemagick(color_count, img, magick_command):
14     """Call Imagemagick to generate a scheme."""
15     flags = ["-resize", "25%", "-colors", str(color_count),
16              "-unique-colors", "txt:-"]
17     img += "[0]"
18
19     return subprocess.check_output([*magick_command, img, *flags]).splitlines()
20
21
22 def has_im():
23     """Check to see if the user has im installed."""
24     if shutil.which("magick"):
25         return ["magick", "convert"]
26
27     if shutil.which("convert"):
28         return ["convert"]
29
30     logging.error("Imagemagick wasn't found on your system.")
31     logging.error("Try another backend. (wal --backend)")
32     sys.exit(1)
33
34
35 def gen_colors(img):
36     """Format the output from imagemagick into a list
37        of hex colors."""
38     magick_command = has_im()
39
40     for i in range(0, 20, 1):
41         raw_colors = imagemagick(16 + i, img, magick_command)
42
43         if len(raw_colors) > 16:
44             break
45
46         elif i == 19:
47             logging.error("Imagemagick couldn't generate a suitable palette.")
48             sys.exit(1)
49
50         else:
51             logging.warning("Imagemagick couldn't generate a palette.")
52             logging.warning("Trying a larger palette size %s", 16 + i)
53
54     return [re.search("#.{6}", str(col)).group(0) for col in raw_colors[1:]]
55
56
57 def adjust(colors, light):
58     """Adjust the generated colors and store them in a dict that
59        we will later save in json format."""
60     raw_colors = colors[:1] + colors[8:16] + colors[8:-1]
61
62     # Manually adjust colors.
63     if light:
64         for color in raw_colors:
65             color = util.saturate_color(color, 0.5)
66
67         raw_colors[0] = util.lighten_color(colors[-1], 0.85)
68         raw_colors[7] = colors[0]
69         raw_colors[8] = util.darken_color(colors[-1], 0.4)
70         raw_colors[15] = colors[0]
71
72     else:
73         # Darken the background color slightly.
74         if raw_colors[0][1] != "0":
75             raw_colors[0] = util.darken_color(raw_colors[0], 0.40)
76
77         raw_colors[7] = util.blend_color(raw_colors[7], "#EEEEEE")
78         raw_colors[8] = util.darken_color(raw_colors[7], 0.30)
79         raw_colors[15] = util.blend_color(raw_colors[15], "#EEEEEE")
80
81     return raw_colors
82
83
84 def get(img, light=False):
85     """Get colorscheme."""
86     colors = gen_colors(img)
87     return adjust(colors, light)