2 Generate a colorscheme using imagemagick.
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:-"]
19 return subprocess.check_output([*magick_command, img, *flags]).splitlines()
23 """Check to see if the user has im installed."""
24 if shutil.which("magick"):
25 return ["magick", "convert"]
27 if shutil.which("convert"):
30 logging.error("Imagemagick wasn't found on your system.")
31 logging.error("Try another backend. (wal --backend)")
36 """Format the output from imagemagick into a list
38 magick_command = has_im()
40 for i in range(0, 20, 1):
41 raw_colors = imagemagick(16 + i, img, magick_command)
43 if len(raw_colors) > 16:
47 logging.error("Imagemagick couldn't generate a suitable palette.")
51 logging.warning("Imagemagick couldn't generate a palette.")
52 logging.warning("Trying a larger palette size %s", 16 + i)
54 return [re.search("#.{6}", str(col)).group(0) for col in raw_colors[1:]]
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]
62 # Manually adjust colors.
64 for color in raw_colors:
65 color = util.saturate_color(color, 0.5)
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]
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)
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")
84 def get(img, light=False):
85 """Get colorscheme."""
86 colors = gen_colors(img)
87 return adjust(colors, light)