Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 51 additions & 32 deletions pywal/backends/wal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import sys

from .. import colors
from .. import util


def imagemagick(color_count, img, magick_command):
Expand Down Expand Up @@ -55,51 +54,71 @@ def has_im():
sys.exit(1)


def try_gen_in_range(img, magick_command):
for i in range(0, 20, 1):
raw_colors = imagemagick(16 + i, img, magick_command)

if len(raw_colors) > 16:
def gen_colors_with_command(
img, magick_command, beginning_color_count=16, iteration_count=20
):
"""Iteratively attempt to generate a 16-color palette
using a specific Imagemagick command."""
hex_pattern = re.compile(r"#[A-F0-9]{6}", re.IGNORECASE)

max_color_count = beginning_color_count + iteration_count - 1
for color_count in range(
beginning_color_count, beginning_color_count + iteration_count
):
raw_output = imagemagick(color_count, img, magick_command)
hex_colors = [
hex_pattern.search(str(col)).group()
for col in raw_output
if hex_pattern.search(str(col))
]

if len(hex_colors) >= 16:
break

if i == 19:
logging.error("Imagemagick couldn't generate a suitable palette.")
logging.warning("will try to do palette concatenation, good results not guaranteed!")
while not len(raw_colors) > 16:
raw_colors = raw_colors + raw_colors
if color_count < max_color_count:
logging.warning(
"Imagemagick couldn't generate a "
f"palette with {magick_command}."
)
logging.warning(
f"Trying a larger palette size {color_count}."
)
else:
logging.warning("Imagemagick couldn't generate a palette.")
logging.warning("Trying a larger palette size %s", 16 + i)
return raw_colors
logging.error(
"Imagemagick couldn't generate a suitable palette "
f"with {magick_command}."
)
logging.warning(
"Will try to do palette concatenation, "
"good results not guaranteed!"
)
while len(hex_colors) < 16:
hex_colors.extend(hex_colors)
return hex_colors


def gen_colors(img):
"""Format the output from imagemagick into a list of hex colors."""
"""Try each Imagemagick command until a color palette
is successfully generated."""
magick_commands = has_im()

for magick_command in magick_commands:
logging.debug(f"Trying {magick_command}...")

try:
raw_colors = try_gen_in_range(img, magick_command)
hex_colors = [
re.search("#.{6}", str(col)).group(0)
for col in raw_colors[1:]
]

if not hex_colors:
logging.warning("Failed to generate colors.")
continue
hex_colors = gen_colors_with_command(img, magick_command)

while len(hex_colors) < 16:
logging.warning("will try to do palette concatenation, good results not guaranteed!")
hex_colors += hex_colors

except AttributeError:
logging.warning(f"{magick_command} failed.")
if not hex_colors:
logging.warning(
f"Failed to generate colors with {magick_command}."
)
continue

return hex_colors
return hex_colors

raise RuntimeError(
"Failed to generate color palette from "
f"{img} with these commands: {magick_commands}"
)


def adjust(cols, light, **kwargs):
Expand Down