Skip to main content

getContrastRatioWCAG20()

function getContrastRatioWCAG20(hexCode1, hexCode2): number

Calculate the WCAG 2.0 contrast ratio between two colors

Parameters

ParameterTypeDescription
hexCode1stringFirst color in hexadecimal format (e.g., "#RRGGBB" or "RRGGBB")
hexCode2stringSecond color in hexadecimal format (e.g., "#RRGGBB" or "RRGGBB")

Returns

number

The WCAG 2.0 contrast ratio between the two colors (a number between 1 and 21)

Throws

If either hexCode1 or hexCode2 is not a valid hexadecimal color code

Example

const ratio = getContrastRatioWCAG20("#FFFFFF", "#000000");
console.log(ratio); // Outputs: 21

Defined in

hacks/hacks.ts:79