@promptbook/javascript 0.102.0-11 → 0.102.0-16

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