UNPKG

@peculiar/color

Version:

Library for color manipulation and conversion in JavaScript.

21 lines 822 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLuminance = void 0; var hex_to_rgb_1 = require("./hex_to_rgb"); /** * The relative brightness of any point in a color space, * normalized to 0 for darkest black and 1 for lightest white. * * Formula: https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests */ function getLuminance(color) { var rgb = typeof color === 'string' ? (0, hex_to_rgb_1.hexToRgb)(color) : color; rgb = rgb.map(function (val) { val /= 255; // normalized return val <= 0.03928 ? val / 12.92 : Math.pow(((val + 0.055) / 1.055), 2.4); }); // Truncate at 3 digits return Number((0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]).toFixed(3)); } exports.getLuminance = getLuminance; //# sourceMappingURL=get_luminance.js.map