{"spec_id":"datamatrix-basic","library":"makie","language":"julia","code":"# anyplot.ai\n# datamatrix-basic: Basic Data Matrix 2D Barcode\n# Library: makie 0.21.9 | Julia 1.11.9\n# Quality: 90/100 | Created: 2026-09-02\n\nusing CairoMakie\nusing Colors\nusing Random\n\nconst THEME    = get(ENV, \"ANYPLOT_THEME\", \"light\")\nconst PAGE_BG  = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nconst INK      = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\n\n# A printed Data Matrix code is black-on-white for scanner contrast under any\n# lighting, so it does not follow the page theme the way surrounding chrome\n# does — these two stay fixed instead of branching on THEME.\nconst DM_BG  = colorant\"#FFFDF6\"\nconst DM_INK = colorant\"#1A1A17\"\n\n# --- Data ---------------------------------------------------------------\n# Pharmaceutical serialization code, one of the spec's listed applications.\ncontent = \"RX-88451-2024-A7\"\nn       = 20  # modules per side, including the finder + clock-track border\nquiet   = 3   # ISO/IEC 16022 quiet zone, in modules (spec asks for >= 1)\nm       = n + 2 * quiet\n\nRandom.seed!(42)\nz = zeros(Bool, m, m)\nfor x in 1:n, y in 1:n\n    gx, gy = quiet + x, quiet + y\n    z[gx, gy] =\n        if x == 1\n            true         # left column: solid L-shaped finder pattern\n        elseif y == 1\n            true         # bottom row: solid L-shaped finder pattern\n        elseif y == n\n            isodd(x)     # top row: alternating clock (timing) track\n        elseif x == n\n            isodd(y)     # right column: alternating clock (timing) track\n        else\n            rand(Bool)   # data region: representative module fill — full\n                          # ECC200 Reed-Solomon codeword placement is out of\n                          # scope for this static illustration\n        end\nend\n\n# --- Plot -----------------------------------------------------------------\n# Structural-region accent colors: annotation chrome that teaches the\n# ISO/IEC 16022 layout, kept fixed across themes like other categorical\n# data colors (only page/ink chrome flips with THEME).\nconst FINDER_COLOR = colorant\"#009E73\"  # Imprint palette 1 — L-shaped finder pattern\nconst CLOCK_COLOR  = colorant\"#4467A3\"  # Imprint palette 3 — alternating clock track\n\ntitle_str      = \"Pharmaceutical Serialization · datamatrix-basic · julia · makie · anyplot.ai\"\ntitle_fontsize = max(13, round(Int, 20 * min(1.0, 67 / length(title_str))))\n\nfig = Figure(\n    size            = (1200, 1200),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    aspect             = DataAspect(),\n    backgroundcolor    = DM_BG,\n    title              = title_str,\n    titlesize          = title_fontsize,\n    titlecolor         = INK,\n    xlabel             = \"Encodes \\\"$content\\\" · $(n)×$(n) modules + quiet zone · finder pattern (green) + clock track (blue) per ISO/IEC 16022\",\n    xlabelsize         = 15,\n    xlabelcolor        = INK_SOFT,\n    topspinevisible    = false,\n    rightspinevisible  = false,\n    leftspinevisible   = false,\n    bottomspinevisible = false,\n    xticksvisible      = false,\n    yticksvisible      = false,\n    xticklabelsvisible = false,\n    yticklabelsvisible = false,\n    xgridvisible       = false,\n    ygridvisible       = false,\n)\n\n# Per-module rectangles via `poly!` (rather than `heatmap!`) give crisp,\n# individually stroked cell borders — a distinctive Makie layered-plotting\n# feature — and let us overlay colored structure annotations on top.\ncell(x0, y0) = Point2f[(x0, y0), (x0 + 1, y0), (x0 + 1, y0 + 1), (x0, y0 + 1)]\ncells  = vec([cell(x - 1, y - 1) for x in 1:m, y in 1:m])\nfills  = vec([z[x, y] ? DM_INK : DM_BG for x in 1:m, y in 1:m])\npoly!(ax, cells; color = fills, strokecolor = DM_BG, strokewidth = 1.0)\n\n# Highlight the L-shaped finder pattern (left column + bottom row) and the\n# alternating clock track (top row + right column) with colored outlines,\n# so the ISO/IEC 16022 structure reads directly from the image instead of\n# relying solely on the caption text.\nfinder_outline = Point2f[\n    (quiet, quiet + n), (quiet, quiet), (quiet + n, quiet),\n    (quiet + n, quiet + 1), (quiet + 1, quiet + 1), (quiet + 1, quiet + n),\n    (quiet, quiet + n),\n]\nclock_outline = Point2f[\n    (quiet, quiet + n), (quiet + n, quiet + n), (quiet + n, quiet),\n    (quiet + n - 1, quiet), (quiet + n - 1, quiet + n - 1), (quiet, quiet + n - 1),\n    (quiet, quiet + n),\n]\nlines!(ax, finder_outline; color = FINDER_COLOR, linewidth = 3)\nlines!(ax, clock_outline; color = CLOCK_COLOR, linewidth = 3)\n\ntext!(ax, \"FINDER\";\n    position  = Point2f(quiet - 1.5, quiet + n / 2),\n    rotation  = pi / 2,\n    align     = (:center, :center),\n    color     = FINDER_COLOR,\n    fontsize  = 11,\n)\ntext!(ax, \"CLOCK TRACK\";\n    position  = Point2f(quiet + n / 2, quiet + n + 1.5),\n    align     = (:center, :center),\n    color     = CLOCK_COLOR,\n    fontsize  = 11,\n)\n\nlimits!(ax, 0, m, 0, m)\n\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}