palette.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 =
  44. /** @class */
  45. function () {
  46. function PaletteMixin() {}
  47. PaletteMixin.prototype.getColorFromPalette = function (name, scope, requestNum) {
  48. var defaultPalette = normalizeToArray(this.get('color', true));
  49. var layeredPalette = this.get('colorLayer', true);
  50. return getFromPalette(this, innerColor, defaultPalette, layeredPalette, name, scope, requestNum);
  51. };
  52. PaletteMixin.prototype.clearColorPalette = function () {
  53. clearPalette(this, innerColor);
  54. };
  55. return PaletteMixin;
  56. }();
  57. export function getDecalFromPalette(ecModel, name, scope, requestNum) {
  58. var defaultDecals = normalizeToArray(ecModel.get(['aria', 'decal', 'decals']));
  59. return getFromPalette(ecModel, innerDecal, defaultDecals, null, name, scope, requestNum);
  60. }
  61. function getNearestPalette(palettes, requestColorNum) {
  62. var paletteNum = palettes.length; // TODO palettes must be in order
  63. for (var i = 0; i < paletteNum; i++) {
  64. if (palettes[i].length > requestColorNum) {
  65. return palettes[i];
  66. }
  67. }
  68. return palettes[paletteNum - 1];
  69. }
  70. /**
  71. * @param name MUST NOT be null/undefined. Otherwise call this function
  72. * twise with the same parameters will get different result.
  73. * @param scope default this.
  74. * @return Can be null/undefined
  75. */
  76. function getFromPalette(that, inner, defaultPalette, layeredPalette, name, scope, requestNum) {
  77. scope = scope || that;
  78. var scopeFields = inner(scope);
  79. var paletteIdx = scopeFields.paletteIdx || 0;
  80. var paletteNameMap = scopeFields.paletteNameMap = scopeFields.paletteNameMap || {}; // 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); // In case can't find in layered color palette.
  85. palette = palette || defaultPalette;
  86. if (!palette || !palette.length) {
  87. return;
  88. }
  89. var pickedPaletteItem = palette[paletteIdx];
  90. if (name) {
  91. paletteNameMap[name] = pickedPaletteItem;
  92. }
  93. scopeFields.paletteIdx = (paletteIdx + 1) % palette.length;
  94. return pickedPaletteItem;
  95. }
  96. function clearPalette(that, inner) {
  97. inner(that).paletteIdx = 0;
  98. inner(that).paletteNameMap = {};
  99. }
  100. export { PaletteMixin };