Skip to content

Instantly share code, notes, and snippets.

@yuheiy
Created April 16, 2018 04:57
Show Gist options
  • Save yuheiy/c8da8c292af955873cc5bb1d2cbf0cb0 to your computer and use it in GitHub Desktop.
Save yuheiy/c8da8c292af955873cc5bb1d2cbf0cb0 to your computer and use it in GitHub Desktop.
const calculateRelativeLuminance = ({ r, g, b }) => {
const [R, G, B] = [r, g, b]
.map((bit) => bit / 255)
.map(
(sRGB) =>
sRGB <= 0.03928 ? sRGB / 12.92 : Math.pow((sRGB + 0.055) / 1.055, 2.4),
)
return 0.2126 * R + 0.7152 * G + 0.0722 * B
}
const calculateContrastRatio = (a, b) => {
const [L1, L2] = [a, b].map(calculateRelativeLuminance).sort((a, b) => b - a)
return (L1 + 0.05) / (L2 + 0.05)
}
calculateContrastRatio({ r: 34, g: 34, b: 34 }, { r: 255, g: 255, b: 255 }) // 15.909984431773273
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment