HEX color contrast ratio

Calculate hex color contrast ratio using the values and assumptions you provide.

Calculate hex color contrast ratio using the values and assumptions you provide.

Your inputs stay in your browser. Displayed results are rounded to a maximum of 12 significant digits.

Method and assumptions

Method version 1.0.0 · Updated

Calculate

Formula: (L_lighter + 0.05) / (L_darker + 0.05), sRGB luminance

Calculate hex color contrast ratio using the values and assumptions you provide.

color1
Text color (#HEX)
color2
Background color (#HEX)

Results depend on the assumptions entered; verify professional or safety-critical decisions independently.

Calculate with code

These short examples use the same inputs and core calculation as this tool. Adapt validation and formatting to your application.

JavaScript
const luminance = rgb => rgb.map(c => c <= .04045 ? c/12.92 : ((c+.055)/1.055)**2.4).reduce((s,c,i)=>s+c*[.2126,.7152,.0722][i],0);
const ratio = (1 + .05) / (luminance([0,0,0]) + .05);
console.log(ratio); // 21 for black on white
TypeScript
const luminance = (rgb: number[]) => rgb.map(c => c <= .04045 ? c/12.92 : ((c+.055)/1.055)**2.4).reduce((s,c,i)=>s+c*[.2126,.7152,.0722][i]!,0);
console.log((1.05)/(luminance([0,0,0])+.05)); // 21
Python
def lum(rgb):
    c = [v/12.92 if v <= .04045 else ((v+.055)/1.055)**2.4 for v in rgb]
    return sum(x*w for x,w in zip(c, (.2126,.7152,.0722)))
print(1.05 / (lum((0,0,0)) + .05))  # 21:1 black/white
Java
double black = 0, white = 1;
System.out.println((white + .05) / (black + .05)); // 21:1 (black/white)
C#
double black = 0, white = 1;
Console.WriteLine((white + .05) / (black + .05)); // 21:1 black/white