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