{"spec_id":"barcode-code128","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nbarcode-code128: Code 128 Barcode\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 84/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\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\nsns.set_theme(\n    style=\"white\",\n    rc={\"figure.facecolor\": PAGE_BG, \"axes.facecolor\": PAGE_BG, \"axes.edgecolor\": INK_SOFT, \"text.color\": INK},\n)\n\n# Code 128 encoding tables (subset B — full printable ASCII)\nCODE128_B_PATTERNS = {\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    \"0\": \"10011101100\",\n    \"1\": \"10011100110\",\n    \"2\": \"11001110010\",\n    \"3\": \"11001011100\",\n    \"4\": \"11001001110\",\n    \"5\": \"11011100100\",\n    \"6\": \"11001110100\",\n    \"7\": \"11101101110\",\n    \"8\": \"11101001100\",\n    \"9\": \"11100101100\",\n    \":\": \"11100100110\",\n    \";\": \"11101100100\",\n    \"<\": \"11100110100\",\n    \"=\": \"11100110010\",\n    \">\": \"11011011000\",\n    \"?\": \"11011000110\",\n    \"@\": \"11000110110\",\n    \"A\": \"10100011000\",\n    \"B\": \"10001011000\",\n    \"C\": \"10001000110\",\n    \"D\": \"10110001000\",\n    \"E\": \"10001101000\",\n    \"F\": \"10001100010\",\n    \"G\": \"11010001000\",\n    \"H\": \"11000101000\",\n    \"I\": \"11000100010\",\n    \"J\": \"10110111000\",\n    \"K\": \"10110001110\",\n    \"L\": \"10001101110\",\n    \"M\": \"10111011000\",\n    \"N\": \"10111000110\",\n    \"O\": \"10001110110\",\n    \"P\": \"11101110110\",\n    \"Q\": \"11010001110\",\n    \"R\": \"11000101110\",\n    \"S\": \"11011101000\",\n    \"T\": \"11011100010\",\n    \"U\": \"11011101110\",\n    \"V\": \"11101011000\",\n    \"W\": \"11101000110\",\n    \"X\": \"11100010110\",\n    \"Y\": \"11101101000\",\n    \"Z\": \"11101100010\",\n    \"[\": \"11100011010\",\n    \"\\\\\": \"11101111010\",\n    \"]\": \"11001000010\",\n    \"^\": \"11110001010\",\n    \"_\": \"10100110000\",\n    \"`\": \"10100001100\",\n    \"a\": \"10010110000\",\n    \"b\": \"10010000110\",\n    \"c\": \"10000101100\",\n    \"d\": \"10000100110\",\n    \"e\": \"10110010000\",\n    \"f\": \"10110000100\",\n    \"g\": \"10011010000\",\n    \"h\": \"10011000010\",\n    \"i\": \"10000110100\",\n    \"j\": \"10000110010\",\n    \"k\": \"11000010010\",\n    \"l\": \"11001010000\",\n    \"m\": \"11110111010\",\n    \"n\": \"11000010100\",\n    \"o\": \"10001111010\",\n    \"p\": \"10100111100\",\n    \"q\": \"10010111100\",\n    \"r\": \"10010011110\",\n    \"s\": \"10111100100\",\n    \"t\": \"10011110100\",\n    \"u\": \"10011110010\",\n    \"v\": \"11110100100\",\n    \"w\": \"11110010100\",\n    \"x\": \"11110010010\",\n    \"y\": \"11011011110\",\n    \"z\": \"11011110110\",\n    \"{\": \"11110110110\",\n    \"|\": \"10101111000\",\n    \"}\": \"10100011110\",\n    \"~\": \"10001011110\",\n}\n\nCODE128_VALUES = {\n    \" \": 0,\n    \"!\": 1,\n    '\"': 2,\n    \"#\": 3,\n    \"$\": 4,\n    \"%\": 5,\n    \"&\": 6,\n    \"'\": 7,\n    \"(\": 8,\n    \")\": 9,\n    \"*\": 10,\n    \"+\": 11,\n    \",\": 12,\n    \"-\": 13,\n    \".\": 14,\n    \"/\": 15,\n    \"0\": 16,\n    \"1\": 17,\n    \"2\": 18,\n    \"3\": 19,\n    \"4\": 20,\n    \"5\": 21,\n    \"6\": 22,\n    \"7\": 23,\n    \"8\": 24,\n    \"9\": 25,\n    \":\": 26,\n    \";\": 27,\n    \"<\": 28,\n    \"=\": 29,\n    \">\": 30,\n    \"?\": 31,\n    \"@\": 32,\n    \"A\": 33,\n    \"B\": 34,\n    \"C\": 35,\n    \"D\": 36,\n    \"E\": 37,\n    \"F\": 38,\n    \"G\": 39,\n    \"H\": 40,\n    \"I\": 41,\n    \"J\": 42,\n    \"K\": 43,\n    \"L\": 44,\n    \"M\": 45,\n    \"N\": 46,\n    \"O\": 47,\n    \"P\": 48,\n    \"Q\": 49,\n    \"R\": 50,\n    \"S\": 51,\n    \"T\": 52,\n    \"U\": 53,\n    \"V\": 54,\n    \"W\": 55,\n    \"X\": 56,\n    \"Y\": 57,\n    \"Z\": 58,\n    \"[\": 59,\n    \"\\\\\": 60,\n    \"]\": 61,\n    \"^\": 62,\n    \"_\": 63,\n    \"`\": 64,\n    \"a\": 65,\n    \"b\": 66,\n    \"c\": 67,\n    \"d\": 68,\n    \"e\": 69,\n    \"f\": 70,\n    \"g\": 71,\n    \"h\": 72,\n    \"i\": 73,\n    \"j\": 74,\n    \"k\": 75,\n    \"l\": 76,\n    \"m\": 77,\n    \"n\": 78,\n    \"o\": 79,\n    \"p\": 80,\n    \"q\": 81,\n    \"r\": 82,\n    \"s\": 83,\n    \"t\": 84,\n    \"u\": 85,\n    \"v\": 86,\n    \"w\": 87,\n    \"x\": 88,\n    \"y\": 89,\n    \"z\": 90,\n    \"{\": 91,\n    \"|\": 92,\n    \"}\": 93,\n    \"~\": 94,\n}\n\n# Patterns for check digit values 95–105 (no ASCII mapping)\nCHECK_PATTERNS = [\n    \"11110101000\",\n    \"11110100010\",\n    \"10111011110\",\n    \"10111101110\",\n    \"10111110110\",\n    \"11101011110\",\n    \"11110101110\",\n    \"11010000100\",\n    \"11010010000\",\n    \"11010011100\",\n    \"11000111010\",\n]\n\nSTART_B = \"11010010000\"\nSTOP = \"1100011101011\"\nQUIET = \"0\" * 10\n\n# Data\ncontent = \"SHIP-2024-ABC123\"\n\n# Encode using Code 128 subset B with modulo-103 check digit\nbarcode_binary = START_B\nchecksum = 104\nfor i, char in enumerate(content):\n    barcode_binary += CODE128_B_PATTERNS[char]\n    checksum += CODE128_VALUES[char] * (i + 1)\n\ncheck_value = checksum % 103\nif check_value < 95:\n    for char, val in CODE128_VALUES.items():\n        if val == check_value:\n            barcode_binary += CODE128_B_PATTERNS[char]\n            break\nelse:\n    barcode_binary += CHECK_PATTERNS[check_value - 95]\n\nbarcode_binary += STOP\nbarcode_with_quiet = QUIET + barcode_binary + QUIET\n\n# Zone boundary positions (column indices)\nn_cols = len(barcode_with_quiet)\nq = len(QUIET)  # 10\ns = len(START_B)  # 11\nd = len(content) * 11  # 176\nck = 11  # check digit symbol\nst = len(STOP)  # 13\n\nzone_defs = [\n    (0, q, INK_SOFT, 0.20, \"\"),\n    (q, q + s, \"#009E73\", 0.85, \"Start\\n128B\"),\n    (q + s, q + s + d, \"#4467A3\", 0.80, f\"Data region · {len(content)} chars\"),\n    (q + s + d, q + s + d + ck, \"#C475FD\", 0.85, \"Check\\nmod 103\"),\n    (q + s + d + ck, q + s + d + ck + st, \"#BD8233\", 0.85, \"Stop\"),\n    (n_cols - q, n_cols, INK_SOFT, 0.20, \"\"),\n]\n\n# Build 2D barcode matrix\nbarcode_array = np.array([[int(bit) for bit in barcode_with_quiet]])\nbarcode_data = np.repeat(barcode_array, 40, axis=0)\n\n# Seaborn-native two-color blend palette as colormap\ncmap = sns.blend_palette([PAGE_BG, INK], n_colors=256, as_cmap=True)\n\n# Layout: barcode panel + zone annotation strip\nfig, (ax, ax_ann) = plt.subplots(\n    2, 1, figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG, gridspec_kw={\"height_ratios\": [6, 1], \"hspace\": 0.03}\n)\n\n# Barcode panel\nsns.heatmap(barcode_data, cmap=cmap, cbar=False, xticklabels=False, yticklabels=False, linewidths=0, ax=ax)\nsns.despine(ax=ax, left=True, bottom=True, top=True, right=True)\nax.set_title(\"barcode-code128 · python · seaborn · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK, pad=10)\n\n# Zone annotation strip — structural callouts for each barcode section\nax_ann.set_facecolor(ELEVATED_BG)\nax_ann.set_xlim(0, n_cols)\nax_ann.set_ylim(0, 1)\n\nfor x0, x1, color, alpha, label in zone_defs:\n    width = x1 - x0\n    ax_ann.add_patch(plt.Rectangle((x0, 0), width, 1, color=color, alpha=alpha))\n    if label and width > 12:\n        ax_ann.text(\n            (x0 + x1) / 2,\n            0.5,\n            label,\n            ha=\"center\",\n            va=\"center\",\n            fontsize=5.5,\n            color=\"white\",\n            fontweight=\"bold\",\n            linespacing=1.3,\n        )\n\nsns.despine(ax=ax_ann, left=True, bottom=True, top=True, right=True)\nax_ann.set_xticks([])\nax_ann.set_yticks([])\n\nfig.subplots_adjust(top=0.88, bottom=0.11, left=0.02, right=0.98)\nfig.text(\n    0.5, 0.04, content, ha=\"center\", va=\"center\", fontsize=10, fontfamily=\"monospace\", fontweight=\"bold\", color=INK\n)\n\n# Save\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}