UNPKG

@kcuf/mere-color

Version:

Mere color utils for generating, manipulation, a11y purposes.

26 lines (24 loc) 891 B
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = rgbComputeLuminance; var _round2 = _interopRequireDefault(require("lodash/round")); /** * Converts an RGB channel [0-255] to its linear light (un-companded) form [0-1]. * Linearized RGB values are widely used for color space conversions and contrast calculations */ function linearizeRgbChannel(value) { var channel = value / 255; return channel <= 0.03928 ? channel / 12.92 : Math.pow((channel + 0.055) / 1.055, 2.4); } /** * Compute the luminance of Rgb. */ function rgbComputeLuminance(rgb) { var cr = linearizeRgbChannel(rgb.r); var cg = linearizeRgbChannel(rgb.g); var cb = linearizeRgbChannel(rgb.b); return (0, _round2.default)(0.2126 * cr + 0.7152 * cg + 0.0722 * cb, 3); }