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