Skip to main content

getContrastRatio()

function getContrastRatio(hexCode1, hexCode2): number

Calculate the 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 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 = getContrastRatio("#FFFFFF", "#000000");
console.log(ratio); // Outputs: 21

Defined in

hacks/hacks.ts:49