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