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