ede712c50d401e451b563043c6a546840d80f31cf9ccad9a61f6bde71ebfe428de98edcf26b26947ebe5b0c565e6889387eeb03f59e7898cf2592e8f3afd6b 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.bounds = exports.random = void 0;
  4. /* eslint-disable @typescript-eslint/no-redundant-type-constituents */
  5. // randomColor by David Merfield under the CC0 license
  6. // https://github.com/davidmerfield/randomColor/
  7. var index_js_1 = require("./index.js");
  8. function random(options) {
  9. if (options === void 0) { options = {}; }
  10. // Check if we need to generate multiple colors
  11. if (options.count !== undefined &&
  12. options.count !== null) {
  13. var totalColors = options.count;
  14. var colors = [];
  15. options.count = undefined;
  16. while (totalColors > colors.length) {
  17. // Since we're generating multiple colors,
  18. // incremement the seed. Otherwise we'd just
  19. // generate the same color each time...
  20. options.count = null;
  21. if (options.seed) {
  22. options.seed += 1;
  23. }
  24. colors.push(random(options));
  25. }
  26. options.count = totalColors;
  27. return colors;
  28. }
  29. // First we pick a hue (H)
  30. var h = pickHue(options.hue, options.seed);
  31. // Then use H to determine saturation (S)
  32. var s = pickSaturation(h, options);
  33. // Then use S and H to determine brightness (B).
  34. var v = pickBrightness(h, s, options);
  35. var res = { h: h, s: s, v: v };
  36. if (options.alpha !== undefined) {
  37. res.a = options.alpha;
  38. }
  39. // Then we return the HSB color in the desired format
  40. return new index_js_1.TinyColor(res);
  41. }
  42. exports.random = random;
  43. function pickHue(hue, seed) {
  44. var hueRange = getHueRange(hue);
  45. var res = randomWithin(hueRange, seed);
  46. // Instead of storing red as two seperate ranges,
  47. // we group them, using negative numbers
  48. if (res < 0) {
  49. res = 360 + res;
  50. }
  51. return res;
  52. }
  53. function pickSaturation(hue, options) {
  54. if (options.hue === 'monochrome') {
  55. return 0;
  56. }
  57. if (options.luminosity === 'random') {
  58. return randomWithin([0, 100], options.seed);
  59. }
  60. var saturationRange = getColorInfo(hue).saturationRange;
  61. var sMin = saturationRange[0];
  62. var sMax = saturationRange[1];
  63. switch (options.luminosity) {
  64. case 'bright':
  65. sMin = 55;
  66. break;
  67. case 'dark':
  68. sMin = sMax - 10;
  69. break;
  70. case 'light':
  71. sMax = 55;
  72. break;
  73. default:
  74. break;
  75. }
  76. return randomWithin([sMin, sMax], options.seed);
  77. }
  78. function pickBrightness(H, S, options) {
  79. var bMin = getMinimumBrightness(H, S);
  80. var bMax = 100;
  81. switch (options.luminosity) {
  82. case 'dark':
  83. bMax = bMin + 20;
  84. break;
  85. case 'light':
  86. bMin = (bMax + bMin) / 2;
  87. break;
  88. case 'random':
  89. bMin = 0;
  90. bMax = 100;
  91. break;
  92. default:
  93. break;
  94. }
  95. return randomWithin([bMin, bMax], options.seed);
  96. }
  97. function getMinimumBrightness(H, S) {
  98. var lowerBounds = getColorInfo(H).lowerBounds;
  99. for (var i = 0; i < lowerBounds.length - 1; i++) {
  100. var s1 = lowerBounds[i][0];
  101. var v1 = lowerBounds[i][1];
  102. var s2 = lowerBounds[i + 1][0];
  103. var v2 = lowerBounds[i + 1][1];
  104. if (S >= s1 && S <= s2) {
  105. var m = (v2 - v1) / (s2 - s1);
  106. var b = v1 - m * s1;
  107. return m * S + b;
  108. }
  109. }
  110. return 0;
  111. }
  112. function getHueRange(colorInput) {
  113. var num = parseInt(colorInput, 10);
  114. if (!Number.isNaN(num) && num < 360 && num > 0) {
  115. return [num, num];
  116. }
  117. if (typeof colorInput === 'string') {
  118. var namedColor = exports.bounds.find(function (n) { return n.name === colorInput; });
  119. if (namedColor) {
  120. var color = defineColor(namedColor);
  121. if (color.hueRange) {
  122. return color.hueRange;
  123. }
  124. }
  125. var parsed = new index_js_1.TinyColor(colorInput);
  126. if (parsed.isValid) {
  127. var hue = parsed.toHsv().h;
  128. return [hue, hue];
  129. }
  130. }
  131. return [0, 360];
  132. }
  133. function getColorInfo(hue) {
  134. // Maps red colors to make picking hue easier
  135. if (hue >= 334 && hue <= 360) {
  136. hue -= 360;
  137. }
  138. for (var _i = 0, bounds_1 = exports.bounds; _i < bounds_1.length; _i++) {
  139. var bound = bounds_1[_i];
  140. var color = defineColor(bound);
  141. if (color.hueRange && hue >= color.hueRange[0] && hue <= color.hueRange[1]) {
  142. return color;
  143. }
  144. }
  145. throw Error('Color not found');
  146. }
  147. function randomWithin(range, seed) {
  148. if (seed === undefined) {
  149. return Math.floor(range[0] + Math.random() * (range[1] + 1 - range[0]));
  150. }
  151. // Seeded random algorithm from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
  152. var max = range[1] || 1;
  153. var min = range[0] || 0;
  154. seed = (seed * 9301 + 49297) % 233280;
  155. var rnd = seed / 233280.0;
  156. return Math.floor(min + rnd * (max - min));
  157. }
  158. function defineColor(bound) {
  159. var sMin = bound.lowerBounds[0][0];
  160. var sMax = bound.lowerBounds[bound.lowerBounds.length - 1][0];
  161. var bMin = bound.lowerBounds[bound.lowerBounds.length - 1][1];
  162. var bMax = bound.lowerBounds[0][1];
  163. return {
  164. name: bound.name,
  165. hueRange: bound.hueRange,
  166. lowerBounds: bound.lowerBounds,
  167. saturationRange: [sMin, sMax],
  168. brightnessRange: [bMin, bMax],
  169. };
  170. }
  171. /**
  172. * @hidden
  173. */
  174. exports.bounds = [
  175. {
  176. name: 'monochrome',
  177. hueRange: null,
  178. lowerBounds: [
  179. [0, 0],
  180. [100, 0],
  181. ],
  182. },
  183. {
  184. name: 'red',
  185. hueRange: [-26, 18],
  186. lowerBounds: [
  187. [20, 100],
  188. [30, 92],
  189. [40, 89],
  190. [50, 85],
  191. [60, 78],
  192. [70, 70],
  193. [80, 60],
  194. [90, 55],
  195. [100, 50],
  196. ],
  197. },
  198. {
  199. name: 'orange',
  200. hueRange: [19, 46],
  201. lowerBounds: [
  202. [20, 100],
  203. [30, 93],
  204. [40, 88],
  205. [50, 86],
  206. [60, 85],
  207. [70, 70],
  208. [100, 70],
  209. ],
  210. },
  211. {
  212. name: 'yellow',
  213. hueRange: [47, 62],
  214. lowerBounds: [
  215. [25, 100],
  216. [40, 94],
  217. [50, 89],
  218. [60, 86],
  219. [70, 84],
  220. [80, 82],
  221. [90, 80],
  222. [100, 75],
  223. ],
  224. },
  225. {
  226. name: 'green',
  227. hueRange: [63, 178],
  228. lowerBounds: [
  229. [30, 100],
  230. [40, 90],
  231. [50, 85],
  232. [60, 81],
  233. [70, 74],
  234. [80, 64],
  235. [90, 50],
  236. [100, 40],
  237. ],
  238. },
  239. {
  240. name: 'blue',
  241. hueRange: [179, 257],
  242. lowerBounds: [
  243. [20, 100],
  244. [30, 86],
  245. [40, 80],
  246. [50, 74],
  247. [60, 60],
  248. [70, 52],
  249. [80, 44],
  250. [90, 39],
  251. [100, 35],
  252. ],
  253. },
  254. {
  255. name: 'purple',
  256. hueRange: [258, 282],
  257. lowerBounds: [
  258. [20, 100],
  259. [30, 87],
  260. [40, 79],
  261. [50, 70],
  262. [60, 65],
  263. [70, 59],
  264. [80, 52],
  265. [90, 45],
  266. [100, 42],
  267. ],
  268. },
  269. {
  270. name: 'pink',
  271. hueRange: [283, 334],
  272. lowerBounds: [
  273. [20, 100],
  274. [30, 90],
  275. [40, 86],
  276. [60, 84],
  277. [80, 80],
  278. [90, 75],
  279. [100, 73],
  280. ],
  281. },
  282. ];