Skip to content

accessibleColorContrast ​

accessibleColorContrast checks whether a foreground/background color pair meets WCAG contrast guidelines.

SignatureaccessibleColorContrast(fg: Color, bg: Color): boolean
tyrus
import { Color, accessibleColorContrast } from "tyrus:os";

const black: Color = { r: 0, g: 0, b: 0 };
const white: Color = { r: 255, g: 255, b: 255 };
print(accessibleColorContrast(black, white)); // true, 21:1

Returns whether fg/bg clear the WCAG 2.x contrast ratio of 4.5:1, the AA threshold for normal-size text. The ratio is computed from each color's relative luminance ((L1 + 0.05) / (L2 + 0.05), L1 the lighter of the two), so which argument is foreground versus background does not change the outcome.

Color Fields ​

FieldTypeMeaning
ru8The red channel, 0 to 255.
gu8The green channel, 0 to 255.
bu8The blue channel, 0 to 255.

Only the AA normal-text threshold

This checks a single fixed threshold (4.5:1). WCAG defines other thresholds for large text (3:1) and the stricter AAA level (7:1/4.5:1) that this function does not expose directly, compute the ratio yourself from the same relative-luminance formula if you need one of those instead.