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