summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Araps <dylan.araps@gmail.com>2017-06-17 12:22:20 +1000
committerDylan Araps <dylan.araps@gmail.com>2017-06-17 12:22:20 +1000
commit9d061fc08361ad3767157faeaba0141a63cc0369 (patch)
treea754c154074014e484cb71d7363f44e5c42b3e5b
parentb831e17555aff988b1be97ab6f82ca70240359d8 (diff)
General: Add error handling to palette generation
-rw-r--r--wal.py31
1 files changed, 25 insertions, 6 deletions
diff --git a/wal.py b/wal.py
index 25cb6d1..7f0612f 100644
--- a/wal.py
+++ b/wal.py
@@ -77,18 +77,36 @@ def get_image(img):
return rand_img
+def magic(color_count, img):
+ """Call Imagemagick to generate a scheme."""
+ colors = subprocess.Popen(["convert", img, "+dither", "-colors",
+ str(color_count), "-unique-colors", "txt:-"],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+
+ return colors.stdout
+
+
def gen_colors(img):
"""Generate a color palette using imagemagick."""
colors = []
- # Long-ass imagemagick command.
- magic = subprocess.Popen(["convert", img, "+dither", "-colors",
- str(COLOR_COUNT), "-unique-colors", "txt:-"],
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
+ # Generate initial scheme.
+ magic_output = magic(COLOR_COUNT, img).readlines()
+
+ # If imagemagick finds less than 16 colors, use a larger source number
+ # of colors.
+ index = 0
+ while len(magic_output) - 1 <= 15:
+ index += 1
+ magic_output = magic(COLOR_COUNT + index, img).readlines()
+
+ print("colors: Imagemagick couldn't generate a", COLOR_COUNT,
+ "color palette, trying a larger palette size",
+ COLOR_COUNT + index)
# Create a list of hex colors.
- for color in magic.stdout:
+ for color in magic_output:
hex_color = re.search('#.{6}', str(color))
if hex_color:
@@ -156,6 +174,7 @@ def send_sequences(colors):
# Decode the string.
sequences = bytes(sequences, "utf-8").decode("unicode_escape")
+ # Send the sequences to all open terminals.
for term in glob.glob("/dev/pts/[0-9]*"):
term_file = open(term, 'w')
term_file.write(sequences)