UNPKG

15.1 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getHslAnimationFromHsl = exports.getHslFromAnimation = exports.getLinkRandomColor = exports.getLinkColor = exports.colorMix = exports.getStyleFromHsv = exports.getStyleFromHsl = exports.getStyleFromRgb = exports.getRandomRgbColor = exports.rgbaToHsva = exports.rgbToHsv = exports.hsvaToRgba = exports.hsvToRgb = exports.hsvaToHsla = exports.hsvToHsl = exports.hslaToHsva = exports.hslToHsv = exports.hslaToRgba = exports.hslToRgb = exports.stringToRgb = exports.stringToAlpha = exports.rgbToHsl = exports.colorToHsl = exports.colorToRgb = void 0;
4const Utils_1 = require("./Utils");
5const Constants_1 = require("./Constants");
6const NumberUtils_1 = require("./NumberUtils");
7const Enums_1 = require("../Enums");
8function hue2rgb(p, q, t) {
9 let tCalc = t;
10 if (tCalc < 0) {
11 tCalc += 1;
12 }
13 if (tCalc > 1) {
14 tCalc -= 1;
15 }
16 if (tCalc < 1 / 6) {
17 return p + (q - p) * 6 * tCalc;
18 }
19 if (tCalc < 1 / 2) {
20 return q;
21 }
22 if (tCalc < 2 / 3) {
23 return p + (q - p) * (2 / 3 - tCalc) * 6;
24 }
25 return p;
26}
27function stringToRgba(input) {
28 if (input.startsWith("rgb")) {
29 const regex = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(,\s*([\d.]+)\s*)?\)/i;
30 const result = regex.exec(input);
31 return result
32 ? {
33 a: result.length > 4 ? parseFloat(result[5]) : 1,
34 b: parseInt(result[3], 10),
35 g: parseInt(result[2], 10),
36 r: parseInt(result[1], 10),
37 }
38 : undefined;
39 }
40 else if (input.startsWith("hsl")) {
41 const regex = /hsla?\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*(,\s*([\d.]+)\s*)?\)/i;
42 const result = regex.exec(input);
43 return result
44 ? hslaToRgba({
45 a: result.length > 4 ? parseFloat(result[5]) : 1,
46 h: parseInt(result[1], 10),
47 l: parseInt(result[3], 10),
48 s: parseInt(result[2], 10),
49 })
50 : undefined;
51 }
52 else if (input.startsWith("hsv")) {
53 const regex = /hsva?\(\s*(\d+)°\s*,\s*(\d+)%\s*,\s*(\d+)%\s*(,\s*([\d.]+)\s*)?\)/i;
54 const result = regex.exec(input);
55 return result
56 ? hsvaToRgba({
57 a: result.length > 4 ? parseFloat(result[5]) : 1,
58 h: parseInt(result[1], 10),
59 s: parseInt(result[2], 10),
60 v: parseInt(result[3], 10),
61 })
62 : undefined;
63 }
64 else {
65 const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])([a-f\d])?$/i;
66 const hexFixed = input.replace(shorthandRegex, (_m, r, g, b, a) => {
67 return r + r + g + g + b + b + (a !== undefined ? a + a : "");
68 });
69 const regex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i;
70 const result = regex.exec(hexFixed);
71 return result
72 ? {
73 a: result[4] !== undefined ? parseInt(result[4], 16) / 0xff : 1,
74 b: parseInt(result[3], 16),
75 g: parseInt(result[2], 16),
76 r: parseInt(result[1], 16),
77 }
78 : undefined;
79 }
80}
81function colorToRgb(input, index, useIndex = true) {
82 var _a, _b, _c;
83 if (input === undefined) {
84 return;
85 }
86 const color = typeof input === "string" ? { value: input } : input;
87 let res;
88 if (typeof color.value === "string") {
89 if (color.value === Constants_1.Constants.randomColorValue) {
90 res = getRandomRgbColor();
91 }
92 else {
93 res = stringToRgb(color.value);
94 }
95 }
96 else {
97 if (color.value instanceof Array) {
98 const colorSelected = (0, Utils_1.itemFromArray)(color.value, index, useIndex);
99 res = colorToRgb({ value: colorSelected });
100 }
101 else {
102 const colorValue = color.value;
103 const rgbColor = (_a = colorValue.rgb) !== null && _a !== void 0 ? _a : color.value;
104 if (rgbColor.r !== undefined) {
105 res = rgbColor;
106 }
107 else {
108 const hslColor = (_b = colorValue.hsl) !== null && _b !== void 0 ? _b : color.value;
109 if (hslColor.h !== undefined && hslColor.l !== undefined) {
110 res = hslToRgb(hslColor);
111 }
112 else {
113 const hsvColor = (_c = colorValue.hsv) !== null && _c !== void 0 ? _c : color.value;
114 if (hsvColor.h !== undefined && hsvColor.v !== undefined) {
115 res = hsvToRgb(hsvColor);
116 }
117 }
118 }
119 }
120 }
121 return res;
122}
123exports.colorToRgb = colorToRgb;
124function colorToHsl(color, index, useIndex = true) {
125 const rgb = colorToRgb(color, index, useIndex);
126 return rgb !== undefined ? rgbToHsl(rgb) : undefined;
127}
128exports.colorToHsl = colorToHsl;
129function rgbToHsl(color) {
130 const r1 = color.r / 255;
131 const g1 = color.g / 255;
132 const b1 = color.b / 255;
133 const max = Math.max(r1, g1, b1);
134 const min = Math.min(r1, g1, b1);
135 const res = {
136 h: 0,
137 l: (max + min) / 2,
138 s: 0,
139 };
140 if (max != min) {
141 res.s = res.l < 0.5 ? (max - min) / (max + min) : (max - min) / (2.0 - max - min);
142 res.h =
143 r1 === max
144 ? (g1 - b1) / (max - min)
145 : (res.h = g1 === max ? 2.0 + (b1 - r1) / (max - min) : 4.0 + (r1 - g1) / (max - min));
146 }
147 res.l *= 100;
148 res.s *= 100;
149 res.h *= 60;
150 if (res.h < 0) {
151 res.h += 360;
152 }
153 return res;
154}
155exports.rgbToHsl = rgbToHsl;
156function stringToAlpha(input) {
157 var _a;
158 return (_a = stringToRgba(input)) === null || _a === void 0 ? void 0 : _a.a;
159}
160exports.stringToAlpha = stringToAlpha;
161function stringToRgb(input) {
162 return stringToRgba(input);
163}
164exports.stringToRgb = stringToRgb;
165function hslToRgb(hsl) {
166 const result = { b: 0, g: 0, r: 0 };
167 const hslPercent = {
168 h: hsl.h / 360,
169 l: hsl.l / 100,
170 s: hsl.s / 100,
171 };
172 if (hslPercent.s === 0) {
173 result.b = hslPercent.l;
174 result.g = hslPercent.l;
175 result.r = hslPercent.l;
176 }
177 else {
178 const q = hslPercent.l < 0.5
179 ? hslPercent.l * (1 + hslPercent.s)
180 : hslPercent.l + hslPercent.s - hslPercent.l * hslPercent.s;
181 const p = 2 * hslPercent.l - q;
182 result.r = hue2rgb(p, q, hslPercent.h + 1 / 3);
183 result.g = hue2rgb(p, q, hslPercent.h);
184 result.b = hue2rgb(p, q, hslPercent.h - 1 / 3);
185 }
186 result.r = Math.floor(result.r * 255);
187 result.g = Math.floor(result.g * 255);
188 result.b = Math.floor(result.b * 255);
189 return result;
190}
191exports.hslToRgb = hslToRgb;
192function hslaToRgba(hsla) {
193 const rgbResult = hslToRgb(hsla);
194 return {
195 a: hsla.a,
196 b: rgbResult.b,
197 g: rgbResult.g,
198 r: rgbResult.r,
199 };
200}
201exports.hslaToRgba = hslaToRgba;
202function hslToHsv(hsl) {
203 const l = hsl.l / 100, sl = hsl.s / 100;
204 const v = l + sl * Math.min(l, 1 - l), sv = !v ? 0 : 2 * (1 - l / v);
205 return {
206 h: hsl.h,
207 s: sv * 100,
208 v: v * 100,
209 };
210}
211exports.hslToHsv = hslToHsv;
212function hslaToHsva(hsla) {
213 const hsvResult = hslToHsv(hsla);
214 return {
215 a: hsla.a,
216 h: hsvResult.h,
217 s: hsvResult.s,
218 v: hsvResult.v,
219 };
220}
221exports.hslaToHsva = hslaToHsva;
222function hsvToHsl(hsv) {
223 const v = hsv.v / 100, sv = hsv.s / 100;
224 const l = v * (1 - sv / 2), sl = l === 0 || l === 1 ? 0 : (v - l) / Math.min(l, 1 - l);
225 return {
226 h: hsv.h,
227 l: l * 100,
228 s: sl * 100,
229 };
230}
231exports.hsvToHsl = hsvToHsl;
232function hsvaToHsla(hsva) {
233 const hslResult = hsvToHsl(hsva);
234 return {
235 a: hsva.a,
236 h: hslResult.h,
237 l: hslResult.l,
238 s: hslResult.s,
239 };
240}
241exports.hsvaToHsla = hsvaToHsla;
242function hsvToRgb(hsv) {
243 const result = { b: 0, g: 0, r: 0 };
244 const hsvPercent = {
245 h: hsv.h / 60,
246 s: hsv.s / 100,
247 v: hsv.v / 100,
248 };
249 const c = hsvPercent.v * hsvPercent.s, x = c * (1 - Math.abs((hsvPercent.h % 2) - 1));
250 let tempRgb;
251 if (hsvPercent.h >= 0 && hsvPercent.h <= 1) {
252 tempRgb = {
253 r: c,
254 g: x,
255 b: 0,
256 };
257 }
258 else if (hsvPercent.h > 1 && hsvPercent.h <= 2) {
259 tempRgb = {
260 r: x,
261 g: c,
262 b: 0,
263 };
264 }
265 else if (hsvPercent.h > 2 && hsvPercent.h <= 3) {
266 tempRgb = {
267 r: 0,
268 g: c,
269 b: x,
270 };
271 }
272 else if (hsvPercent.h > 3 && hsvPercent.h <= 4) {
273 tempRgb = {
274 r: 0,
275 g: x,
276 b: c,
277 };
278 }
279 else if (hsvPercent.h > 4 && hsvPercent.h <= 5) {
280 tempRgb = {
281 r: x,
282 g: 0,
283 b: c,
284 };
285 }
286 else if (hsvPercent.h > 5 && hsvPercent.h <= 6) {
287 tempRgb = {
288 r: c,
289 g: 0,
290 b: x,
291 };
292 }
293 if (tempRgb) {
294 const m = hsvPercent.v - c;
295 result.r = Math.floor((tempRgb.r + m) * 255);
296 result.g = Math.floor((tempRgb.g + m) * 255);
297 result.b = Math.floor((tempRgb.b + m) * 255);
298 }
299 return result;
300}
301exports.hsvToRgb = hsvToRgb;
302function hsvaToRgba(hsva) {
303 const rgbResult = hsvToRgb(hsva);
304 return {
305 a: hsva.a,
306 b: rgbResult.b,
307 g: rgbResult.g,
308 r: rgbResult.r,
309 };
310}
311exports.hsvaToRgba = hsvaToRgba;
312function rgbToHsv(rgb) {
313 const rgbPercent = {
314 r: rgb.r / 255,
315 g: rgb.g / 255,
316 b: rgb.b / 255,
317 }, xMax = Math.max(rgbPercent.r, rgbPercent.g, rgbPercent.b), xMin = Math.min(rgbPercent.r, rgbPercent.g, rgbPercent.b), v = xMax, c = xMax - xMin;
318 let h = 0;
319 if (v === rgbPercent.r) {
320 h = 60 * ((rgbPercent.g - rgbPercent.b) / c);
321 }
322 else if (v === rgbPercent.g) {
323 h = 60 * (2 + (rgbPercent.b - rgbPercent.r) / c);
324 }
325 else if (v === rgbPercent.b) {
326 h = 60 * (4 + (rgbPercent.r - rgbPercent.g) / c);
327 }
328 const s = !v ? 0 : c / v;
329 return {
330 h,
331 s: s * 100,
332 v: v * 100,
333 };
334}
335exports.rgbToHsv = rgbToHsv;
336function rgbaToHsva(rgba) {
337 const hsvResult = rgbToHsv(rgba);
338 return {
339 a: rgba.a,
340 h: hsvResult.h,
341 s: hsvResult.s,
342 v: hsvResult.v,
343 };
344}
345exports.rgbaToHsva = rgbaToHsva;
346function getRandomRgbColor(min) {
347 const fixedMin = min !== null && min !== void 0 ? min : 0;
348 return {
349 b: Math.floor((0, NumberUtils_1.randomInRange)((0, NumberUtils_1.setRangeValue)(fixedMin, 256))),
350 g: Math.floor((0, NumberUtils_1.randomInRange)((0, NumberUtils_1.setRangeValue)(fixedMin, 256))),
351 r: Math.floor((0, NumberUtils_1.randomInRange)((0, NumberUtils_1.setRangeValue)(fixedMin, 256))),
352 };
353}
354exports.getRandomRgbColor = getRandomRgbColor;
355function getStyleFromRgb(color, opacity) {
356 return `rgba(${color.r}, ${color.g}, ${color.b}, ${opacity !== null && opacity !== void 0 ? opacity : 1})`;
357}
358exports.getStyleFromRgb = getStyleFromRgb;
359function getStyleFromHsl(color, opacity) {
360 return `hsla(${color.h}, ${color.s}%, ${color.l}%, ${opacity !== null && opacity !== void 0 ? opacity : 1})`;
361}
362exports.getStyleFromHsl = getStyleFromHsl;
363function getStyleFromHsv(color, opacity) {
364 return getStyleFromHsl(hsvToHsl(color), opacity);
365}
366exports.getStyleFromHsv = getStyleFromHsv;
367function colorMix(color1, color2, size1, size2) {
368 let rgb1 = color1;
369 let rgb2 = color2;
370 if (rgb1.r === undefined) {
371 rgb1 = hslToRgb(color1);
372 }
373 if (rgb2.r === undefined) {
374 rgb2 = hslToRgb(color2);
375 }
376 return {
377 b: (0, NumberUtils_1.mix)(rgb1.b, rgb2.b, size1, size2),
378 g: (0, NumberUtils_1.mix)(rgb1.g, rgb2.g, size1, size2),
379 r: (0, NumberUtils_1.mix)(rgb1.r, rgb2.r, size1, size2),
380 };
381}
382exports.colorMix = colorMix;
383function getLinkColor(p1, p2, linkColor) {
384 var _a, _b;
385 if (linkColor === Constants_1.Constants.randomColorValue) {
386 return getRandomRgbColor();
387 }
388 else if (linkColor === "mid") {
389 const sourceColor = (_a = p1.getFillColor()) !== null && _a !== void 0 ? _a : p1.getStrokeColor();
390 const destColor = (_b = p2 === null || p2 === void 0 ? void 0 : p2.getFillColor()) !== null && _b !== void 0 ? _b : p2 === null || p2 === void 0 ? void 0 : p2.getStrokeColor();
391 if (sourceColor && destColor && p2) {
392 return colorMix(sourceColor, destColor, p1.getRadius(), p2.getRadius());
393 }
394 else {
395 const hslColor = sourceColor !== null && sourceColor !== void 0 ? sourceColor : destColor;
396 if (hslColor) {
397 return hslToRgb(hslColor);
398 }
399 }
400 }
401 else {
402 return linkColor;
403 }
404}
405exports.getLinkColor = getLinkColor;
406function getLinkRandomColor(optColor, blink, consent) {
407 const color = typeof optColor === "string" ? optColor : optColor.value;
408 if (color === Constants_1.Constants.randomColorValue) {
409 if (consent) {
410 return colorToRgb({
411 value: color,
412 });
413 }
414 else if (blink) {
415 return Constants_1.Constants.randomColorValue;
416 }
417 else {
418 return Constants_1.Constants.midColorValue;
419 }
420 }
421 else {
422 return colorToRgb({
423 value: color,
424 });
425 }
426}
427exports.getLinkRandomColor = getLinkRandomColor;
428function getHslFromAnimation(animation) {
429 return animation !== undefined
430 ? {
431 h: animation.h.value,
432 s: animation.s.value,
433 l: animation.l.value,
434 }
435 : undefined;
436}
437exports.getHslFromAnimation = getHslFromAnimation;
438function getHslAnimationFromHsl(hsl, animationOptions, reduceFactor) {
439 const resColor = {
440 h: {
441 enable: false,
442 value: hsl.h,
443 },
444 s: {
445 enable: false,
446 value: hsl.s,
447 },
448 l: {
449 enable: false,
450 value: hsl.l,
451 },
452 };
453 if (animationOptions) {
454 setColorAnimation(resColor.h, animationOptions.h, reduceFactor);
455 setColorAnimation(resColor.s, animationOptions.s, reduceFactor);
456 setColorAnimation(resColor.l, animationOptions.l, reduceFactor);
457 }
458 return resColor;
459}
460exports.getHslAnimationFromHsl = getHslAnimationFromHsl;
461function setColorAnimation(colorValue, colorAnimation, reduceFactor) {
462 colorValue.enable = colorAnimation.enable;
463 if (colorValue.enable) {
464 colorValue.velocity = (colorAnimation.speed / 100) * reduceFactor;
465 if (colorAnimation.sync) {
466 return;
467 }
468 colorValue.status = Enums_1.AnimationStatus.increasing;
469 colorValue.velocity *= Math.random();
470 if (colorValue.value) {
471 colorValue.value *= Math.random();
472 }
473 }
474 else {
475 colorValue.velocity = 0;
476 }
477}