Source

style/getColorLuminance.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var convertColorToRgbaArray_1 = __importDefault(require("./convertColorToRgbaArray"));
var RGB_LENGTH = 3;
var MAX_RGB_VALUE = 255;
var REL_R = 0.2126;
var REL_G = 0.7152;
var REL_B = 0.0722;
var VALUE_THRESOLD = 0.03928;
var VALUE_DIVIDEND = 12.92;
/**
 * Get color luminance. Return 0 - 1.
 * Reference: https://dev.to/alvaromontoro/building-your-own-color-contrast-checker-4j7o
 * @param color
 * @category style
 * @module getColorLuminance
 */
var getColorLuminance = function (color) {
    var colorRgb = (0, convertColorToRgbaArray_1.default)(color).slice(0, RGB_LENGTH);
    var a = colorRgb.map(function (value) {
        var v = value / MAX_RGB_VALUE;
        return v <= VALUE_THRESOLD
            ? v / VALUE_DIVIDEND
            : (function () {
                var x = 0.055;
                var y = 1.055;
                var n = 2.4;
                return Math.pow(((v + x) / y), n);
            })();
    });
    return (a[0] * REL_R) + (a[1] * REL_G) + (a[2] * REL_B);
};
exports.default = getColorLuminance;