Skip to content

Function.getContrastRatio

github-actions[bot] edited this page Nov 18, 2026 · 13 revisions

umt / getContrastRatio

Function: getContrastRatio()

getContrastRatio(color1, color2): number

Defined in: Color/getContrastRatio.ts:10

Calculate the WCAG 2.1 contrast ratio between two RGBA colors

Parameters

color1

Object containing r, g, b values (0-255) and optional a (0-1)

a?

number

b

number

g

number

r

number

color2

Object containing r, g, b values (0-255) and optional a (0-1)

a?

number

b

number

g

number

r

number

Returns

number

Contrast ratio from 1 (identical luminance) to 21 (black on white); the argument order does not matter

Example

getContrastRatio({ r: 0, g: 0, b: 0 }, { r: 255, g: 255, b: 255 }); // 21

API

Classes

Interfaces

Type Aliases

Variables

Functions

Clone this wiki locally