@promptbook/anthropic-claude 0.102.0-11 → 0.102.0-12

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.
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-11';
28
+ const PROMPTBOOK_ENGINE_VERSION = '0.102.0-12';
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,886 @@
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 (/^#(?:[0-9a-fA-F]{3}){1,2}$/.test(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
+ * Creates new Color object
614
+ *
615
+ * Note: Consider using one of static methods like `from` or `fromString`
616
+ *
617
+ * @param red number from 0 to 255
618
+ * @param green number from 0 to 255
619
+ * @param blue number from 0 to 255
620
+ * @param alpha number from 0 (transparent) to 255 (opaque)
621
+ */
622
+ constructor(red, green, blue, alpha = 255) {
623
+ this.red = red;
624
+ this.green = green;
625
+ this.blue = blue;
626
+ this.alpha = alpha;
627
+ checkChannelValue('Red', red);
628
+ checkChannelValue('Green', green);
629
+ checkChannelValue('Blue', blue);
630
+ checkChannelValue('Alpha', alpha);
631
+ }
632
+ /**
633
+ * Shortcut for `red` property
634
+ * Number from 0 to 255
635
+ * @alias red
636
+ */
637
+ get r() {
638
+ return this.red;
639
+ }
640
+ /**
641
+ * Shortcut for `green` property
642
+ * Number from 0 to 255
643
+ * @alias green
644
+ */
645
+ get g() {
646
+ return this.green;
647
+ }
648
+ /**
649
+ * Shortcut for `blue` property
650
+ * Number from 0 to 255
651
+ * @alias blue
652
+ */
653
+ get b() {
654
+ return this.blue;
655
+ }
656
+ /**
657
+ * Shortcut for `alpha` property
658
+ * Number from 0 (transparent) to 255 (opaque)
659
+ * @alias alpha
660
+ */
661
+ get a() {
662
+ return this.alpha;
663
+ }
664
+ /**
665
+ * Shortcut for `alpha` property
666
+ * Number from 0 (transparent) to 255 (opaque)
667
+ * @alias alpha
668
+ */
669
+ get opacity() {
670
+ return this.alpha;
671
+ }
672
+ /**
673
+ * Shortcut for 1-`alpha` property
674
+ */
675
+ get transparency() {
676
+ return 255 - this.alpha;
677
+ }
678
+ clone() {
679
+ return take(new Color(this.red, this.green, this.blue, this.alpha));
680
+ }
681
+ toString() {
682
+ return this.toHex();
683
+ }
684
+ toHex() {
685
+ if (this.alpha === 255) {
686
+ return `#${this.red.toString(16).padStart(2, '0')}${this.green.toString(16).padStart(2, '0')}${this.blue
687
+ .toString(16)
688
+ .padStart(2, '0')}`;
689
+ }
690
+ else {
691
+ return `#${this.red.toString(16).padStart(2, '0')}${this.green.toString(16).padStart(2, '0')}${this.blue
692
+ .toString(16)
693
+ .padStart(2, '0')}${this.alpha.toString(16).padStart(2, '0')}`;
694
+ }
695
+ }
696
+ toRgb() {
697
+ if (this.alpha === 255) {
698
+ return `rgb(${this.red}, ${this.green}, ${this.blue})`;
699
+ }
700
+ else {
701
+ return `rgba(${this.red}, ${this.green}, ${this.blue}, ${Math.round((this.alpha / 255) * 100)}%)`;
702
+ }
703
+ }
704
+ toHsl() {
705
+ throw new Error(`Getting HSL is not implemented`);
706
+ }
707
+ }
708
+ /**
709
+ * TODO: [🥻] Split Color class and color type
710
+ * TODO: For each method a corresponding static method should be created
711
+ * Like clone can be done by color.clone() OR Color.clone(color)
712
+ * TODO: Probably as an independent LIB OR add to LIB xyzt (ask @roseckyj)
713
+ * TODO: !! Transfer back to Collboard (whole directory)
714
+ * TODO: Maybe [🏌️‍♂️] change ACRY toString => (toHex) toRgb when there will be toRgb and toRgba united
715
+ * TODO: Convert getters to methods - getters only for values
716
+ * TODO: Write tests
717
+ * TODO: Getters for alpha, opacity, transparency, r, b, g, h, s, l, a,...
718
+ * TODO: [0] Should be fromRgbString and fromRgbaString one or two functions + one or two regex
719
+ * TODO: Use rgb, rgba, hsl for testing and parsing with the same regex
720
+ * TODO: Regex for rgb, rgba, hsl does not support all options like deg, rad, turn,...
721
+ * TODO: Convolution matrix
722
+ * TODO: Maybe connect with textures
723
+ */
724
+
725
+ /**
726
+ * Converts HSL values to RGB values
727
+ *
728
+ * @param hue [0-1]
729
+ * @param saturation [0-1]
730
+ * @param lightness [0-1]
731
+ * @returns [red, green, blue] [0-255]
732
+ *
733
+ * @private util of `@promptbook/color`
734
+ */
735
+ function hslToRgb(hue, saturation, lightness) {
736
+ let red;
737
+ let green;
738
+ let blue;
739
+ if (saturation === 0) {
740
+ // achromatic
741
+ red = lightness;
742
+ green = lightness;
743
+ blue = lightness;
744
+ }
745
+ else {
746
+ // TODO: Extract to separate function
747
+ const hue2rgb = (p, q, t) => {
748
+ if (t < 0)
749
+ t += 1;
750
+ if (t > 1)
751
+ t -= 1;
752
+ if (t < 1 / 6)
753
+ return p + (q - p) * 6 * t;
754
+ if (t < 1 / 2)
755
+ return q;
756
+ if (t < 2 / 3)
757
+ return p + (q - p) * (2 / 3 - t) * 6;
758
+ return p;
759
+ };
760
+ const q = lightness < 0.5 ? lightness * (1 + saturation) : lightness + saturation - lightness * saturation;
761
+ const p = 2 * lightness - q;
762
+ red = hue2rgb(p, q, hue + 1 / 3);
763
+ green = hue2rgb(p, q, hue);
764
+ blue = hue2rgb(p, q, hue - 1 / 3);
765
+ }
766
+ return [Math.round(red * 255), Math.round(green * 255), Math.round(blue * 255)];
767
+ }
768
+ /**
769
+ * TODO: Properly name all used internal variables
770
+ */
771
+
772
+ /**
773
+ * Converts RGB values to HSL values
774
+ *
775
+ * @param red [0-255]
776
+ * @param green [0-255]
777
+ * @param blue [0-255]
778
+ * @returns [hue, saturation, lightness] [0-1]
779
+ *
780
+ * @private util of `@promptbook/color`
781
+ */
782
+ function rgbToHsl(red, green, blue) {
783
+ red /= 255;
784
+ green /= 255;
785
+ blue /= 255;
786
+ const max = Math.max(red, green, blue);
787
+ const min = Math.min(red, green, blue);
788
+ let hue;
789
+ let saturation;
790
+ const lightness = (max + min) / 2;
791
+ if (max === min) {
792
+ // achromatic
793
+ hue = 0;
794
+ saturation = 0;
795
+ }
796
+ else {
797
+ const d = max - min;
798
+ saturation = lightness > 0.5 ? d / (2 - max - min) : d / (max + min);
799
+ switch (max) {
800
+ case red:
801
+ hue = (green - blue) / d + (green < blue ? 6 : 0);
802
+ break;
803
+ case green:
804
+ hue = (blue - red) / d + 2;
805
+ break;
806
+ case blue:
807
+ hue = (red - green) / d + 4;
808
+ break;
809
+ default:
810
+ hue = 0;
811
+ }
812
+ hue /= 6;
813
+ }
814
+ return [hue, saturation, lightness];
815
+ }
816
+ /**
817
+ * TODO: Properly name all used internal variables
818
+ */
819
+
820
+ /**
821
+ * Makes color transformer which lighten the given color
822
+ *
823
+ * @param amount from 0 to 1
824
+ *
825
+ * @public exported from `@promptbook/color`
826
+ */
827
+ function lighten(amount) {
828
+ return ({ red, green, blue, alpha }) => {
829
+ const [h, s, lInitial] = rgbToHsl(red, green, blue);
830
+ let l = lInitial + amount;
831
+ l = Math.max(0, Math.min(l, 1)); // Replace lodash clamp with Math.max and Math.min
832
+ const [r, g, b] = hslToRgb(h, s, l);
833
+ return Color.fromValues(r, g, b, alpha);
834
+ };
835
+ }
836
+ /**
837
+ * TODO: Maybe implement by mix+hsl
838
+ */
839
+
840
+ /**
841
+ * Calculates distance between two colors
842
+ *
843
+ * @param color1 first color
844
+ * @param color2 second color
845
+ *
846
+ * Note: This function is inefficient. Use colorDistanceSquared instead if possible.
847
+ *
848
+ * @public exported from `@promptbook/color`
849
+ */
850
+ /**
851
+ * Calculates distance between two colors without square root
852
+ *
853
+ * @param color1 first color
854
+ * @param color2 second color
855
+ *
856
+ * @public exported from `@promptbook/color`
857
+ */
858
+ function colorDistanceSquared(color1, color2) {
859
+ const rmean = (color1.red + color2.red) / 2;
860
+ const r = color1.red - color2.red;
861
+ const g = color1.green - color2.green;
862
+ const b = color1.blue - color2.blue;
863
+ const weightR = 2 + rmean / 256;
864
+ const weightG = 4.0;
865
+ const weightB = 2 + (255 - rmean) / 256;
866
+ const distance = weightR * r * r + weightG * g * g + weightB * b * b;
867
+ return distance;
868
+ }
869
+
870
+ /**
871
+ * Makes color transformer which finds the nearest color from the given list
872
+ *
873
+ * @param colors array of colors to choose from
874
+ *
875
+ * @public exported from `@promptbook/color`
876
+ */
877
+ function nearest(...colors) {
878
+ return (color) => {
879
+ const distances = colors.map((c) => colorDistanceSquared(c, color));
880
+ const minDistance = Math.min(...distances);
881
+ const minIndex = distances.indexOf(minDistance);
882
+ const nearestColor = colors[minIndex];
883
+ return nearestColor;
884
+ };
885
+ }
886
+
887
+ /**
888
+ * Color transformer which returns the negative color
889
+ *
890
+ * @public exported from `@promptbook/color`
891
+ */
892
+ function negative(color) {
893
+ const r = 255 - color.red;
894
+ const g = 255 - color.green;
895
+ const b = 255 - color.blue;
896
+ return Color.fromValues(r, g, b, color.alpha);
897
+ }
898
+
899
+ /**
900
+ * Makes color transformer which finds the furthest color from the given list
901
+ *
902
+ * @param colors array of colors to choose from
903
+ *
904
+ * @public exported from `@promptbook/color`
905
+ */
906
+ function furthest(...colors) {
907
+ return (color) => {
908
+ const furthestColor = negative(nearest(...colors.map(negative))(color));
909
+ return furthestColor;
910
+ };
911
+ }
912
+ /**
913
+ * Makes color transformer which finds the best text color (black or white) for the given background color
914
+ *
915
+ * @public exported from `@promptbook/color`
916
+ */
917
+ furthest(Color.get('white'), Color.from('black'));
918
+
919
+ /**
920
+ * Makes color transformer which returns a grayscale version of the color
921
+ *
922
+ * @param amount from 0 to 1
923
+ *
924
+ * @public exported from `@promptbook/color`
925
+ */
926
+ function grayscale(amount) {
927
+ return ({ red, green, blue, alpha }) => {
928
+ const average = (red + green + blue) / 3;
929
+ red = Math.round(average * amount + red * (1 - amount));
930
+ green = Math.round(average * amount + green * (1 - amount));
931
+ blue = Math.round(average * amount + blue * (1 - amount));
932
+ return Color.fromValues(red, green, blue, alpha);
933
+ };
934
+ }
935
+
936
+ /**
937
+ * Makes color transformer which saturate the given color
938
+ *
939
+ * @param amount from -1 to 1
940
+ *
941
+ * @public exported from `@promptbook/color`
942
+ */
943
+ function saturate(amount) {
944
+ return ({ red, green, blue, alpha }) => {
945
+ const [h, sInitial, l] = rgbToHsl(red, green, blue);
946
+ let s = sInitial + amount;
947
+ s = Math.max(0, Math.min(s, 1));
948
+ const [r, g, b] = hslToRgb(h, s, l);
949
+ return Color.fromValues(r, g, b, alpha);
950
+ };
951
+ }
952
+ /**
953
+ * TODO: Maybe implement by mix+hsl
954
+ */
955
+
76
956
  /**
77
957
  * Name for the Promptbook
78
958
  *
@@ -93,6 +973,32 @@
93
973
  * @public exported from `@promptbook/core`
94
974
  */
95
975
  const ADMIN_GITHUB_NAME = 'hejny';
976
+ // <- TODO: [🐊] Pick the best claim
977
+ /**
978
+ * Color of the Promptbook
979
+ *
980
+ * TODO: [🗽] Unite branding and make single place for it
981
+ *
982
+ * @public exported from `@promptbook/core`
983
+ */
984
+ const PROMPTBOOK_COLOR = Color.fromHex('#79EAFD');
985
+ // <- TODO: !!! How much is this adding to package size of @promptbook/core
986
+ /**
987
+ * Dark color of the Promptbook
988
+ *
989
+ * TODO: [🗽] Unite branding and make single place for it
990
+ *
991
+ * @public exported from `@promptbook/core`
992
+ */
993
+ PROMPTBOOK_COLOR.then(lighten(0.1)).then(saturate(0.9)).then(grayscale(0.9));
994
+ /**
995
+ * Color of the user (in chat)
996
+ *
997
+ * TODO: [🗽] Unite branding and make single place for it
998
+ *
999
+ * @public exported from `@promptbook/core`
1000
+ */
1001
+ Color.fromHex('#1D4ED8');
96
1002
  // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
97
1003
  /**
98
1004
  * The maximum number of iterations for a loops