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