From 5015f2706c58e2f697cba7541f92d16bee08df68 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Fri, 30 Jun 2017 10:21:20 +1000 Subject: general: Rename gen_colors to magic --- pywal/__main__.py | 8 ++-- pywal/gen_colors.py | 106 ----------------------------------------------- pywal/magic.py | 106 +++++++++++++++++++++++++++++++++++++++++++++++ tests/test_gen_colors.py | 17 -------- tests/test_magic.py | 17 ++++++++ 5 files changed, 127 insertions(+), 127 deletions(-) delete mode 100755 pywal/gen_colors.py create mode 100755 pywal/magic.py delete mode 100755 tests/test_gen_colors.py create mode 100755 tests/test_magic.py diff --git a/pywal/__main__.py b/pywal/__main__.py index 9c1d000..228ceb4 100755 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -10,11 +10,11 @@ import sys from pywal.settings import CACHE_DIR, __version__ from pywal import export from pywal import image -from pywal import gen_colors -from pywal import sequences +from pywal import magic from pywal import reload -from pywal import wallpaper +from pywal import sequences from pywal import util +from pywal import wallpaper def get_args(): @@ -91,7 +91,7 @@ def process_args(args): image_file = image.get_image(args.i) # Create a list of hex colors. - colors_plain = gen_colors.get_colors(image_file, args.q) + colors_plain = magic.get_colors(image_file, args.q) if not args.n: wallpaper.set_wallpaper(image_file) diff --git a/pywal/gen_colors.py b/pywal/gen_colors.py deleted file mode 100755 index 3006c72..0000000 --- a/pywal/gen_colors.py +++ /dev/null @@ -1,106 +0,0 @@ -""" -Generate a colorscheme. -""" -import re -import shutil -import subprocess - -from pywal.settings import CACHE_DIR, COLOR_COUNT -from pywal import util - - -def imagemagick(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) - - return colors.stdout.readlines() - - -def gen_colors(img): - """Generate a color palette using imagemagick.""" - # Check if the user has Imagemagick installed. - if not shutil.which("convert"): - print("error: imagemagick not found, exiting...\n" - "error: wal requires imagemagick to function.") - exit(1) - - # Generate initial scheme. - raw_colors = imagemagick(COLOR_COUNT, img) - - # If imagemagick finds less than 16 colors, use a larger source number - # of colors. - index = 0 - while len(raw_colors) - 1 < COLOR_COUNT: - index += 1 - raw_colors = imagemagick(COLOR_COUNT + index, img) - - print("colors: Imagemagick couldn't generate a", COLOR_COUNT, - "color palette, trying a larger palette size", - COLOR_COUNT + index) - - # Remove the first element, which isn't a color. - del raw_colors[0] - - # Create a list of hex colors. - return [re.search("#.{6}", str(col)).group(0) for col in raw_colors] - - -def get_colors(img, quiet): - """Generate a colorscheme using imagemagick.""" - # Cache the wallpaper name. - util.save_file(img, CACHE_DIR / "wal") - - # Cache the sequences file. - cache_file = CACHE_DIR / "schemes" / img.replace("/", "_") - cache_file = cache_file.with_suffix(".json") - - if cache_file.is_file(): - colors = util.read_file_json(cache_file) - print("colors: Found cached colorscheme.") - - else: - print("colors: Generating a colorscheme...") - if not quiet: - util.disown("notify-send", "wal: Generating a colorscheme...") - - # Generate the colors. - colors = gen_colors(img) - colors = sort_colors(colors) - - # Cache the colorscheme. - util.save_file_json(colors, cache_file) - - print("colors: Generated colorscheme") - if not quiet: - util.disown("notify-send", "wal: Generation complete.") - - return colors - - -def sort_colors(colors): - """Sort the generated colors and store them in a dict that - we will later save in json format.""" - raw_colors = colors[:1] + colors[9:] + colors[8:] - - # Special colors. - colors_special = {} - colors_special.update({"background": raw_colors[0]}) - colors_special.update({"foreground": raw_colors[15]}) - colors_special.update({"cursor": raw_colors[15]}) - - # Colors 0-15 - colors_hex = {} - [colors_hex.update({f"color{index}": color}) # pylint: disable=W0106 - for index, color in enumerate(raw_colors)] - - # Color 8 - colors_hex["color8"] = util.set_grey(raw_colors) - - # Add the colors to a dict. - colors = {} - colors["special"] = colors_special - colors["colors"] = colors_hex - - return colors diff --git a/pywal/magic.py b/pywal/magic.py new file mode 100755 index 0000000..b435a0a --- /dev/null +++ b/pywal/magic.py @@ -0,0 +1,106 @@ +""" +Generate a colorscheme using imagemagick. +""" +import re +import shutil +import subprocess + +from pywal.settings import CACHE_DIR, COLOR_COUNT +from pywal import util + + +def imagemagick(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) + + return colors.stdout.readlines() + + +def gen_colors(img): + """Generate a color palette using imagemagick.""" + # Check if the user has Imagemagick installed. + if not shutil.which("convert"): + print("error: imagemagick not found, exiting...\n" + "error: wal requires imagemagick to function.") + exit(1) + + # Generate initial scheme. + raw_colors = imagemagick(COLOR_COUNT, img) + + # If imagemagick finds less than 16 colors, use a larger source number + # of colors. + index = 0 + while len(raw_colors) - 1 < COLOR_COUNT: + index += 1 + raw_colors = imagemagick(COLOR_COUNT + index, img) + + print("colors: Imagemagick couldn't generate a", COLOR_COUNT, + "color palette, trying a larger palette size", + COLOR_COUNT + index) + + # Remove the first element, which isn't a color. + del raw_colors[0] + + # Create a list of hex colors. + return [re.search("#.{6}", str(col)).group(0) for col in raw_colors] + + +def get_colors(img, quiet): + """Generate a colorscheme using imagemagick.""" + # Cache the wallpaper name. + util.save_file(img, CACHE_DIR / "wal") + + # Cache the sequences file. + cache_file = CACHE_DIR / "schemes" / img.replace("/", "_") + cache_file = cache_file.with_suffix(".json") + + if cache_file.is_file(): + colors = util.read_file_json(cache_file) + print("colors: Found cached colorscheme.") + + else: + print("colors: Generating a colorscheme...") + if not quiet: + util.disown("notify-send", "wal: Generating a colorscheme...") + + # Generate the colors. + colors = gen_colors(img) + colors = sort_colors(colors) + + # Cache the colorscheme. + util.save_file_json(colors, cache_file) + + print("colors: Generated colorscheme") + if not quiet: + util.disown("notify-send", "wal: Generation complete.") + + return colors + + +def sort_colors(colors): + """Sort the generated colors and store them in a dict that + we will later save in json format.""" + raw_colors = colors[:1] + colors[9:] + colors[8:] + + # Special colors. + colors_special = {} + colors_special.update({"background": raw_colors[0]}) + colors_special.update({"foreground": raw_colors[15]}) + colors_special.update({"cursor": raw_colors[15]}) + + # Colors 0-15 + colors_hex = {} + [colors_hex.update({f"color{index}": color}) # pylint: disable=W0106 + for index, color in enumerate(raw_colors)] + + # Color 8 + colors_hex["color8"] = util.set_grey(raw_colors) + + # Add the colors to a dict. + colors = {} + colors["special"] = colors_special + colors["colors"] = colors_hex + + return colors diff --git a/tests/test_gen_colors.py b/tests/test_gen_colors.py deleted file mode 100755 index 5b407af..0000000 --- a/tests/test_gen_colors.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Test gen functions.""" -import unittest - -from pywal import gen_colors - - -class TestGenColors(unittest.TestCase): - """Test the gen_colors functions.""" - - def test_gen_colors(self): - """> Generate a colorscheme.""" - result = gen_colors.gen_colors("tests/test_files/test.jpg") - self.assertEqual(result[0], "#0F191A") - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_magic.py b/tests/test_magic.py new file mode 100755 index 0000000..673420c --- /dev/null +++ b/tests/test_magic.py @@ -0,0 +1,17 @@ +"""Test imagemagick functions.""" +import unittest + +from pywal import magic + + +class TestGenColors(unittest.TestCase): + """Test the gen_colors functions.""" + + def test_gen_colors(self): + """> Generate a colorscheme.""" + result = magic.gen_colors("tests/test_files/test.jpg") + self.assertEqual(result[0], "#0F191A") + + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.3