From 232b67d27fdb5dad70ae96c757da3e54f7a7003b Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Sun, 14 Mar 2021 16:18:53 +1100 Subject: [PATCH 01/11] Add a stars test --- test/test_stars.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/test_stars.py diff --git a/test/test_stars.py b/test/test_stars.py new file mode 100644 index 0000000..e78b393 --- /dev/null +++ b/test/test_stars.py @@ -0,0 +1,39 @@ +from click.testing import CliRunner +from pathlib import Path +from PIL import Image, ImageDraw + +from ih.cli import main +from ih.palette import get_palette +from ih import helpers + + +from test_cli import runner, TEST_OUTPUT + +s = 1 #square +length = 8 +stars_image = "stars_image.png" + +def test_stars(): + + stars = len(helpers.STARS) + w = length + 1 + h = int(stars/length) + 1 + + img = Image.new('RGB', (w, h)) + draw = ImageDraw.Draw(img) + + # Draw palette from file + palette = [tuple(x['rgb']) for x in get_palette("floss")][:(w*h)] + + ct = 0 + for i in range(0, w): + for j in range(0, h): + c = palette[ct] #(0, int(255/length * i), int(255/(stars/length) * j)) + ct += 1 + draw.polygon([(i,j),(i+s, j), (i, j+s), (i+s, j+s)], fill=c) + + path = Path(TEST_OUTPUT).parent.joinpath("images").joinpath(stars_image) + img.save(path) + runner(["-p", "floss", "-c", 256], image=str(path)) + + From b5bd8060980ad06eea524abce7449e92e80d15ab Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Sun, 14 Mar 2021 16:19:14 +1100 Subject: [PATCH 02/11] output stem image name --- ih/chart.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ih/chart.py b/ih/chart.py index bdd90a4..68ef455 100644 --- a/ih/chart.py +++ b/ih/chart.py @@ -1,6 +1,7 @@ import json import click from textwrap import dedent +from pathlib import Path import os from tabulate import tabulate @@ -32,9 +33,14 @@ def debug_data(image_name, scale, colors, palette_name, chartimage, fileformat="html"): import pkg_resources + # format to a nice name + if hasattr(image_name, "name"): + image_name = image_name.name + name = Path(image_name).name + ih_version = pkg_resources.require("ih")[0].version data = [ - f"Image: {image_name}", + f"Image: {name}", f"Scale: {scale}x", f"Image size: {chartimage.height} x {chartimage.width}", f"Palette: {palette_name}", From 51c6a9ccdc037ca55faa5d9e5ea2434f25339187 Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Sun, 14 Mar 2021 16:19:51 +1100 Subject: [PATCH 03/11] output colours used, as well as colour param --- ih/chart.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ih/chart.py b/ih/chart.py index 68ef455..1d2ff75 100644 --- a/ih/chart.py +++ b/ih/chart.py @@ -30,7 +30,7 @@ GUIDECOL = (0, 0, 0, 0) -def debug_data(image_name, scale, colors, palette_name, chartimage, fileformat="html"): +def debug_data(image_name, scale, colors, palette_name, chartimage, colorsused, fileformat="html"): import pkg_resources # format to a nice name @@ -44,7 +44,7 @@ def debug_data(image_name, scale, colors, palette_name, chartimage, fileformat=" f"Scale: {scale}x", f"Image size: {chartimage.height} x {chartimage.width}", f"Palette: {palette_name}", - f"Colors: {colors}", + f"Colors used: {colorsused} (of possible {colors})", f"ih version: {ih_version}", ] if fileformat == "html": @@ -360,6 +360,7 @@ def chart( palette_name=palette_name, chartimage=chartimage, fileformat=fileformat, + colorsused=len(sorted(chartimage.getcolors())), ) if fileformat == "html": From adc5372607ebb1c1e174102a25be84d755c19f4a Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Sun, 14 Mar 2021 16:26:40 +1100 Subject: [PATCH 04/11] Fix starclasses to be unique on col Fixes issue where charts above len(STARS) unique colours would have duplicate listings in legend --- ih/chart.py | 4 ++-- ih/helpers.py | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/ih/chart.py b/ih/chart.py index 1d2ff75..0a39e86 100644 --- a/ih/chart.py +++ b/ih/chart.py @@ -91,7 +91,7 @@ def get_legend(chartimage): rgb = x[1] h = helpers.rgb2hex(rgb) star = STARS[idx % len(STARS)] - sclass = helpers.star_class(star) + sclass = helpers.col_class(h) # Choose the best text colour if (rgb[0] * 0.299 + rgb[1] * 0.587 + rgb[2] * 0.114) > 186: @@ -156,7 +156,7 @@ def generate_html_chart( if not render: html.append( '.%s::after { content: "%s" }' - % (helpers.star_class(helpers.WHITESTAR), helpers.WHITESTAR) + % (helpers.col_class(helpers.WHITECOL), helpers.WHITESTAR) ) html.append("") diff --git a/ih/helpers.py b/ih/helpers.py index 452333b..80e0137 100644 --- a/ih/helpers.py +++ b/ih/helpers.py @@ -36,6 +36,7 @@ ] WHITESTAR = "·" +WHITECOL = "#ffffff" def rgb2hex(pix): @@ -66,10 +67,10 @@ def color_cell( else: td = "span" - if color == "#ffffff": + if color == WHITECOL: star = WHITESTAR - classes = f"s {star_class(star)}" + classes = f"s {col_class(color)}" if center: classes += " center" if guide[1]: @@ -80,8 +81,11 @@ def color_cell( return f'<{td} class="{classes}">' -def star_class(star): - return f"u{hex(ord(star))[2:]}" +#def star_class(star): +# return f"u{hex(ord(star))[2:]}" + +def col_class(col): + return f"u-{col[1:].lower()}" def base_path(path): From 6312325516271699faee9d01a32aaa20627779b9 Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Sun, 14 Mar 2021 16:37:25 +1100 Subject: [PATCH 05/11] ensure completely valid html (self-closed singletons) --- ih/chart.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ih/chart.py b/ih/chart.py index 0a39e86..d33b9f3 100644 --- a/ih/chart.py +++ b/ih/chart.py @@ -48,7 +48,7 @@ def debug_data(image_name, scale, colors, palette_name, chartimage, colorsused, f"ih version: {ih_version}", ] if fileformat == "html": - return f'
' + "
".join(data) + "
" + return f'
' + "
".join(data) + "
" else: return "\n".join(data) @@ -114,7 +114,7 @@ def generate_html_chart( data="", ): - html = [''] + html = [''] with open(helpers.base_path("styling").joinpath("styling.css")) as s: html.append("