@promptbook/browser 0.102.0-10 → 0.102.0-12

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.
package/umd/index.umd.js CHANGED
@@ -22,7 +22,7 @@
22
22
  * @generated
23
23
  * @see https://github.com/webgptorg/promptbook
24
24
  */
25
- const PROMPTBOOK_ENGINE_VERSION = '0.102.0-10';
25
+ const PROMPTBOOK_ENGINE_VERSION = '0.102.0-12';
26
26
  /**
27
27
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
28
28
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -152,6 +152,886 @@
152
152
  * TODO: [🎺]
153
153
  */
154
154
 
155
+ /**
156
+ * @private util of `@promptbook/color`
157
+ * @de
158
+ */
159
+ class TakeChain {
160
+ constructor(value) {
161
+ this.value = value;
162
+ }
163
+ then(callback) {
164
+ const newValue = callback(this.value);
165
+ return take(newValue);
166
+ }
167
+ }
168
+
169
+ /**
170
+ * A function that takes an initial value and returns a proxy object with chainable methods.
171
+ *
172
+ * @param {*} initialValue - The initial value.
173
+ * @returns {Proxy<WithTake<TValue>>} - A proxy object with a `take` method.
174
+ *
175
+ * @private util of `@promptbook/color`
176
+ * @deprecated [🤡] Use some better functional library instead of `TakeChain`
177
+ */
178
+ function take(initialValue) {
179
+ if (initialValue instanceof TakeChain) {
180
+ return initialValue;
181
+ }
182
+ return new Proxy(new TakeChain(initialValue), {
183
+ get(target, property, receiver) {
184
+ if (Reflect.has(target, property)) {
185
+ return Reflect.get(target, property, receiver);
186
+ }
187
+ else if (Reflect.has(initialValue, property)) {
188
+ return Reflect.get(initialValue, property, receiver);
189
+ }
190
+ else {
191
+ return undefined;
192
+ }
193
+ },
194
+ });
195
+ }
196
+
197
+ /**
198
+ * 🎨 List of all 140 color names which are supported by CSS
199
+ *
200
+ * @public exported from `@promptbook/color`
201
+ */
202
+ const CSS_COLORS = {
203
+ transparent: 'rgba(0,0,0,0)',
204
+ aliceblue: '#f0f8ff',
205
+ antiquewhite: '#faebd7',
206
+ aqua: '#00ffff',
207
+ aquamarine: '#7fffd4',
208
+ azure: '#f0ffff',
209
+ beige: '#f5f5dc',
210
+ bisque: '#ffe4c4',
211
+ black: '#000000',
212
+ blanchedalmond: '#ffebcd',
213
+ blue: '#0000ff',
214
+ blueviolet: '#8a2be2',
215
+ brown: '#a52a2a',
216
+ burlywood: '#deb887',
217
+ cadetblue: '#5f9ea0',
218
+ chartreuse: '#7fff00',
219
+ chocolate: '#d2691e',
220
+ coral: '#ff7f50',
221
+ cornflowerblue: '#6495ed',
222
+ cornsilk: '#fff8dc',
223
+ crimson: '#dc143c',
224
+ cyan: '#00ffff',
225
+ darkblue: '#00008b',
226
+ darkcyan: '#008b8b',
227
+ darkgoldenrod: '#b8860b',
228
+ darkgray: '#a9a9a9',
229
+ darkgrey: '#a9a9a9',
230
+ darkgreen: '#006400',
231
+ darkkhaki: '#bdb76b',
232
+ darkmagenta: '#8b008b',
233
+ darkolivegreen: '#556b2f',
234
+ darkorange: '#ff8c00',
235
+ darkorchid: '#9932cc',
236
+ darkred: '#8b0000',
237
+ darksalmon: '#e9967a',
238
+ darkseagreen: '#8fbc8f',
239
+ darkslateblue: '#483d8b',
240
+ darkslategray: '#2f4f4f',
241
+ darkslategrey: '#2f4f4f',
242
+ darkturquoise: '#00ced1',
243
+ darkviolet: '#9400d3',
244
+ deeppink: '#ff1493',
245
+ deepskyblue: '#00bfff',
246
+ dimgray: '#696969',
247
+ dimgrey: '#696969',
248
+ dodgerblue: '#1e90ff',
249
+ firebrick: '#b22222',
250
+ floralwhite: '#fffaf0',
251
+ forestgreen: '#228b22',
252
+ fuchsia: '#ff00ff',
253
+ gainsboro: '#dcdcdc',
254
+ ghostwhite: '#f8f8ff',
255
+ gold: '#ffd700',
256
+ goldenrod: '#daa520',
257
+ gray: '#808080',
258
+ grey: '#808080',
259
+ green: '#008000',
260
+ greenyellow: '#adff2f',
261
+ honeydew: '#f0fff0',
262
+ hotpink: '#ff69b4',
263
+ indianred: '#cd5c5c',
264
+ indigo: '#4b0082',
265
+ ivory: '#fffff0',
266
+ khaki: '#f0e68c',
267
+ lavender: '#e6e6fa',
268
+ lavenderblush: '#fff0f5',
269
+ lawngreen: '#7cfc00',
270
+ lemonchiffon: '#fffacd',
271
+ lightblue: '#add8e6',
272
+ lightcoral: '#f08080',
273
+ lightcyan: '#e0ffff',
274
+ lightgoldenrodyellow: '#fafad2',
275
+ lightgray: '#d3d3d3',
276
+ lightgrey: '#d3d3d3',
277
+ lightgreen: '#90ee90',
278
+ lightpink: '#ffb6c1',
279
+ lightsalmon: '#ffa07a',
280
+ lightseagreen: '#20b2aa',
281
+ lightskyblue: '#87cefa',
282
+ lightslategray: '#778899',
283
+ lightslategrey: '#778899',
284
+ lightsteelblue: '#b0c4de',
285
+ lightyellow: '#ffffe0',
286
+ lime: '#00ff00',
287
+ limegreen: '#32cd32',
288
+ linen: '#faf0e6',
289
+ magenta: '#ff00ff',
290
+ maroon: '#800000',
291
+ mediumaquamarine: '#66cdaa',
292
+ mediumblue: '#0000cd',
293
+ mediumorchid: '#ba55d3',
294
+ mediumpurple: '#9370db',
295
+ mediumseagreen: '#3cb371',
296
+ mediumslateblue: '#7b68ee',
297
+ mediumspringgreen: '#00fa9a',
298
+ mediumturquoise: '#48d1cc',
299
+ mediumvioletred: '#c71585',
300
+ midnightblue: '#191970',
301
+ mintcream: '#f5fffa',
302
+ mistyrose: '#ffe4e1',
303
+ moccasin: '#ffe4b5',
304
+ navajowhite: '#ffdead',
305
+ navy: '#000080',
306
+ oldlace: '#fdf5e6',
307
+ olive: '#808000',
308
+ olivedrab: '#6b8e23',
309
+ orange: '#ffa500',
310
+ orangered: '#ff4500',
311
+ orchid: '#da70d6',
312
+ palegoldenrod: '#eee8aa',
313
+ palegreen: '#98fb98',
314
+ paleturquoise: '#afeeee',
315
+ palevioletred: '#db7093',
316
+ papayawhip: '#ffefd5',
317
+ peachpuff: '#ffdab9',
318
+ peru: '#cd853f',
319
+ pink: '#ffc0cb',
320
+ plum: '#dda0dd',
321
+ powderblue: '#b0e0e6',
322
+ purple: '#800080',
323
+ rebeccapurple: '#663399',
324
+ red: '#ff0000',
325
+ rosybrown: '#bc8f8f',
326
+ royalblue: '#4169e1',
327
+ saddlebrown: '#8b4513',
328
+ salmon: '#fa8072',
329
+ sandybrown: '#f4a460',
330
+ seagreen: '#2e8b57',
331
+ seashell: '#fff5ee',
332
+ sienna: '#a0522d',
333
+ silver: '#c0c0c0',
334
+ skyblue: '#87ceeb',
335
+ slateblue: '#6a5acd',
336
+ slategray: '#708090',
337
+ slategrey: '#708090',
338
+ snow: '#fffafa',
339
+ springgreen: '#00ff7f',
340
+ steelblue: '#4682b4',
341
+ tan: '#d2b48c',
342
+ teal: '#008080',
343
+ thistle: '#d8bfd8',
344
+ tomato: '#ff6347',
345
+ turquoise: '#40e0d0',
346
+ violet: '#ee82ee',
347
+ wheat: '#f5deb3',
348
+ white: '#ffffff',
349
+ whitesmoke: '#f5f5f5',
350
+ yellow: '#ffff00',
351
+ yellowgreen: '#9acd32',
352
+ };
353
+ /**
354
+ * Note: [💞] Ignore a discrepancy between file name and entity name
355
+ */
356
+
357
+ /**
358
+ * Validates that a channel value is a valid number within the range of 0 to 255.
359
+ * Throws an error if the value is not valid.
360
+ *
361
+ * @param channelName - The name of the channel being validated.
362
+ * @param value - The value of the channel to validate.
363
+ * @throws Will throw an error if the value is not a valid channel number.
364
+ *
365
+ * @private util of `@promptbook/color`
366
+ */
367
+ function checkChannelValue(channelName, value) {
368
+ if (typeof value !== 'number') {
369
+ throw new Error(`${channelName} channel value is not number but ${typeof value}`);
370
+ }
371
+ if (isNaN(value)) {
372
+ throw new Error(`${channelName} channel value is NaN`);
373
+ }
374
+ if (Math.round(value) !== value) {
375
+ throw new Error(`${channelName} channel is not whole number, it is ${value}`);
376
+ }
377
+ if (value < 0) {
378
+ throw new Error(`${channelName} channel is lower than 0, it is ${value}`);
379
+ }
380
+ if (value > 255) {
381
+ throw new Error(`${channelName} channel is greater than 255, it is ${value}`);
382
+ }
383
+ }
384
+ /**
385
+ * TODO: [🧠][🚓] Is/which combination it better to use asserts/check, validate or is utility function?
386
+ */
387
+
388
+ /**
389
+ * Color object represents an RGB color with alpha channel
390
+ *
391
+ * Note: There is no fromObject/toObject because the most logical way to serialize color is as a hex string (#009edd)
392
+ *
393
+ * @public exported from `@promptbook/color`
394
+ */
395
+ class Color {
396
+ /**
397
+ * Creates a new Color instance from miscellaneous formats
398
+ * - It can receive Color instance and just return the same instance
399
+ * - It can receive color in string format for example `#009edd`, `rgb(0,158,221)`, `rgb(0%,62%,86.7%)`, `hsl(197.1,100%,43.3%)`
400
+ *
401
+ * Note: This is not including fromImage because detecting color from an image is heavy task which requires async stuff and we cannot safely determine with overloading if return value will be a promise
402
+ *
403
+ * @param color
404
+ * @returns Color object
405
+ */
406
+ static from(color) {
407
+ if (color instanceof Color) {
408
+ return take(color);
409
+ }
410
+ else if (Color.isColor(color)) {
411
+ return take(color);
412
+ }
413
+ else if (typeof color === 'string') {
414
+ return Color.fromString(color);
415
+ }
416
+ else {
417
+ console.error({ color });
418
+ throw new Error(`Can not create color from given object`);
419
+ }
420
+ }
421
+ /**
422
+ * Creates a new Color instance from miscellaneous string formats
423
+ *
424
+ * @param color as a string for example `#009edd`, `rgb(0,158,221)`, `rgb(0%,62%,86.7%)`, `hsl(197.1,100%,43.3%)`, `red`, `darkgrey`,...
425
+ * @returns Color object
426
+ */
427
+ static fromString(color) {
428
+ if (CSS_COLORS[color]) {
429
+ return Color.fromString(CSS_COLORS[color]);
430
+ // -----
431
+ }
432
+ else if (/^#(?:[0-9a-fA-F]{3}){1,2}$/.test(color)) {
433
+ return Color.fromHex(color);
434
+ // -----
435
+ }
436
+ else if (/^hsl\(\s*(\d+)\s*,\s*(\d+(?:\.\d+)?%)\s*,\s*(\d+(?:\.\d+)?%)\)$/.test(color)) {
437
+ return Color.fromHsl(color);
438
+ // -----
439
+ }
440
+ else if (/^rgb\((\s*[0-9-.%]+\s*,?){3}\)$/.test(color)) {
441
+ // TODO: [0] Should be fromRgbString and fromRgbaString one or two functions
442
+ return Color.fromRgbString(color);
443
+ // -----
444
+ }
445
+ else if (/^rgba\((\s*[0-9-.%]+\s*,?){4}\)$/.test(color)) {
446
+ return Color.fromRgbaString(color);
447
+ // -----
448
+ }
449
+ else {
450
+ throw new Error(`Can not create a new Color instance from string "${color}".`);
451
+ }
452
+ }
453
+ /**
454
+ * Gets common color
455
+ *
456
+ * @param key as a css string like `midnightblue`
457
+ * @returns Color object
458
+ */
459
+ static get(key) {
460
+ if (!CSS_COLORS[key]) {
461
+ throw new Error(`"${key}" is not a common css color.`);
462
+ }
463
+ return Color.fromString(CSS_COLORS[key]);
464
+ }
465
+ /**
466
+ * Creates a new Color instance from average color of given image
467
+ *
468
+ * @param image as a source for example `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUGFdjYJh39z8ABJgCe/ZvAS4AAAAASUVORK5CYII=`
469
+ * @returns Color object
470
+ */
471
+ static async fromImage(image) {
472
+ return Color.fromHex(`#009edd`);
473
+ }
474
+ /**
475
+ * Creates a new Color instance from color in hex format
476
+ *
477
+ * @param color in hex for example `#009edd`, `009edd`, `#555`,...
478
+ * @returns Color object
479
+ */
480
+ static fromHex(hex) {
481
+ const hexOriginal = hex;
482
+ if (hex.startsWith('#')) {
483
+ hex = hex.substring(1);
484
+ }
485
+ if (hex.length === 3) {
486
+ return Color.fromHex3(hex);
487
+ }
488
+ if (hex.length === 6) {
489
+ return Color.fromHex6(hex);
490
+ }
491
+ if (hex.length === 8) {
492
+ return Color.fromHex8(hex);
493
+ }
494
+ throw new Error(`Can not parse color from hex string "${hexOriginal}"`);
495
+ }
496
+ /**
497
+ * Creates a new Color instance from color in hex format with 3 color digits (without alpha channel)
498
+ *
499
+ * @param color in hex for example `09d`
500
+ * @returns Color object
501
+ */
502
+ static fromHex3(hex) {
503
+ const r = parseInt(hex.substr(0, 1), 16) * 16;
504
+ const g = parseInt(hex.substr(1, 1), 16) * 16;
505
+ const b = parseInt(hex.substr(2, 1), 16) * 16;
506
+ return take(new Color(r, g, b));
507
+ }
508
+ /**
509
+ * Creates a new Color instance from color in hex format with 6 color digits (without alpha channel)
510
+ *
511
+ * @param color in hex for example `009edd`
512
+ * @returns Color object
513
+ */
514
+ static fromHex6(hex) {
515
+ const r = parseInt(hex.substr(0, 2), 16);
516
+ const g = parseInt(hex.substr(2, 2), 16);
517
+ const b = parseInt(hex.substr(4, 2), 16);
518
+ return take(new Color(r, g, b));
519
+ }
520
+ /**
521
+ * Creates a new Color instance from color in hex format with 8 color digits (with alpha channel)
522
+ *
523
+ * @param color in hex for example `009edd`
524
+ * @returns Color object
525
+ */
526
+ static fromHex8(hex) {
527
+ const r = parseInt(hex.substr(0, 2), 16);
528
+ const g = parseInt(hex.substr(2, 2), 16);
529
+ const b = parseInt(hex.substr(4, 2), 16);
530
+ const a = parseInt(hex.substr(6, 2), 16);
531
+ return take(new Color(r, g, b, a));
532
+ }
533
+ /**
534
+ * Creates a new Color instance from color in hsl format
535
+ *
536
+ * @param color as a hsl for example `hsl(197.1,100%,43.3%)`
537
+ * @returns Color object
538
+ */
539
+ static fromHsl(hsl) {
540
+ const match = hsl.match(/^hsl\(\s*([0-9.]+)\s*,\s*([0-9.]+)%\s*,\s*([0-9.]+)%\s*\)$/);
541
+ if (!match) {
542
+ throw new Error(`Invalid hsl string format: "${hsl}"`);
543
+ }
544
+ const h = parseFloat(match[1]);
545
+ const s = parseFloat(match[2]) / 100;
546
+ const l = parseFloat(match[3]) / 100;
547
+ // HSL to RGB conversion
548
+ const c = (1 - Math.abs(2 * l - 1)) * s;
549
+ const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
550
+ const m = l - c / 2;
551
+ let r1 = 0, g1 = 0, b1 = 0;
552
+ if (h >= 0 && h < 60) {
553
+ r1 = c;
554
+ g1 = x;
555
+ b1 = 0;
556
+ }
557
+ else if (h >= 60 && h < 120) {
558
+ r1 = x;
559
+ g1 = c;
560
+ b1 = 0;
561
+ }
562
+ else if (h >= 120 && h < 180) {
563
+ r1 = 0;
564
+ g1 = c;
565
+ b1 = x;
566
+ }
567
+ else if (h >= 180 && h < 240) {
568
+ r1 = 0;
569
+ g1 = x;
570
+ b1 = c;
571
+ }
572
+ else if (h >= 240 && h < 300) {
573
+ r1 = x;
574
+ g1 = 0;
575
+ b1 = c;
576
+ }
577
+ else if (h >= 300 && h < 360) {
578
+ r1 = c;
579
+ g1 = 0;
580
+ b1 = x;
581
+ }
582
+ const r = Math.round((r1 + m) * 255);
583
+ const g = Math.round((g1 + m) * 255);
584
+ const b = Math.round((b1 + m) * 255);
585
+ return take(new Color(r, g, b));
586
+ }
587
+ /**
588
+ * Creates a new Color instance from color in rgb format
589
+ *
590
+ * @param color as a rgb for example `rgb(0,158,221)`, `rgb(0%,62%,86.7%)`
591
+ * @returns Color object
592
+ */
593
+ static fromRgbString(rgb) {
594
+ const match = rgb.match(/^rgb\(\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*\)$/);
595
+ if (!match) {
596
+ throw new Error(`Invalid rgb string format: "${rgb}"`);
597
+ }
598
+ const parseChannel = (value) => {
599
+ if (value.endsWith('%')) {
600
+ // Percentage value
601
+ const percent = parseFloat(value);
602
+ return Math.round((percent / 100) * 255);
603
+ }
604
+ else {
605
+ // Numeric value
606
+ return Math.round(parseFloat(value));
607
+ }
608
+ };
609
+ const r = parseChannel(match[1]);
610
+ const g = parseChannel(match[2]);
611
+ const b = parseChannel(match[3]);
612
+ return take(new Color(r, g, b));
613
+ }
614
+ /**
615
+ * Creates a new Color instance from color in rbga format
616
+ *
617
+ * @param color as a rgba for example `rgba(0,158,221,0.5)`, `rgb(0%,62%,86.7%,50%)`
618
+ * @returns Color object
619
+ */
620
+ static fromRgbaString(rgba) {
621
+ const match = rgba.match(/^rgba\(\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*\)$/);
622
+ if (!match) {
623
+ throw new Error(`Invalid rgba string format: "${rgba}"`);
624
+ }
625
+ const parseChannel = (value) => {
626
+ if (value.endsWith('%')) {
627
+ const percent = parseFloat(value);
628
+ return Math.round((percent / 100) * 255);
629
+ }
630
+ else {
631
+ return Math.round(parseFloat(value));
632
+ }
633
+ };
634
+ const parseAlpha = (value) => {
635
+ if (value.endsWith('%')) {
636
+ const percent = parseFloat(value);
637
+ return Math.round((percent / 100) * 255);
638
+ }
639
+ else {
640
+ const alphaFloat = parseFloat(value);
641
+ // If alpha is between 0 and 1, treat as float
642
+ if (alphaFloat <= 1) {
643
+ return Math.round(alphaFloat * 255);
644
+ }
645
+ // Otherwise, treat as 0-255
646
+ return Math.round(alphaFloat);
647
+ }
648
+ };
649
+ const r = parseChannel(match[1]);
650
+ const g = parseChannel(match[2]);
651
+ const b = parseChannel(match[3]);
652
+ const a = parseAlpha(match[4]);
653
+ return take(new Color(r, g, b, a));
654
+ }
655
+ /**
656
+ * Creates a new Color for color channels values
657
+ *
658
+ * @param red number from 0 to 255
659
+ * @param green number from 0 to 255
660
+ * @param blue number from 0 to 255
661
+ * @param alpha number from 0 (transparent) to 255 (opaque = default)
662
+ * @returns Color object
663
+ */
664
+ static fromValues(red, green, blue, alpha = 255) {
665
+ return take(new Color(red, green, blue, alpha));
666
+ }
667
+ /**
668
+ * Checks if the given value is a valid Color object.
669
+ *
670
+ * @param {unknown} value - The value to check.
671
+ * @return {value is WithTake<Color>} Returns true if the value is a valid Color object, false otherwise.
672
+ */
673
+ static isColor(value) {
674
+ if (typeof value !== 'object') {
675
+ return false;
676
+ }
677
+ if (value === null) {
678
+ return false;
679
+ }
680
+ if (typeof value.red !== 'number' ||
681
+ typeof value.green !== 'number' ||
682
+ typeof value.blue !== 'number' ||
683
+ typeof value.alpha !== 'number') {
684
+ return false;
685
+ }
686
+ if (typeof value.then !== 'function') {
687
+ return false;
688
+ }
689
+ return true;
690
+ }
691
+ /**
692
+ * Creates new Color object
693
+ *
694
+ * Note: Consider using one of static methods like `from` or `fromString`
695
+ *
696
+ * @param red number from 0 to 255
697
+ * @param green number from 0 to 255
698
+ * @param blue number from 0 to 255
699
+ * @param alpha number from 0 (transparent) to 255 (opaque)
700
+ */
701
+ constructor(red, green, blue, alpha = 255) {
702
+ this.red = red;
703
+ this.green = green;
704
+ this.blue = blue;
705
+ this.alpha = alpha;
706
+ checkChannelValue('Red', red);
707
+ checkChannelValue('Green', green);
708
+ checkChannelValue('Blue', blue);
709
+ checkChannelValue('Alpha', alpha);
710
+ }
711
+ /**
712
+ * Shortcut for `red` property
713
+ * Number from 0 to 255
714
+ * @alias red
715
+ */
716
+ get r() {
717
+ return this.red;
718
+ }
719
+ /**
720
+ * Shortcut for `green` property
721
+ * Number from 0 to 255
722
+ * @alias green
723
+ */
724
+ get g() {
725
+ return this.green;
726
+ }
727
+ /**
728
+ * Shortcut for `blue` property
729
+ * Number from 0 to 255
730
+ * @alias blue
731
+ */
732
+ get b() {
733
+ return this.blue;
734
+ }
735
+ /**
736
+ * Shortcut for `alpha` property
737
+ * Number from 0 (transparent) to 255 (opaque)
738
+ * @alias alpha
739
+ */
740
+ get a() {
741
+ return this.alpha;
742
+ }
743
+ /**
744
+ * Shortcut for `alpha` property
745
+ * Number from 0 (transparent) to 255 (opaque)
746
+ * @alias alpha
747
+ */
748
+ get opacity() {
749
+ return this.alpha;
750
+ }
751
+ /**
752
+ * Shortcut for 1-`alpha` property
753
+ */
754
+ get transparency() {
755
+ return 255 - this.alpha;
756
+ }
757
+ clone() {
758
+ return take(new Color(this.red, this.green, this.blue, this.alpha));
759
+ }
760
+ toString() {
761
+ return this.toHex();
762
+ }
763
+ toHex() {
764
+ if (this.alpha === 255) {
765
+ return `#${this.red.toString(16).padStart(2, '0')}${this.green.toString(16).padStart(2, '0')}${this.blue
766
+ .toString(16)
767
+ .padStart(2, '0')}`;
768
+ }
769
+ else {
770
+ return `#${this.red.toString(16).padStart(2, '0')}${this.green.toString(16).padStart(2, '0')}${this.blue
771
+ .toString(16)
772
+ .padStart(2, '0')}${this.alpha.toString(16).padStart(2, '0')}`;
773
+ }
774
+ }
775
+ toRgb() {
776
+ if (this.alpha === 255) {
777
+ return `rgb(${this.red}, ${this.green}, ${this.blue})`;
778
+ }
779
+ else {
780
+ return `rgba(${this.red}, ${this.green}, ${this.blue}, ${Math.round((this.alpha / 255) * 100)}%)`;
781
+ }
782
+ }
783
+ toHsl() {
784
+ throw new Error(`Getting HSL is not implemented`);
785
+ }
786
+ }
787
+ /**
788
+ * TODO: [🥻] Split Color class and color type
789
+ * TODO: For each method a corresponding static method should be created
790
+ * Like clone can be done by color.clone() OR Color.clone(color)
791
+ * TODO: Probably as an independent LIB OR add to LIB xyzt (ask @roseckyj)
792
+ * TODO: !! Transfer back to Collboard (whole directory)
793
+ * TODO: Maybe [🏌️‍♂️] change ACRY toString => (toHex) toRgb when there will be toRgb and toRgba united
794
+ * TODO: Convert getters to methods - getters only for values
795
+ * TODO: Write tests
796
+ * TODO: Getters for alpha, opacity, transparency, r, b, g, h, s, l, a,...
797
+ * TODO: [0] Should be fromRgbString and fromRgbaString one or two functions + one or two regex
798
+ * TODO: Use rgb, rgba, hsl for testing and parsing with the same regex
799
+ * TODO: Regex for rgb, rgba, hsl does not support all options like deg, rad, turn,...
800
+ * TODO: Convolution matrix
801
+ * TODO: Maybe connect with textures
802
+ */
803
+
804
+ /**
805
+ * Converts HSL values to RGB values
806
+ *
807
+ * @param hue [0-1]
808
+ * @param saturation [0-1]
809
+ * @param lightness [0-1]
810
+ * @returns [red, green, blue] [0-255]
811
+ *
812
+ * @private util of `@promptbook/color`
813
+ */
814
+ function hslToRgb(hue, saturation, lightness) {
815
+ let red;
816
+ let green;
817
+ let blue;
818
+ if (saturation === 0) {
819
+ // achromatic
820
+ red = lightness;
821
+ green = lightness;
822
+ blue = lightness;
823
+ }
824
+ else {
825
+ // TODO: Extract to separate function
826
+ const hue2rgb = (p, q, t) => {
827
+ if (t < 0)
828
+ t += 1;
829
+ if (t > 1)
830
+ t -= 1;
831
+ if (t < 1 / 6)
832
+ return p + (q - p) * 6 * t;
833
+ if (t < 1 / 2)
834
+ return q;
835
+ if (t < 2 / 3)
836
+ return p + (q - p) * (2 / 3 - t) * 6;
837
+ return p;
838
+ };
839
+ const q = lightness < 0.5 ? lightness * (1 + saturation) : lightness + saturation - lightness * saturation;
840
+ const p = 2 * lightness - q;
841
+ red = hue2rgb(p, q, hue + 1 / 3);
842
+ green = hue2rgb(p, q, hue);
843
+ blue = hue2rgb(p, q, hue - 1 / 3);
844
+ }
845
+ return [Math.round(red * 255), Math.round(green * 255), Math.round(blue * 255)];
846
+ }
847
+ /**
848
+ * TODO: Properly name all used internal variables
849
+ */
850
+
851
+ /**
852
+ * Converts RGB values to HSL values
853
+ *
854
+ * @param red [0-255]
855
+ * @param green [0-255]
856
+ * @param blue [0-255]
857
+ * @returns [hue, saturation, lightness] [0-1]
858
+ *
859
+ * @private util of `@promptbook/color`
860
+ */
861
+ function rgbToHsl(red, green, blue) {
862
+ red /= 255;
863
+ green /= 255;
864
+ blue /= 255;
865
+ const max = Math.max(red, green, blue);
866
+ const min = Math.min(red, green, blue);
867
+ let hue;
868
+ let saturation;
869
+ const lightness = (max + min) / 2;
870
+ if (max === min) {
871
+ // achromatic
872
+ hue = 0;
873
+ saturation = 0;
874
+ }
875
+ else {
876
+ const d = max - min;
877
+ saturation = lightness > 0.5 ? d / (2 - max - min) : d / (max + min);
878
+ switch (max) {
879
+ case red:
880
+ hue = (green - blue) / d + (green < blue ? 6 : 0);
881
+ break;
882
+ case green:
883
+ hue = (blue - red) / d + 2;
884
+ break;
885
+ case blue:
886
+ hue = (red - green) / d + 4;
887
+ break;
888
+ default:
889
+ hue = 0;
890
+ }
891
+ hue /= 6;
892
+ }
893
+ return [hue, saturation, lightness];
894
+ }
895
+ /**
896
+ * TODO: Properly name all used internal variables
897
+ */
898
+
899
+ /**
900
+ * Makes color transformer which lighten the given color
901
+ *
902
+ * @param amount from 0 to 1
903
+ *
904
+ * @public exported from `@promptbook/color`
905
+ */
906
+ function lighten(amount) {
907
+ return ({ red, green, blue, alpha }) => {
908
+ const [h, s, lInitial] = rgbToHsl(red, green, blue);
909
+ let l = lInitial + amount;
910
+ l = Math.max(0, Math.min(l, 1)); // Replace lodash clamp with Math.max and Math.min
911
+ const [r, g, b] = hslToRgb(h, s, l);
912
+ return Color.fromValues(r, g, b, alpha);
913
+ };
914
+ }
915
+ /**
916
+ * TODO: Maybe implement by mix+hsl
917
+ */
918
+
919
+ /**
920
+ * Calculates distance between two colors
921
+ *
922
+ * @param color1 first color
923
+ * @param color2 second color
924
+ *
925
+ * Note: This function is inefficient. Use colorDistanceSquared instead if possible.
926
+ *
927
+ * @public exported from `@promptbook/color`
928
+ */
929
+ /**
930
+ * Calculates distance between two colors without square root
931
+ *
932
+ * @param color1 first color
933
+ * @param color2 second color
934
+ *
935
+ * @public exported from `@promptbook/color`
936
+ */
937
+ function colorDistanceSquared(color1, color2) {
938
+ const rmean = (color1.red + color2.red) / 2;
939
+ const r = color1.red - color2.red;
940
+ const g = color1.green - color2.green;
941
+ const b = color1.blue - color2.blue;
942
+ const weightR = 2 + rmean / 256;
943
+ const weightG = 4.0;
944
+ const weightB = 2 + (255 - rmean) / 256;
945
+ const distance = weightR * r * r + weightG * g * g + weightB * b * b;
946
+ return distance;
947
+ }
948
+
949
+ /**
950
+ * Makes color transformer which finds the nearest color from the given list
951
+ *
952
+ * @param colors array of colors to choose from
953
+ *
954
+ * @public exported from `@promptbook/color`
955
+ */
956
+ function nearest(...colors) {
957
+ return (color) => {
958
+ const distances = colors.map((c) => colorDistanceSquared(c, color));
959
+ const minDistance = Math.min(...distances);
960
+ const minIndex = distances.indexOf(minDistance);
961
+ const nearestColor = colors[minIndex];
962
+ return nearestColor;
963
+ };
964
+ }
965
+
966
+ /**
967
+ * Color transformer which returns the negative color
968
+ *
969
+ * @public exported from `@promptbook/color`
970
+ */
971
+ function negative(color) {
972
+ const r = 255 - color.red;
973
+ const g = 255 - color.green;
974
+ const b = 255 - color.blue;
975
+ return Color.fromValues(r, g, b, color.alpha);
976
+ }
977
+
978
+ /**
979
+ * Makes color transformer which finds the furthest color from the given list
980
+ *
981
+ * @param colors array of colors to choose from
982
+ *
983
+ * @public exported from `@promptbook/color`
984
+ */
985
+ function furthest(...colors) {
986
+ return (color) => {
987
+ const furthestColor = negative(nearest(...colors.map(negative))(color));
988
+ return furthestColor;
989
+ };
990
+ }
991
+ /**
992
+ * Makes color transformer which finds the best text color (black or white) for the given background color
993
+ *
994
+ * @public exported from `@promptbook/color`
995
+ */
996
+ furthest(Color.get('white'), Color.from('black'));
997
+
998
+ /**
999
+ * Makes color transformer which returns a grayscale version of the color
1000
+ *
1001
+ * @param amount from 0 to 1
1002
+ *
1003
+ * @public exported from `@promptbook/color`
1004
+ */
1005
+ function grayscale(amount) {
1006
+ return ({ red, green, blue, alpha }) => {
1007
+ const average = (red + green + blue) / 3;
1008
+ red = Math.round(average * amount + red * (1 - amount));
1009
+ green = Math.round(average * amount + green * (1 - amount));
1010
+ blue = Math.round(average * amount + blue * (1 - amount));
1011
+ return Color.fromValues(red, green, blue, alpha);
1012
+ };
1013
+ }
1014
+
1015
+ /**
1016
+ * Makes color transformer which saturate the given color
1017
+ *
1018
+ * @param amount from -1 to 1
1019
+ *
1020
+ * @public exported from `@promptbook/color`
1021
+ */
1022
+ function saturate(amount) {
1023
+ return ({ red, green, blue, alpha }) => {
1024
+ const [h, sInitial, l] = rgbToHsl(red, green, blue);
1025
+ let s = sInitial + amount;
1026
+ s = Math.max(0, Math.min(s, 1));
1027
+ const [r, g, b] = hslToRgb(h, s, l);
1028
+ return Color.fromValues(r, g, b, alpha);
1029
+ };
1030
+ }
1031
+ /**
1032
+ * TODO: Maybe implement by mix+hsl
1033
+ */
1034
+
155
1035
  /**
156
1036
  * Name for the Promptbook
157
1037
  *
@@ -172,6 +1052,32 @@
172
1052
  * @public exported from `@promptbook/core`
173
1053
  */
174
1054
  const ADMIN_GITHUB_NAME = 'hejny';
1055
+ // <- TODO: [🐊] Pick the best claim
1056
+ /**
1057
+ * Color of the Promptbook
1058
+ *
1059
+ * TODO: [🗽] Unite branding and make single place for it
1060
+ *
1061
+ * @public exported from `@promptbook/core`
1062
+ */
1063
+ const PROMPTBOOK_COLOR = Color.fromHex('#79EAFD');
1064
+ // <- TODO: !!! How much is this adding to package size of @promptbook/core
1065
+ /**
1066
+ * Dark color of the Promptbook
1067
+ *
1068
+ * TODO: [🗽] Unite branding and make single place for it
1069
+ *
1070
+ * @public exported from `@promptbook/core`
1071
+ */
1072
+ PROMPTBOOK_COLOR.then(lighten(0.1)).then(saturate(0.9)).then(grayscale(0.9));
1073
+ /**
1074
+ * Color of the user (in chat)
1075
+ *
1076
+ * TODO: [🗽] Unite branding and make single place for it
1077
+ *
1078
+ * @public exported from `@promptbook/core`
1079
+ */
1080
+ Color.fromHex('#1D4ED8');
175
1081
  // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
176
1082
  /**
177
1083
  * The maximum number of iterations for a loops