plotboilerplate 1.24.0 → 1.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/README.md +13 -1
  2. package/dist/index.esm.js +294 -129
  3. package/dist/index.esm.js.map +1 -1
  4. package/dist/index.esm.min.js +1 -1
  5. package/dist/index.esm.min.js.map +1 -1
  6. package/dist/plotboilerplate.browser.min.js +1 -1
  7. package/dist/plotboilerplate.browser.min.js.map +1 -1
  8. package/dist/plotboilerplate.js +12084 -11919
  9. package/dist/plotboilerplate.min.js +1 -1
  10. package/package.json +9 -5
  11. package/src/cjs/PlotBoilerplate.d.ts +37 -6
  12. package/src/cjs/PlotBoilerplate.js +222 -116
  13. package/src/cjs/PlotBoilerplate.js.map +1 -1
  14. package/src/cjs/Polygon.d.ts +26 -7
  15. package/src/cjs/Polygon.js +56 -10
  16. package/src/cjs/Polygon.js.map +1 -1
  17. package/src/cjs/Triangle.js +2 -2
  18. package/src/cjs/Triangle.js.map +1 -1
  19. package/src/cjs/Vertex.d.ts +15 -3
  20. package/src/cjs/Vertex.js +18 -1
  21. package/src/cjs/Vertex.js.map +1 -1
  22. package/src/cjs/interfaces/core.d.ts +28 -0
  23. package/src/cjs/interfaces/core.js +2 -0
  24. package/src/cjs/interfaces/core.js.map +1 -1
  25. package/src/cjs/utils/datastructures/Color.d.ts +147 -1
  26. package/src/cjs/utils/datastructures/Color.js +385 -41
  27. package/src/cjs/utils/datastructures/Color.js.map +1 -1
  28. package/src/cjs/utils/datastructures/ColorGradient.d.ts +79 -0
  29. package/src/cjs/utils/datastructures/ColorGradient.js +171 -0
  30. package/src/cjs/utils/datastructures/ColorGradient.js.map +1 -0
  31. package/src/cjs/utils/dom/components/ColorGradientPicker.d.ts +245 -0
  32. package/src/cjs/utils/dom/components/ColorGradientPicker.js +667 -0
  33. package/src/cjs/utils/dom/components/ColorGradientPicker.js.map +1 -0
  34. package/src/cjs/utils/dom/components/ColorGradientPickerJSX.d.ts +129 -0
  35. package/src/cjs/utils/dom/components/ColorGradientPickerJSX.js +520 -0
  36. package/src/cjs/utils/dom/components/ColorGradientPickerJSX.js.map +1 -0
  37. package/src/cjs/utils/dom/components/ColorGradientSelector.d.ts +117 -0
  38. package/src/cjs/utils/dom/components/ColorGradientSelector.js +325 -0
  39. package/src/cjs/utils/dom/components/ColorGradientSelector.js.map +1 -0
  40. package/src/cjs/utils/dom/components/Test.d.ts +7 -0
  41. package/src/cjs/utils/dom/components/Test.js +30 -0
  42. package/src/cjs/utils/dom/components/Test.js.map +1 -0
  43. package/src/cjs/utils/dom/guiSizeToggler.js +1 -1
  44. package/src/cjs/utils/dom/guiSizeToggler.js.map +1 -1
  45. package/src/cjs/utils/helpers/BezierPathInteractionHelper.d.ts +9 -3
  46. package/src/cjs/utils/helpers/BezierPathInteractionHelper.js +20 -13
  47. package/src/cjs/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
  48. package/src/cjs/utils/helpers/CircleHelper.d.ts +10 -1
  49. package/src/cjs/utils/helpers/CircleHelper.js +13 -2
  50. package/src/cjs/utils/helpers/CircleHelper.js.map +1 -1
  51. package/src/cjs/utils/helpers/CircleSectorHelper.d.ts +2 -2
  52. package/src/cjs/utils/helpers/CircleSectorHelper.js +1 -0
  53. package/src/cjs/utils/helpers/CircleSectorHelper.js.map +1 -1
  54. package/src/cjs/utils/helpers/TriangleHelper.d.ts +57 -0
  55. package/src/cjs/utils/helpers/TriangleHelper.js +83 -0
  56. package/src/cjs/utils/helpers/TriangleHelper.js.map +1 -0
  57. package/src/cjs/utils/helpers/VEllipseHelper.d.ts +6 -4
  58. package/src/cjs/utils/helpers/VEllipseHelper.js +4 -2
  59. package/src/cjs/utils/helpers/VEllipseHelper.js.map +1 -1
  60. package/src/cjs/utils/helpers/VEllipseSectorHelper.d.ts +3 -2
  61. package/src/cjs/utils/helpers/VEllipseSectorHelper.js +1 -0
  62. package/src/cjs/utils/helpers/VEllipseSectorHelper.js.map +1 -1
  63. package/src/cjs/utils/parsers/LinearColorGradientParser.d.ts +78 -0
  64. package/src/cjs/utils/parsers/LinearColorGradientParser.js +224 -0
  65. package/src/cjs/utils/parsers/LinearColorGradientParser.js.map +1 -0
  66. package/src/createDemoDrawables.js +1 -0
  67. package/src/esm/PlotBoilerplate.d.ts +37 -6
  68. package/src/esm/PlotBoilerplate.js +222 -116
  69. package/src/esm/PlotBoilerplate.js.map +1 -1
  70. package/src/esm/Polygon.d.ts +26 -7
  71. package/src/esm/Polygon.js +53 -10
  72. package/src/esm/Polygon.js.map +1 -1
  73. package/src/esm/Triangle.js +2 -2
  74. package/src/esm/Triangle.js.map +1 -1
  75. package/src/esm/Vertex.d.ts +15 -3
  76. package/src/esm/Vertex.js +18 -1
  77. package/src/esm/Vertex.js.map +1 -1
  78. package/src/esm/interfaces/core.d.ts +28 -0
  79. package/src/esm/interfaces/core.js +2 -0
  80. package/src/esm/interfaces/core.js.map +1 -1
  81. package/src/esm/utils/datastructures/Color.d.ts +147 -1
  82. package/src/esm/utils/datastructures/Color.js +386 -42
  83. package/src/esm/utils/datastructures/Color.js.map +1 -1
  84. package/src/esm/utils/datastructures/ColorGradient.d.ts +79 -0
  85. package/src/esm/utils/datastructures/ColorGradient.js +166 -0
  86. package/src/esm/utils/datastructures/ColorGradient.js.map +1 -0
  87. package/src/esm/utils/dom/components/ColorGradientPicker.d.ts +245 -0
  88. package/src/esm/utils/dom/components/ColorGradientPicker.js +732 -0
  89. package/src/esm/utils/dom/components/ColorGradientPicker.js.map +1 -0
  90. package/src/esm/utils/dom/components/ColorGradientPickerJSX.d.ts +129 -0
  91. package/src/esm/utils/dom/components/ColorGradientPickerJSX.js +582 -0
  92. package/src/esm/utils/dom/components/ColorGradientPickerJSX.js.map +1 -0
  93. package/src/esm/utils/dom/components/ColorGradientSelector.d.ts +117 -0
  94. package/src/esm/utils/dom/components/ColorGradientSelector.js +332 -0
  95. package/src/esm/utils/dom/components/ColorGradientSelector.js.map +1 -0
  96. package/src/esm/utils/dom/components/Test.d.ts +7 -0
  97. package/src/esm/utils/dom/components/Test.js +26 -0
  98. package/src/esm/utils/dom/components/Test.js.map +1 -0
  99. package/src/esm/utils/dom/guiSizeToggler.js +1 -1
  100. package/src/esm/utils/dom/guiSizeToggler.js.map +1 -1
  101. package/src/esm/utils/helpers/BezierPathInteractionHelper.d.ts +9 -3
  102. package/src/esm/utils/helpers/BezierPathInteractionHelper.js +20 -13
  103. package/src/esm/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
  104. package/src/esm/utils/helpers/CircleHelper.d.ts +10 -1
  105. package/src/esm/utils/helpers/CircleHelper.js +13 -2
  106. package/src/esm/utils/helpers/CircleHelper.js.map +1 -1
  107. package/src/esm/utils/helpers/CircleSectorHelper.d.ts +2 -2
  108. package/src/esm/utils/helpers/CircleSectorHelper.js +1 -0
  109. package/src/esm/utils/helpers/CircleSectorHelper.js.map +1 -1
  110. package/src/esm/utils/helpers/TriangleHelper.d.ts +57 -0
  111. package/src/esm/utils/helpers/TriangleHelper.js +78 -0
  112. package/src/esm/utils/helpers/TriangleHelper.js.map +1 -0
  113. package/src/esm/utils/helpers/VEllipseHelper.d.ts +6 -4
  114. package/src/esm/utils/helpers/VEllipseHelper.js +4 -2
  115. package/src/esm/utils/helpers/VEllipseHelper.js.map +1 -1
  116. package/src/esm/utils/helpers/VEllipseSectorHelper.d.ts +3 -2
  117. package/src/esm/utils/helpers/VEllipseSectorHelper.js +1 -0
  118. package/src/esm/utils/helpers/VEllipseSectorHelper.js.map +1 -1
  119. package/src/esm/utils/parsers/LinearColorGradientParser.d.ts +78 -0
  120. package/src/esm/utils/parsers/LinearColorGradientParser.js +217 -0
  121. package/src/esm/utils/parsers/LinearColorGradientParser.js.map +1 -0
  122. package/src/external.d.ts +9 -0
  123. package/src/ts/PlotBoilerplate.ts +233 -105
  124. package/src/ts/Polygon.ts +68 -14
  125. package/src/ts/Triangle.ts +2 -2
  126. package/src/ts/Vertex.ts +21 -3
  127. package/src/ts/interfaces/core.ts +31 -0
  128. package/src/ts/utils/datastructures/Color.ts +423 -44
  129. package/src/ts/utils/datastructures/ColorGradient.ts +196 -0
  130. package/src/ts/utils/dom/components/ColorGradientPicker.tsx +877 -0
  131. package/src/ts/utils/dom/components/ColorGradientSelector.tsx +445 -0
  132. package/src/ts/utils/dom/components/Test.tsx +44 -0
  133. package/src/ts/utils/dom/components/_ColorGradientPicker_raw.ts_ +392 -0
  134. package/src/ts/utils/dom/guiSizeToggler.ts +1 -1
  135. package/src/ts/utils/helpers/BezierPathInteractionHelper.ts +25 -15
  136. package/src/ts/utils/helpers/CircleHelper.ts +17 -3
  137. package/src/ts/utils/helpers/CircleSectorHelper.ts +4 -2
  138. package/src/ts/utils/helpers/TriangleHelper.ts +102 -0
  139. package/src/ts/utils/helpers/VEllipseHelper.ts +6 -4
  140. package/src/ts/utils/helpers/VEllipseSectorHelper.ts +3 -2
  141. package/src/ts/utils/parsers/LinearColorGradientParser.ts +297 -0
@@ -0,0 +1,732 @@
1
+ /**
2
+ * Approach for a 'simple' color gradient picker for my PlotBoilerplate library.
3
+ *
4
+ * As it turned out a bit more complex in the end – due to the fact that color gradient
5
+ * picking is not a super simple task – so I used JSX here. Thus a JSX library is required
6
+ * to get this running: NoReact.
7
+ *
8
+ *
9
+ * @author Ikaros Kappler
10
+ * @date 2025-06-27
11
+ * @version 1.0.0
12
+ */
13
+ import * as NoReact from "noreact";
14
+ import { Color } from "../../datastructures/Color";
15
+ import { ColorGradient } from "../../datastructures/ColorGradient";
16
+ export class ColorGradientPicker {
17
+ /**
18
+ * The constructor: creates a new color gradient picker in the given container.
19
+ * If no container or ID is given then a new unbound `container` will be created (DIV).
20
+ *
21
+ * Pass a container ID or nothing – in the latter case the constructor will create
22
+ * a new DIV element.
23
+ *
24
+ * @param {string?} containerID - (optional) If you want to use an existing container (should be a DIV).
25
+ * @param {boolean?} isMobileMode - (optional) If `true` then the elements are rendered in double size.
26
+ */
27
+ constructor(containerID, isMobileMode) {
28
+ this._sliderElementRefs = [];
29
+ this.sliderMin = 0;
30
+ this.sliderMax = 100;
31
+ // Only used during initialization!
32
+ this.isMobileMode = false;
33
+ this.css_indicatorWidth_num = 1.0;
34
+ this.css_indicatorWidth = "1em";
35
+ this.css_indicatorHeight = "1em";
36
+ this.css_thumbWidth = "0.5em";
37
+ this.css_thumbHeight = "1.333em";
38
+ this.css_containerHeight = "20px";
39
+ this.installedChangeListeners = [];
40
+ this.__mouseDownPosition = null;
41
+ /**
42
+ * Creates a callback function for range slider.
43
+ *
44
+ * @returns
45
+ */
46
+ this.__createSliderChangeHandler = () => {
47
+ const _self = this;
48
+ return (e) => {
49
+ const targetSlider = e.target;
50
+ // console.log("Clicked", targetSlider ? targetSlider.getAttribute("id") : null);
51
+ if (!targetSlider) {
52
+ return false;
53
+ }
54
+ const currentSliderValue = Number.parseFloat(targetSlider.value);
55
+ const rangeSliderIndexRaw = targetSlider.dataset["rangeSliderIndex"];
56
+ // console.log("rangeSliderIndexRaw", rangeSliderIndexRaw);
57
+ if (!rangeSliderIndexRaw) {
58
+ return false;
59
+ }
60
+ const rangeSliderIndex = Number.parseInt(rangeSliderIndexRaw);
61
+ var leftSliderValue = this.__getSliderValue(rangeSliderIndex - 1, _self.sliderMin);
62
+ var rightSliderValue = this.__getSliderValue(rangeSliderIndex + 1, _self.sliderMax);
63
+ if (leftSliderValue >= currentSliderValue) {
64
+ targetSlider.value = `${leftSliderValue}`;
65
+ _self.__updateBackgroundGradient();
66
+ _self.__updateColorIndicator(rangeSliderIndex);
67
+ return false;
68
+ }
69
+ else if (rightSliderValue <= currentSliderValue) {
70
+ targetSlider.value = `${rightSliderValue}`;
71
+ _self.__updateBackgroundGradient();
72
+ _self.__updateColorIndicator(rangeSliderIndex);
73
+ return false;
74
+ }
75
+ else {
76
+ _self.__updateBackgroundGradient();
77
+ _self.__updateColorIndicator(rangeSliderIndex);
78
+ return true;
79
+ }
80
+ };
81
+ };
82
+ /**
83
+ * Adds custom styles (global STYLE tag).
84
+ *
85
+ * @private
86
+ */
87
+ this.__createCustomStylesElement = () => {
88
+ // Thanks to Ana Tudor
89
+ // https://css-tricks.com/multi-thumb-sliders-particular-two-thumb-case/
90
+ return (NoReact.createElement("style", null, `
91
+ #${this.elementID} {
92
+ margin-top: 1em;
93
+ }
94
+
95
+ #${this.elementID} input[type='range'] {
96
+
97
+ -webkit-appearance: none;
98
+
99
+ grid-column: 1;
100
+ grid-row: 2;
101
+
102
+ /* same as before */
103
+ background: none; /* get rid of white Chrome background */
104
+ color: #000;
105
+ font: inherit; /* fix too small font-size in both Chrome & Firefox */
106
+ margin: 0;
107
+ pointer-events: none; /* let clicks pass through */
108
+ }
109
+
110
+ #${this.elementID} input[type='range']::-webkit-slider-runnable-track {
111
+ -webkit-appearance: none;
112
+
113
+ background: none; /* get rid of Firefox track background */
114
+ height: 100%;
115
+ width: 100%;
116
+
117
+ pointer-events: none;
118
+ }
119
+
120
+ #${this.elementID} input[type='range']::-webkit-slider-thumb {
121
+ -webkit-appearance: none;
122
+ background: currentcolor;
123
+ border: none; /* get rid of Firefox thumb border */
124
+ border-radius: 6px; /* get rid of Firefox corner rounding */
125
+ pointer-events: auto; /* catch clicks */
126
+ width: ${this.css_thumbWidth};
127
+ height: ${this.css_thumbHeight};
128
+ }
129
+
130
+ #${this.elementID} input[type='range']:focus::-webkit-slider-thumb {
131
+ border: 2px solid white;
132
+ }
133
+
134
+ #${this.elementID} input[type='range']::-moz-range-track {
135
+ -webkit-appearance: none;
136
+ background: none; /* get rid of Firefox track background */
137
+ height: 100%;
138
+ width: 100%;
139
+ pointer-events: none;
140
+ }
141
+
142
+ #${this.elementID} input[type='range']::-moz-range-thumb {
143
+ /* -webkit-appearance: none; */
144
+ background: currentcolor;
145
+ border: none; /* get rid of Firefox thumb border */
146
+ border-radius: 6px; /* get rid of Firefox corner rounding */
147
+ pointer-events: auto; /* catch clicks */
148
+ width: ${this.css_thumbWidth};
149
+ height: ${this.css_thumbHeight};
150
+ }
151
+
152
+ #${this.elementID} input[type='range']:focus::-moz-range-thumb {
153
+ border: 2px solid white;
154
+ }
155
+
156
+ #${this.elementID} input[type='range'] {
157
+ /* same as before */
158
+ z-index: 1;
159
+ }
160
+
161
+ #${this.elementID} input[type='range']:focus {
162
+ z-index: 2;
163
+ /* outline: dotted 1px orange; */
164
+ color: darkorange;
165
+ }
166
+ `));
167
+ };
168
+ if (containerID) {
169
+ const cont = document.getElementById(containerID);
170
+ if (!cont) {
171
+ throw "Cannot create ColorGradientPicker. Component ID does not exist.";
172
+ }
173
+ this.container = cont;
174
+ }
175
+ else {
176
+ this.container = document.createElement("div");
177
+ }
178
+ this.baseID = Math.floor(Math.random() * 65535);
179
+ this.elementID = `color-gradient-picker-${this.baseID}`;
180
+ this.colorGradient = ColorGradient.createDefault();
181
+ this.isMobileMode = Boolean(isMobileMode);
182
+ if (isMobileMode) {
183
+ // Double size :)
184
+ this.css_indicatorWidth_num = 2.0;
185
+ this.css_indicatorWidth = "2em";
186
+ this.css_indicatorHeight = "2em";
187
+ this.css_thumbWidth = "1.0em";
188
+ this.css_thumbHeight = "2.666em";
189
+ this.css_containerHeight = "40px";
190
+ }
191
+ document.head.appendChild(this.__createCustomStylesElement());
192
+ this.container.append(this._render());
193
+ this.__initializeDataSets();
194
+ this.__updateColorIndicator(0);
195
+ this.__updateBackgroundGradient();
196
+ } // END constructor
197
+ setGradient(gradient, fireChangeEvent) {
198
+ // We are lazy: just throw away everything and rebuild all.
199
+ this.__removeAllChildNodes(this.container);
200
+ this.colorGradient = gradient;
201
+ this.container.append(this._render());
202
+ this.__initializeDataSets();
203
+ this.__updateColorIndicator(0);
204
+ this.__updateBackgroundGradient();
205
+ if (fireChangeEvent) {
206
+ this.__fireChangeEvent();
207
+ }
208
+ }
209
+ // +---------------------------------------------------------------------------------
210
+ // | A helper function to remove all child nodes.
211
+ // +-------------------------------
212
+ __removeAllChildNodes(node) {
213
+ while (node.lastChild) {
214
+ node.removeChild(node.lastChild);
215
+ }
216
+ }
217
+ // +---------------------------------------------------------------------------------
218
+ // | Used during initialization: define dataset values for each slider.
219
+ // +-------------------------------
220
+ __initializeDataSets() {
221
+ for (var i = 0; i < this._sliderElementRefs.length; i++) {
222
+ const sliderColor = this.colorGradient.values[i].color;
223
+ // console.log("Setting up slider color data set", i, sliderColor, sliderColor.cssRGB(), sliderColor.cssHEX());
224
+ this._sliderElementRefs[i].current.dataset.colorValue = sliderColor.cssRGB();
225
+ this._sliderElementRefs[i].current.dataset.colorValueHEX = sliderColor.cssHEX();
226
+ }
227
+ }
228
+ /**
229
+ * Adds a new color gradient change listener to this ColorGradientPicker.
230
+ *
231
+ * @param {ColorGradientChangeListener} listener - The listener to add.
232
+ * @returns {boolean} True, if the listener was added and did not exist before.
233
+ */
234
+ addChangeListener(listener) {
235
+ for (var i = 0; i < this.installedChangeListeners.length; i++) {
236
+ if (this.installedChangeListeners[i] === listener) {
237
+ return false;
238
+ }
239
+ }
240
+ this.installedChangeListeners.push(listener);
241
+ return true;
242
+ }
243
+ /**
244
+ *
245
+ * @param {ColorGradientChangeListener} listener The listener to remove.
246
+ * @returns {boolean} True, if the listener existed and has been removed.
247
+ */
248
+ removeChangeListener(listener) {
249
+ for (var i = 0; i < this.installedChangeListeners.length; i++) {
250
+ if (this.installedChangeListeners[i] === listener) {
251
+ this.installedChangeListeners.splice(i, 1);
252
+ return true;
253
+ }
254
+ }
255
+ return false;
256
+ }
257
+ /**
258
+ * Fires a change event to all installed listeners.
259
+ */
260
+ __fireChangeEvent() {
261
+ const newColorGradient = this.getColorGradient();
262
+ for (var i = 0; i < this.installedChangeListeners.length; i++) {
263
+ this.installedChangeListeners[i](newColorGradient, this);
264
+ }
265
+ }
266
+ /**
267
+ * Creates a new color range input element.
268
+ *
269
+ * @private
270
+ * @param {number} baseID
271
+ * @param index
272
+ * @param sliderMin
273
+ * @param sliderMax
274
+ * @param initialValue
275
+ * @param initialColor
276
+ * @returns
277
+ */
278
+ __createColorRangeInput(index, sliderMin, sliderMax, initialValue, initialColor) {
279
+ const sliderHandler = this.__createSliderChangeHandler();
280
+ const ref = NoReact.useRef();
281
+ // this._sliderElementRefs.push(ref);
282
+ this._sliderElementRefs.splice(index, 0, ref);
283
+ // console.log("new _sliderElementRefs", this._sliderElementRefs);
284
+ // Update all elements to the right of the new elelemt
285
+ this.__updateSliderDataSetIndices(index + 1);
286
+ return (NoReact.createElement("input", { id: `rage-slider-${this.baseID}-${index}`, type: "range", min: sliderMin, max: sliderMax, value: initialValue, style: { pos: "absolute", l: "0px", t: "0px", w: "100%", h: "60%" }, "data-range-slider-index": `${index}`, "data-colorValue": initialColor.cssRGB(), "data-colorValueHEX": initialColor.cssHEX(), onChange: sliderHandler, onClick: sliderHandler, ref: ref }));
287
+ }
288
+ /**
289
+ * Get the absolute length of this range, in slider units.
290
+ * @returns
291
+ */
292
+ getRangeLength() {
293
+ return this.sliderMax - this.sliderMin;
294
+ }
295
+ /**
296
+ * Handles color value changes.
297
+ *
298
+ * @returns
299
+ */
300
+ __colorChangeHandler() {
301
+ const _self = this;
302
+ return (_evt) => {
303
+ // console.log("__colorChangeHandler");
304
+ const activeSliderIndex_raw = this.colorInputRef.current.dataset.activeSliderIndex;
305
+ if (!activeSliderIndex_raw) {
306
+ console.warn("Cannot update color indicator; no active range slider set. This is likely a program error and should not happen.");
307
+ return false;
308
+ }
309
+ const activeSliderIndex = Number.parseInt(activeSliderIndex_raw);
310
+ if (Number.isNaN(activeSliderIndex) || activeSliderIndex < 0 || activeSliderIndex >= _self._sliderElementRefs.length) {
311
+ console.warn("Cannot update color indicator; active index invalid or out of bounds. This is likely a program error and should not happen.", activeSliderIndex);
312
+ return false;
313
+ }
314
+ const newColor = _self.colorInputRef.current.value;
315
+ const rangeSlider = _self._sliderElementRefs[activeSliderIndex].current;
316
+ rangeSlider.dataset.colorValue = newColor;
317
+ // rangeSlider.dataset["colorValue"] = newColor;
318
+ this.colorIndicatorColorButtonRef.current.style["background-color"] = newColor;
319
+ _self.__updateBackgroundGradient();
320
+ _self.__fireChangeEvent();
321
+ return true;
322
+ };
323
+ }
324
+ /**
325
+ * Removed the current color slider from the DOM and highlights the left neighbour.
326
+ *
327
+ * @returns {boolean} True if the element could be successfully removed.
328
+ */
329
+ __handleRemoveColor() {
330
+ // const colorInput : HTMLInputElement | null = evt.target;
331
+ // console.log("__colorChangeHandler");
332
+ const activeSliderIndex_raw = this.colorInputRef.current.dataset.activeSliderIndex;
333
+ if (!activeSliderIndex_raw) {
334
+ console.warn("Cannot remove color indicator; no active range slider set.");
335
+ return false;
336
+ }
337
+ const activeSliderIndex = Number.parseInt(activeSliderIndex_raw);
338
+ if (Number.isNaN(activeSliderIndex) || activeSliderIndex < 0 || activeSliderIndex >= this._sliderElementRefs.length) {
339
+ console.warn("Cannot remove color indicator; active index invalid or out of bounds.", activeSliderIndex);
340
+ return false;
341
+ }
342
+ if (activeSliderIndex === 0 || activeSliderIndex + 1 === this.getRangeLength()) {
343
+ console.warn("Cannot remove color indicator; leftmost and rightmost slider must not be removed.", activeSliderIndex);
344
+ return false;
345
+ }
346
+ const sliderElementRef = this._sliderElementRefs[activeSliderIndex];
347
+ sliderElementRef.current.remove();
348
+ this._sliderElementRefs.splice(activeSliderIndex, 1);
349
+ // Update all elements to the right of the removed elelemt
350
+ this.__updateSliderDataSetIndices(activeSliderIndex);
351
+ // Set left element active
352
+ this.__updateColorIndicator(activeSliderIndex - 1);
353
+ this.__updateBackgroundGradient();
354
+ this.__fireChangeEvent();
355
+ return true;
356
+ }
357
+ /**
358
+ * Once a slider element was added or removed then the following indices must be updated.
359
+ *
360
+ * @param {number} startIndex - The slider index to start updating at.
361
+ */
362
+ __updateSliderDataSetIndices(startIndex) {
363
+ // Update all elements to the right of the new elelemt
364
+ for (var i = startIndex; i < this._sliderElementRefs.length; i++) {
365
+ this._sliderElementRefs[i].current.setAttribute("data-range-slider-index", `${i}`);
366
+ this._sliderElementRefs[i].current.setAttribute("id", `rage-slider-${this.baseID}-${i}`);
367
+ }
368
+ }
369
+ /**
370
+ * To avoid too many sliders added on each click, check if the mouse was moved in the meantime (detect drag event).
371
+ *
372
+ * @returns
373
+ */
374
+ __containerMouseDownHandler() {
375
+ const _self = this;
376
+ return (event) => {
377
+ _self.__mouseDownPosition = { x: event.clientX, y: event.clientY };
378
+ // console.log("__containerMouseDownHandler _self.__mouseDownPosition", _self.__mouseDownPosition);
379
+ };
380
+ }
381
+ /**
382
+ * Creates a handler for click events on the container. If the click event is far
383
+ * enough from existing sliders, then it can be added.
384
+ *
385
+ * @returns
386
+ */
387
+ __containerClickHandler() {
388
+ const _self = this;
389
+ const maxDifference = _self.getRangeLength() * 0.03;
390
+ return (evt) => {
391
+ // console.log("Container click handler");
392
+ const relativeValue = _self.__clickEventToRelativeValue(evt);
393
+ if (relativeValue < 0 || relativeValue > 1.0) {
394
+ // Clicked somewhere outside the range
395
+ return;
396
+ }
397
+ // Check if element was moved in the meantime
398
+ if (_self.__mouseDownPosition && Math.abs(_self.__mouseDownPosition.x - evt.clientX) >= 10.0) {
399
+ // Ignore event if mouse was moved more than 10 pixels between down- and up-event
400
+ // console.log("Mouse was moved more than 10 px. Cancelling.");
401
+ _self.__mouseDownPosition = null;
402
+ return;
403
+ }
404
+ const absoluteValue = this.__relativeToAbsolute(relativeValue);
405
+ // console.log("click", "relativeValue", relativeValue, "absoluteValue", absoluteValue);
406
+ // Check if click position (ratio) is far enough away from any slider
407
+ const [leftSliderIndex, closestSliderValue] = _self.__locateClosestSliderValue(absoluteValue);
408
+ const diff = Math.abs(closestSliderValue - absoluteValue);
409
+ if (diff >= maxDifference) {
410
+ // console.log("Add slider here");
411
+ _self.__addSliderAt(absoluteValue, leftSliderIndex);
412
+ }
413
+ else {
414
+ // console.debug("Don't add slider here.");
415
+ }
416
+ // Finally clear mousedown-position
417
+ _self.__mouseDownPosition = null;
418
+ };
419
+ }
420
+ /**
421
+ * Find that slider (index) that's value is closest to the given absolute value. The function will return
422
+ * the closest value and the left index, indicating the containing interval index.
423
+ *
424
+ * @param {number} absoluteValue - The value to look for.
425
+ * @returns {[number,number]} A pair of left slider index and closest value.
426
+ */
427
+ __locateClosestSliderValue(absoluteValue) {
428
+ // As the colorGradient always reflects the slider values we can just ust the color gradient itself to search.
429
+ const relativeValue = this.__absoluteToRelative(absoluteValue);
430
+ const colorGradientEntry = this.colorGradient.locateClosestRatio(relativeValue);
431
+ return [colorGradientEntry[0], this.__relativeToAbsolute(colorGradientEntry[1].ratio)];
432
+ }
433
+ /**
434
+ * Convert the click event to the relative x value in [0..1].
435
+ *
436
+ * @param {MouseEvent} evt - The mouse event.
437
+ * @returns {number} The relative x value.
438
+ */
439
+ __clickEventToRelativeValue(evt) {
440
+ const target = evt.target;
441
+ const rect = target.getBoundingClientRect();
442
+ const x = evt.clientX - rect.left; //x position within the element.
443
+ const width = rect.width; // target.clientWidth;
444
+ const relativeValue = x / width;
445
+ // console.log("width", width, "x", x, "relativeValue", relativeValue);
446
+ return relativeValue;
447
+ }
448
+ /**
449
+ * Adds a slider at the given interval index.
450
+ *
451
+ * @param {number} absoluteValue - The absolute new slider value.
452
+ * @param {number} leftSliderIndex - The new slider's position (interval index).
453
+ */
454
+ __addSliderAt(absoluteValue, leftSliderIndex) {
455
+ // console.log("__addSliderAt", "absoluteValue", absoluteValue, "leftSliderIndex", leftSliderIndex);
456
+ const leftSlider = this._sliderElementRefs[leftSliderIndex].current;
457
+ // const colorAtPosition = this.__getSliderColorAt(relativeValue);
458
+ const newColor = this.__getSliderColorAt(absoluteValue);
459
+ const newSliderIndex = leftSliderIndex + 1;
460
+ const _newSlider = this.__createColorRangeInput(newSliderIndex, this.sliderMin, this.sliderMax, absoluteValue, newColor // initialColor: string
461
+ );
462
+ const sliderRef = this._sliderElementRefs[newSliderIndex];
463
+ leftSlider.after(sliderRef.current);
464
+ sliderRef.current.dataset.colorValue = newColor.cssRGB();
465
+ sliderRef.current.dataset.colorValueHEX = newColor.cssRGB();
466
+ // No visible change, but let's reflect this in the output gradient anyway
467
+ this.__updateBackgroundGradient();
468
+ // Highlight the newly added range slider
469
+ this.__updateColorIndicator(newSliderIndex);
470
+ this.__fireChangeEvent();
471
+ }
472
+ /**
473
+ * Get the slider color at the given absolute value. That value must be inside the [MIN_VALUE, MAX_VALUE] interval.
474
+ * @param absoluteValue
475
+ * @returns
476
+ */
477
+ __getSliderColorAt(absoluteValue) {
478
+ // console.log("__getSliderColorAt", "absoluteValue", absoluteValue);
479
+ // Locate interval
480
+ const leftIndex = this.__locateIntervalAt(absoluteValue);
481
+ const leftSliderValue = this.__getSliderValue(leftIndex, NaN);
482
+ const rightSliderValue = this.__getSliderValue(leftIndex + 1, NaN);
483
+ if (Number.isNaN(leftSliderValue) || Number.isNaN(rightSliderValue)) {
484
+ console.warn(`[Warn] Failed to determine left/right slider values at indices ${leftIndex} or ${rightSliderValue}. Cannot proceed for absolute value ${absoluteValue}.`);
485
+ return null;
486
+ }
487
+ const leftColorString = this.__getSliderColorString(leftIndex, null);
488
+ const rightColorString = this.__getSliderColorString(leftIndex + 1, null);
489
+ if (!leftColorString || !rightColorString) {
490
+ console.warn(`[Warn] Failed to determine left/right color string values at indices ${leftIndex} or ${rightSliderValue}. Cannot proceed for absolute value ${absoluteValue}.`);
491
+ return null;
492
+ }
493
+ const positionInsideInterval = (absoluteValue - leftSliderValue) / (rightSliderValue - leftSliderValue);
494
+ // console.log("leftColorString", leftColorString, "rightColorString", rightColorString);
495
+ const leftColorObject = Color.parse(leftColorString);
496
+ const rightColorObject = Color.parse(rightColorString);
497
+ const newColor = leftColorObject.interpolate(rightColorObject, positionInsideInterval);
498
+ return newColor;
499
+ }
500
+ /**
501
+ * Locate the slider interval that contains the given (absolute) slider value. The given value must be
502
+ * somewhere between MIN_VALUE and MAX_VALUE (usually between 0 and 100 in the default configuraion.).
503
+ *
504
+ * @param {number} absoluteValue - The absolute value to search for.
505
+ * @returns {number} The left slider inder the containing interval is starting with – or -1 if out of bounds.
506
+ */
507
+ __locateIntervalAt(absoluteValue) {
508
+ for (var i = 0; i + 1 < this._sliderElementRefs.length; i++) {
509
+ if (this.__getSliderValue(i, this.sliderMin) <= absoluteValue &&
510
+ absoluteValue < this.__getSliderValue(i + 1, this.sliderMax)) {
511
+ return i;
512
+ }
513
+ }
514
+ return -1;
515
+ }
516
+ /**
517
+ * Converts a relative value in [0..1] to [min..max].
518
+ * @param relativeValue
519
+ */
520
+ __relativeToAbsolute(relativeValue) {
521
+ return this.sliderMin + (this.sliderMax - this.sliderMin) * relativeValue;
522
+ }
523
+ /**
524
+ * Converts a relative value in [0..1] to [min..max].
525
+ * @param relativeValue
526
+ */
527
+ __absoluteToRelative(absolute) {
528
+ return (absolute - this.sliderMin) / (this.sliderMax - this.sliderMin);
529
+ }
530
+ /**
531
+ * Get the value of the n-th rangel slider.
532
+ *
533
+ * @param {number} sliderIndex
534
+ * @param {number} fallback
535
+ * @returns
536
+ */
537
+ __getSliderValue(sliderIndex, fallback) {
538
+ if (sliderIndex < 0 || sliderIndex >= this._sliderElementRefs.length) {
539
+ return fallback;
540
+ }
541
+ return Number.parseFloat(this._sliderElementRefs[sliderIndex].current.value);
542
+ }
543
+ /**
544
+ * Get all current slider values as an array.
545
+ * @returns
546
+ */
547
+ __getAllSliderValues() {
548
+ return this._sliderElementRefs.map((ref, index) => this.__getSliderValue(index, NaN));
549
+ }
550
+ /**
551
+ * Updates the container's background to display the configured color gradient.
552
+ *
553
+ * @private
554
+ */
555
+ __updateBackgroundGradient() {
556
+ // const colorGradient = this.getColorGradient();
557
+ this.colorGradient = new ColorGradient(this.__getAllSliderValues().map((sliderValue, sliderIndex) => ({
558
+ color: this.__getSliderColor(sliderIndex, null),
559
+ ratio: this.__absoluteToRelative(sliderValue)
560
+ })), this.colorGradient.angle);
561
+ this.containerRef.current.style["background"] = this.colorGradient.toColorGradientString();
562
+ }
563
+ /**
564
+ * After changes this function updates the color indicator button.
565
+ *
566
+ * @param {number} rangeSliderIndex - The new active slider index.
567
+ */
568
+ __updateColorIndicator(rangeSliderIndex) {
569
+ const colorValue = this.__getSliderColorHEXString(rangeSliderIndex, "#808080");
570
+ const ratio = this.__getSliderPercentage(rangeSliderIndex);
571
+ // Set left offset of color input container
572
+ this.colorInputContainerRef.current.style["left"] = `calc( ${ratio * 100}% + ${(1.0 - ratio) * this.css_indicatorWidth_num * 0.5}em - ${ratio * this.css_indicatorWidth_num * 0.5}em)`;
573
+ // Also set offset of colot input element (safari will open the native color dialog at this position)
574
+ this.colorInputRef.current.style["left"] = `calc( ${ratio * 100}% + ${(1.0 - ratio) * this.css_indicatorWidth_num * 0.5}em - ${ratio * this.css_indicatorWidth_num * 0.5}em)`;
575
+ this.colorIndicatorColorButtonRef.current.style["background-color"] = colorValue;
576
+ this.colorInputRef.current.value = colorValue;
577
+ // console.log("Setting new color value", this.colorInputRef.current.value, colorValue);
578
+ this.colorInputRef.current.dataset.activeSliderIndex = `${rangeSliderIndex}`;
579
+ if (rangeSliderIndex <= 0 || rangeSliderIndex + 1 >= this._sliderElementRefs.length) {
580
+ this.colorIndicatorRemoveButtonRef.current.style["visibility"] = "hidden";
581
+ }
582
+ else {
583
+ this.colorIndicatorRemoveButtonRef.current.style["visibility"] = "visible";
584
+ }
585
+ }
586
+ /**
587
+ * Get a color gradient CSS value string from the current editor settings.
588
+ *
589
+ * @instance
590
+ * @memberof ColorGradientPicker
591
+ * @returns {ColorGradient}
592
+ */
593
+ getColorGradient() {
594
+ return this.colorGradient;
595
+ }
596
+ /**
597
+ * Get the configured color value of the n-th rangel slider.
598
+ *
599
+ * @param {number} sliderIndex
600
+ * @param {string} fallback
601
+ * @returns
602
+ */
603
+ __getSliderColorString(sliderIndex, fallback) {
604
+ if (sliderIndex < 0 || sliderIndex >= this._sliderElementRefs.length) {
605
+ return fallback;
606
+ }
607
+ const colorValue = this._sliderElementRefs[sliderIndex].current.dataset.colorValue;
608
+ return colorValue ? colorValue : fallback;
609
+ }
610
+ /**
611
+ * Get the configured color value of the n-th rangel slider.
612
+ *
613
+ * @param {number} sliderIndex
614
+ * @param {string} fallback
615
+ * @returns
616
+ */
617
+ __getSliderColorHEXString(sliderIndex, fallback) {
618
+ if (sliderIndex < 0 || sliderIndex >= this._sliderElementRefs.length) {
619
+ return fallback;
620
+ }
621
+ const colorValue = this._sliderElementRefs[sliderIndex].current.dataset.colorValueHEX;
622
+ return colorValue ? colorValue : fallback;
623
+ }
624
+ /**
625
+ * Get get color of this gradient picker at the given slider index.
626
+ *
627
+ * @param {number} sliderIndex - The index to get the slider color from. Something between 0 and _sliderElementRefs.length.
628
+ * @param {Color} fallback
629
+ * @returns
630
+ */
631
+ __getSliderColor(sliderIndex, fallback) {
632
+ if (sliderIndex < 0 || sliderIndex >= this._sliderElementRefs.length) {
633
+ return fallback;
634
+ }
635
+ const colorValueString = this._sliderElementRefs[sliderIndex].current.dataset["colorValue"];
636
+ const colorValue = Color.parse(colorValueString);
637
+ return colorValue ? colorValue : fallback;
638
+ }
639
+ /**
640
+ * Get the slider's value in a mapped range of 0.0 ... 1.0.
641
+ *
642
+ * @param sliderIndex
643
+ * @returns
644
+ */
645
+ __getSliderPercentage(sliderIndex) {
646
+ const sliderValue = this.__getSliderValue(sliderIndex, 0.0);
647
+ const percentage = (this.sliderMin + sliderValue) / (this.sliderMax - this.sliderMin);
648
+ return percentage;
649
+ }
650
+ /**
651
+ * Init the container contents.
652
+ *
653
+ * @private
654
+ */
655
+ _render() {
656
+ const _self = this;
657
+ // console.log("Rendering ...", NoReact);
658
+ this._sliderElementRefs = [];
659
+ this.colorIndicatorColorButtonRef = NoReact.useRef();
660
+ this.colorIndicatorRemoveButtonRef = NoReact.useRef();
661
+ this.colorInputRef = NoReact.useRef();
662
+ this.colorInputContainerRef = NoReact.useRef();
663
+ this.containerRef = NoReact.useRef();
664
+ /**
665
+ * This helper method is called when the tiny color button below the active slider is clicked. The method
666
+ * triggers the colorInput dialog to be triggered.
667
+ *
668
+ * @param {Event} evt
669
+ */
670
+ const handleIndicatorButtonClick = (evt) => {
671
+ evt.preventDefault(); // Prevent other inputs to react to this event.
672
+ evt.stopPropagation();
673
+ // But fake a click on the input element
674
+ // console.log("Current selected color: " + _self.colorInputRef.current.value);
675
+ _self.colorInputRef.current.click();
676
+ };
677
+ /**
678
+ * This helper method is called when the remove-color button is clicked. It removes the
679
+ * currently active color slider – if possible (leftmost and rightmost slider cannot be removed).
680
+ * @param evt
681
+ */
682
+ const handleRemoveColorButtonClick = (evt) => {
683
+ evt.preventDefault(); // Prevent other inputs to react to this event.
684
+ evt.stopPropagation();
685
+ _self.__handleRemoveColor();
686
+ };
687
+ return (NoReact.createElement("div", { id: this.elementID, style: {
688
+ d: "flex",
689
+ fd: "column",
690
+ w: "100%",
691
+ h: this.css_containerHeight,
692
+ pos: "relative",
693
+ mb: this.isMobileMode ? "5em" : "3em"
694
+ }, ref: this.containerRef, onMouseDown: this.__containerMouseDownHandler(), onClick: this.__containerClickHandler() },
695
+ this.colorGradient.values.map((colorGradientItem, index) => {
696
+ const initialValue = _self.__relativeToAbsolute(colorGradientItem.ratio);
697
+ return this.__createColorRangeInput(index, this.sliderMin, this.sliderMax, initialValue, colorGradientItem.color);
698
+ }),
699
+ NoReact.createElement("div", { style: { w: "100%" } },
700
+ NoReact.createElement("input", { id: `color-indicator-input-${this.baseID}`, type: "color", style: { v: "hidden", pos: "absolute", l: 0 },
701
+ // data-active-slider-index=""
702
+ ref: this.colorInputRef, onInput: this.__colorChangeHandler() }),
703
+ NoReact.createElement("div", { ref: this.colorInputContainerRef, style: {
704
+ pos: "absolute",
705
+ b: "0px",
706
+ l: "0%",
707
+ d: "flex",
708
+ fd: "column",
709
+ transform: "translate(0%, 100%)"
710
+ } },
711
+ NoReact.createElement("button", { id: `color-indicator-button-${this.baseID}`, style: {
712
+ backgroundColor: "grey",
713
+ borderRadius: "10%",
714
+ border: "1px solid grey",
715
+ w: this.css_indicatorWidth, // "1em",
716
+ h: this.css_indicatorHeight, // "1em",
717
+ transform: "translate(-50%, 0%)"
718
+ }, onClick: handleIndicatorButtonClick, ref: this.colorIndicatorColorButtonRef }),
719
+ NoReact.createElement("button", { id: `color-remove-button-${this.baseID}`, className: "color-remove-button", style: {
720
+ backgroundColor: "grey",
721
+ borderRadius: "10%",
722
+ border: "1px solid grey",
723
+ w: this.css_indicatorWidth, // "1em",
724
+ h: this.css_indicatorHeight, // "1em",
725
+ transform: "translate(-50%, 50%)",
726
+ lineHeight: "0.5em",
727
+ p: 0
728
+ }, onClick: handleRemoveColorButtonClick, ref: this.colorIndicatorRemoveButtonRef },
729
+ NoReact.createElement("span", { style: { fontSize: this.isMobileMode ? "1.0em" : "0.5em" } }, "\uD83D\uDDD1"))))));
730
+ } // END function render()
731
+ }
732
+ //# sourceMappingURL=ColorGradientPicker.js.map