{"spec_id":"barcode-ean13","library":"pygal","language":"python","code":"\"\"\" anyplot.ai\nbarcode-ean13: EAN-13 Barcode\nLibrary: pygal 3.1.0 | Python 3.13.13\nQuality: 86/100 | Created: 2026-05-21\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove this file's directory from sys.path so 'import pygal' finds the\n# installed library, not this file (pygal.py) itself.\n_this_dir = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if p not in (\"\", \".\") and not (p and os.path.abspath(p) == _this_dir)]\n\nimport pygal\nfrom pygal.style import Style\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\n# Barcode bars use ink tokens (not Okabe-Ito): monochrome is correct for EAN-13 semantics\nBAR_COLOR = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\n\n# EAN-13 encoding tables\nL_CODE = [\"0001101\", \"0011001\", \"0010011\", \"0111101\", \"0100011\", \"0110001\", \"0101111\", \"0111011\", \"0110111\", \"0001011\"]\nG_CODE = [\"0100111\", \"0110011\", \"0011011\", \"0100001\", \"0011101\", \"0111001\", \"0000101\", \"0010001\", \"0001001\", \"0010111\"]\nR_CODE = [\"1110010\", \"1100110\", \"1101100\", \"1000010\", \"1011100\", \"1001110\", \"1010000\", \"1000100\", \"1001000\", \"1110100\"]\n\n# Parity pattern: first digit selects which L/G codes to use for left 6 digits\nPARITY = [\"LLLLLL\", \"LLGLGG\", \"LLGGLG\", \"LLGGGL\", \"LGLLGG\", \"LGGLLG\", \"LGGGLL\", \"LGLGLG\", \"LGLGGL\", \"LGGLGL\"]\n\n# Data: German product barcode (pre-validated 13 digits with correct check digit)\nproduct_code = \"4006381333931\"\ndigits = [int(c) for c in product_code]\nparity_pattern = PARITY[digits[0]]\n\n# Encode EAN-13 to binary bit string (95 modules total)\nbits = \"101\"  # start guard\nfor i, d in enumerate(digits[1:7]):\n    bits += L_CODE[d] if parity_pattern[i] == \"L\" else G_CODE[d]\nbits += \"01010\"  # center guard\nfor d in digits[7:]:\n    bits += R_CODE[d]\nbits += \"101\"  # end guard\n\n# Build per-bar data: 9-module quiet zones on each side (spec minimum) + barcode modules\nquiet_zone = [{\"value\": 1, \"color\": PAGE_BG}] * 9\nbar_data = (\n    quiet_zone\n    + [{\"value\": 1, \"color\": BAR_COLOR} if b == \"1\" else {\"value\": 1, \"color\": PAGE_BG} for b in bits]\n    + quiet_zone\n)\n\n# EAN-13 digit grouping with structural annotation: GS1 prefix · manufacturer · product · check\nx_label = (\n    f\"GS1 Prefix: {product_code[0:3]} (DE)\"\n    f\" · Mfr: {product_code[3:7]}\"\n    f\" · Product: {product_code[7:12]}\"\n    f\" · Check: {product_code[12]}\"\n)\ntitle = \"barcode-ean13 · python · pygal · anyplot.ai\"\n\n# Chart style\ncustom_style = Style(\n    background=PAGE_BG,\n    plot_background=PAGE_BG,\n    foreground=INK,\n    foreground_strong=INK,\n    foreground_subtle=INK_MUTED,\n    colors=(BAR_COLOR,),\n    title_font_size=66,\n    label_font_size=56,\n    major_label_font_size=44,\n    legend_font_size=44,\n    value_font_size=36,\n    stroke_width=0,\n)\n\n# Chart — landscape 3200×1800 (hard contract)\nchart = pygal.Bar(\n    width=3200,\n    height=1800,\n    style=custom_style,\n    title=title,\n    x_title=x_label,\n    show_legend=False,\n    show_x_labels=False,\n    show_y_labels=False,\n    show_x_guides=False,\n    show_y_guides=False,\n    margin=80,\n    print_values=False,\n)\n\nchart.add(\"modules\", bar_data)\n\n# Save PNG and interactive HTML\nchart.render_to_png(f\"plot-{THEME}.png\")\nwith open(f\"plot-{THEME}.html\", \"wb\") as f:\n    f.write(chart.render())\n"}