INNER CODE UNIT · Python
build_svg
WenyuChiou/awesome-agentic-ai-zh · scripts/build-role-map.py:104
def build_svg(locale, art):
from PIL import Image
_, (width, height), title, description = LOCALES[locale]
parts = [f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}" viewBox="0 0 {width} {height}" lang="{locale}" role="img" aria-labelledby="title desc">',
f'<title id="title">{escape(title)}</title><desc id="desc">{escape(description)}</desc>',
f'<metadata>Original artwork: {SOURCE_COMMIT}; WebP quality 95; icon motion only. Existing localized labels preserved.</metadata>',
f'<style>{animation_css()}</style>',
f'<image id="original-art" width="{width}" height="{height}" href="data:image/webp;base64,{base64.b64encode(art).decode("ascii")}"/>']
with Image.open(io.BytesIO(art)) as source:
source = source.convert("RGB")
for role, (x, y, w, h) in zip(ROLES, ICONS[locale], strict=True):
corners = [source.getpixel(p) for p in ((x,y), (x+w-1,y), (x,y+h-1), (x+w-1,y+h-1))]
paper = "#" + "".join(f"{int(median(c)):02x}" for c in zip(*corners))
cx, cy = x+w/2, y+h if role == "knowledge" else y+h/2
parts.append(f'<g id="role-{role}" class="role-layer role-{role}" aria-hidden="true">'
f'<defs><clipPath id="clip-{role}"><rect x="{x}" y="{y}" width="{w}" height="{h}"/></clipPath></defs>'
f'<rect x="{x}" y="{y}" width="{w}" height="{h}" fill="{paper}"/>'