{"spec_id":"barcode-code128","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nbarcode-code128: Code 128 Barcode\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 86/100 | Created: 2026-05-21\n\"\"\"\n\nimport os\nimport sys\n\n\n# Prevent this script from shadowing the installed 'plotnine' package\n_here = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if not (p and os.path.abspath(p) == _here)]\nos.chdir(_here)\n\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    annotate,\n    element_rect,\n    element_text,\n    geom_rect,\n    ggplot,\n    labs,\n    scale_x_continuous,\n    scale_y_continuous,\n    theme,\n    theme_void,\n)\n\n\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\"\nACCENT = \"#009E73\"  # Okabe-Ito green for label header accent\n\n# Code 128 Subset B patterns (binary: 1=bar, 0=space), 11 modules each, symbols 0-102\nCODE128 = [\n    \"11011001100\",\n    \"11001101100\",\n    \"11001100110\",\n    \"10010011000\",\n    \"10010001100\",\n    \"10001001100\",\n    \"10011001000\",\n    \"10011000100\",\n    \"10001100100\",\n    \"11001001000\",\n    \"11001000100\",\n    \"11000100100\",\n    \"10110011100\",\n    \"10011011100\",\n    \"10011001110\",\n    \"10111001100\",\n    \"10011101100\",\n    \"10011100110\",\n    \"11001110010\",\n    \"11001011100\",\n    \"11001001110\",\n    \"11011100100\",\n    \"11001110100\",\n    \"11101101110\",\n    \"11101001100\",\n    \"11100101100\",\n    \"11100100110\",\n    \"11101100100\",\n    \"11100110100\",\n    \"11100110010\",\n    \"11011011000\",\n    \"11011000110\",\n    \"11000110110\",\n    \"10100011000\",\n    \"10001011000\",\n    \"10001000110\",\n    \"10110001000\",\n    \"10001101000\",\n    \"10001100010\",\n    \"11010001000\",\n    \"11000101000\",\n    \"11000100010\",\n    \"10110111000\",\n    \"10110001110\",\n    \"10001101110\",\n    \"10111011000\",\n    \"10111000110\",\n    \"10001110110\",\n    \"11101110110\",\n    \"11010001110\",\n    \"11000101110\",\n    \"11011101000\",\n    \"11011100010\",\n    \"11011101110\",\n    \"11101011000\",\n    \"11101000110\",\n    \"11100010110\",\n    \"11101101000\",\n    \"11101100010\",\n    \"11100011010\",\n    \"11101111010\",\n    \"11001000010\",\n    \"11110001010\",\n    \"10100110000\",\n    \"10100001100\",\n    \"10010110000\",\n    \"10010000110\",\n    \"10000101100\",\n    \"10000100110\",\n    \"10110010000\",\n    \"10110000100\",\n    \"10011010000\",\n    \"10011000010\",\n    \"10000110100\",\n    \"10000110010\",\n    \"11000010010\",\n    \"11001010000\",\n    \"11110111010\",\n    \"11000010100\",\n    \"10001111010\",\n    \"10100111100\",\n    \"10010111100\",\n    \"10010011110\",\n    \"10111100100\",\n    \"10011110100\",\n    \"10011110010\",\n    \"11110100100\",\n    \"11110010100\",\n    \"11110010010\",\n    \"11011011110\",\n    \"11011110110\",\n    \"11110110110\",\n    \"10101111000\",\n    \"10100011110\",\n    \"10001011110\",\n    \"10111101000\",\n    \"10111100010\",\n    \"11110101000\",\n    \"11110100010\",\n    \"10111011110\",\n    \"10111101110\",\n    \"11101011110\",\n    \"11110101110\",\n]\nSTART_B = \"11010010000\"\nSTOP = \"1100011101011\"\n\n# Encode specimen label using Code 128 Subset B (ASCII 32-127, symbol = ASCII - 32)\ntext = \"SPECIMEN-2024-B047\"\nsymbol_vals = [ord(c) - 32 for c in text]\n\ncheck = 104  # Start B value\nfor i, v in enumerate(symbol_vals):\n    check += (i + 1) * v\ncheck %= 103\n\nbinary = START_B + \"\".join(CODE128[v] for v in symbol_vals) + CODE128[check] + STOP\n\n# Convert binary string to bar segments (group consecutive 1-runs)\nQUIET = 10\nBAR_Y1, BAR_Y2 = 0.31, 0.79\n\nbar_rows = []\ni = 0\nx = QUIET\nwhile i < len(binary):\n    bit = binary[i]\n    j = i + 1\n    while j < len(binary) and binary[j] == bit:\n        j += 1\n    run = j - i\n    if bit == \"1\":\n        bar_rows.append({\"xmin\": float(x), \"xmax\": float(x + run), \"ymin\": BAR_Y1, \"ymax\": BAR_Y2})\n    x += run\n    i = j\n\ntotal_w = float(x + QUIET)\ndf_bars = pd.DataFrame(bar_rows)\n\nLABEL_X1 = 2.0\nLABEL_X2 = total_w - 2.0\nLABEL_MID = total_w / 2\n\n# White label card (barcode always black-on-white for scan reliability)\ndf_label = pd.DataFrame({\"xmin\": [LABEL_X1], \"xmax\": [LABEL_X2], \"ymin\": [0.05], \"ymax\": [0.95]})\n# Accent header strip for visual hierarchy\ndf_header = pd.DataFrame({\"xmin\": [LABEL_X1], \"xmax\": [LABEL_X2], \"ymin\": [0.81], \"ymax\": [0.95]})\n\nplot = (\n    ggplot()\n    + geom_rect(\n        data=df_label, mapping=aes(xmin=\"xmin\", xmax=\"xmax\", ymin=\"ymin\", ymax=\"ymax\"), fill=\"#FFFFFF\", colour=\"none\"\n    )\n    + geom_rect(\n        data=df_header, mapping=aes(xmin=\"xmin\", xmax=\"xmax\", ymin=\"ymin\", ymax=\"ymax\"), fill=ACCENT, colour=\"none\"\n    )\n    + annotate(\"text\", x=LABEL_MID, y=0.875, label=\"CODE 128 · SUBSET B\", colour=\"#FFFFFF\", size=7, family=\"monospace\")\n    + annotate(\"segment\", x=LABEL_X1, xend=LABEL_X2, y=0.81, yend=0.81, colour=INK_SOFT, size=0.4)\n    + geom_rect(\n        data=df_bars, mapping=aes(xmin=\"xmin\", xmax=\"xmax\", ymin=\"ymin\", ymax=\"ymax\"), fill=\"#1A1A17\", colour=\"none\"\n    )\n    + annotate(\"text\", x=LABEL_MID, y=0.20, label=text, colour=\"#1A1A17\", size=9, family=\"monospace\")\n    + annotate(\n        \"text\",\n        x=LABEL_MID,\n        y=0.10,\n        label=\"Code 128B · 18 characters · Mod-103 check digit\",\n        colour=INK_SOFT,\n        size=6,\n        family=\"monospace\",\n    )\n    + scale_x_continuous(limits=(0, total_w), expand=(0, 0))\n    + scale_y_continuous(limits=(0, 1), expand=(0, 0))\n    + labs(title=\"Specimen Label · barcode-code128 · python · plotnine · anyplot.ai\")\n    + theme_void()\n    + theme(\n        figure_size=(8, 4.5),\n        plot_background=element_rect(fill=PAGE_BG, colour=PAGE_BG),\n        plot_title=element_text(colour=INK, size=12, hjust=0.5),\n    )\n)\n\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=8, height=4.5, units=\"in\")\n"}