From 042397d7c74c7b3512803dee55e196433dffe014 Mon Sep 17 00:00:00 2001 From: eylles Date: Sun, 24 Nov 2024 22:47:45 -0600 Subject: [PATCH 1/2] Use kwargs for cols16 and contrast This improves the backwards compatibility of the API. Closes #77 --- pywal/__main__.py | 8 ++--- pywal/backends/colorthief.py | 16 +++++++--- pywal/backends/colorz.py | 16 +++++++--- pywal/backends/fast_colorthief.py | 16 +++++++--- pywal/backends/haishoku.py | 16 +++++++--- pywal/backends/modern_colorthief.py | 16 +++++++--- pywal/backends/okthief.py | 16 +++++++--- pywal/backends/schemer2.py | 16 +++++++--- pywal/backends/wal.py | 16 +++++++--- pywal/colors.py | 45 +++++++++++++++++++++++------ 10 files changed, 136 insertions(+), 45 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 6fdb2cf..b2c5945 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -268,10 +268,10 @@ def parse_args(parser): colors_plain = colors.get( image_file, args.l, - args.cols16, args.backend, sat=args.saturate, - contrast=args.contrast, + c16=args.cols16, + cst=args.contrast, ) if args.theme: @@ -287,10 +287,10 @@ def parse_args(parser): colors_plain = colors.get( cached_wallpaper[0], args.l, - args.cols16, args.backend, sat=args.saturate, - contrast=args.contrast, + c16=args.cols16, + cst=args.contrast, ) if args.b: diff --git a/pywal/backends/colorthief.py b/pywal/backends/colorthief.py index db5de6a..d2322d9 100644 --- a/pywal/backends/colorthief.py +++ b/pywal/backends/colorthief.py @@ -38,18 +38,26 @@ def gen_colors(img): return [util.rgb_to_hex(color) for color in raw_colors] -def adjust(cols, light, cols16): +def adjust(cols, light, **kwargs): """Create palette.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False cols.sort(key=util.rgb_to_yiq) raw_colors = [*cols, *cols] for color in raw_colors: color = util.lighten_color(color, 0.40) raw_colors[0] = util.darken_color(cols[0], 0.80) - return colors.generic_adjust(raw_colors, light, cols16) + return colors.generic_adjust(raw_colors, light, c16=cols16) -def get(img, light=False, cols16=False): +def get(img, light=False, **kwargs): """Get colorscheme.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False cols = gen_colors(img) - return adjust(cols, light, cols16) + return adjust(cols, light, c16=cols16) diff --git a/pywal/backends/colorz.py b/pywal/backends/colorz.py index 36bd560..caa7985 100644 --- a/pywal/backends/colorz.py +++ b/pywal/backends/colorz.py @@ -24,16 +24,24 @@ def gen_colors(img): return [util.rgb_to_hex([*color[0]]) for color in raw_colors] -def adjust(cols, light, cols16): +def adjust(cols, light, **kwargs): """Create palette.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False raw_colors = [cols[0], *cols, "#FFFFFF", "#000000", *cols, "#FFFFFF"] raw_colors[0] = util.darken_color(cols[0], 0.80) - return colors.generic_adjust(raw_colors, light, cols16) + return colors.generic_adjust(raw_colors, light, c16=cols16) -def get(img, light=False, cols16=False): +def get(img, light=False, **kwargs): """Get colorscheme.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False cols = gen_colors(img) if len(cols) < 6: @@ -41,4 +49,4 @@ def get(img, light=False, cols16=False): logging.error("Try another backend or another image. (wal --backend)") sys.exit(1) - return adjust(cols, light, cols16) + return adjust(cols, light, c16=cols16) diff --git a/pywal/backends/fast_colorthief.py b/pywal/backends/fast_colorthief.py index de44f58..45aa762 100644 --- a/pywal/backends/fast_colorthief.py +++ b/pywal/backends/fast_colorthief.py @@ -24,16 +24,24 @@ def gen_colors(img): return [util.rgb_to_hex(color) for color in raw_colors] -def adjust(cols, light, cols16): +def adjust(cols, light, **kwargs): """Create palette.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False cols.sort(key=util.rgb_to_yiq) raw_colors = [*cols, *cols] raw_colors[0] = util.darken_color(cols[0], 0.80) - return colors.generic_adjust(raw_colors, light, cols16) + return colors.generic_adjust(raw_colors, light, c16=cols16) -def get(img, light=False, cols16=False): +def get(img, light=False, **kwargs): """Get colorscheme.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False cols = gen_colors(img) - return adjust(cols, light, cols16) + return adjust(cols, light, c16=cols16) diff --git a/pywal/backends/haishoku.py b/pywal/backends/haishoku.py index 6b34030..167d140 100644 --- a/pywal/backends/haishoku.py +++ b/pywal/backends/haishoku.py @@ -23,17 +23,25 @@ def gen_colors(img): return [util.rgb_to_hex(col[1]) for col in palette] -def adjust(cols, light, cols16): +def adjust(cols, light, **kwargs): """Create palette.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False cols.sort(key=util.rgb_to_yiq) raw_colors = [*cols, *cols] raw_colors[0] = util.lighten_color(cols[0], 0.40) raw_colors[0] = util.darken_color(cols[0], 0.80) - return colors.generic_adjust(raw_colors, light, cols16) + return colors.generic_adjust(raw_colors, light, c16=cols16) -def get(img, light=False, cols16=False): +def get(img, light=False, **kwargs): """Get colorscheme.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False cols = gen_colors(img) - return adjust(cols, light, cols16) + return adjust(cols, light, c16=cols16) diff --git a/pywal/backends/modern_colorthief.py b/pywal/backends/modern_colorthief.py index d5b6a52..ae6a303 100644 --- a/pywal/backends/modern_colorthief.py +++ b/pywal/backends/modern_colorthief.py @@ -24,16 +24,24 @@ def gen_colors(img): return [util.rgb_to_hex(color) for color in raw_colors] -def adjust(cols, light, cols16): +def adjust(cols, light, **kwargs): """Create palette.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False cols.sort(key=util.rgb_to_yiq) raw_colors = [*cols, *cols] raw_colors[0] = util.darken_color(cols[0], 0.80) - return colors.generic_adjust(raw_colors, light, cols16) + return colors.generic_adjust(raw_colors, light, c16=cols16) -def get(img, light=False, cols16=False): +def get(img, light=False, **kwargs): """Get colorscheme.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False cols = gen_colors(img) - return adjust(cols, light, cols16) + return adjust(cols, light, c16=cols16) diff --git a/pywal/backends/okthief.py b/pywal/backends/okthief.py index 15bfd93..3ec0b2a 100644 --- a/pywal/backends/okthief.py +++ b/pywal/backends/okthief.py @@ -25,13 +25,21 @@ def gen_colors(img): return cols -def adjust(cols, light, cols16): +def adjust(cols, light, **kwargs): """Create palette.""" - return colors.generic_adjust(cols, light, cols16) + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False + return colors.generic_adjust(cols, light, c16=cols16) -def get(img, light=False, cols16=False): +def get(img, light=False, **kwargs): """Get colorscheme.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False if not shutil.which("okthief"): logging.error("okthief wasn't found on your system.") logging.error("Try another backend. (wal --backend)") @@ -39,4 +47,4 @@ def get(img, light=False, cols16=False): cols = gen_colors(img) cols[0] = util.darken_color(cols[0], 0.80) - return adjust(cols, light, cols16) + return adjust(cols, light, c16=cols16) diff --git a/pywal/backends/schemer2.py b/pywal/backends/schemer2.py index d19d053..04f8bb0 100644 --- a/pywal/backends/schemer2.py +++ b/pywal/backends/schemer2.py @@ -17,21 +17,29 @@ def gen_colors(img): return subprocess.check_output([*cmd, img]).splitlines() -def adjust(cols, light, cols16): +def adjust(cols, light, **kwargs): """Create palette.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False cols.sort(key=util.rgb_to_yiq) raw_colors = [*cols[8:], *cols[8:]] raw_colors[0] = util.darken_color(cols[0], 0.80) - return colors.generic_adjust(raw_colors, light, cols16) + return colors.generic_adjust(raw_colors, light, c16=cols16) -def get(img, light=False, cols16=False): +def get(img, light=False, **kwargs): """Get colorscheme.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False if not shutil.which("schemer2"): logging.error("Schemer2 wasn't found on your system.") logging.error("Try another backend. (wal --backend)") sys.exit(1) cols = [col.decode("UTF-8") for col in gen_colors(img)] - return adjust(cols, light, cols16) + return adjust(cols, light, c16=cols16) diff --git a/pywal/backends/wal.py b/pywal/backends/wal.py index 3f2a5bc..6f0a424 100644 --- a/pywal/backends/wal.py +++ b/pywal/backends/wal.py @@ -88,19 +88,27 @@ def gen_colors(img): return out -def adjust(cols, light, cols16): +def adjust(cols, light, **kwargs): """Adjust the generated colors and store them in a dict that we will later save in json format.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False raw_colors = cols[:1] + cols[8:16] + cols[8:-1] - return colors.generic_adjust(raw_colors, light, cols16) + return colors.generic_adjust(raw_colors, light, c16=cols16) -def get(img, light=False, cols16=False): +def get(img, light=False, **kwargs): """Get colorscheme.""" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False colors = gen_colors(img) # it is possible we could have picked garbage data garbage = "# Image" if garbage in colors: colors.remove(garbage) - return adjust(colors, light, cols16) + return adjust(colors, light, c16=cols16) diff --git a/pywal/colors.py b/pywal/colors.py index 3a088a0..6e02c3c 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -65,8 +65,13 @@ def colors_to_dict(colors, img): } -def generic_adjust(colors, light, cols16): - """Generic color adjustment for themers.""" +def generic_adjust(colors, light, **kwargs): + """Generic color adjustment for themers. + :keyword args: c16 - [ lighten | darken ] + """ + if 'c16' in kwargs: + cols16 = kwargs["c16"] + if light: for color in colors: color = util.saturate_color(color, 0.60) @@ -267,9 +272,17 @@ def binary_luminance_adjust( ) -def cache_fname(img, backend, cols16, light, cache_dir, sat="", contrast=""): +def cache_fname(img, backend, light, cache_dir, sat="", **kwargs): """Create the cache file name.""" color_type = "light" if light else "dark" + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False + if 'cst' in kwargs: + contrast = kwargs["cst"] + else: + contrast = "" color_num = "16" if cols16 else "9" file_name = re.sub("[/|\\|.]", "_", img) file_size = os.path.getsize(img) @@ -336,21 +349,35 @@ def palette(): def get( img, light=False, - cols16=False, backend="wal", cache_dir=CACHE_DIR, sat="", - contrast="", + **kwargs, ): - """Generate a palette.""" + """Generate a palette. + :keyword args: + c16: [ lighten | darken ] - generates a 16 color palette + cst: float - applies contrast ratio to palette + """ + if 'c16' in kwargs: + cols16 = kwargs["c16"] + else: + cols16 = False + if 'cst' in kwargs: + contrast = kwargs["cst"] + else: + contrast = "" + # home_dylan_img_jpg_backend_1.2.2.json if not contrast or contrast == "": cache_name = cache_fname( - img, backend, cols16, light, cache_dir, sat, "None" + img, backend, light, cache_dir, sat, + c16=cols16, cst="None", ) else: cache_name = cache_fname( - img, backend, cols16, light, cache_dir, sat, float(contrast) + img, backend, light, cache_dir, sat, + c16=cols16, cst=float(contrast) ) cache_file = os.path.join(*cache_name) @@ -377,7 +404,7 @@ def get( logging.info("Using %s backend.", backend) backend = sys.modules["pywal.backends.%s" % backend] - colors = getattr(backend, "get")(img, light, cols16) + colors = getattr(backend, "get")(img, light, c16=cols16) # Post-processing steps from command-line arguments colors = saturate_colors(colors, sat) From 1d002acdfc68cd4db2846634a3610d98c92f8629 Mon Sep 17 00:00:00 2001 From: eylles Date: Sun, 24 Nov 2024 23:00:41 -0600 Subject: [PATCH 2/2] Add docstrings for kwargs --- pywal/backends/colorthief.py | 10 ++++++++-- pywal/backends/colorz.py | 10 ++++++++-- pywal/backends/fast_colorthief.py | 10 ++++++++-- pywal/backends/haishoku.py | 10 ++++++++-- pywal/backends/modern_colorthief.py | 10 ++++++++-- pywal/backends/okthief.py | 10 ++++++++-- pywal/backends/schemer2.py | 10 ++++++++-- pywal/backends/wal.py | 10 ++++++++-- pywal/colors.py | 17 ++++++++++++----- 9 files changed, 76 insertions(+), 21 deletions(-) diff --git a/pywal/backends/colorthief.py b/pywal/backends/colorthief.py index d2322d9..3a01fce 100644 --- a/pywal/backends/colorthief.py +++ b/pywal/backends/colorthief.py @@ -39,7 +39,10 @@ def gen_colors(img): def adjust(cols, light, **kwargs): - """Create palette.""" + """Create palette. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: @@ -54,7 +57,10 @@ def adjust(cols, light, **kwargs): def get(img, light=False, **kwargs): - """Get colorscheme.""" + """Get colorscheme. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: diff --git a/pywal/backends/colorz.py b/pywal/backends/colorz.py index caa7985..394b081 100644 --- a/pywal/backends/colorz.py +++ b/pywal/backends/colorz.py @@ -25,7 +25,10 @@ def gen_colors(img): def adjust(cols, light, **kwargs): - """Create palette.""" + """Create palette. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: @@ -37,7 +40,10 @@ def adjust(cols, light, **kwargs): def get(img, light=False, **kwargs): - """Get colorscheme.""" + """Get colorscheme. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: diff --git a/pywal/backends/fast_colorthief.py b/pywal/backends/fast_colorthief.py index 45aa762..64dd959 100644 --- a/pywal/backends/fast_colorthief.py +++ b/pywal/backends/fast_colorthief.py @@ -25,7 +25,10 @@ def gen_colors(img): def adjust(cols, light, **kwargs): - """Create palette.""" + """Create palette. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: @@ -38,7 +41,10 @@ def adjust(cols, light, **kwargs): def get(img, light=False, **kwargs): - """Get colorscheme.""" + """Get colorscheme. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: diff --git a/pywal/backends/haishoku.py b/pywal/backends/haishoku.py index 167d140..ad3f5de 100644 --- a/pywal/backends/haishoku.py +++ b/pywal/backends/haishoku.py @@ -24,7 +24,10 @@ def gen_colors(img): def adjust(cols, light, **kwargs): - """Create palette.""" + """Create palette. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: @@ -38,7 +41,10 @@ def adjust(cols, light, **kwargs): def get(img, light=False, **kwargs): - """Get colorscheme.""" + """Get colorscheme. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: diff --git a/pywal/backends/modern_colorthief.py b/pywal/backends/modern_colorthief.py index ae6a303..7363592 100644 --- a/pywal/backends/modern_colorthief.py +++ b/pywal/backends/modern_colorthief.py @@ -25,7 +25,10 @@ def gen_colors(img): def adjust(cols, light, **kwargs): - """Create palette.""" + """Create palette. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: @@ -38,7 +41,10 @@ def adjust(cols, light, **kwargs): def get(img, light=False, **kwargs): - """Get colorscheme.""" + """Get colorscheme. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: diff --git a/pywal/backends/okthief.py b/pywal/backends/okthief.py index 3ec0b2a..c8d8641 100644 --- a/pywal/backends/okthief.py +++ b/pywal/backends/okthief.py @@ -26,7 +26,10 @@ def gen_colors(img): def adjust(cols, light, **kwargs): - """Create palette.""" + """Create palette. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: @@ -35,7 +38,10 @@ def adjust(cols, light, **kwargs): def get(img, light=False, **kwargs): - """Get colorscheme.""" + """Get colorscheme. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: diff --git a/pywal/backends/schemer2.py b/pywal/backends/schemer2.py index 04f8bb0..c01e91c 100644 --- a/pywal/backends/schemer2.py +++ b/pywal/backends/schemer2.py @@ -18,7 +18,10 @@ def gen_colors(img): def adjust(cols, light, **kwargs): - """Create palette.""" + """Create palette. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: @@ -31,7 +34,10 @@ def adjust(cols, light, **kwargs): def get(img, light=False, **kwargs): - """Get colorscheme.""" + """Get colorscheme. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: diff --git a/pywal/backends/wal.py b/pywal/backends/wal.py index 6f0a424..e5255db 100644 --- a/pywal/backends/wal.py +++ b/pywal/backends/wal.py @@ -90,7 +90,10 @@ def gen_colors(img): def adjust(cols, light, **kwargs): """Adjust the generated colors and store them in a dict that - we will later save in json format.""" + we will later save in json format. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: @@ -101,7 +104,10 @@ def adjust(cols, light, **kwargs): def get(img, light=False, **kwargs): - """Get colorscheme.""" + """Get colorscheme. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + """ if 'c16' in kwargs: cols16 = kwargs["c16"] else: diff --git a/pywal/colors.py b/pywal/colors.py index 6e02c3c..6dd2427 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -67,10 +67,13 @@ def colors_to_dict(colors, img): def generic_adjust(colors, light, **kwargs): """Generic color adjustment for themers. - :keyword args: c16 - [ lighten | darken ] + :keyword-args: + - c16 - [ "lighten" | "darken" ] """ if 'c16' in kwargs: cols16 = kwargs["c16"] + else: + cols16 = False if light: for color in colors: @@ -273,7 +276,11 @@ def binary_luminance_adjust( def cache_fname(img, backend, light, cache_dir, sat="", **kwargs): - """Create the cache file name.""" + """Create the cache file name. + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + - cst: palette contrast ratio - float + """ color_type = "light" if light else "dark" if 'c16' in kwargs: cols16 = kwargs["c16"] @@ -355,9 +362,9 @@ def get( **kwargs, ): """Generate a palette. - :keyword args: - c16: [ lighten | darken ] - generates a 16 color palette - cst: float - applies contrast ratio to palette + :keyword-args: + - c16: use 16 colors through specified method - [ "lighten" | "darken" ] + - cst: apply contrast ratio to palette - float """ if 'c16' in kwargs: cols16 = kwargs["c16"]