|
| 1 | +Sample a Region's Text Contrast (WCAG) |
| 2 | +====================================== |
| 3 | + |
| 4 | +:func:`a11y_audit.contrast_ratio` grades a foreground / background pair you |
| 5 | +already know. But when you only have a *region* of the screen — a button, a |
| 6 | +label — you don't know those two colours; you have a patch of pixels. |
| 7 | +``contrast_map`` closes that gap: split a sampled region into its dominant |
| 8 | +foreground (the minority — usually the text) and background (the majority) |
| 9 | +colours, then grade their WCAG contrast. |
| 10 | + |
| 11 | +* :func:`grade_contrast` — pure: a foreground / background pair to |
| 12 | + ``{ratio, aa, aaa, aa_large, aaa_large}`` against the WCAG 2.x thresholds. |
| 13 | +* :func:`dominant_pair` — pure: split a list of sampled RGB pixels into the |
| 14 | + dominant ``{foreground, background}`` colours by luminance. |
| 15 | +* :func:`region_contrast` — sample a screen region and grade it, through an |
| 16 | + injectable ``sampler`` (the real screen grab by default). |
| 17 | + |
| 18 | +The grading and split are pure and reuse :func:`a11y_audit.contrast_ratio`, so |
| 19 | +they are fully testable without a screen. Imports no ``PySide6``. |
| 20 | + |
| 21 | +Headless API |
| 22 | +------------ |
| 23 | + |
| 24 | +.. code-block:: python |
| 25 | +
|
| 26 | + from je_auto_control import grade_contrast, dominant_pair, region_contrast |
| 27 | +
|
| 28 | + # If you already know the colours: |
| 29 | + grade_contrast((90, 90, 90), (255, 255, 255)) |
| 30 | + # {'ratio': 3.9, 'aa': False, 'aaa': False, 'aa_large': True, ...} |
| 31 | +
|
| 32 | + # If you only have a region of the screen, sample and grade it: |
| 33 | + report = region_contrast(region=[x, y, w, h]) |
| 34 | + if not report["aa"]: |
| 35 | + print("low-contrast text", report["foreground"], report["background"]) |
| 36 | +
|
| 37 | +``dominant_pair`` partitions the sampled pixels at the mean luminance and treats |
| 38 | +the larger group as the background and the smaller as the text — a uniform patch |
| 39 | +yields the same colour for both (no contrast). ``region_contrast`` accepts an |
| 40 | +injectable ``sampler`` (``region -> list of RGB pixels``) so the logic is tested |
| 41 | +without a real screen. |
| 42 | + |
| 43 | +Executor commands |
| 44 | +----------------- |
| 45 | + |
| 46 | +``AC_grade_contrast`` (``foreground`` / ``background`` ``[r, g, b]`` → the |
| 47 | +grade), ``AC_dominant_pair`` (``pixels`` JSON list of ``[r, g, b]`` → |
| 48 | +``{foreground, background}``) and ``AC_region_contrast`` (``region`` |
| 49 | +``[x, y, w, h]`` → the grade + colours + ``samples``). They are the matching |
| 50 | +read-only ``ac_*`` MCP tools and Script Builder commands under **Image**. |
0 commit comments