@promptbook/anthropic-claude 0.102.0-9 → 0.102.0

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