Toolbox-XToolbox-X

Contrast & Accessibility

Check WCAG ratings, contrast ratios, and color brightness using the Color class.

Contrast & Accessibility Methods

contrastRatio()

Calculates the contrast ratio between the current color and another color based on the WCAG formula.

Signature

contrastRatio(other: ColorType | CSSColor): number

Parameters

ParameterTypeDescription
otherColorType | CSSColorThe color to compare against.

Returns

  • number: The WCAG contrast ratio (rounded to 2 decimal places). Ranges from 1 (no contrast) to 21 (maximum contrast, e.g., black vs. white).

Example

playground.ts

getWCAGRating()

Determines the WCAG readability rating (Fail, AA, AAA) for standard text.

Signature

getWCAGRating(other: ColorType | CSSColor): 'Fail' | 'AA' | 'AAA'

Parameters

ParameterTypeDescription
otherColorType | CSSColorThe color to test against.

Returns

  • 'Fail' | 'AA' | 'AAA':
    • "Fail" (Ratio < 4.5)
    • "AA" (Ratio ≥ 4.5)
    • "AAA" (Ratio ≥ 7)

Example

playground.ts

isLightColor()

Checks if the color's perceived brightness exceeds a given threshold, indicating a light color.

Signature

isLightColor(threshold?: number): boolean

Parameters

ParameterTypeDescription
thresholdnumber (optional)Perceived brightness cutoff value (0–255). Defaults to 127.5. Values outside the range are clamped.

Returns

  • boolean: true if perceived brightness exceeds the threshold; otherwise false.

Example

playground.ts

Last updated: Mon, Jun 15, 2026 09:49:35PM (UTC)

On this page