palette.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /**
  2. * DevExtreme (viz/palette.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. Object.defineProperty(exports, "__esModule", {
  11. value: true
  12. });
  13. exports._DEBUG_palettes = void 0;
  14. var _palettes;
  15. exports.currentPalette = currentPalette;
  16. exports.generateColors = generateColors;
  17. exports.getPalette = getPalette;
  18. exports.registerPalette = registerPalette;
  19. exports.getAccentColor = getAccentColor;
  20. exports.createPalette = createPalette;
  21. exports.getDiscretePalette = getDiscretePalette;
  22. exports.getGradientPalette = getGradientPalette;
  23. var _utils = require("./core/utils");
  24. var _extend = require("../core/utils/extend");
  25. var _errors = require("../core/errors");
  26. var _errors2 = _interopRequireDefault(_errors);
  27. function _interopRequireDefault(obj) {
  28. return obj && obj.__esModule ? obj : {
  29. "default": obj
  30. }
  31. }
  32. function _defineProperty(obj, key, value) {
  33. if (key in obj) {
  34. Object.defineProperty(obj, key, {
  35. value: value,
  36. enumerable: true,
  37. configurable: true,
  38. writable: true
  39. })
  40. } else {
  41. obj[key] = value
  42. }
  43. return obj
  44. }
  45. var _floor = Math.floor;
  46. var _ceil = Math.ceil;
  47. var _Color = require("../color");
  48. var _isArray = Array.isArray;
  49. var _isString = require("../core/utils/type").isString;
  50. var HIGHLIGHTING_STEP = 50;
  51. var DEFAULT_PALETTE = "material";
  52. var officePalette = {
  53. simpleSet: ["#5f8b95", "#ba4d51", "#af8a53", "#955f71", "#859666", "#7e688c"],
  54. indicatingSet: ["#a3b97c", "#e1b676", "#ec7f83"],
  55. gradientSet: ["#5f8b95", "#ba4d51"],
  56. accentColor: "#ba4d51"
  57. };
  58. var palettes = (_palettes = {}, _defineProperty(_palettes, DEFAULT_PALETTE, {
  59. simpleSet: ["#1db2f5", "#f5564a", "#97c95c", "#ffc720", "#eb3573", "#a63db8"],
  60. indicatingSet: ["#97c95c", "#ffc720", "#f5564a"],
  61. gradientSet: ["#1db2f5", "#97c95c"],
  62. accentColor: "#1db2f5"
  63. }), _defineProperty(_palettes, "default", officePalette), _defineProperty(_palettes, "office", officePalette), _defineProperty(_palettes, "harmony light", {
  64. simpleSet: ["#fcb65e", "#679ec5", "#ad79ce", "#7abd5c", "#e18e92", "#b6d623", "#b7abea", "#85dbd5"],
  65. indicatingSet: ["#b6d623", "#fcb65e", "#e18e92"],
  66. gradientSet: ["#7abd5c", "#fcb65e"],
  67. accentColor: "#679ec5"
  68. }), _defineProperty(_palettes, "soft pastel", {
  69. simpleSet: ["#60a69f", "#78b6d9", "#6682bb", "#a37182", "#eeba69", "#90ba58", "#456c68", "#7565a4"],
  70. indicatingSet: ["#90ba58", "#eeba69", "#a37182"],
  71. gradientSet: ["#78b6d9", "#eeba69"],
  72. accentColor: "#60a69f"
  73. }), _defineProperty(_palettes, "pastel", {
  74. simpleSet: ["#bb7862", "#70b3a1", "#bb626a", "#057d85", "#ab394b", "#dac599", "#153459", "#b1d2c6"],
  75. indicatingSet: ["#70b3a1", "#dac599", "#bb626a"],
  76. gradientSet: ["#bb7862", "#70b3a1"],
  77. accentColor: "#bb7862"
  78. }), _defineProperty(_palettes, "bright", {
  79. simpleSet: ["#70c92f", "#f8ca00", "#bd1550", "#e97f02", "#9d419c", "#7e4452", "#9ab57e", "#36a3a6"],
  80. indicatingSet: ["#70c92f", "#f8ca00", "#bd1550"],
  81. gradientSet: ["#e97f02", "#f8ca00"],
  82. accentColor: "#e97f02"
  83. }), _defineProperty(_palettes, "soft", {
  84. simpleSet: ["#cbc87b", "#9ab57e", "#e55253", "#7e4452", "#e8c267", "#565077", "#6babac", "#ad6082"],
  85. indicatingSet: ["#9ab57e", "#e8c267", "#e55253"],
  86. gradientSet: ["#9ab57e", "#e8c267"],
  87. accentColor: "#565077"
  88. }), _defineProperty(_palettes, "ocean", {
  89. simpleSet: ["#75c099", "#acc371", "#378a8a", "#5fa26a", "#064970", "#38c5d2", "#00a7c6", "#6f84bb"],
  90. indicatingSet: ["#c8e394", "#7bc59d", "#397c8b"],
  91. gradientSet: ["#acc371", "#38c5d2"],
  92. accentColor: "#378a8a"
  93. }), _defineProperty(_palettes, "vintage", {
  94. simpleSet: ["#dea484", "#efc59c", "#cb715e", "#eb9692", "#a85c4c", "#f2c0b5", "#c96374", "#dd956c"],
  95. indicatingSet: ["#ffe5c6", "#f4bb9d", "#e57660"],
  96. gradientSet: ["#efc59c", "#cb715e"],
  97. accentColor: "#cb715e"
  98. }), _defineProperty(_palettes, "violet", {
  99. simpleSet: ["#d1a1d1", "#eeacc5", "#7b5685", "#7e7cad", "#a13d73", "#5b41ab", "#e287e2", "#689cc1"],
  100. indicatingSet: ["#d8e2f6", "#d0b2da", "#d56a8a"],
  101. gradientSet: ["#eeacc5", "#7b5685"],
  102. accentColor: "#7b5685"
  103. }), _defineProperty(_palettes, "carmine", {
  104. simpleSet: ["#fb7764", "#73d47f", "#fed85e", "#d47683", "#dde392", "#757ab2"],
  105. indicatingSet: ["#5cb85c", "#f0ad4e", "#d9534f"],
  106. gradientSet: ["#fb7764", "#73d47f"],
  107. accentColor: "#f05b41"
  108. }), _defineProperty(_palettes, "dark moon", {
  109. simpleSet: ["#4ddac1", "#f4c99a", "#80dd9b", "#f998b3", "#4aaaa0", "#a5aef1"],
  110. indicatingSet: ["#59d8a4", "#f0ad4e", "#f9517e"],
  111. gradientSet: ["#4ddac1", "#f4c99a"],
  112. accentColor: "#3debd3"
  113. }), _defineProperty(_palettes, "soft blue", {
  114. simpleSet: ["#7ab8eb", "#97da97", "#facb86", "#e78683", "#839bda", "#4db7be"],
  115. indicatingSet: ["#5cb85c", "#f0ad4e", "#d9534f"],
  116. gradientSet: ["#7ab8eb", "#97da97"],
  117. accentColor: "#7ab8eb"
  118. }), _defineProperty(_palettes, "dark violet", {
  119. simpleSet: ["#9c63ff", "#64c064", "#eead51", "#d2504b", "#4b6bbf", "#2da7b0"],
  120. indicatingSet: ["#5cb85c", "#f0ad4e", "#d9534f"],
  121. gradientSet: ["#9c63ff", "#64c064"],
  122. accentColor: "#9c63ff"
  123. }), _defineProperty(_palettes, "green mist", {
  124. simpleSet: ["#3cbab2", "#8ed962", "#5b9d95", "#efcc7c", "#f1929f", "#4d8dab"],
  125. indicatingSet: ["#72d63c", "#ffc852", "#f74a5e"],
  126. gradientSet: ["#3cbab2", "#8ed962"],
  127. accentColor: "#3cbab2"
  128. }), _palettes);
  129. var currentPaletteName;
  130. function currentPalette(name) {
  131. if (void 0 === name) {
  132. return currentPaletteName || DEFAULT_PALETTE
  133. } else {
  134. name = (0, _utils.normalizeEnum)(name);
  135. currentPaletteName = name in palettes ? name : void 0
  136. }
  137. }
  138. function generateColors(palette, count) {
  139. var options = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : {
  140. keepLastColorInEnd: false
  141. };
  142. options.type = options.baseColorSet;
  143. options.extensionMode = options.paletteExtensionMode;
  144. return createPalette(palette, options).generateColors(count)
  145. }
  146. function getPalette(palette, parameters) {
  147. parameters = parameters || {};
  148. palette = selectPaletteOnSeniority(palette, parameters.themeDefault);
  149. var result;
  150. var type = parameters.type;
  151. if (_isArray(palette)) {
  152. return palette.slice(0)
  153. } else {
  154. if (_isString(palette)) {
  155. result = palettes[(0, _utils.normalizeEnum)(palette)]
  156. }
  157. if (!result) {
  158. result = palettes[currentPalette()]
  159. }
  160. }
  161. return type ? result[type].slice(0) : result
  162. }
  163. function registerPalette(name, palette) {
  164. var item = {};
  165. var paletteName;
  166. if (_isArray(palette)) {
  167. item.simpleSet = palette.slice(0)
  168. } else {
  169. if (palette) {
  170. item.simpleSet = _isArray(palette.simpleSet) ? palette.simpleSet.slice(0) : void 0;
  171. item.indicatingSet = _isArray(palette.indicatingSet) ? palette.indicatingSet.slice(0) : void 0;
  172. item.gradientSet = _isArray(palette.gradientSet) ? palette.gradientSet.slice(0) : void 0;
  173. item.accentColor = palette.accentColor
  174. }
  175. }
  176. if (!item.accentColor) {
  177. item.accentColor = item.simpleSet && item.simpleSet[0]
  178. }
  179. if (item.simpleSet || item.indicatingSet || item.gradientSet) {
  180. paletteName = (0, _utils.normalizeEnum)(name);
  181. (0, _extend.extend)(palettes[paletteName] = palettes[paletteName] || {}, item)
  182. }
  183. }
  184. function getAccentColor(palette, themeDefault) {
  185. palette = getPalette(palette, {
  186. themeDefault: themeDefault
  187. });
  188. return palette.accentColor || palette[0]
  189. }
  190. function RingBuf(buf) {
  191. var ind = 0;
  192. this.next = function() {
  193. var res = buf[ind++];
  194. if (ind === buf.length) {
  195. this.reset()
  196. }
  197. return res
  198. };
  199. this.reset = function() {
  200. ind = 0
  201. }
  202. }
  203. function getAlternateColorsStrategy(palette, parameters) {
  204. var stepHighlight = parameters.useHighlight ? HIGHLIGHTING_STEP : 0;
  205. var paletteSteps = new RingBuf([0, stepHighlight, -stepHighlight]);
  206. var currentPalette = [];
  207. function _reset() {
  208. var step = paletteSteps.next();
  209. currentPalette = step ? getAlteredPalette(palette, step) : palette.slice(0)
  210. }
  211. return {
  212. getColor: function(index) {
  213. var color = currentPalette[index % palette.length];
  214. if (index % palette.length === palette.length - 1) {
  215. _reset()
  216. }
  217. return color
  218. },
  219. generateColors: function(count) {
  220. var colors = [];
  221. count = count || parameters.count;
  222. for (var i = 0; i < count; i++) {
  223. colors.push(this.getColor(i))
  224. }
  225. return colors
  226. },
  227. reset: function() {
  228. paletteSteps.reset();
  229. _reset()
  230. }
  231. }
  232. }
  233. function getExtrapolateColorsStrategy(palette, parameters) {
  234. function convertColor(color, cycleIndex, cycleCount) {
  235. var hsl = new _Color(color).hsl;
  236. var l = hsl.l / 100;
  237. var diapason = cycleCount - 1 / cycleCount;
  238. var minL = l - .5 * diapason;
  239. var maxL = l + .5 * diapason;
  240. var cycleMiddle = (cycleCount - 1) / 2;
  241. var cycleDiff = cycleIndex - cycleMiddle;
  242. if (minL < Math.min(.5, .9 * l)) {
  243. minL = Math.min(.5, .9 * l)
  244. }
  245. if (maxL > Math.max(.8, l + .15 * (1 - l))) {
  246. maxL = Math.max(.8, l + .15 * (1 - l))
  247. }
  248. if (cycleDiff < 0) {
  249. l -= (minL - l) * cycleDiff / cycleMiddle
  250. } else {
  251. l += (maxL - l) * (cycleDiff / cycleMiddle)
  252. }
  253. hsl.l = 100 * l;
  254. return _Color.prototype.fromHSL(hsl).toHex()
  255. }
  256. return {
  257. getColor: function(index, count) {
  258. var paletteCount = palette.length;
  259. var cycles = _floor((count - 1) / paletteCount + 1);
  260. var color = palette[index % paletteCount];
  261. if (cycles > 1) {
  262. return convertColor(color, _floor(index / paletteCount), cycles)
  263. }
  264. return color
  265. },
  266. generateColors: function(count) {
  267. var colors = [];
  268. count = count || parameters.count;
  269. for (var i = 0; i < count; i++) {
  270. colors.push(this.getColor(i, count))
  271. }
  272. return colors
  273. },
  274. reset: function() {}
  275. }
  276. }
  277. function getColorMixer(palette, parameters) {
  278. var paletteCount = palette.length;
  279. var extendedPalette = [];
  280. function distributeColors(count, colorsCount, startIndex, distribution) {
  281. var groupSize = Math.floor(count / colorsCount);
  282. var extraItems = count - colorsCount * groupSize;
  283. var i = startIndex;
  284. var middleIndex;
  285. var size;
  286. while (i < startIndex + count) {
  287. size = groupSize;
  288. if (extraItems > 0) {
  289. size += 1;
  290. extraItems--
  291. }
  292. middleIndex = size > 2 ? Math.floor(size / 2) : 0;
  293. distribution.push(i + middleIndex);
  294. i += size
  295. }
  296. return distribution.sort(function(a, b) {
  297. return a - b
  298. })
  299. }
  300. function getColorAndDistance(arr, startIndex, count) {
  301. startIndex = (count + startIndex) % count;
  302. var distance = 0;
  303. for (var i = startIndex; i < 2 * count; i += 1) {
  304. var index = (count + i) % count;
  305. if (arr[index]) {
  306. return [arr[index], distance]
  307. }
  308. distance++
  309. }
  310. }
  311. function blendColors(paletteWithEmptyColors, paletteLength) {
  312. for (var i = 0; i < paletteLength; i++) {
  313. var color = paletteWithEmptyColors[i];
  314. if (!color) {
  315. var color1 = paletteWithEmptyColors[i - 1];
  316. if (!color1) {
  317. continue
  318. } else {
  319. var c2 = getColorAndDistance(paletteWithEmptyColors, i, paletteLength);
  320. var color2 = new _Color(c2[0]);
  321. color1 = new _Color(color1);
  322. for (var j = 0; j < c2[1]; j++, i++) {
  323. paletteWithEmptyColors[i] = color1.blend(color2, (j + 1) / (c2[1] + 1)).toHex()
  324. }
  325. }
  326. }
  327. }
  328. return paletteWithEmptyColors
  329. }
  330. function extendPalette(count) {
  331. if (count <= paletteCount) {
  332. return palette
  333. }
  334. var result = [];
  335. var colorInGroups = paletteCount - 2;
  336. var currentColorIndex = 0;
  337. var cleanColorIndices = [];
  338. if (parameters.keepLastColorInEnd) {
  339. cleanColorIndices = distributeColors(count - 2, colorInGroups, 1, [0, count - 1])
  340. } else {
  341. cleanColorIndices = distributeColors(count - 1, paletteCount - 1, 1, [0])
  342. }
  343. for (var i = 0; i < count; i++) {
  344. if (cleanColorIndices.indexOf(i) > -1) {
  345. result[i] = palette[currentColorIndex++]
  346. }
  347. }
  348. result = blendColors(result, count);
  349. return result
  350. }
  351. return {
  352. getColor: function(index, count) {
  353. count = count || parameters.count || paletteCount;
  354. if (extendedPalette.length !== count) {
  355. extendedPalette = extendPalette(count)
  356. }
  357. return extendedPalette[index % count]
  358. },
  359. generateColors: function(count, repeat) {
  360. count = count || parameters.count || paletteCount;
  361. if (repeat && count > paletteCount) {
  362. var colors = extendPalette(paletteCount);
  363. for (var i = 0; i < count - paletteCount; i++) {
  364. colors.push(colors[i])
  365. }
  366. return colors
  367. } else {
  368. return paletteCount > 0 ? extendPalette(count).slice(0, count) : []
  369. }
  370. },
  371. reset: function() {}
  372. }
  373. }
  374. function createPalette(palette, parameters, themeDefaultPalette) {
  375. var paletteObj = {
  376. dispose: function() {
  377. this._extensionStrategy = null
  378. },
  379. getNextColor: function(count) {
  380. return this._extensionStrategy.getColor(this._currentColor++, count)
  381. },
  382. generateColors: function(count, parameters) {
  383. return this._extensionStrategy.generateColors(count, (parameters || {}).repeat)
  384. },
  385. reset: function() {
  386. this._currentColor = 0;
  387. this._extensionStrategy.reset();
  388. return this
  389. }
  390. };
  391. parameters = parameters || {};
  392. var extensionMode = (parameters.extensionMode || "").toLowerCase();
  393. var colors = getPalette(palette, {
  394. type: parameters.type || "simpleSet",
  395. themeDefault: themeDefaultPalette
  396. });
  397. if ("alternate" === extensionMode) {
  398. paletteObj._extensionStrategy = getAlternateColorsStrategy(colors, parameters)
  399. } else {
  400. if ("extrapolate" === extensionMode) {
  401. paletteObj._extensionStrategy = getExtrapolateColorsStrategy(colors, parameters)
  402. } else {
  403. paletteObj._extensionStrategy = getColorMixer(colors, parameters)
  404. }
  405. }
  406. paletteObj.reset();
  407. return paletteObj
  408. }
  409. function getAlteredPalette(originalPalette, step) {
  410. var palette = [];
  411. var i;
  412. var ii = originalPalette.length;
  413. for (i = 0; i < ii; ++i) {
  414. palette.push(getNewColor(originalPalette[i], step))
  415. }
  416. return palette
  417. }
  418. function getNewColor(currentColor, step) {
  419. var newColor = new _Color(currentColor).alter(step);
  420. var lightness = getLightness(newColor);
  421. if (lightness > 200 || lightness < 55) {
  422. newColor = new _Color(currentColor).alter(-step / 2)
  423. }
  424. return newColor.toHex()
  425. }
  426. function getLightness(color) {
  427. return .3 * color.r + .59 * color.g + .11 * color.b
  428. }
  429. function getDiscretePalette(source, size, themeDefaultPalette) {
  430. var palette = size > 0 ? createDiscreteColors(getPalette(source, {
  431. type: "gradientSet",
  432. themeDefault: themeDefaultPalette
  433. }), size) : [];
  434. return {
  435. getColor: function(index) {
  436. return palette[index] || null
  437. }
  438. }
  439. }
  440. function createDiscreteColors(source, count) {
  441. var colorCount = count - 1;
  442. var sourceCount = source.length - 1;
  443. var colors = [];
  444. var gradient = [];
  445. var i;
  446. function addColor(pos) {
  447. var k = sourceCount * pos;
  448. var kl = _floor(k);
  449. var kr = _ceil(k);
  450. gradient.push(colors[kl].blend(colors[kr], k - kl).toHex())
  451. }
  452. for (i = 0; i <= sourceCount; ++i) {
  453. colors.push(new _Color(source[i]))
  454. }
  455. if (colorCount > 0) {
  456. for (i = 0; i <= colorCount; ++i) {
  457. addColor(i / colorCount)
  458. }
  459. } else {
  460. addColor(.5)
  461. }
  462. return gradient
  463. }
  464. function getGradientPalette(source, themeDefaultPalette) {
  465. var palette = getPalette(source, {
  466. type: "gradientSet",
  467. themeDefault: themeDefaultPalette
  468. });
  469. var color1 = new _Color(palette[0]);
  470. var color2 = new _Color(palette[1]);
  471. return {
  472. getColor: function(ratio) {
  473. return 0 <= ratio && ratio <= 1 ? color1.blend(color2, ratio).toHex() : null
  474. }
  475. }
  476. }
  477. function selectPaletteOnSeniority(source, themeDefaultPalette) {
  478. var result = source || (void 0 === currentPaletteName ? themeDefaultPalette : currentPalette());
  479. if ("default" === result) {
  480. _errors2.default.log("W0016", '"palette"', "Default", "18.1", 'Use the "Office" value instead.')
  481. }
  482. return result
  483. }