AgPerry's picture
Add files using upload-large-folder tool
489b6e2 verified
Raw History Blame Contribute Delete
6.67 kB
"""Deterministic, locally generated product artwork.
The clone ships no source photography. Every product tile and gallery frame is
an SVG drawn here from the product's own row: the category supplies the two
palette colors, and the product id supplies a stable pseudo-variation so the
same product always renders the same picture. There is no randomness, so a
reset produces byte-identical images.
"""
from __future__ import annotations
from typing import Mapping
VIEWS = ("front", "angle", "detail")
def _spread(product_id: int, view_index: int, span: int) -> int:
"""Stable small offset in ``0..span-1`` derived from id and view."""
return ((product_id * 37) + (view_index * 11)) % span
def _shade(hex_color: str, factor: float) -> str:
value = hex_color.lstrip("#")
parts = [int(value[index : index + 2], 16) for index in (0, 2, 4)]
mixed = [max(0, min(255, int(part * factor))) for part in parts]
return "#{:02x}{:02x}{:02x}".format(*mixed)
def _shape(category_id: str, light: str, dark: str, jitter: int) -> str:
mid = _shade(dark, 1.25)
edge = _shade(dark, 0.75)
if category_id == "appliances":
return (
f'<rect x="70" y="{40 + jitter}" width="180" height="{220 - jitter}" rx="10" fill="{light}"/>'
f'<rect x="82" y="{54 + jitter}" width="156" height="92" rx="6" fill="{mid}"/>'
f'<rect x="82" y="{158 + jitter}" width="156" height="{88 - jitter}" rx="6" fill="{mid}"/>'
f'<rect x="156" y="{40 + jitter}" width="8" height="{220 - jitter}" fill="{edge}"/>'
f'<rect x="100" y="{92 + jitter}" width="42" height="8" rx="4" fill="{edge}"/>'
)
if category_id == "tools":
return (
f'<rect x="72" y="{112 + jitter}" width="126" height="62" rx="12" fill="{dark}"/>'
f'<rect x="186" y="{124 + jitter}" width="58" height="38" rx="8" fill="{mid}"/>'
f'<path d="M104 {174 + jitter} l52 0 l10 74 l-72 0 z" fill="{light}"/>'
f'<rect x="236" y="{134 + jitter}" width="34" height="18" rx="4" fill="{edge}"/>'
f'<circle cx="122" cy="{143 + jitter}" r="12" fill="{light}"/>'
)
if category_id in {"lawn-garden", "outdoor-living"}:
return (
f'<path d="M60 {216 + jitter} q100 -96 200 0 z" fill="{dark}"/>'
f'<rect x="96" y="{132 + jitter}" width="128" height="60" rx="16" fill="{light}"/>'
f'<circle cx="104" cy="{224 + jitter}" r="24" fill="{edge}"/>'
f'<circle cx="216" cy="{224 + jitter}" r="24" fill="{edge}"/>'
f'<rect x="200" y="{88 + jitter}" width="14" height="56" rx="7" fill="{mid}"/>'
)
if category_id in {"flooring", "building-supplies"}:
return (
f'<rect x="54" y="{92 + jitter}" width="212" height="34" rx="4" fill="{light}"/>'
f'<rect x="54" y="{132 + jitter}" width="212" height="34" rx="4" fill="{mid}"/>'
f'<rect x="54" y="{172 + jitter}" width="212" height="34" rx="4" fill="{light}"/>'
f'<rect x="54" y="{212 + jitter}" width="212" height="34" rx="4" fill="{edge}"/>'
)
if category_id in {"plumbing", "bathroom", "kitchen"}:
return (
f'<path d="M132 {242 + jitter} l0 -96 q0 -52 52 -52 q46 0 46 46 l0 26"'
f' fill="none" stroke="{dark}" stroke-width="20" stroke-linecap="round"/>'
f'<rect x="104" y="{238 + jitter}" width="112" height="20" rx="8" fill="{mid}"/>'
f'<circle cx="132" cy="{126 + jitter}" r="16" fill="{light}"/>'
)
if category_id == "lighting":
return (
f'<path d="M160 {60 + jitter} l84 78 l-168 0 z" fill="{light}"/>'
f'<rect x="150" y="{138 + jitter}" width="20" height="52" fill="{mid}"/>'
f'<circle cx="160" cy="{212 + jitter}" r="34" fill="{dark}"/>'
f'<circle cx="160" cy="{212 + jitter}" r="18" fill="{_shade(light, 1.05)}"/>'
)
if category_id == "paint":
return (
f'<rect x="98" y="{100 + jitter}" width="124" height="146" rx="8" fill="{dark}"/>'
f'<rect x="98" y="{100 + jitter}" width="124" height="34" rx="8" fill="{mid}"/>'
f'<path d="M92 {104 + jitter} q68 -50 136 0" fill="none" stroke="{edge}" stroke-width="8"/>'
f'<rect x="122" y="{160 + jitter}" width="76" height="56" rx="4" fill="{light}"/>'
)
if category_id in {"electrical", "hardware"}:
return (
f'<rect x="92" y="{92 + jitter}" width="136" height="152" rx="12" fill="{light}"/>'
f'<circle cx="160" cy="{140 + jitter}" r="22" fill="{dark}"/>'
f'<rect x="140" y="{184 + jitter}" width="40" height="46" rx="6" fill="{mid}"/>'
f'<rect x="112" y="{112 + jitter}" width="16" height="16" rx="3" fill="{edge}"/>'
)
return (
f'<rect x="80" y="{80 + jitter}" width="160" height="164" rx="10" fill="{light}"/>'
f'<rect x="100" y="{104 + jitter}" width="120" height="56" rx="6" fill="{mid}"/>'
f'<rect x="100" y="{176 + jitter}" width="120" height="44" rx="6" fill="{edge}"/>'
)
def product_svg(
product: Mapping[str, object],
category: Mapping[str, object],
view: str = "front",
) -> str:
view_index = VIEWS.index(view) if view in VIEWS else 0
product_id = int(product["id"])
light = str(category.get("art_light", "#d6d9dd"))
dark = str(category.get("art_dark", "#7b828b"))
jitter = _spread(product_id, view_index, 12) - 6
backdrop = "#ffffff" if view_index != 2 else _shade(light, 1.06)
label = str(product.get("brand", ""))
title = str(product.get("title", ""))
rotation = ""
if view_index == 1:
rotation = ' transform="rotate(-8 160 160)"'
context_floor = (
f'<rect x="0" y="250" width="320" height="70" fill="{_shade(dark, 1.5)}"/>'
if view_index == 2
else ""
)
return (
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 320" '
'width="320" height="320" role="img" '
f'aria-label="{_escape(title)}">'
f"<title>{_escape(title)}</title>"
f'<rect width="320" height="320" fill="{backdrop}"/>'
f"{context_floor}"
f"<g{rotation}>{_shape(str(product['category_id']), light, dark, jitter)}</g>"
f'<text x="160" y="300" text-anchor="middle" font-family="Helvetica, Arial, sans-serif"'
f' font-size="15" fill="{_shade(dark, 0.8)}">{_escape(label)}</text>'
"</svg>"
)
def _escape(value: str) -> str:
return (
value.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace('"', "&quot;")
)