efficient vim config
[dotfiles/.git] / .local / lib / python3.9 / site-packages / pywal / backends / colorthief.py
1 """
2 Generate a colorscheme using ColorThief.
3 """
4 import logging
5 import sys
6
7 try:
8     from colorthief import ColorThief
9
10 except ImportError:
11     logging.error("ColorThief wasn't found on your system.")
12     logging.error("Try another backend. (wal --backend)")
13     sys.exit(1)
14
15 from .. import util
16
17
18 def gen_colors(img):
19     """Loop until 16 colors are generated."""
20     color_cmd = ColorThief(img).get_palette
21
22     for i in range(0, 10, 1):
23         raw_colors = color_cmd(color_count=8 + i)
24
25         if len(raw_colors) >= 8:
26             break
27
28         elif i == 10:
29             logging.error("ColorThief couldn't generate a suitable palette.")
30             sys.exit(1)
31
32         else:
33             logging.warning("ColorThief couldn't generate a palette.")
34             logging.warning("Trying a larger palette size %s", 8 + i)
35
36     return [util.rgb_to_hex(color) for color in raw_colors]
37
38
39 def adjust(cols, light):
40     """Create palette."""
41     cols.sort(key=util.rgb_to_yiq)
42     raw_colors = [*cols, *cols]
43
44     if light:
45         raw_colors[0] = util.lighten_color(cols[0], 0.90)
46         raw_colors[7] = util.darken_color(cols[0], 0.75)
47
48     else:
49         for color in raw_colors:
50             color = util.lighten_color(color, 0.40)
51
52         raw_colors[0] = util.darken_color(cols[0], 0.80)
53         raw_colors[7] = util.lighten_color(cols[0], 0.60)
54
55     raw_colors[8] = util.lighten_color(cols[0], 0.20)
56     raw_colors[15] = raw_colors[7]
57
58     return raw_colors
59
60
61 def get(img, light=False):
62     """Get colorscheme."""
63     cols = gen_colors(img)
64     return adjust(cols, light)