1 line
22 KiB
JSON
1 line
22 KiB
JSON
|
{"ast":null,"code":"/* eslint-disable @typescript-eslint/no-redundant-type-constituents */\n// randomColor by David Merfield under the CC0 license\n// https://github.com/davidmerfield/randomColor/\nimport { TinyColor } from './index.js';\nexport function random(options) {\n if (options === void 0) {\n options = {};\n }\n // Check if we need to generate multiple colors\n if (options.count !== undefined && options.count !== null) {\n var totalColors = options.count;\n var colors = [];\n options.count = undefined;\n while (totalColors > colors.length) {\n // Since we're generating multiple colors,\n // incremement the seed. Otherwise we'd just\n // generate the same color each time...\n options.count = null;\n if (options.seed) {\n options.seed += 1;\n }\n colors.push(random(options));\n }\n options.count = totalColors;\n return colors;\n }\n // First we pick a hue (H)\n var h = pickHue(options.hue, options.seed);\n // Then use H to determine saturation (S)\n var s = pickSaturation(h, options);\n // Then use S and H to determine brightness (B).\n var v = pickBrightness(h, s, options);\n var res = {\n h: h,\n s: s,\n v: v\n };\n if (options.alpha !== undefined) {\n res.a = options.alpha;\n }\n // Then we return the HSB color in the desired format\n return new TinyColor(res);\n}\nfunction pickHue(hue, seed) {\n var hueRange = getHueRange(hue);\n var res = randomWithin(hueRange, seed);\n // Instead of storing red as two seperate ranges,\n // we group them, using negative numbers\n if (res < 0) {\n res = 360 + res;\n }\n return res;\n}\nfunction pickSaturation(hue, options) {\n if (options.hue === 'monochrome') {\n return 0;\n }\n if (options.luminosity === 'random') {\n return randomWithin([0, 100], options.seed);\n }\n var saturationRange = getColorInfo(hue).saturationRange;\n var sMin = saturationRange[0];\n var sMax = saturationRange[1];\n switch (options.luminosity) {\n case 'bright':\n sMin = 55;\n break;\n case 'dark':\n sMin = sMax - 10;\n break;\n case 'light':\n sMax = 55;\n break;\n default:\n break;\n }\n return randomWithin([sMin, sMax], options.seed);\n}\nfunction pickBrightness(H, S, options) {\n var bMin = getMinimumBrightness(H, S);\n var bMax = 100;\n switch (options.luminosity) {\n case 'dark':\n bMax = bMin + 20;\n break;\n case 'light':\n bMin = (bMax + bMin) / 2;\n break;\n case 'random':\n bMin = 0;\n bMax = 100;\n break;\n default:\n break;\n }\n return randomWithin([bMin, bMax], options.seed);\n}\nfunction getMinimumBrightness(H, S) {\n var lowerBounds = getColorInfo(H).lowerBounds;\n for (var i = 0; i < lowerBounds.length - 1; i++) {\n var s1 = lowerBounds[i][0];\n var v1 = lowerBounds[i][1];\n var s2 = lowerBounds[i + 1][0];\n var v2 = lowerBounds[i + 1][1];\n if (S >= s1 && S <= s2) {\n var m = (v2 - v1) / (s2 - s1);\n var b = v1 - m * s1;\n return m * S + b;\n }\n }\n return 0;\n}\nfunction getHueRange(colorInput) {\n var num = parseInt(colorInput, 10);\n if (!Number.isNaN(num) && num < 360 && num > 0) {\n return [num, num];\n }\n if (typeof colorInput === 'string') {\n var namedColor = bounds.find(function (n) {\n return n.name === colorInput;\n });\n if (namedColor) {\n var color = defineColor(namedColor);\n if (color.hueRange) {\n return color.hueRange;\n }\n }\n var parsed = new TinyColor(colorInput);\n if (parsed.isValid) {\n var hue = parsed.toHsv().h;\n return [hue, hue];\n }\n }\n return [0, 360];\n}\nfunction getColorInfo(hue) {\n // Maps red colors to make picking hue easier\n if (hue >= 334 && hue <= 360) {\n hue -= 360;\n }\n for (var _i = 0, bounds_1 = bounds; _i < bounds_1.length; _i++) {\n var bound = bounds_1[_i];\n var color = defineColor(bound);\n if (color.hueRange && hue >= color.hueRange[0] && hue <= color.hueRange[1]) {\n return colo
|