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