Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
test(logo): a title in both keys' hues is refused before the lettering is bought, a key in the title's hue is not chosen (a colour the brief names counts at a smaller share), a named key in it is kept and said, and see-through inside the letters is refused with the cut kept aside — synthetic patchwork letters with mint patches on green; glows and pooled counters stay clear (red)
Soohong Kim6bafc4d37fd8
diff --git a/tests/frames/test_logo_matte.py b/tests/frames/test_logo_matte.pyindex 20b44ba..e0c852d 100644--- a/tests/frames/test_logo_matte.py+++ b/tests/frames/test_logo_matte.py@@ -13,7 +13,7 @@ import numpy as npimport pytestfrom PIL import Image, ImageFilter-from logo_lettering import GREEN_PAINTED, key_excess, lettering, over, truth_over+from logo_lettering import BUTTER, GREEN_PAINTED, MAGENTA_PAINTED, MINT, SKY, key_excess, lettering, over, quilt, truth_overfrom sprite_gen.frames import logo_matte@@ -203,6 +203,71 @@ def test_the_key_keeps_a_glows_hue_longest() -> None:assert logo_matte.choose_key(Image.new("RGB", (60, 40), (255, 120, 20)))["key"] == "magenta"+def test_a_title_in_both_keys_hues_gets_neither_key() -> None:+ # patchwork letters: pink patches and a plum outline in magenta's hue, mint patches in green's+ choice = logo_matte.choose_key(quilt(key=(30, 40, 90))[0])+ assert choice["scores"]["magenta"]["hued_pct"] >= logo_matte.CLASH_HUED_PCT+ assert choice["scores"]["green"]["hued_pct"] >= logo_matte.CLASH_HUED_PCT+ assert choice["scores"]["green"]["clashes"] and choice["scores"]["magenta"]["clashes"]+ assert choice["key"] is None+++def _cyan_title_with_mint(mint_pct: float) -> Image.Image:+ # cyan lettering keeps green's hue longest (it would win green), with a few mint patches in it+ mood = np.full((50, 100, 3), (40, 230, 255), dtype=np.uint8)+ mood.reshape(-1, 3)[:int(round(mint_pct / 100 * 5000))] = (150, 235, 190)+ return Image.fromarray(mood, "RGB")+++def test_a_colour_in_a_keys_hue_rules_the_key_out() -> None:+ assert logo_matte.choose_key(_cyan_title_with_mint(0))["key"] == "green"+ choice = logo_matte.choose_key(_cyan_title_with_mint(6))+ assert choice["scores"]["green"]["clashes"] and choice["key"] == "magenta"+++def test_a_colour_the_brief_names_rules_its_key_out_at_a_smaller_share() -> None:+ named = {"green": ["mint"], "magenta": []}+ choice = logo_matte.choose_key(_cyan_title_with_mint(2), named=named)+ assert choice["scores"]["green"]["named"] == ["mint"] and choice["key"] == "magenta"+ assert logo_matte.choose_key(_cyan_title_with_mint(2))["key"] == "green" # not named: 2 % may be scenery+ assert logo_matte.choose_key(_cyan_title_with_mint(0), named=named)["key"] == "green" # a word alone does not+++def test_see_through_inside_the_letters_is_found() -> None:+ # on green the mint patches are a mix with the key, cut see-through inside the plum-outlined letters+ image, mint = quilt()+ cut, _ = logo_matte.matte(image, key="green")+ inside = logo_matte.see_through_inside(cut)+ alpha = np.asarray(cut)[..., 3]+ assert ((alpha[mint] > 0) & (alpha[mint] < 255)).mean() > 0.9+ assert inside["px"] >= 0.4 * mint.sum() and inside["wide"]+ assert inside["pct_of_visible"] >= logo_matte.INSIDE_MAX_PCT+++def test_the_other_key_keeps_the_mint_patches_and_finds_nothing_inside() -> None:+ # the same letters with peach for pink in a navy outline: nothing in magenta's hue, so magenta keeps them all+ image, mint = quilt(key=MAGENTA_PAINTED, colours=((240, 170, 120), BUTTER, MINT, SKY), outline=(30, 34, 70))+ cut, _ = logo_matte.matte(image, key="magenta")+ assert (np.asarray(cut)[..., 3][mint] == 255).mean() > 0.95+ assert not logo_matte.see_through_inside(cut)["wide"]++[email protected]("item, key", [+ (lambda: lettering(), "magenta"),+ (lambda: lettering(fill=(232, 182, 52), outline=(20, 24, 40), glow=(40, 230, 255), glow_radius=4.0,+ glow_strength=0.8), "magenta"),+ (lambda: lettering(fill=(232, 182, 52), outline=(20, 24, 40), glow=(40, 230, 255), glow_radius=9.0,+ key=GREEN_PAINTED), "green"), # a glow pooled in the counter of the O+ (lambda: lettering(fill=(232, 182, 52), outline=(20, 24, 40), glow=(40, 230, 255), glow_radius=9.0), "magenta"),+ (lambda: lettering(fill=(255, 214, 40), glow=(255, 120, 20), glow_radius=5.0, glow_strength=0.7,+ key=GREEN_PAINTED), "green"),+ (lambda: lettering(glow=None, pixel=True, noise=0.0, size=(96, 48)), "magenta"),+], ids=["white-glow", "outlined-cyan-glow", "pooled-glow-green", "pooled-glow-magenta", "orange-glow-green", "pixel"])+def test_a_glow_is_not_see_through_inside(item, key) -> None:+ cut, _ = logo_matte.matte(item().image, key=key)+ assert not logo_matte.see_through_inside(cut)["wide"]++def test_the_key_is_read_off_the_lettering_box_not_the_scene() -> None:mood = Image.new("RGB", (100, 50), (40, 200, 60)) # a forest scene ...for x in range(40, 60):diff --git a/tests/gen/test_logo.py b/tests/gen/test_logo.pyindex 2a4f17f..13e20e7 100644--- a/tests/gen/test_logo.py+++ b/tests/gen/test_logo.py@@ -17,8 +17,9 @@ import numpy as npimport pytestfrom PIL import Image-from logo_lettering import GREEN_PAINTED, MAGENTA_PAINTED, lettering+from logo_lettering import GREEN_PAINTED, MAGENTA_PAINTED, lettering, quiltfrom sprite_gen import gen+from sprite_gen.frames import logo_mattefrom sprite_gen.gen import base as gen_basefrom sprite_gen.gen import logo@@ -374,6 +375,60 @@ def test_an_explicit_key_is_kept(tmp_path, fake) -> None:assert _report(tmp_path)["steps"]["key"]["source"] == "explicit"+# A key in the title's own hue turns those letters see-through (docs/logo.md, which key): patchwork letters in pink and+# plum (magenta's hue) with mint patches (green's) leave neither key, and a see-through inside the letters is refused.+QUILT_STYLE = "patchwork pixel letters of pastel fabric patches (pink, mint, butter yellow, sky blue), a plum outline"+++def test_a_title_in_both_keys_hues_is_refused_before_the_lettering_is_bought(tmp_path, fake) -> None:+ provider = fake(mood=quilt(key=(30, 40, 90))[0])+ with pytest.raises(SystemExit, match="both keys' hues") as refusal:+ _run(tmp_path, style=QUILT_STYLE)+ assert "--chroma-key" in str(refusal.value) and "mint" in str(refusal.value)+ assert len(provider.requests) == 1 # the mood image only: the lettering was not bought+ assert not (tmp_path / "logo" / "lettering.png").exists()+ report = _report(tmp_path)+ assert report["status"] == "refused" and report["refused_at"] == "key"+ key = report["steps"]["key"]+ assert key["key"] is None and key["source"] == "auto"+ assert key["scores"]["green"]["clashes"] and key["scores"]["magenta"]["clashes"]+ assert key["scores"]["green"]["named"] == ["mint"] and key["scores"]["magenta"]["named"] == ["pink", "plum"]+++def test_a_key_named_in_the_titles_hue_is_kept_and_said(tmp_path, fake, capsys) -> None:+ provider = fake(mood=quilt(key=(30, 40, 90))[0])+ assert _run(tmp_path, style=QUILT_STYLE, chroma_key="magenta") == 0+ assert "#FF00FF" in provider.requests[1].prompt+ report = _report(tmp_path)+ assert report["steps"]["key"]["key"] == "magenta" and report["steps"]["key"]["source"] == "explicit"+ assert any("magenta key named is one of the title's colours" in warning for warning in report["warnings"])+ assert "see-through" in capsys.readouterr().err+++def test_see_through_inside_the_letters_is_refused_and_kept_aside(tmp_path, fake) -> None:+ provider = fake(reads=(["ION"],))+ out = tmp_path / "logo"+ out.mkdir()+ quilt()[0].save(out / "lettering.png") # on green: the mint patches are cut see-through+ with pytest.raises(SystemExit, match="see-through inside") as refusal:+ _run(tmp_path, lettering=out / "lettering.png")+ assert "--chroma-key magenta" in str(refusal.value)+ assert provider.looked == [] # refused before the text check+ assert not (out / "logo.png").exists() and (out / "logo.rejected.png").is_file()+ assert (out / "logo.preview.png").is_file()+ report = _report(tmp_path)+ assert report["status"] == "refused" and report["refused_at"] == "matte"+ inside = report["logo"]["see_through_inside"]+ assert inside["pct_of_visible"] >= logo_matte.INSIDE_MAX_PCT and inside["wide"]+++def test_a_glowing_title_is_not_see_through_inside(tmp_path, fake) -> None:+ fake()+ assert _run(tmp_path) == 0+ report = _report(tmp_path)+ assert report["status"] == "ok" and not report["logo"]["see_through_inside"]["wide"]++def test_no_font_is_ever_drawn(tmp_path, fake, monkeypatch) -> None:from PIL import ImageDraw, ImageFontdiff --git a/tests/logo_lettering.py b/tests/logo_lettering.pyindex bdeee8d..d09104d 100644--- a/tests/logo_lettering.py+++ b/tests/logo_lettering.py@@ -93,6 +93,38 @@ def lettering(return Lettering(image=image, alpha=alpha, colour=true_colour, letters=solid, background=background)+PINK, BUTTER, MINT, SKY = (240, 140, 200), (250, 230, 140), (150, 235, 190), (140, 200, 240)+PLUM = (90, 40, 80)+QUILT = (PINK, BUTTER, MINT, SKY) # pink and plum are in magenta's hue, mint in green's+++def quilt(*, size: tuple[int, int] = (320, 160), key: tuple[int, int, int] = GREEN_PAINTED,+ colours: tuple[tuple[int, int, int], ...] = QUILT, outline: tuple[int, int, int] = PLUM, noise: float = 2.0,+ patch: int = 14, seed: int = 7) -> tuple[Image.Image, np.ndarray]:+ """Patchwork pixel letters: three outlined blocks (the middle one with a counter the key shows through)+ filled with square fabric patches (pink, butter yellow, mint and sky blue in a plum outline), on a key.+ Returns (the RGB picture, the mint patches' mask)."""+ width, height = size+ rgb = np.broadcast_to(np.array(key, dtype=np.float64), (height, width, 3)).copy()+ mint = np.zeros((height, width), dtype=bool)+ top, bottom, wide, rim = int(height * 0.18), int(height * 0.82), int(width * 0.2), 4+ for number, left in enumerate((int(width * 0.08), int(width * 0.4), int(width * 0.72))):+ rgb[top:bottom, left:left + wide] = outline+ for row, y in enumerate(range(top + rim, bottom - rim, patch)):+ for column, x in enumerate(range(left + rim, left + wide - rim, patch)):+ y1, x1 = min(y + patch, bottom - rim), min(x + patch, left + wide - rim)+ colour = colours[(row + column + number) % len(colours)]+ rgb[y:y1, x:x1] = colour+ mint[y:y1, x:x1] = colour == MINT+ if number == 1: # a counter, outlined, with the key showing through it+ cy, cx, half_h, half_w = (top + bottom) // 2, left + wide // 2, height // 8, wide // 6+ rgb[cy - half_h - rim:cy + half_h + rim, cx - half_w - rim:cx + half_w + rim] = outline+ rgb[cy - half_h:cy + half_h, cx - half_w:cx + half_w] = key+ mint[cy - half_h - rim:cy + half_h + rim, cx - half_w - rim:cx + half_w + rim] = False+ rgb += np.random.default_rng(seed).normal(0.0, noise, rgb.shape)+ return Image.fromarray(np.uint8(np.clip(np.round(rgb), 0, 255)), "RGB"), mint++def key_excess(rgb: np.ndarray, key: str = "magenta") -> np.ndarray:"""How much of the key's hue a colour carries: min(R,B)-G for magenta, G-max(R,B) for green."""rgb = np.asarray(rgb, dtype=np.float64)