Skip to main content

contrast-matrix

CI PyPI Python License: Apache-2.0

A CI-first WCAG color-contrast matrix checker for design tokens, with no browser or network required.

Quickstart

pip install "contrast-matrix[yaml]"
contrast-matrix check examples/matrix.yaml
contrast-matrix check examples/matrix.yaml --format sarif --level aaa

JSON support uses only the Python standard library. Install contrast-matrix[yaml] to read YAML. The exit status is 0 when every token passes, 1 when any token fails, and 2 for usage or input errors.

Input schema

The root object has backgrounds, tokens, and an optional thresholds map:

thresholds: {normal: 4.5, large: 3.0, aaa_normal: 7.0}
backgrounds:
  surface: "#ffffff"
  surface-alt: "#f4f4f5"
  overlay: "rgba(0,0,0,0.6) over #3b82f6"
tokens:
  - name: text-primary
    color: "#18181b"
    over: [surface, surface-alt]
    level: normal
  - name: text-on-overlay
    color: "rgba(255,255,255,0.9)"
    over: [overlay]
    level: large

Background keys are arbitrary names. Colors accept CSS #rgb, #rgba, #rrggbb, #rrggbbaa, rgb(), rgba(), hsl(), and hsla(). Derived expressions accept foreground over background and mix(first, second, t), where t ranges from 0 to 1. Each token requires a unique name, a color, a non-empty over list of background names, and a threshold level. Built-ins are normal (4.5), large (3.0), aaa_normal (7.0), and aaa_large (4.5); the threshold map can add or override names. --level aaa maps normal and large to their AAA counterparts, while --fail-under N overrides every token's threshold.

Derived expressions may use background names as operands (for example, rgba(0,0,0,.5) over surface); references may be nested or chained, and unknown or cyclic references are rejected as input errors.

Why a matrix?

Pairwise checkers answer whether one foreground works on one background. Design tokens often render on several surfaces, including translucent and derived colors. This tool evaluates every declared pair and reports the minimum ratio and the background that produced it, making the worst case an explicit, deterministic CI result.

The calculation follows WCAG 2.x: sRGB channels are linearized, relative luminance uses the 0.2126/0.7152/0.0722 coefficients, and contrast is (Llight + 0.05) / (Ldark + 0.05). Alpha foregrounds are composited before comparison. This implements the WCAG 2.x contrast formula; it is not an APCA/WCAG 3 implementation.

Output

--format table prints a compact summary. json emits the complete sorted result, including all pairs. sarif emits SARIF 2.1.0 findings for failed tokens. Inputs, tokens, backgrounds, and JSON keys are ordered deterministically so repeated runs produce clean diffs. See examples.

Development

python -m pip install -e '.[dev]'
pytest -q

License

Apache-2.0.

Built and maintained by Gexiro Global Enterprises Ltd.

Part of the Gexiro open-source toolkit.

Metadata

Release files for contrast-matrix 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contrast-matrix 0.1.1
File Size Uploaded
contrast_matrix-0.1.1.tar.gz 22.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for contrast-matrix 0.1.1
File Interpreter ABI Platform
contrast_matrix-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 40.8 kB

Release files / contrast_matrix-0.1.1.tar.gz

Download URL contrast_matrix-0.1.1.tar.gz
Size 22.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bbb1ace24ffcaed5156adc31181c7f91c38c46c1d179b33674e98290e112cf26
BLAKE2b-256 checksum
How to use checksums
7af44eb952c308c033d183366b32b10f6a6b5932a9c0ce883ccde524b3621bd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / contrast_matrix-0.1.1-py3-none-any.whl

Download URL contrast_matrix-0.1.1-py3-none-any.whl
Size 18.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6d0dca3b5eb12f816760e340dde2be37f04ac253919773e6f22437821a2b6c89
BLAKE2b-256 checksum
How to use checksums
d0dbda1c7a5f1c06672629587e5b2d17b9a8615afe6dab26bea3ec2d7c6a1951
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page