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