design-system: logo legibility check script

logo_check.py renders one SVG into one PNG with four panels: 16 px,
one-colour, inverted and squint, small panels enlarged nearest-neighbour.

Todo: f75e1003-1
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Krzysztof Rudnicki 2026-11-19 12:30:41 +01:00
parent 7ec573a373
commit d729d732a1
3 changed files with 222 additions and 0 deletions

View File

@ -0,0 +1,138 @@
"""Render the four logo legibility checks for one SVG into one PNG.
Panels, left to right (order is the only label: the PNG is a dev artifact
and carries no text):
1. full colour at 16 px, enlarged to 128 px nearest-neighbour
2. one-colour: field dropped, every mark in ink, on paper
3. inverted: ink and accent swapped (accent field, ink mark)
4. squint: 128 px render blurred, reduced to 16 px, enlarged nearest-neighbour
A candidate SVG paints its background as an element with ``id="field"`` and
uses only the palette's ink and accent. Rendering goes through
``rsvg-convert``; compositing through Pillow.
Run: python3 unified-design-system/scripts/logo_check.py <svg> [--out PNG]
Exit 0 = PNG written; 1 = SVG missing or render failed.
"""
from __future__ import annotations
import argparse
import io
import re
import subprocess
import sys
import xml.etree.ElementTree as ET
from pathlib import Path
from PIL import Image, ImageFilter
INK = "#211D1B"
ACCENT = "#B8862E"
PAPER = "#F6F4F3"
PANEL = 128 # every panel is shown at this size
SMALL = 16 # favicon-size check
SQUINT = 16 # squint grid: blur, then this many pixels across
SQUINT_BLUR = 6 # Gaussian radius at PANEL size
GAP = 8
SVG_NS = "http://www.w3.org/2000/svg"
ET.register_namespace("", SVG_NS)
_INK_RE = re.compile(re.escape(INK), re.IGNORECASE)
_ACCENT_RE = re.compile(re.escape(ACCENT), re.IGNORECASE)
def render(svg_text: str, size: int) -> Image.Image:
"""Rasterise SVG text to a square RGBA image of ``size`` pixels."""
png = subprocess.run(
["rsvg-convert", "-w", str(size), "-h", str(size), "-f", "png"],
input=svg_text.encode(),
capture_output=True,
check=True,
).stdout
return Image.open(io.BytesIO(png)).convert("RGBA")
def on_background(image: Image.Image, colour: str) -> Image.Image:
"""Flatten an RGBA image onto a solid colour, returning RGB."""
base = Image.new("RGBA", image.size, colour)
base.alpha_composite(image)
return base.convert("RGB")
def enlarge(image: Image.Image, size: int = PANEL) -> Image.Image:
"""Scale up with hard pixel edges so small-size artefacts stay visible."""
return image.resize((size, size), Image.Resampling.NEAREST)
def inverted(svg_text: str) -> str:
"""Swap ink and accent everywhere, so the field turns accent."""
placeholder = "#__SWAP__"
text = _INK_RE.sub(placeholder, svg_text)
text = _ACCENT_RE.sub(INK, text)
return text.replace(placeholder, ACCENT)
def one_colour(svg_text: str) -> str:
"""Drop the field and paint every mark in ink."""
root = ET.fromstring(svg_text)
for parent in root.iter():
for child in list(parent):
if child.get("id") == "field":
parent.remove(child)
return _ACCENT_RE.sub(INK, ET.tostring(root, encoding="unicode"))
def squint(svg_text: str) -> Image.Image:
"""Blur and coarsen the render, the way the mark reads at a glance."""
blurred = on_background(render(svg_text, PANEL), PAPER).filter(
ImageFilter.GaussianBlur(SQUINT_BLUR)
)
return blurred.resize((SQUINT, SQUINT), Image.Resampling.BOX)
def panels(svg_text: str) -> list[Image.Image]:
"""The four check panels in their fixed order, each PANEL px square."""
return [
enlarge(on_background(render(svg_text, SMALL), PAPER)),
on_background(render(one_colour(svg_text), PANEL), PAPER),
on_background(render(inverted(svg_text), PANEL), PAPER),
enlarge(squint(svg_text)),
]
def compose(images: list[Image.Image]) -> Image.Image:
"""Lay the panels out in one row with a paper gutter."""
width = len(images) * PANEL + (len(images) + 1) * GAP
sheet = Image.new("RGB", (width, PANEL + 2 * GAP), PAPER)
for index, image in enumerate(images):
sheet.paste(image, (GAP + index * (PANEL + GAP), GAP))
return sheet
def main(argv: list[str] | None = None) -> int:
"""Write ``<svg stem>-check.png`` (or ``--out``) for one candidate SVG."""
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("svg", type=Path)
parser.add_argument("--out", type=Path)
args = parser.parse_args(argv)
svg: Path = args.svg
if not svg.is_file():
print(f"logo_check: no such file: {svg}", file=sys.stderr)
return 1
out: Path = args.out or svg.with_name(f"{svg.stem}-check.png")
try:
sheet = compose(panels(svg.read_text()))
except (subprocess.CalledProcessError, ET.ParseError) as exc:
print(f"logo_check: cannot render {svg}: {exc}", file=sys.stderr)
return 1
sheet.save(out, optimize=True)
print(out)
return 0
if __name__ == "__main__":
sys.exit(main())

View File

@ -0,0 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
<rect id="field" width="512" height="512" fill="#211D1B"/>
<path d="M176 136V376M336 136L176 256L336 376" fill="none" stroke="#B8862E" stroke-width="40" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

After

Width:  |  Height:  |  Size: 301 B

View File

@ -0,0 +1,80 @@
"""Tests for logo_check.py: one PNG, four panels, hard pixel edges, no text."""
from __future__ import annotations
import shutil
from pathlib import Path
import pytest
Image = pytest.importorskip("PIL.Image")
if shutil.which("rsvg-convert") is None: # pragma: no cover - env guard
pytest.skip("rsvg-convert not installed", allow_module_level=True)
import logo_check
FIXTURE = Path(__file__).parent / "fixtures" / "logo-fixture.svg"
INK = (0x21, 0x1D, 0x1B)
ACCENT = (0xB8, 0x86, 0x2E)
def _panel(sheet: Image.Image, index: int) -> Image.Image:
left = logo_check.GAP + index * (logo_check.PANEL + logo_check.GAP)
top = logo_check.GAP
return sheet.crop((left, top, left + logo_check.PANEL, top + logo_check.PANEL))
@pytest.fixture
def sheet(tmp_path: Path) -> Image.Image:
out = tmp_path / "check.png"
assert logo_check.main([str(FIXTURE), "--out", str(out)]) == 0
assert out.is_file()
return Image.open(out).convert("RGB")
def test_sheet_has_four_panels_side_by_side(sheet: Image.Image) -> None:
gap, panel = logo_check.GAP, logo_check.PANEL
assert sheet.size == (4 * panel + 5 * gap, panel + 2 * gap)
def test_small_panel_is_nearest_neighbour_blocks(sheet: Image.Image) -> None:
"""The 16 px render is enlarged 8x with hard edges: every 8x8 block is flat."""
small = _panel(sheet, 0)
step = logo_check.PANEL // logo_check.SMALL
for by in range(logo_check.SMALL):
for bx in range(logo_check.SMALL):
block = small.crop((bx * step, by * step, (bx + 1) * step, (by + 1) * step))
assert len(block.getcolors()) == 1
def test_squint_panel_is_nearest_neighbour_blocks(sheet: Image.Image) -> None:
squint = _panel(sheet, 3)
step = logo_check.PANEL // logo_check.SQUINT
block = squint.crop((0, 0, step, step))
assert len(block.getcolors()) == 1
assert len(squint.getcolors(maxcolors=4096)) > 2 # blurred, not flat
def test_one_colour_panel_uses_ink_on_paper_only(sheet: Image.Image) -> None:
panel = _panel(sheet, 1)
colours = {c for _, c in panel.getcolors(maxcolors=4096)}
assert INK in colours
assert ACCENT not in colours
def test_inverted_panel_swaps_field_and_mark(sheet: Image.Image) -> None:
panel = _panel(sheet, 2)
assert panel.getpixel((2, 2)) == ACCENT # field corner is now accent
colours = {c for _, c in panel.getcolors(maxcolors=4096)}
assert INK in colours
def test_default_out_path_sits_next_to_svg(tmp_path: Path) -> None:
svg = tmp_path / "x.svg"
svg.write_text(FIXTURE.read_text())
assert logo_check.main([str(svg)]) == 0
assert (tmp_path / "x-check.png").is_file()
def test_missing_svg_exits_nonzero(tmp_path: Path) -> None:
assert logo_check.main([str(tmp_path / "nope.svg")]) == 1