UNPKG

is-dark-color

Version:

Detects if a hex color is dark or light. It is based on the w3 documentation for color luminance: https://www.w3.org/TR/WCAG20/#relativeluminancedef

37 lines (29 loc) 994 B
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.isDarkColor = undefined; var _hexToRgb2 = require('./hexToRgb'); // adapted from https://stackoverflow.com/a/3943023/491075 var isDarkColor = exports.isDarkColor = function isDarkColor(hexColor, options) { if (options && options.override) { var overridedColor = Object.keys(options.override).find(function (k) { return k.toLowerCase() === hexColor.toLowerCase(); }); if (overridedColor !== undefined) { return options.override[overridedColor]; } } var _hexToRgb = (0, _hexToRgb2.hexToRgb)(hexColor), r = _hexToRgb.r, g = _hexToRgb.g, b = _hexToRgb.b; var colorArray = [r / 255, g / 255, b / 255].map(function (v) { if (v <= 0.03928) { return v / 12.92; } return Math.pow((v + 0.055) / 1.055, 2.4); }); var luminance = 0.2126 * colorArray[0] + 0.7152 * colorArray[1] + 0.0722 * colorArray[2]; return luminance <= 0.179; };