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