{"spec_id":"datamatrix-basic","library":"altair","language":"python","code":"\"\"\" anyplot.ai\ndatamatrix-basic: Basic Data Matrix 2D Barcode\nLibrary: altair 6.1.0 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-20\n\"\"\"\n\nimport os\nimport sys\nimport zlib\n\n\n# Remove this file's directory from sys.path to avoid circular import with the altair package\nif sys.path and os.path.exists(os.path.join(sys.path[0] or \".\", \"altair.py\")):\n    sys.path = sys.path[1:]\n\nimport altair as alt\nimport numpy as np\nimport pandas as pd\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nQUIET_FILL = \"#C8C5BE\"  # warm gray on white inner bg shows quiet zone in both themes\n\n# Data — 16×16 Data Matrix ECC 200 barcode encoding \"SERIAL:12345678\"\nnp.random.seed(42)\nsize = 16\nquiet_zone = 1  # minimum 1 module per spec\ntotal_size = size + 2 * quiet_zone  # 18\n\nmatrix = np.zeros((total_size, total_size), dtype=int)\n\n# L-shaped finder pattern: solid black on left and bottom edges\nfor row in range(size):\n    matrix[quiet_zone + row, quiet_zone] = 1\nfor col in range(size):\n    matrix[quiet_zone + size - 1, quiet_zone + col] = 1\n\n# Alternating timing pattern on top and right edges\nfor col in range(size):\n    matrix[quiet_zone, quiet_zone + col] = col % 2\nfor row in range(size):\n    matrix[quiet_zone + row, quiet_zone + size - 1] = row % 2\n\n# Interior data area: deterministic bit pattern derived from content\ncontent = \"SERIAL:12345678\"\nhash_val = zlib.crc32(content.encode())  # zlib.crc32 is deterministic unlike hash()\nfor row in range(1, size - 1):\n    for col in range(1, size - 1):\n        idx = row * size + col\n        matrix[quiet_zone + row, quiet_zone + col] = ((hash_val >> (idx % 32)) ^ (idx * 13)) % 2\n\n# Convert to DataFrame; color_key collapses cell_type × value for Altair's color encoding\nrows = []\nfor row in range(total_size):\n    for col in range(total_size):\n        r, c = row - quiet_zone, col - quiet_zone\n        if r < 0 or r >= size or c < 0 or c >= size:\n            cell_type = \"Quiet Zone\"\n        elif c == 0 or r == size - 1:\n            cell_type = \"Finder Pattern\"\n        elif r == 0 or c == size - 1:\n            cell_type = \"Timing Pattern\"\n        else:\n            cell_type = \"Data Cell\"\n        val = matrix[row, col]\n        color_key = \"quiet\" if cell_type == \"Quiet Zone\" else (\"on\" if val == 1 else \"off\")\n        rows.append(\n            {\n                \"x\": col,\n                \"y\": total_size - 1 - row,  # flip y so row=0 maps to chart top\n                \"value\": val,\n                \"cell_type\": cell_type,\n                \"color_key\": color_key,\n            }\n        )\ndf = pd.DataFrame(rows)\n\n# Interactive selection — click a region type to highlight it; empty=True keeps all opaque when idle\nregion_sel = alt.selection_point(fields=[\"cell_type\"], on=\"click\", empty=True, clear=\"dblclick\")\n\n# 720×720 plot area: 18 cells × 40 px/cell = 720 px (integer — eliminates sub-pixel gap artifacts)\nno_pad = alt.Scale(paddingInner=0, paddingOuter=0)\ncolor_scale = alt.Scale(domain=[\"on\", \"off\", \"quiet\"], range=[\"#000000\", \"#FFFFFF\", QUIET_FILL])\n\nchart = (\n    alt.Chart(df)\n    .mark_rect(stroke=None)\n    .encode(\n        x=alt.X(\"x:O\", axis=None, scale=no_pad),\n        y=alt.Y(\"y:O\", axis=None, scale=no_pad),\n        color=alt.Color(\"color_key:N\", scale=color_scale, legend=None),\n        opacity=alt.condition(region_sel, alt.value(1.0), alt.value(0.35)),\n        tooltip=[\n            alt.Tooltip(\"cell_type:N\", title=\"Region\"),\n            alt.Tooltip(\"x:O\", title=\"Column\"),\n            alt.Tooltip(\"y:O\", title=\"Row\"),\n        ],\n    )\n    .add_params(region_sel)\n    .properties(\n        width=720,\n        height=720,\n        background=PAGE_BG,\n        title=alt.Title(\n            \"datamatrix-basic · python · altair · anyplot.ai\",\n            subtitle=[\n                \"Content: SERIAL:12345678  ·  16×16 ECC 200  ·  Quiet zone shown in gray\",\n                \"Click a region to highlight: Finder (L-shape) · Timing (alternating) · Data · Quiet Zone\",\n            ],\n            fontSize=16,\n            subtitleFontSize=10,\n            color=INK,\n            subtitleColor=INK_SOFT,\n            anchor=\"middle\",\n        ),\n    )\n    .configure_view(fill=\"#FFFFFF\", strokeWidth=0)\n    .configure_axis(grid=False)\n)\n\n# Save\nchart.save(f\"plot-{THEME}.png\", scale_factor=4.0)\nchart.save(f\"plot-{THEME}.html\")\n"}