@promptbook/google 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 +916 -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 +916 -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
@@ -609,6 +609,895 @@
609
609
  return new Date().toISOString();
610
610
  }
611
611
 
612
+ /**
613
+ * @private util of `@promptbook/color`
614
+ * @de
615
+ */
616
+ class TakeChain {
617
+ constructor(value) {
618
+ this.value = value;
619
+ }
620
+ then(callback) {
621
+ const newValue = callback(this.value);
622
+ return take(newValue);
623
+ }
624
+ }
625
+
626
+ /**
627
+ * A function that takes an initial value and returns a proxy object with chainable methods.
628
+ *
629
+ * @param {*} initialValue - The initial value.
630
+ * @returns {Proxy<WithTake<TValue>>} - A proxy object with a `take` method.
631
+ *
632
+ * @private util of `@promptbook/color`
633
+ * @deprecated [🤡] Use some better functional library instead of `TakeChain`
634
+ */
635
+ function take(initialValue) {
636
+ if (initialValue instanceof TakeChain) {
637
+ return initialValue;
638
+ }
639
+ return new Proxy(new TakeChain(initialValue), {
640
+ get(target, property, receiver) {
641
+ if (Reflect.has(target, property)) {
642
+ return Reflect.get(target, property, receiver);
643
+ }
644
+ else if (Reflect.has(initialValue, property)) {
645
+ return Reflect.get(initialValue, property, receiver);
646
+ }
647
+ else {
648
+ return undefined;
649
+ }
650
+ },
651
+ });
652
+ }
653
+
654
+ /**
655
+ * 🎨 List of all 140 color names which are supported by CSS
656
+ *
657
+ * @public exported from `@promptbook/color`
658
+ */
659
+ const CSS_COLORS = {
660
+ transparent: 'rgba(0,0,0,0)',
661
+ aliceblue: '#f0f8ff',
662
+ antiquewhite: '#faebd7',
663
+ aqua: '#00ffff',
664
+ aquamarine: '#7fffd4',
665
+ azure: '#f0ffff',
666
+ beige: '#f5f5dc',
667
+ bisque: '#ffe4c4',
668
+ black: '#000000',
669
+ blanchedalmond: '#ffebcd',
670
+ blue: '#0000ff',
671
+ blueviolet: '#8a2be2',
672
+ brown: '#a52a2a',
673
+ burlywood: '#deb887',
674
+ cadetblue: '#5f9ea0',
675
+ chartreuse: '#7fff00',
676
+ chocolate: '#d2691e',
677
+ coral: '#ff7f50',
678
+ cornflowerblue: '#6495ed',
679
+ cornsilk: '#fff8dc',
680
+ crimson: '#dc143c',
681
+ cyan: '#00ffff',
682
+ darkblue: '#00008b',
683
+ darkcyan: '#008b8b',
684
+ darkgoldenrod: '#b8860b',
685
+ darkgray: '#a9a9a9',
686
+ darkgrey: '#a9a9a9',
687
+ darkgreen: '#006400',
688
+ darkkhaki: '#bdb76b',
689
+ darkmagenta: '#8b008b',
690
+ darkolivegreen: '#556b2f',
691
+ darkorange: '#ff8c00',
692
+ darkorchid: '#9932cc',
693
+ darkred: '#8b0000',
694
+ darksalmon: '#e9967a',
695
+ darkseagreen: '#8fbc8f',
696
+ darkslateblue: '#483d8b',
697
+ darkslategray: '#2f4f4f',
698
+ darkslategrey: '#2f4f4f',
699
+ darkturquoise: '#00ced1',
700
+ darkviolet: '#9400d3',
701
+ deeppink: '#ff1493',
702
+ deepskyblue: '#00bfff',
703
+ dimgray: '#696969',
704
+ dimgrey: '#696969',
705
+ dodgerblue: '#1e90ff',
706
+ firebrick: '#b22222',
707
+ floralwhite: '#fffaf0',
708
+ forestgreen: '#228b22',
709
+ fuchsia: '#ff00ff',
710
+ gainsboro: '#dcdcdc',
711
+ ghostwhite: '#f8f8ff',
712
+ gold: '#ffd700',
713
+ goldenrod: '#daa520',
714
+ gray: '#808080',
715
+ grey: '#808080',
716
+ green: '#008000',
717
+ greenyellow: '#adff2f',
718
+ honeydew: '#f0fff0',
719
+ hotpink: '#ff69b4',
720
+ indianred: '#cd5c5c',
721
+ indigo: '#4b0082',
722
+ ivory: '#fffff0',
723
+ khaki: '#f0e68c',
724
+ lavender: '#e6e6fa',
725
+ lavenderblush: '#fff0f5',
726
+ lawngreen: '#7cfc00',
727
+ lemonchiffon: '#fffacd',
728
+ lightblue: '#add8e6',
729
+ lightcoral: '#f08080',
730
+ lightcyan: '#e0ffff',
731
+ lightgoldenrodyellow: '#fafad2',
732
+ lightgray: '#d3d3d3',
733
+ lightgrey: '#d3d3d3',
734
+ lightgreen: '#90ee90',
735
+ lightpink: '#ffb6c1',
736
+ lightsalmon: '#ffa07a',
737
+ lightseagreen: '#20b2aa',
738
+ lightskyblue: '#87cefa',
739
+ lightslategray: '#778899',
740
+ lightslategrey: '#778899',
741
+ lightsteelblue: '#b0c4de',
742
+ lightyellow: '#ffffe0',
743
+ lime: '#00ff00',
744
+ limegreen: '#32cd32',
745
+ linen: '#faf0e6',
746
+ magenta: '#ff00ff',
747
+ maroon: '#800000',
748
+ mediumaquamarine: '#66cdaa',
749
+ mediumblue: '#0000cd',
750
+ mediumorchid: '#ba55d3',
751
+ mediumpurple: '#9370db',
752
+ mediumseagreen: '#3cb371',
753
+ mediumslateblue: '#7b68ee',
754
+ mediumspringgreen: '#00fa9a',
755
+ mediumturquoise: '#48d1cc',
756
+ mediumvioletred: '#c71585',
757
+ midnightblue: '#191970',
758
+ mintcream: '#f5fffa',
759
+ mistyrose: '#ffe4e1',
760
+ moccasin: '#ffe4b5',
761
+ navajowhite: '#ffdead',
762
+ navy: '#000080',
763
+ oldlace: '#fdf5e6',
764
+ olive: '#808000',
765
+ olivedrab: '#6b8e23',
766
+ orange: '#ffa500',
767
+ orangered: '#ff4500',
768
+ orchid: '#da70d6',
769
+ palegoldenrod: '#eee8aa',
770
+ palegreen: '#98fb98',
771
+ paleturquoise: '#afeeee',
772
+ palevioletred: '#db7093',
773
+ papayawhip: '#ffefd5',
774
+ peachpuff: '#ffdab9',
775
+ peru: '#cd853f',
776
+ pink: '#ffc0cb',
777
+ plum: '#dda0dd',
778
+ powderblue: '#b0e0e6',
779
+ purple: '#800080',
780
+ rebeccapurple: '#663399',
781
+ red: '#ff0000',
782
+ rosybrown: '#bc8f8f',
783
+ royalblue: '#4169e1',
784
+ saddlebrown: '#8b4513',
785
+ salmon: '#fa8072',
786
+ sandybrown: '#f4a460',
787
+ seagreen: '#2e8b57',
788
+ seashell: '#fff5ee',
789
+ sienna: '#a0522d',
790
+ silver: '#c0c0c0',
791
+ skyblue: '#87ceeb',
792
+ slateblue: '#6a5acd',
793
+ slategray: '#708090',
794
+ slategrey: '#708090',
795
+ snow: '#fffafa',
796
+ springgreen: '#00ff7f',
797
+ steelblue: '#4682b4',
798
+ tan: '#d2b48c',
799
+ teal: '#008080',
800
+ thistle: '#d8bfd8',
801
+ tomato: '#ff6347',
802
+ turquoise: '#40e0d0',
803
+ violet: '#ee82ee',
804
+ wheat: '#f5deb3',
805
+ white: '#ffffff',
806
+ whitesmoke: '#f5f5f5',
807
+ yellow: '#ffff00',
808
+ yellowgreen: '#9acd32',
809
+ };
810
+ /**
811
+ * Note: [💞] Ignore a discrepancy between file name and entity name
812
+ */
813
+
814
+ /**
815
+ * Validates that a channel value is a valid number within the range of 0 to 255.
816
+ * Throws an error if the value is not valid.
817
+ *
818
+ * @param channelName - The name of the channel being validated.
819
+ * @param value - The value of the channel to validate.
820
+ * @throws Will throw an error if the value is not a valid channel number.
821
+ *
822
+ * @private util of `@promptbook/color`
823
+ */
824
+ function checkChannelValue(channelName, value) {
825
+ if (typeof value !== 'number') {
826
+ throw new Error(`${channelName} channel value is not number but ${typeof value}`);
827
+ }
828
+ if (isNaN(value)) {
829
+ throw new Error(`${channelName} channel value is NaN`);
830
+ }
831
+ if (Math.round(value) !== value) {
832
+ throw new Error(`${channelName} channel is not whole number, it is ${value}`);
833
+ }
834
+ if (value < 0) {
835
+ throw new Error(`${channelName} channel is lower than 0, it is ${value}`);
836
+ }
837
+ if (value > 255) {
838
+ throw new Error(`${channelName} channel is greater than 255, it is ${value}`);
839
+ }
840
+ }
841
+ /**
842
+ * TODO: [🧠][🚓] Is/which combination it better to use asserts/check, validate or is utility function?
843
+ */
844
+
845
+ /**
846
+ * Color object represents an RGB color with alpha channel
847
+ *
848
+ * Note: There is no fromObject/toObject because the most logical way to serialize color is as a hex string (#009edd)
849
+ *
850
+ * @public exported from `@promptbook/color`
851
+ */
852
+ class Color {
853
+ /**
854
+ * Creates a new Color instance from miscellaneous formats
855
+ * - It can receive Color instance and just return the same instance
856
+ * - 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%)`
857
+ *
858
+ * 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
859
+ *
860
+ * @param color
861
+ * @returns Color object
862
+ */
863
+ static from(color) {
864
+ if (color instanceof Color) {
865
+ return take(color);
866
+ }
867
+ else if (Color.isColor(color)) {
868
+ return take(color);
869
+ }
870
+ else if (typeof color === 'string') {
871
+ return Color.fromString(color);
872
+ }
873
+ else {
874
+ console.error({ color });
875
+ throw new Error(`Can not create color from given object`);
876
+ }
877
+ }
878
+ /**
879
+ * Creates a new Color instance from miscellaneous string formats
880
+ *
881
+ * @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`,...
882
+ * @returns Color object
883
+ */
884
+ static fromString(color) {
885
+ if (CSS_COLORS[color]) {
886
+ return Color.fromString(CSS_COLORS[color]);
887
+ // -----
888
+ }
889
+ else if (Color.isHexColorString(color)) {
890
+ return Color.fromHex(color);
891
+ // -----
892
+ }
893
+ else if (/^hsl\(\s*(\d+)\s*,\s*(\d+(?:\.\d+)?%)\s*,\s*(\d+(?:\.\d+)?%)\)$/.test(color)) {
894
+ return Color.fromHsl(color);
895
+ // -----
896
+ }
897
+ else if (/^rgb\((\s*[0-9-.%]+\s*,?){3}\)$/.test(color)) {
898
+ // TODO: [0] Should be fromRgbString and fromRgbaString one or two functions
899
+ return Color.fromRgbString(color);
900
+ // -----
901
+ }
902
+ else if (/^rgba\((\s*[0-9-.%]+\s*,?){4}\)$/.test(color)) {
903
+ return Color.fromRgbaString(color);
904
+ // -----
905
+ }
906
+ else {
907
+ throw new Error(`Can not create a new Color instance from string "${color}".`);
908
+ }
909
+ }
910
+ /**
911
+ * Gets common color
912
+ *
913
+ * @param key as a css string like `midnightblue`
914
+ * @returns Color object
915
+ */
916
+ static get(key) {
917
+ if (!CSS_COLORS[key]) {
918
+ throw new Error(`"${key}" is not a common css color.`);
919
+ }
920
+ return Color.fromString(CSS_COLORS[key]);
921
+ }
922
+ /**
923
+ * Creates a new Color instance from average color of given image
924
+ *
925
+ * @param image as a source for example `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUGFdjYJh39z8ABJgCe/ZvAS4AAAAASUVORK5CYII=`
926
+ * @returns Color object
927
+ */
928
+ static async fromImage(image) {
929
+ return Color.fromHex(`#009edd`);
930
+ }
931
+ /**
932
+ * Creates a new Color instance from color in hex format
933
+ *
934
+ * @param color in hex for example `#009edd`, `009edd`, `#555`,...
935
+ * @returns Color object
936
+ */
937
+ static fromHex(hex) {
938
+ const hexOriginal = hex;
939
+ if (hex.startsWith('#')) {
940
+ hex = hex.substring(1);
941
+ }
942
+ if (hex.length === 3) {
943
+ return Color.fromHex3(hex);
944
+ }
945
+ if (hex.length === 6) {
946
+ return Color.fromHex6(hex);
947
+ }
948
+ if (hex.length === 8) {
949
+ return Color.fromHex8(hex);
950
+ }
951
+ throw new Error(`Can not parse color from hex string "${hexOriginal}"`);
952
+ }
953
+ /**
954
+ * Creates a new Color instance from color in hex format with 3 color digits (without alpha channel)
955
+ *
956
+ * @param color in hex for example `09d`
957
+ * @returns Color object
958
+ */
959
+ static fromHex3(hex) {
960
+ const r = parseInt(hex.substr(0, 1), 16) * 16;
961
+ const g = parseInt(hex.substr(1, 1), 16) * 16;
962
+ const b = parseInt(hex.substr(2, 1), 16) * 16;
963
+ return take(new Color(r, g, b));
964
+ }
965
+ /**
966
+ * Creates a new Color instance from color in hex format with 6 color digits (without alpha channel)
967
+ *
968
+ * @param color in hex for example `009edd`
969
+ * @returns Color object
970
+ */
971
+ static fromHex6(hex) {
972
+ const r = parseInt(hex.substr(0, 2), 16);
973
+ const g = parseInt(hex.substr(2, 2), 16);
974
+ const b = parseInt(hex.substr(4, 2), 16);
975
+ return take(new Color(r, g, b));
976
+ }
977
+ /**
978
+ * Creates a new Color instance from color in hex format with 8 color digits (with alpha channel)
979
+ *
980
+ * @param color in hex for example `009edd`
981
+ * @returns Color object
982
+ */
983
+ static fromHex8(hex) {
984
+ const r = parseInt(hex.substr(0, 2), 16);
985
+ const g = parseInt(hex.substr(2, 2), 16);
986
+ const b = parseInt(hex.substr(4, 2), 16);
987
+ const a = parseInt(hex.substr(6, 2), 16);
988
+ return take(new Color(r, g, b, a));
989
+ }
990
+ /**
991
+ * Creates a new Color instance from color in hsl format
992
+ *
993
+ * @param color as a hsl for example `hsl(197.1,100%,43.3%)`
994
+ * @returns Color object
995
+ */
996
+ static fromHsl(hsl) {
997
+ const match = hsl.match(/^hsl\(\s*([0-9.]+)\s*,\s*([0-9.]+)%\s*,\s*([0-9.]+)%\s*\)$/);
998
+ if (!match) {
999
+ throw new Error(`Invalid hsl string format: "${hsl}"`);
1000
+ }
1001
+ const h = parseFloat(match[1]);
1002
+ const s = parseFloat(match[2]) / 100;
1003
+ const l = parseFloat(match[3]) / 100;
1004
+ // HSL to RGB conversion
1005
+ const c = (1 - Math.abs(2 * l - 1)) * s;
1006
+ const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
1007
+ const m = l - c / 2;
1008
+ let r1 = 0, g1 = 0, b1 = 0;
1009
+ if (h >= 0 && h < 60) {
1010
+ r1 = c;
1011
+ g1 = x;
1012
+ b1 = 0;
1013
+ }
1014
+ else if (h >= 60 && h < 120) {
1015
+ r1 = x;
1016
+ g1 = c;
1017
+ b1 = 0;
1018
+ }
1019
+ else if (h >= 120 && h < 180) {
1020
+ r1 = 0;
1021
+ g1 = c;
1022
+ b1 = x;
1023
+ }
1024
+ else if (h >= 180 && h < 240) {
1025
+ r1 = 0;
1026
+ g1 = x;
1027
+ b1 = c;
1028
+ }
1029
+ else if (h >= 240 && h < 300) {
1030
+ r1 = x;
1031
+ g1 = 0;
1032
+ b1 = c;
1033
+ }
1034
+ else if (h >= 300 && h < 360) {
1035
+ r1 = c;
1036
+ g1 = 0;
1037
+ b1 = x;
1038
+ }
1039
+ const r = Math.round((r1 + m) * 255);
1040
+ const g = Math.round((g1 + m) * 255);
1041
+ const b = Math.round((b1 + m) * 255);
1042
+ return take(new Color(r, g, b));
1043
+ }
1044
+ /**
1045
+ * Creates a new Color instance from color in rgb format
1046
+ *
1047
+ * @param color as a rgb for example `rgb(0,158,221)`, `rgb(0%,62%,86.7%)`
1048
+ * @returns Color object
1049
+ */
1050
+ static fromRgbString(rgb) {
1051
+ const match = rgb.match(/^rgb\(\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*\)$/);
1052
+ if (!match) {
1053
+ throw new Error(`Invalid rgb string format: "${rgb}"`);
1054
+ }
1055
+ const parseChannel = (value) => {
1056
+ if (value.endsWith('%')) {
1057
+ // Percentage value
1058
+ const percent = parseFloat(value);
1059
+ return Math.round((percent / 100) * 255);
1060
+ }
1061
+ else {
1062
+ // Numeric value
1063
+ return Math.round(parseFloat(value));
1064
+ }
1065
+ };
1066
+ const r = parseChannel(match[1]);
1067
+ const g = parseChannel(match[2]);
1068
+ const b = parseChannel(match[3]);
1069
+ return take(new Color(r, g, b));
1070
+ }
1071
+ /**
1072
+ * Creates a new Color instance from color in rbga format
1073
+ *
1074
+ * @param color as a rgba for example `rgba(0,158,221,0.5)`, `rgb(0%,62%,86.7%,50%)`
1075
+ * @returns Color object
1076
+ */
1077
+ static fromRgbaString(rgba) {
1078
+ const match = rgba.match(/^rgba\(\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*,\s*([0-9.%-]+)\s*\)$/);
1079
+ if (!match) {
1080
+ throw new Error(`Invalid rgba string format: "${rgba}"`);
1081
+ }
1082
+ const parseChannel = (value) => {
1083
+ if (value.endsWith('%')) {
1084
+ const percent = parseFloat(value);
1085
+ return Math.round((percent / 100) * 255);
1086
+ }
1087
+ else {
1088
+ return Math.round(parseFloat(value));
1089
+ }
1090
+ };
1091
+ const parseAlpha = (value) => {
1092
+ if (value.endsWith('%')) {
1093
+ const percent = parseFloat(value);
1094
+ return Math.round((percent / 100) * 255);
1095
+ }
1096
+ else {
1097
+ const alphaFloat = parseFloat(value);
1098
+ // If alpha is between 0 and 1, treat as float
1099
+ if (alphaFloat <= 1) {
1100
+ return Math.round(alphaFloat * 255);
1101
+ }
1102
+ // Otherwise, treat as 0-255
1103
+ return Math.round(alphaFloat);
1104
+ }
1105
+ };
1106
+ const r = parseChannel(match[1]);
1107
+ const g = parseChannel(match[2]);
1108
+ const b = parseChannel(match[3]);
1109
+ const a = parseAlpha(match[4]);
1110
+ return take(new Color(r, g, b, a));
1111
+ }
1112
+ /**
1113
+ * Creates a new Color for color channels values
1114
+ *
1115
+ * @param red number from 0 to 255
1116
+ * @param green number from 0 to 255
1117
+ * @param blue number from 0 to 255
1118
+ * @param alpha number from 0 (transparent) to 255 (opaque = default)
1119
+ * @returns Color object
1120
+ */
1121
+ static fromValues(red, green, blue, alpha = 255) {
1122
+ return take(new Color(red, green, blue, alpha));
1123
+ }
1124
+ /**
1125
+ * Checks if the given value is a valid Color object.
1126
+ *
1127
+ * @param {unknown} value - The value to check.
1128
+ * @return {value is WithTake<Color>} Returns true if the value is a valid Color object, false otherwise.
1129
+ */
1130
+ static isColor(value) {
1131
+ if (typeof value !== 'object') {
1132
+ return false;
1133
+ }
1134
+ if (value === null) {
1135
+ return false;
1136
+ }
1137
+ if (typeof value.red !== 'number' ||
1138
+ typeof value.green !== 'number' ||
1139
+ typeof value.blue !== 'number' ||
1140
+ typeof value.alpha !== 'number') {
1141
+ return false;
1142
+ }
1143
+ if (typeof value.then !== 'function') {
1144
+ return false;
1145
+ }
1146
+ return true;
1147
+ }
1148
+ /**
1149
+ * Checks if the given value is a valid hex color string
1150
+ *
1151
+ * @param value - value to check
1152
+ * @returns true if the value is a valid hex color string (e.g., `#009edd`, `#fff`, etc.)
1153
+ */
1154
+ static isHexColorString(value) {
1155
+ return typeof value === 'string' && /^#(?:[0-9a-fA-F]{3}){1,2}$/.test(value);
1156
+ }
1157
+ /**
1158
+ * Creates new Color object
1159
+ *
1160
+ * Note: Consider using one of static methods like `from` or `fromString`
1161
+ *
1162
+ * @param red number from 0 to 255
1163
+ * @param green number from 0 to 255
1164
+ * @param blue number from 0 to 255
1165
+ * @param alpha number from 0 (transparent) to 255 (opaque)
1166
+ */
1167
+ constructor(red, green, blue, alpha = 255) {
1168
+ this.red = red;
1169
+ this.green = green;
1170
+ this.blue = blue;
1171
+ this.alpha = alpha;
1172
+ checkChannelValue('Red', red);
1173
+ checkChannelValue('Green', green);
1174
+ checkChannelValue('Blue', blue);
1175
+ checkChannelValue('Alpha', alpha);
1176
+ }
1177
+ /**
1178
+ * Shortcut for `red` property
1179
+ * Number from 0 to 255
1180
+ * @alias red
1181
+ */
1182
+ get r() {
1183
+ return this.red;
1184
+ }
1185
+ /**
1186
+ * Shortcut for `green` property
1187
+ * Number from 0 to 255
1188
+ * @alias green
1189
+ */
1190
+ get g() {
1191
+ return this.green;
1192
+ }
1193
+ /**
1194
+ * Shortcut for `blue` property
1195
+ * Number from 0 to 255
1196
+ * @alias blue
1197
+ */
1198
+ get b() {
1199
+ return this.blue;
1200
+ }
1201
+ /**
1202
+ * Shortcut for `alpha` property
1203
+ * Number from 0 (transparent) to 255 (opaque)
1204
+ * @alias alpha
1205
+ */
1206
+ get a() {
1207
+ return this.alpha;
1208
+ }
1209
+ /**
1210
+ * Shortcut for `alpha` property
1211
+ * Number from 0 (transparent) to 255 (opaque)
1212
+ * @alias alpha
1213
+ */
1214
+ get opacity() {
1215
+ return this.alpha;
1216
+ }
1217
+ /**
1218
+ * Shortcut for 1-`alpha` property
1219
+ */
1220
+ get transparency() {
1221
+ return 255 - this.alpha;
1222
+ }
1223
+ clone() {
1224
+ return take(new Color(this.red, this.green, this.blue, this.alpha));
1225
+ }
1226
+ toString() {
1227
+ return this.toHex();
1228
+ }
1229
+ toHex() {
1230
+ if (this.alpha === 255) {
1231
+ return `#${this.red.toString(16).padStart(2, '0')}${this.green.toString(16).padStart(2, '0')}${this.blue
1232
+ .toString(16)
1233
+ .padStart(2, '0')}`;
1234
+ }
1235
+ else {
1236
+ return `#${this.red.toString(16).padStart(2, '0')}${this.green.toString(16).padStart(2, '0')}${this.blue
1237
+ .toString(16)
1238
+ .padStart(2, '0')}${this.alpha.toString(16).padStart(2, '0')}`;
1239
+ }
1240
+ }
1241
+ toRgb() {
1242
+ if (this.alpha === 255) {
1243
+ return `rgb(${this.red}, ${this.green}, ${this.blue})`;
1244
+ }
1245
+ else {
1246
+ return `rgba(${this.red}, ${this.green}, ${this.blue}, ${Math.round((this.alpha / 255) * 100)}%)`;
1247
+ }
1248
+ }
1249
+ toHsl() {
1250
+ throw new Error(`Getting HSL is not implemented`);
1251
+ }
1252
+ }
1253
+ /**
1254
+ * TODO: [🥻] Split Color class and color type
1255
+ * TODO: For each method a corresponding static method should be created
1256
+ * Like clone can be done by color.clone() OR Color.clone(color)
1257
+ * TODO: Probably as an independent LIB OR add to LIB xyzt (ask @roseckyj)
1258
+ * TODO: !! Transfer back to Collboard (whole directory)
1259
+ * TODO: Maybe [🏌️‍♂️] change ACRY toString => (toHex) toRgb when there will be toRgb and toRgba united
1260
+ * TODO: Convert getters to methods - getters only for values
1261
+ * TODO: Write tests
1262
+ * TODO: Getters for alpha, opacity, transparency, r, b, g, h, s, l, a,...
1263
+ * TODO: [0] Should be fromRgbString and fromRgbaString one or two functions + one or two regex
1264
+ * TODO: Use rgb, rgba, hsl for testing and parsing with the same regex
1265
+ * TODO: Regex for rgb, rgba, hsl does not support all options like deg, rad, turn,...
1266
+ * TODO: Convolution matrix
1267
+ * TODO: Maybe connect with textures
1268
+ */
1269
+
1270
+ /**
1271
+ * Converts HSL values to RGB values
1272
+ *
1273
+ * @param hue [0-1]
1274
+ * @param saturation [0-1]
1275
+ * @param lightness [0-1]
1276
+ * @returns [red, green, blue] [0-255]
1277
+ *
1278
+ * @private util of `@promptbook/color`
1279
+ */
1280
+ function hslToRgb(hue, saturation, lightness) {
1281
+ let red;
1282
+ let green;
1283
+ let blue;
1284
+ if (saturation === 0) {
1285
+ // achromatic
1286
+ red = lightness;
1287
+ green = lightness;
1288
+ blue = lightness;
1289
+ }
1290
+ else {
1291
+ // TODO: Extract to separate function
1292
+ const hue2rgb = (p, q, t) => {
1293
+ if (t < 0)
1294
+ t += 1;
1295
+ if (t > 1)
1296
+ t -= 1;
1297
+ if (t < 1 / 6)
1298
+ return p + (q - p) * 6 * t;
1299
+ if (t < 1 / 2)
1300
+ return q;
1301
+ if (t < 2 / 3)
1302
+ return p + (q - p) * (2 / 3 - t) * 6;
1303
+ return p;
1304
+ };
1305
+ const q = lightness < 0.5 ? lightness * (1 + saturation) : lightness + saturation - lightness * saturation;
1306
+ const p = 2 * lightness - q;
1307
+ red = hue2rgb(p, q, hue + 1 / 3);
1308
+ green = hue2rgb(p, q, hue);
1309
+ blue = hue2rgb(p, q, hue - 1 / 3);
1310
+ }
1311
+ return [Math.round(red * 255), Math.round(green * 255), Math.round(blue * 255)];
1312
+ }
1313
+ /**
1314
+ * TODO: Properly name all used internal variables
1315
+ */
1316
+
1317
+ /**
1318
+ * Converts RGB values to HSL values
1319
+ *
1320
+ * @param red [0-255]
1321
+ * @param green [0-255]
1322
+ * @param blue [0-255]
1323
+ * @returns [hue, saturation, lightness] [0-1]
1324
+ *
1325
+ * @private util of `@promptbook/color`
1326
+ */
1327
+ function rgbToHsl(red, green, blue) {
1328
+ red /= 255;
1329
+ green /= 255;
1330
+ blue /= 255;
1331
+ const max = Math.max(red, green, blue);
1332
+ const min = Math.min(red, green, blue);
1333
+ let hue;
1334
+ let saturation;
1335
+ const lightness = (max + min) / 2;
1336
+ if (max === min) {
1337
+ // achromatic
1338
+ hue = 0;
1339
+ saturation = 0;
1340
+ }
1341
+ else {
1342
+ const d = max - min;
1343
+ saturation = lightness > 0.5 ? d / (2 - max - min) : d / (max + min);
1344
+ switch (max) {
1345
+ case red:
1346
+ hue = (green - blue) / d + (green < blue ? 6 : 0);
1347
+ break;
1348
+ case green:
1349
+ hue = (blue - red) / d + 2;
1350
+ break;
1351
+ case blue:
1352
+ hue = (red - green) / d + 4;
1353
+ break;
1354
+ default:
1355
+ hue = 0;
1356
+ }
1357
+ hue /= 6;
1358
+ }
1359
+ return [hue, saturation, lightness];
1360
+ }
1361
+ /**
1362
+ * TODO: Properly name all used internal variables
1363
+ */
1364
+
1365
+ /**
1366
+ * Makes color transformer which lighten the given color
1367
+ *
1368
+ * @param amount from 0 to 1
1369
+ *
1370
+ * @public exported from `@promptbook/color`
1371
+ */
1372
+ function lighten(amount) {
1373
+ return ({ red, green, blue, alpha }) => {
1374
+ const [h, s, lInitial] = rgbToHsl(red, green, blue);
1375
+ let l = lInitial + amount;
1376
+ l = Math.max(0, Math.min(l, 1)); // Replace lodash clamp with Math.max and Math.min
1377
+ const [r, g, b] = hslToRgb(h, s, l);
1378
+ return Color.fromValues(r, g, b, alpha);
1379
+ };
1380
+ }
1381
+ /**
1382
+ * TODO: Maybe implement by mix+hsl
1383
+ */
1384
+
1385
+ /**
1386
+ * Calculates distance between two colors
1387
+ *
1388
+ * @param color1 first color
1389
+ * @param color2 second color
1390
+ *
1391
+ * Note: This function is inefficient. Use colorDistanceSquared instead if possible.
1392
+ *
1393
+ * @public exported from `@promptbook/color`
1394
+ */
1395
+ /**
1396
+ * Calculates distance between two colors without square root
1397
+ *
1398
+ * @param color1 first color
1399
+ * @param color2 second color
1400
+ *
1401
+ * @public exported from `@promptbook/color`
1402
+ */
1403
+ function colorDistanceSquared(color1, color2) {
1404
+ const rmean = (color1.red + color2.red) / 2;
1405
+ const r = color1.red - color2.red;
1406
+ const g = color1.green - color2.green;
1407
+ const b = color1.blue - color2.blue;
1408
+ const weightR = 2 + rmean / 256;
1409
+ const weightG = 4.0;
1410
+ const weightB = 2 + (255 - rmean) / 256;
1411
+ const distance = weightR * r * r + weightG * g * g + weightB * b * b;
1412
+ return distance;
1413
+ }
1414
+
1415
+ /**
1416
+ * Makes color transformer which finds the nearest color from the given list
1417
+ *
1418
+ * @param colors array of colors to choose from
1419
+ *
1420
+ * @public exported from `@promptbook/color`
1421
+ */
1422
+ function nearest(...colors) {
1423
+ return (color) => {
1424
+ const distances = colors.map((c) => colorDistanceSquared(c, color));
1425
+ const minDistance = Math.min(...distances);
1426
+ const minIndex = distances.indexOf(minDistance);
1427
+ const nearestColor = colors[minIndex];
1428
+ return nearestColor;
1429
+ };
1430
+ }
1431
+
1432
+ /**
1433
+ * Color transformer which returns the negative color
1434
+ *
1435
+ * @public exported from `@promptbook/color`
1436
+ */
1437
+ function negative(color) {
1438
+ const r = 255 - color.red;
1439
+ const g = 255 - color.green;
1440
+ const b = 255 - color.blue;
1441
+ return Color.fromValues(r, g, b, color.alpha);
1442
+ }
1443
+
1444
+ /**
1445
+ * Makes color transformer which finds the furthest color from the given list
1446
+ *
1447
+ * @param colors array of colors to choose from
1448
+ *
1449
+ * @public exported from `@promptbook/color`
1450
+ */
1451
+ function furthest(...colors) {
1452
+ return (color) => {
1453
+ const furthestColor = negative(nearest(...colors.map(negative))(color));
1454
+ return furthestColor;
1455
+ };
1456
+ }
1457
+ /**
1458
+ * Makes color transformer which finds the best text color (black or white) for the given background color
1459
+ *
1460
+ * @public exported from `@promptbook/color`
1461
+ */
1462
+ furthest(Color.get('white'), Color.from('black'));
1463
+
1464
+ /**
1465
+ * Makes color transformer which returns a grayscale version of the color
1466
+ *
1467
+ * @param amount from 0 to 1
1468
+ *
1469
+ * @public exported from `@promptbook/color`
1470
+ */
1471
+ function grayscale(amount) {
1472
+ return ({ red, green, blue, alpha }) => {
1473
+ const average = (red + green + blue) / 3;
1474
+ red = Math.round(average * amount + red * (1 - amount));
1475
+ green = Math.round(average * amount + green * (1 - amount));
1476
+ blue = Math.round(average * amount + blue * (1 - amount));
1477
+ return Color.fromValues(red, green, blue, alpha);
1478
+ };
1479
+ }
1480
+
1481
+ /**
1482
+ * Makes color transformer which saturate the given color
1483
+ *
1484
+ * @param amount from -1 to 1
1485
+ *
1486
+ * @public exported from `@promptbook/color`
1487
+ */
1488
+ function saturate(amount) {
1489
+ return ({ red, green, blue, alpha }) => {
1490
+ const [h, sInitial, l] = rgbToHsl(red, green, blue);
1491
+ let s = sInitial + amount;
1492
+ s = Math.max(0, Math.min(s, 1));
1493
+ const [r, g, b] = hslToRgb(h, s, l);
1494
+ return Color.fromValues(r, g, b, alpha);
1495
+ };
1496
+ }
1497
+ /**
1498
+ * TODO: Maybe implement by mix+hsl
1499
+ */
1500
+
612
1501
  /**
613
1502
  * Name for the Promptbook
614
1503
  *
@@ -629,6 +1518,32 @@
629
1518
  * @public exported from `@promptbook/core`
630
1519
  */
631
1520
  const ADMIN_GITHUB_NAME = 'hejny';
1521
+ // <- TODO: [🐊] Pick the best claim
1522
+ /**
1523
+ * Color of the Promptbook
1524
+ *
1525
+ * TODO: [🗽] Unite branding and make single place for it
1526
+ *
1527
+ * @public exported from `@promptbook/core`
1528
+ */
1529
+ const PROMPTBOOK_COLOR = Color.fromHex('#79EAFD');
1530
+ // <- TODO: [🧠] Using `Color` here increases the package size approx 3kb, maybe remove it
1531
+ /**
1532
+ * Dark color of the Promptbook
1533
+ *
1534
+ * TODO: [🗽] Unite branding and make single place for it
1535
+ *
1536
+ * @public exported from `@promptbook/core`
1537
+ */
1538
+ PROMPTBOOK_COLOR.then(lighten(0.1)).then(saturate(0.9)).then(grayscale(0.9));
1539
+ /**
1540
+ * Color of the user (in chat)
1541
+ *
1542
+ * TODO: [🗽] Unite branding and make single place for it
1543
+ *
1544
+ * @public exported from `@promptbook/core`
1545
+ */
1546
+ Color.fromHex('#1D4ED8');
632
1547
  // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
633
1548
  /**
634
1549
  * The maximum number of iterations for a loops