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