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