{"spec_id":"barcode-ean13","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nbarcode-ean13: EAN-13 Barcode\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 86/100 | Updated: 2026-05-21\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\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\"\n\n# EAN-13 encoding tables\nL_CODES = {\n    \"0\": \"0001101\",\n    \"1\": \"0011001\",\n    \"2\": \"0010011\",\n    \"3\": \"0111101\",\n    \"4\": \"0100011\",\n    \"5\": \"0110001\",\n    \"6\": \"0101111\",\n    \"7\": \"0111011\",\n    \"8\": \"0110111\",\n    \"9\": \"0001011\",\n}\nG_CODES = {\n    \"0\": \"0100111\",\n    \"1\": \"0110011\",\n    \"2\": \"0011011\",\n    \"3\": \"0100001\",\n    \"4\": \"0011101\",\n    \"5\": \"0111001\",\n    \"6\": \"0000101\",\n    \"7\": \"0010001\",\n    \"8\": \"0001001\",\n    \"9\": \"0010111\",\n}\nR_CODES = {\n    \"0\": \"1110010\",\n    \"1\": \"1100110\",\n    \"2\": \"1101100\",\n    \"3\": \"1000010\",\n    \"4\": \"1011100\",\n    \"5\": \"1001110\",\n    \"6\": \"1010000\",\n    \"7\": \"1000100\",\n    \"8\": \"1001000\",\n    \"9\": \"1110100\",\n}\nFIRST_DIGIT_PATTERNS = {\n    \"0\": \"LLLLLL\",\n    \"1\": \"LLGLGG\",\n    \"2\": \"LLGGLG\",\n    \"3\": \"LLGGGL\",\n    \"4\": \"LGLLGG\",\n    \"5\": \"LGGLLG\",\n    \"6\": \"LGGGLL\",\n    \"7\": \"LGLGLG\",\n    \"8\": \"LGLGGL\",\n    \"9\": \"LGGLGL\",\n}\n\n# Data\ncode = \"4006381333931\"\n\nif len(code) == 12:\n    odd_sum = sum(int(code[i]) for i in range(0, 12, 2))\n    even_sum = sum(int(code[i]) for i in range(1, 12, 2))\n    check = (10 - (odd_sum + even_sum * 3) % 10) % 10\n    code = code + str(check)\n\n# Build binary barcode pattern\nfirst_digit = code[0]\nbarcode_binary = \"101\"  # start guard\npattern = FIRST_DIGIT_PATTERNS[first_digit]\nfor i, digit in enumerate(code[1:7]):\n    barcode_binary += L_CODES[digit] if pattern[i] == \"L\" else G_CODES[digit]\nbarcode_binary += \"01010\"  # center guard\nfor digit in code[7:13]:\n    barcode_binary += R_CODES[digit]\nbarcode_binary += \"101\"  # end guard\n\nquiet_zone = \"0\" * 9\nbarcode_with_quiet = quiet_zone + barcode_binary + quiet_zone\nbarcode_array = np.array([[int(b) for b in barcode_with_quiet]])\nbarcode_data = np.repeat(barcode_array, 60, axis=0)\n\n# Set seaborn theme\nsns.set_theme(style=\"white\", rc={\"figure.facecolor\": PAGE_BG, \"axes.facecolor\": PAGE_BG, \"text.color\": INK})\n\n# Plot — canonical 3200×1800 px\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\ncmap = sns.color_palette([PAGE_BG, INK], as_cmap=True)\nsns.heatmap(\n    barcode_data, cmap=cmap, cbar=False, xticklabels=False, yticklabels=False, linewidths=0, linecolor=\"none\", ax=ax\n)\n\nfor spine in ax.spines.values():\n    spine.set_visible(False)\n\n# EAN-13 standard digit positioning:\n#   code[0]  → left quiet zone (outside/left of start guard at col 9)\n#   code[1:7] → centered below left data section (cols 12–53)\n#   code[7:]  → centered below right data section (cols 59–100)\ndigit_y = barcode_data.shape[0] + 7\ntext_kw = {\"fontsize\": 10, \"fontfamily\": \"monospace\", \"fontweight\": \"bold\", \"va\": \"top\", \"color\": INK}\n\nax.text(4.5, digit_y, code[0], ha=\"center\", **text_kw)\nax.text(33.0, digit_y, code[1:7], ha=\"center\", **text_kw)\nax.text(80.0, digit_y, code[7:], ha=\"center\", **text_kw)\n\nax.set_xlim(-5, len(barcode_with_quiet) + 5)\nax.set_ylim(barcode_data.shape[0] + 18, -5)\n\nax.set_title(\"barcode-ean13 · python · seaborn · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK, pad=10)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}