Source

style/generateRandomColors.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var isNullOrUndef_1 = __importDefault(require("../validators/isNullOrUndef"));
var shuffle_1 = __importDefault(require("../array/shuffle"));
var MAX_HUE = 360;
var SAT_BASE = 40;
var SAT_OFFSET = 60;
var LIGHT_BASE = 40;
var LIGHT_OFFSET = 40;
/**
 * Generate array of random colors
 * @param numberOfColors
 * @param options
 * @returns array of HSL color code
 * @category style
 * @module generateRandomColors
 */
var generateRandomColors = function (numberOfColors, options) {
    if (options === void 0) { options = {}; }
    var saturation = options.saturation, lightness = options.lightness;
    // Generate numberOfColors + 1 colors
    var startHue = Math.random() * MAX_HUE;
    var hueDiff = MAX_HUE / (numberOfColors);
    var colors = new Array(numberOfColors + 1).fill('')
        .map(function (d, index) {
        var derivedSaturation = (0, isNullOrUndef_1.default)(saturation)
            ? (Math.random() * SAT_BASE) + SAT_OFFSET
            : saturation;
        var derivedLightness = (0, isNullOrUndef_1.default)(lightness)
            ? (Math.random() * LIGHT_BASE) + LIGHT_OFFSET
            : lightness;
        return "hsl(".concat(startHue + (hueDiff * index), ", ").concat(derivedSaturation, "%, ").concat(derivedLightness, "%)");
    });
    // Drop the last color because first color hue is same as last color
    colors.pop();
    // Shuffle colors
    (0, shuffle_1.default)(colors);
    return colors;
};
exports.default = generateRandomColors;