UNPKG

uuid-color

Version:

A lightweight package to generate unique and uniformly sampled colors from UUIDs.

102 lines (101 loc) 4.21 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.colorFromUuid = void 0; var uuid_1 = require("uuid"); var UuidEncoder = require("uuid-encoder"); var convert = require("color-convert"); var DEFAULT_COLOR_FORMAT = "hex"; var DEFAULT_IS_RAW = false; var encoder = new UuidEncoder("base10"); /** * Returns the generated color associated with the given uuid. * * @param uuid - The uuid for which to generate a color * @param options - An optional object to configure the color generation, and attach callbacks that directly receive the generated color code or components in various formats * @returns The generated color as a CSS `<color>` notation string * * @throws {@link https://developer.mozilla.org/en-US/docs/web/javascript/reference/global_objects/error | Error} * This exception is thrown if the input uuid string is not a valid UUID. * * @public */ function colorFromUuid(uuid, options) { if (options === void 0) { options = {}; } if (!(0, uuid_1.validate)(uuid)) { throw new Error("Given string is not a valid UUID."); } var encodedUuid = BigInt(encoder.encode(uuid)); var colorCode = Number(encodedUuid % BigInt(0x1000000)); var red = colorCode >> 16; var green = (colorCode >> 8) & 0xff; var blue = colorCode & 0xff; var receivers = {}; if (options.hasOwnProperty("receivers")) { ["rgb", "hsl", "hex"].forEach(function (format) { if (options.receivers.hasOwnProperty(format)) { receivers[format] = options.receivers[format]; // link to callbacks } }); } var isRaw = DEFAULT_IS_RAW; if (options.hasOwnProperty("raw")) { isRaw = options.raw; } var alpha; if (options.hasOwnProperty("alpha")) { alpha = Math.min(Math.max(options.alpha, 0), 1); // clamp to [0; 1] } if ("rgb" in receivers) { if (alpha === undefined) { receivers.rgb(red, green, blue); } else { receivers.rgb(red, green, blue, alpha); } } if ("hsl" in receivers) { var hsl = isRaw ? convert.rgb.hsl.raw(red, green, blue) : convert.rgb.hsl(red, green, blue); if (alpha === undefined) { receivers.hsl.apply(receivers, hsl); } else { receivers.hsl.apply(receivers, __spreadArray(__spreadArray([], hsl, true), [alpha], false)); } } if ("hex" in receivers) { var hexColorCode = convert.rgb.hex(red, green, blue).toLowerCase(); if (alpha === undefined) { receivers.hex(hexColorCode); } else { var hexAlphaCode = Math.floor((alpha * 255)).toString(16); receivers.hex(hexColorCode + hexAlphaCode); } } var format = DEFAULT_COLOR_FORMAT; if (options.hasOwnProperty("format")) { format = options.format; } switch (format) { case "rgb": return alpha === undefined ? "rgb(".concat(red, ", ").concat(green, ", ").concat(blue, ")") : "rgb(".concat(red, ", ").concat(green, ", ").concat(blue, ", ").concat(alpha, ")"); case "hsl": var hsl = isRaw ? convert.rgb.hsl.raw(red, green, blue) : convert.rgb.hsl(red, green, blue); return alpha === undefined ? "hsl(".concat(hsl[0], ", ").concat(hsl[1], "%, ").concat(hsl[2], "%)") : "hsl(".concat(hsl[0], ", ").concat(hsl[1], "%, ").concat(hsl[2], "%, ").concat(alpha, ")"); default: // don't error case "hex": var hexColorCode = convert.rgb.hex(red, green, blue).toLowerCase(); var hexAlphaCode = Math.floor((alpha * 255)).toString(16); return alpha === undefined ? "#".concat(hexColorCode) : "#".concat(hexColorCode).concat(hexAlphaCode); } } exports.colorFromUuid = colorFromUuid;