"""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'' f'' f'' f'' f'' ) if category_id == "tools": return ( f'' f'' f'' f'' f'' ) if category_id in {"lawn-garden", "outdoor-living"}: return ( f'' f'' f'' f'' f'' ) if category_id in {"flooring", "building-supplies"}: return ( f'' f'' f'' f'' ) if category_id in {"plumbing", "bathroom", "kitchen"}: return ( f'' f'' f'' ) if category_id == "lighting": return ( f'' f'' f'' f'' ) if category_id == "paint": return ( f'' f'' f'' f'' ) if category_id in {"electrical", "hardware"}: return ( f'' f'' f'' f'' ) return ( f'' f'' f'' ) 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'' if view_index == 2 else "" ) return ( '' f"{_escape(title)}" f'' f"{context_floor}" f"{_shape(str(product['category_id']), light, dark, jitter)}" f'{_escape(label)}' "" ) def _escape(value: str) -> str: return ( value.replace("&", "&") .replace("<", "<") .replace(">", ">") .replace('"', """) )