contrastRatio
Computes the WCAG 2 contrast ratio between two colors, from 1 (identical) to 21 (black on white). The lighter color is always put over the darker one, so the order of the arguments does not matter. The ratio is not rounded, so it can be compared as it is with the WCAG thresholds: 4.5 (AA, normal text), 3 (AA, large text and UI components), 7 (AAA, normal text) and 4.5 (AAA, large text). Alpha is ignored: flatten translucent colors first (see blendColors).
Available since v3.1.10
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
foreground | RgbColor | The text (or foreground) color |
background | RgbColor | The color behind it |
Returns
Section titled “Returns”number — The contrast ratio, between 1 and 21
Examples
Section titled “Examples”Black on white is the maximum contrast
Section titled “Black on white is the maximum contrast”The ratio goes from 1 (identical colors) to 21.
Check a text color against the WCAG AA threshold
Section titled “Check a text color against the WCAG AA threshold”Normal text needs 4.5; #777777 on white is just under it, #767676 just over.
Related Types
Section titled “Related Types”RgbColor
Section titled “RgbColor”An RGB(A) color with channels expressed as plain numbers, convenient for further conversion or manipulation.
