1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.updateColor = exports.updateColorValue = exports.getHslAnimationFromHsl = exports.getHslFromAnimation = exports.getLinkRandomColor = exports.getLinkColor = exports.colorMix = exports.getStyleFromHsl = exports.getStyleFromRgb = exports.getRandomRgbColor = exports.hslaToRgba = exports.hslToRgb = exports.stringToRgb = exports.stringToAlpha = exports.rgbToHsl = exports.rangeColorToHsl = exports.colorToHsl = exports.colorToRgb = exports.rangeColorToRgb = exports.addColorManager = void 0;
|
4 | const NumberUtils_js_1 = require("./NumberUtils.js");
|
5 | const TypeUtils_js_1 = require("./TypeUtils.js");
|
6 | const Constants_js_1 = require("../Core/Utils/Constants.js");
|
7 | const AnimationStatus_js_1 = require("../Enums/AnimationStatus.js");
|
8 | const Utils_js_1 = require("./Utils.js");
|
9 | var RgbIndexes;
|
10 | (function (RgbIndexes) {
|
11 | RgbIndexes[RgbIndexes["r"] = 1] = "r";
|
12 | RgbIndexes[RgbIndexes["g"] = 2] = "g";
|
13 | RgbIndexes[RgbIndexes["b"] = 3] = "b";
|
14 | RgbIndexes[RgbIndexes["a"] = 4] = "a";
|
15 | })(RgbIndexes || (RgbIndexes = {}));
|
16 | const randomColorValue = "random", midColorValue = "mid", colorManagers = new Map();
|
17 | function addColorManager(manager) {
|
18 | colorManagers.set(manager.key, manager);
|
19 | }
|
20 | exports.addColorManager = addColorManager;
|
21 | function stringToRgba(input) {
|
22 | for (const [, manager] of colorManagers) {
|
23 | if (input.startsWith(manager.stringPrefix)) {
|
24 | return manager.parseString(input);
|
25 | }
|
26 | }
|
27 | const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])([a-f\d])?$/i, hexFixed = input.replace(shorthandRegex, (_, r, g, b, a) => {
|
28 | return r + r + g + g + b + b + (a !== undefined ? a + a : "");
|
29 | }), regex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i, result = regex.exec(hexFixed), radix = 16, defaultAlpha = 1, alphaFactor = 0xff;
|
30 | return result
|
31 | ? {
|
32 | a: result[RgbIndexes.a] !== undefined
|
33 | ? parseInt(result[RgbIndexes.a], radix) / alphaFactor
|
34 | : defaultAlpha,
|
35 | b: parseInt(result[RgbIndexes.b], radix),
|
36 | g: parseInt(result[RgbIndexes.g], radix),
|
37 | r: parseInt(result[RgbIndexes.r], radix),
|
38 | }
|
39 | : undefined;
|
40 | }
|
41 | function rangeColorToRgb(input, index, useIndex = true) {
|
42 | if (!input) {
|
43 | return;
|
44 | }
|
45 | const color = (0, TypeUtils_js_1.isString)(input) ? { value: input } : input;
|
46 | if ((0, TypeUtils_js_1.isString)(color.value)) {
|
47 | return colorToRgb(color.value, index, useIndex);
|
48 | }
|
49 | if ((0, TypeUtils_js_1.isArray)(color.value)) {
|
50 | return rangeColorToRgb({
|
51 | value: (0, Utils_js_1.itemFromArray)(color.value, index, useIndex),
|
52 | });
|
53 | }
|
54 | for (const [, manager] of colorManagers) {
|
55 | const res = manager.handleRangeColor(color);
|
56 | if (res) {
|
57 | return res;
|
58 | }
|
59 | }
|
60 | }
|
61 | exports.rangeColorToRgb = rangeColorToRgb;
|
62 | function colorToRgb(input, index, useIndex = true) {
|
63 | if (!input) {
|
64 | return;
|
65 | }
|
66 | const color = (0, TypeUtils_js_1.isString)(input) ? { value: input } : input;
|
67 | if ((0, TypeUtils_js_1.isString)(color.value)) {
|
68 | return color.value === randomColorValue ? getRandomRgbColor() : stringToRgb(color.value);
|
69 | }
|
70 | if ((0, TypeUtils_js_1.isArray)(color.value)) {
|
71 | return colorToRgb({
|
72 | value: (0, Utils_js_1.itemFromArray)(color.value, index, useIndex),
|
73 | });
|
74 | }
|
75 | for (const [, manager] of colorManagers) {
|
76 | const res = manager.handleColor(color);
|
77 | if (res) {
|
78 | return res;
|
79 | }
|
80 | }
|
81 | }
|
82 | exports.colorToRgb = colorToRgb;
|
83 | function colorToHsl(color, index, useIndex = true) {
|
84 | const rgb = colorToRgb(color, index, useIndex);
|
85 | return rgb ? rgbToHsl(rgb) : undefined;
|
86 | }
|
87 | exports.colorToHsl = colorToHsl;
|
88 | function rangeColorToHsl(color, index, useIndex = true) {
|
89 | const rgb = rangeColorToRgb(color, index, useIndex);
|
90 | return rgb ? rgbToHsl(rgb) : undefined;
|
91 | }
|
92 | exports.rangeColorToHsl = rangeColorToHsl;
|
93 | function rgbToHsl(color) {
|
94 | const rgbMax = 255, hMax = 360, sMax = 100, lMax = 100, hMin = 0, sMin = 0, hPhase = 60, half = 0.5, double = 2, r1 = color.r / rgbMax, g1 = color.g / rgbMax, b1 = color.b / rgbMax, max = Math.max(r1, g1, b1), min = Math.min(r1, g1, b1), res = {
|
95 | h: hMin,
|
96 | l: (max + min) * half,
|
97 | s: sMin,
|
98 | };
|
99 | if (max !== min) {
|
100 | res.s = res.l < half ? (max - min) / (max + min) : (max - min) / (double - max - min);
|
101 | res.h =
|
102 | r1 === max
|
103 | ? (g1 - b1) / (max - min)
|
104 | : (res.h = g1 === max ? double + (b1 - r1) / (max - min) : double * double + (r1 - g1) / (max - min));
|
105 | }
|
106 | res.l *= lMax;
|
107 | res.s *= sMax;
|
108 | res.h *= hPhase;
|
109 | if (res.h < hMin) {
|
110 | res.h += hMax;
|
111 | }
|
112 | if (res.h >= hMax) {
|
113 | res.h -= hMax;
|
114 | }
|
115 | return res;
|
116 | }
|
117 | exports.rgbToHsl = rgbToHsl;
|
118 | function stringToAlpha(input) {
|
119 | return stringToRgba(input)?.a;
|
120 | }
|
121 | exports.stringToAlpha = stringToAlpha;
|
122 | function stringToRgb(input) {
|
123 | return stringToRgba(input);
|
124 | }
|
125 | exports.stringToRgb = stringToRgb;
|
126 | function hslToRgb(hsl) {
|
127 | const hMax = 360, sMax = 100, lMax = 100, sMin = 0, lMin = 0, h = ((hsl.h % hMax) + hMax) % hMax, s = Math.max(sMin, Math.min(sMax, hsl.s)), l = Math.max(lMin, Math.min(lMax, hsl.l)), hNormalized = h / hMax, sNormalized = s / sMax, lNormalized = l / lMax, rgbFactor = 255, triple = 3;
|
128 | if (s === sMin) {
|
129 | const grayscaleValue = Math.round(lNormalized * rgbFactor);
|
130 | return { r: grayscaleValue, g: grayscaleValue, b: grayscaleValue };
|
131 | }
|
132 | const half = 0.5, double = 2, channel = (temp1, temp2, temp3) => {
|
133 | const temp3Min = 0, temp3Max = 1, sextuple = 6;
|
134 | if (temp3 < temp3Min) {
|
135 | temp3++;
|
136 | }
|
137 | if (temp3 > temp3Max) {
|
138 | temp3--;
|
139 | }
|
140 | if (temp3 * sextuple < temp3Max) {
|
141 | return temp1 + (temp2 - temp1) * sextuple * temp3;
|
142 | }
|
143 | if (temp3 * double < temp3Max) {
|
144 | return temp2;
|
145 | }
|
146 | if (temp3 * triple < temp3Max * double) {
|
147 | const temp3Offset = double / triple;
|
148 | return temp1 + (temp2 - temp1) * (temp3Offset - temp3) * sextuple;
|
149 | }
|
150 | return temp1;
|
151 | }, sNormalizedOffset = 1, temp1 = lNormalized < half
|
152 | ? lNormalized * (sNormalizedOffset + sNormalized)
|
153 | : lNormalized + sNormalized - lNormalized * sNormalized, temp2 = double * lNormalized - temp1, phaseNumerator = 1, phaseThird = phaseNumerator / triple, red = Math.min(rgbFactor, rgbFactor * channel(temp2, temp1, hNormalized + phaseThird)), green = Math.min(rgbFactor, rgbFactor * channel(temp2, temp1, hNormalized)), blue = Math.min(rgbFactor, rgbFactor * channel(temp2, temp1, hNormalized - phaseThird));
|
154 | return { r: Math.round(red), g: Math.round(green), b: Math.round(blue) };
|
155 | }
|
156 | exports.hslToRgb = hslToRgb;
|
157 | function hslaToRgba(hsla) {
|
158 | const rgbResult = hslToRgb(hsla);
|
159 | return {
|
160 | a: hsla.a,
|
161 | b: rgbResult.b,
|
162 | g: rgbResult.g,
|
163 | r: rgbResult.r,
|
164 | };
|
165 | }
|
166 | exports.hslaToRgba = hslaToRgba;
|
167 | function getRandomRgbColor(min) {
|
168 | const defaultMin = 0, fixedMin = min ?? defaultMin, rgbMax = 256;
|
169 | return {
|
170 | b: Math.floor((0, NumberUtils_js_1.randomInRange)((0, NumberUtils_js_1.setRangeValue)(fixedMin, rgbMax))),
|
171 | g: Math.floor((0, NumberUtils_js_1.randomInRange)((0, NumberUtils_js_1.setRangeValue)(fixedMin, rgbMax))),
|
172 | r: Math.floor((0, NumberUtils_js_1.randomInRange)((0, NumberUtils_js_1.setRangeValue)(fixedMin, rgbMax))),
|
173 | };
|
174 | }
|
175 | exports.getRandomRgbColor = getRandomRgbColor;
|
176 | function getStyleFromRgb(color, opacity) {
|
177 | const defaultOpacity = 1;
|
178 | return `rgba(${color.r}, ${color.g}, ${color.b}, ${opacity ?? defaultOpacity})`;
|
179 | }
|
180 | exports.getStyleFromRgb = getStyleFromRgb;
|
181 | function getStyleFromHsl(color, opacity) {
|
182 | const defaultOpacity = 1;
|
183 | return `hsla(${color.h}, ${color.s}%, ${color.l}%, ${opacity ?? defaultOpacity})`;
|
184 | }
|
185 | exports.getStyleFromHsl = getStyleFromHsl;
|
186 | function colorMix(color1, color2, size1, size2) {
|
187 | let rgb1 = color1, rgb2 = color2;
|
188 | if (rgb1.r === undefined) {
|
189 | rgb1 = hslToRgb(color1);
|
190 | }
|
191 | if (rgb2.r === undefined) {
|
192 | rgb2 = hslToRgb(color2);
|
193 | }
|
194 | return {
|
195 | b: (0, NumberUtils_js_1.mix)(rgb1.b, rgb2.b, size1, size2),
|
196 | g: (0, NumberUtils_js_1.mix)(rgb1.g, rgb2.g, size1, size2),
|
197 | r: (0, NumberUtils_js_1.mix)(rgb1.r, rgb2.r, size1, size2),
|
198 | };
|
199 | }
|
200 | exports.colorMix = colorMix;
|
201 | function getLinkColor(p1, p2, linkColor) {
|
202 | if (linkColor === randomColorValue) {
|
203 | return getRandomRgbColor();
|
204 | }
|
205 | else if (linkColor === midColorValue) {
|
206 | const sourceColor = p1.getFillColor() ?? p1.getStrokeColor(), destColor = p2?.getFillColor() ?? p2?.getStrokeColor();
|
207 | if (sourceColor && destColor && p2) {
|
208 | return colorMix(sourceColor, destColor, p1.getRadius(), p2.getRadius());
|
209 | }
|
210 | else {
|
211 | const hslColor = sourceColor ?? destColor;
|
212 | if (hslColor) {
|
213 | return hslToRgb(hslColor);
|
214 | }
|
215 | }
|
216 | }
|
217 | else {
|
218 | return linkColor;
|
219 | }
|
220 | }
|
221 | exports.getLinkColor = getLinkColor;
|
222 | function getLinkRandomColor(optColor, blink, consent) {
|
223 | const color = (0, TypeUtils_js_1.isString)(optColor) ? optColor : optColor.value;
|
224 | if (color === randomColorValue) {
|
225 | if (consent) {
|
226 | return rangeColorToRgb({
|
227 | value: color,
|
228 | });
|
229 | }
|
230 | if (blink) {
|
231 | return randomColorValue;
|
232 | }
|
233 | return midColorValue;
|
234 | }
|
235 | else if (color === midColorValue) {
|
236 | return midColorValue;
|
237 | }
|
238 | else {
|
239 | return rangeColorToRgb({
|
240 | value: color,
|
241 | });
|
242 | }
|
243 | }
|
244 | exports.getLinkRandomColor = getLinkRandomColor;
|
245 | function getHslFromAnimation(animation) {
|
246 | return animation !== undefined
|
247 | ? {
|
248 | h: animation.h.value,
|
249 | s: animation.s.value,
|
250 | l: animation.l.value,
|
251 | }
|
252 | : undefined;
|
253 | }
|
254 | exports.getHslFromAnimation = getHslFromAnimation;
|
255 | function getHslAnimationFromHsl(hsl, animationOptions, reduceFactor) {
|
256 | const resColor = {
|
257 | h: {
|
258 | enable: false,
|
259 | value: hsl.h,
|
260 | },
|
261 | s: {
|
262 | enable: false,
|
263 | value: hsl.s,
|
264 | },
|
265 | l: {
|
266 | enable: false,
|
267 | value: hsl.l,
|
268 | },
|
269 | };
|
270 | if (animationOptions) {
|
271 | setColorAnimation(resColor.h, animationOptions.h, reduceFactor);
|
272 | setColorAnimation(resColor.s, animationOptions.s, reduceFactor);
|
273 | setColorAnimation(resColor.l, animationOptions.l, reduceFactor);
|
274 | }
|
275 | return resColor;
|
276 | }
|
277 | exports.getHslAnimationFromHsl = getHslAnimationFromHsl;
|
278 | function setColorAnimation(colorValue, colorAnimation, reduceFactor) {
|
279 | colorValue.enable = colorAnimation.enable;
|
280 | const defaultVelocity = 0, decayOffset = 1, defaultLoops = 0, defaultTime = 0;
|
281 | if (colorValue.enable) {
|
282 | colorValue.velocity = ((0, NumberUtils_js_1.getRangeValue)(colorAnimation.speed) / Constants_js_1.percentDenominator) * reduceFactor;
|
283 | colorValue.decay = decayOffset - (0, NumberUtils_js_1.getRangeValue)(colorAnimation.decay);
|
284 | colorValue.status = AnimationStatus_js_1.AnimationStatus.increasing;
|
285 | colorValue.loops = defaultLoops;
|
286 | colorValue.maxLoops = (0, NumberUtils_js_1.getRangeValue)(colorAnimation.count);
|
287 | colorValue.time = defaultTime;
|
288 | colorValue.delayTime = (0, NumberUtils_js_1.getRangeValue)(colorAnimation.delay) * Constants_js_1.millisecondsToSeconds;
|
289 | if (!colorAnimation.sync) {
|
290 | colorValue.velocity *= (0, NumberUtils_js_1.getRandom)();
|
291 | colorValue.value *= (0, NumberUtils_js_1.getRandom)();
|
292 | }
|
293 | colorValue.initialValue = colorValue.value;
|
294 | colorValue.offset = (0, NumberUtils_js_1.setRangeValue)(colorAnimation.offset);
|
295 | }
|
296 | else {
|
297 | colorValue.velocity = defaultVelocity;
|
298 | }
|
299 | }
|
300 | function updateColorValue(data, range, decrease, delta) {
|
301 | const minLoops = 0, minDelay = 0, identity = 1, minVelocity = 0, minOffset = 0, velocityFactor = 3.6;
|
302 | if (!data ||
|
303 | !data.enable ||
|
304 | ((data.maxLoops ?? minLoops) > minLoops && (data.loops ?? minLoops) > (data.maxLoops ?? minLoops))) {
|
305 | return;
|
306 | }
|
307 | if (!data.time) {
|
308 | data.time = 0;
|
309 | }
|
310 | if ((data.delayTime ?? minDelay) > minDelay && data.time < (data.delayTime ?? minDelay)) {
|
311 | data.time += delta.value;
|
312 | }
|
313 | if ((data.delayTime ?? minDelay) > minDelay && data.time < (data.delayTime ?? minDelay)) {
|
314 | return;
|
315 | }
|
316 | const offset = data.offset ? (0, NumberUtils_js_1.randomInRange)(data.offset) : minOffset, velocity = (data.velocity ?? minVelocity) * delta.factor + offset * velocityFactor, decay = data.decay ?? identity, max = (0, NumberUtils_js_1.getRangeMax)(range), min = (0, NumberUtils_js_1.getRangeMin)(range);
|
317 | if (!decrease || data.status === AnimationStatus_js_1.AnimationStatus.increasing) {
|
318 | data.value += velocity;
|
319 | if (data.value > max) {
|
320 | if (!data.loops) {
|
321 | data.loops = 0;
|
322 | }
|
323 | data.loops++;
|
324 | if (decrease) {
|
325 | data.status = AnimationStatus_js_1.AnimationStatus.decreasing;
|
326 | }
|
327 | else {
|
328 | data.value -= max;
|
329 | }
|
330 | }
|
331 | }
|
332 | else {
|
333 | data.value -= velocity;
|
334 | const minValue = 0;
|
335 | if (data.value < minValue) {
|
336 | if (!data.loops) {
|
337 | data.loops = 0;
|
338 | }
|
339 | data.loops++;
|
340 | data.status = AnimationStatus_js_1.AnimationStatus.increasing;
|
341 | }
|
342 | }
|
343 | if (data.velocity && decay !== identity) {
|
344 | data.velocity *= decay;
|
345 | }
|
346 | data.value = (0, NumberUtils_js_1.clamp)(data.value, min, max);
|
347 | }
|
348 | exports.updateColorValue = updateColorValue;
|
349 | function updateColor(color, delta) {
|
350 | if (!color) {
|
351 | return;
|
352 | }
|
353 | const { h, s, l } = color;
|
354 | const ranges = {
|
355 | h: { min: 0, max: 360 },
|
356 | s: { min: 0, max: 100 },
|
357 | l: { min: 0, max: 100 },
|
358 | };
|
359 | if (h) {
|
360 | updateColorValue(h, ranges.h, false, delta);
|
361 | }
|
362 | if (s) {
|
363 | updateColorValue(s, ranges.s, true, delta);
|
364 | }
|
365 | if (l) {
|
366 | updateColorValue(l, ranges.l, true, delta);
|
367 | }
|
368 | }
|
369 | exports.updateColor = updateColor;
|