UNPKG

canvas-sketch-util

Version:

Utilities for sketching in Canvas, WebGL and generative art

31 lines (26 loc) 973 B
// Extracted from @tmcw / wcag-contrast // https://github.com/tmcw/relative-luminance/blob/master/index.js // # Relative luminance // http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef // https://en.wikipedia.org/wiki/Luminance_(relative) // https://en.wikipedia.org/wiki/Luminosity_function // https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients // red, green, and blue coefficients var rc = 0.2126; var gc = 0.7152; var bc = 0.0722; // low-gamma adjust coefficient var lowc = 1 / 12.92; function adjustGamma (a) { return Math.pow((a + 0.055) / 1.055, 2.4); } module.exports = relativeLuminance; function relativeLuminance (rgb) { var rsrgb = rgb[0] / 255; var gsrgb = rgb[1] / 255; var bsrgb = rgb[2] / 255; var r = rsrgb <= 0.03928 ? rsrgb * lowc : adjustGamma(rsrgb); var g = gsrgb <= 0.03928 ? gsrgb * lowc : adjustGamma(gsrgb); var b = bsrgb <= 0.03928 ? bsrgb * lowc : adjustGamma(bsrgb); return r * rc + g * gc + b * bc; }