palette.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { makeInner, normalizeToArray } from '../../util/model.js';
  41. var innerColor = makeInner();
  42. var innerDecal = makeInner();
  43. var PaletteMixin = /** @class */function () {
  44. function PaletteMixin() {}
  45. PaletteMixin.prototype.getColorFromPalette = function (name, scope, requestNum) {
  46. var defaultPalette = normalizeToArray(this.get('color', true));
  47. var layeredPalette = this.get('colorLayer', true);
  48. return getFromPalette(this, innerColor, defaultPalette, layeredPalette, name, scope, requestNum);
  49. };
  50. PaletteMixin.prototype.clearColorPalette = function () {
  51. clearPalette(this, innerColor);
  52. };
  53. return PaletteMixin;
  54. }();
  55. export function getDecalFromPalette(ecModel, name, scope, requestNum) {
  56. var defaultDecals = normalizeToArray(ecModel.get(['aria', 'decal', 'decals']));
  57. return getFromPalette(ecModel, innerDecal, defaultDecals, null, name, scope, requestNum);
  58. }
  59. function getNearestPalette(palettes, requestColorNum) {
  60. var paletteNum = palettes.length;
  61. // TODO palettes must be in order
  62. for (var i = 0; i < paletteNum; i++) {
  63. if (palettes[i].length > requestColorNum) {
  64. return palettes[i];
  65. }
  66. }
  67. return palettes[paletteNum - 1];
  68. }
  69. /**
  70. * @param name MUST NOT be null/undefined. Otherwise call this function
  71. * twise with the same parameters will get different result.
  72. * @param scope default this.
  73. * @return Can be null/undefined
  74. */
  75. function getFromPalette(that, inner, defaultPalette, layeredPalette, name, scope, requestNum) {
  76. scope = scope || that;
  77. var scopeFields = inner(scope);
  78. var paletteIdx = scopeFields.paletteIdx || 0;
  79. var paletteNameMap = scopeFields.paletteNameMap = scopeFields.paletteNameMap || {};
  80. // Use `hasOwnProperty` to avoid conflict with Object.prototype.
  81. if (paletteNameMap.hasOwnProperty(name)) {
  82. return paletteNameMap[name];
  83. }
  84. var palette = requestNum == null || !layeredPalette ? defaultPalette : getNearestPalette(layeredPalette, requestNum);
  85. // In case can't find in layered color palette.
  86. palette = palette || defaultPalette;
  87. if (!palette || !palette.length) {
  88. return;
  89. }
  90. var pickedPaletteItem = palette[paletteIdx];
  91. if (name) {
  92. paletteNameMap[name] = pickedPaletteItem;
  93. }
  94. scopeFields.paletteIdx = (paletteIdx + 1) % palette.length;
  95. return pickedPaletteItem;
  96. }
  97. function clearPalette(that, inner) {
  98. inner(that).paletteIdx = 0;
  99. inner(that).paletteNameMap = {};
  100. }
  101. export { PaletteMixin };