@reliverse/relico 1.3.0 → 1.3.1

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/bin/mod.js DELETED
@@ -1,455 +0,0 @@
1
- const ESC = "\x1B[";
2
- const RESET = `${ESC}0m`;
3
- const OP_SYMBOL = Symbol("re.ops");
4
- let CURRENT_LEVEL = 3;
5
- export const setColorLevel = (level) => {
6
- if (level !== 0 && level !== 1 && level !== 2 && level !== 3) {
7
- throw new Error("Invalid color level");
8
- }
9
- CURRENT_LEVEL = level;
10
- };
11
- const clampByte = (n) => {
12
- if (!Number.isFinite(n)) return 0;
13
- if (n < 0) return 0;
14
- if (n > 255) return 255;
15
- return Math.round(n);
16
- };
17
- const clampPct = (n) => {
18
- if (!Number.isFinite(n)) return 0;
19
- if (n < 0) return 0;
20
- if (n > 100) return 100;
21
- return n;
22
- };
23
- const hexRe = /^#?([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/;
24
- const hexToRgb = (hex2) => {
25
- const len = hex2.length;
26
- const start = hex2[0] === "#" ? 1 : 0;
27
- const raw = hex2.slice(start);
28
- const rawLen = raw.length;
29
- if (rawLen !== 3 && rawLen !== 6) return null;
30
- for (let i = 0; i < rawLen; i++) {
31
- const c = raw.charCodeAt(i);
32
- if (!(c >= 48 && c <= 57 || c >= 65 && c <= 70 || c >= 97 && c <= 102)) {
33
- return null;
34
- }
35
- }
36
- if (rawLen === 3) {
37
- const r = parseInt(raw[0] + raw[0], 16);
38
- const g = parseInt(raw[1] + raw[1], 16);
39
- const b = parseInt(raw[2] + raw[2], 16);
40
- return { r, g, b };
41
- }
42
- return {
43
- r: parseInt(raw.slice(0, 2), 16),
44
- g: parseInt(raw.slice(2, 4), 16),
45
- b: parseInt(raw.slice(4, 6), 16)
46
- };
47
- };
48
- const hslToRgb = (h, s, l) => {
49
- const H = (h % 360 + 360) % 360;
50
- const S = Math.min(100, Math.max(0, s)) / 100;
51
- const L = Math.min(100, Math.max(0, l)) / 100;
52
- if (S === 0) {
53
- const v = Math.round(L * 255);
54
- return { r: v, g: v, b: v };
55
- }
56
- const c = (1 - Math.abs(2 * L - 1)) * S;
57
- const x = c * (1 - Math.abs(H / 60 % 2 - 1));
58
- const m = L - c / 2;
59
- const sector = Math.floor(H / 60) % 6;
60
- const rgbValues = [
61
- [c, x, 0],
62
- [x, c, 0],
63
- [0, c, x],
64
- [0, x, c],
65
- [x, 0, c],
66
- [c, 0, x]
67
- ][sector];
68
- return {
69
- r: Math.round((rgbValues[0] + m) * 255),
70
- g: Math.round((rgbValues[1] + m) * 255),
71
- b: Math.round((rgbValues[2] + m) * 255)
72
- };
73
- };
74
- const BASIC8 = [
75
- { r: 0, g: 0, b: 0 },
76
- // black
77
- { r: 205, g: 0, b: 0 },
78
- // red
79
- { r: 0, g: 205, b: 0 },
80
- // green
81
- { r: 205, g: 205, b: 0 },
82
- // yellow
83
- { r: 0, g: 0, b: 238 },
84
- // blue
85
- { r: 205, g: 0, b: 205 },
86
- // magenta
87
- { r: 0, g: 205, b: 205 },
88
- // cyan
89
- { r: 229, g: 229, b: 229 }
90
- // white (light gray)
91
- ];
92
- const sgr = (codes) => `${ESC}${codes.join(";")}m`;
93
- const nearestBasicIndex = (rgb2) => {
94
- let best = 0;
95
- let bestDist = Number.POSITIVE_INFINITY;
96
- for (let i = 0; i < BASIC8.length; i++) {
97
- const c = BASIC8[i];
98
- const dr = c.r - rgb2.r;
99
- const dg = c.g - rgb2.g;
100
- const db = c.b - rgb2.b;
101
- const d = dr * dr + dg * dg + db * db;
102
- if (d < bestDist) {
103
- bestDist = d;
104
- best = i;
105
- }
106
- }
107
- return best;
108
- };
109
- const rgbToAnsi256 = (rgb2) => {
110
- if (rgb2.r === rgb2.g && rgb2.g === rgb2.b) {
111
- if (rgb2.r < 8) return 16;
112
- if (rgb2.r > 248) return 231;
113
- const step = Math.round((rgb2.r - 8) / 247 * 24);
114
- return 232 + step;
115
- }
116
- const r = Math.round(rgb2.r / 255 * 5);
117
- const g = Math.round(rgb2.g / 255 * 5);
118
- const b = Math.round(rgb2.b / 255 * 5);
119
- return 16 + 36 * r + 6 * g + b;
120
- };
121
- const CORE_COLORS = {
122
- black: "#000000",
123
- red: "#ff0000",
124
- green: "#00ff00",
125
- yellow: "#ffff00",
126
- blue: "#0000ff",
127
- magenta: "#ff00ff",
128
- cyan: "#00ffff",
129
- white: "#ffffff",
130
- gray: "#808080"
131
- };
132
- const EXTENDED_COLORS = {
133
- orange: "#ffa500",
134
- pink: "#ffc0cb",
135
- purple: "#800080",
136
- teal: "#008080",
137
- lime: "#00ff00",
138
- brown: "#a52a2a",
139
- navy: "#000080",
140
- maroon: "#800000",
141
- olive: "#808000",
142
- silver: "#c0c0c0"
143
- };
144
- let NAMED_HEX;
145
- const getNamedColors = () => {
146
- if (NAMED_HEX) return NAMED_HEX;
147
- NAMED_HEX = { ...CORE_COLORS, ...EXTENDED_COLORS };
148
- return NAMED_HEX;
149
- };
150
- let GRAYS;
151
- const getGrayColors = () => {
152
- if (GRAYS) return GRAYS;
153
- GRAYS = {};
154
- for (let i = 1; i <= 9; i++) {
155
- const value = Math.round(26 * i - 10);
156
- const hex2 = value.toString(16).padStart(2, "0");
157
- const key = `gray${i}0`;
158
- GRAYS[key] = `#${hex2}${hex2}${hex2}`;
159
- }
160
- return GRAYS;
161
- };
162
- const mixWithWhite = (rgb2, factor) => {
163
- const t = factor;
164
- return {
165
- r: clampByte(rgb2.r * (1 - t) + 255 * t),
166
- g: clampByte(rgb2.g * (1 - t) + 255 * t),
167
- b: clampByte(rgb2.b * (1 - t) + 255 * t)
168
- };
169
- };
170
- const fromNamed = (name) => {
171
- const hex2 = getNamedColors()[name];
172
- const rgb2 = hexToRgb(hex2);
173
- return rgb2 ?? { r: 255, g: 255, b: 255 };
174
- };
175
- const fromGrayName = (name) => {
176
- const hex2 = getGrayColors()[name];
177
- const rgb2 = hexToRgb(hex2);
178
- return rgb2 ?? { r: 128, g: 128, b: 128 };
179
- };
180
- const toBaseName = (compound) => {
181
- const base = compound.replace(/(Bright|Pastel)$/u, "");
182
- const key = base.charAt(0).toLowerCase() + base.slice(1);
183
- return key;
184
- };
185
- const parseColorName = (name) => {
186
- if (name.endsWith("Bright")) {
187
- const base = toBaseName(name);
188
- const rgb2 = fromNamed(base);
189
- const rgbAdj = mixWithWhite(rgb2, 0.25);
190
- return { rgb: rgbAdj, wantBright: true };
191
- }
192
- if (name.endsWith("Pastel")) {
193
- const base = toBaseName(name);
194
- const rgb2 = fromNamed(base);
195
- const rgbAdj = mixWithWhite(rgb2, 0.6);
196
- return { rgb: rgbAdj, wantBright: false };
197
- }
198
- if (name.startsWith("gray")) {
199
- return { rgb: fromGrayName(name), wantBright: false };
200
- }
201
- return { rgb: fromNamed(name), wantBright: false };
202
- };
203
- const openForOp = (op) => {
204
- if (CURRENT_LEVEL === 0) return "";
205
- switch (op.kind) {
206
- case "style":
207
- return sgr(op.open);
208
- case "fg-basic":
209
- return sgr([(op.bright ? 90 : 30) + op.idx]);
210
- case "bg-basic":
211
- return sgr([(op.bright ? 100 : 40) + op.idx]);
212
- case "fg-256":
213
- return `${ESC}38;5;${op.code}m`;
214
- case "bg-256":
215
- return `${ESC}48;5;${op.code}m`;
216
- case "fg-true":
217
- return `${ESC}38;2;${op.rgb.r};${op.rgb.g};${op.rgb.b}m`;
218
- case "bg-true":
219
- return `${ESC}48;2;${op.rgb.r};${op.rgb.g};${op.rgb.b}m`;
220
- }
221
- };
222
- const opsToOpen = (ops) => {
223
- if (CURRENT_LEVEL === 0) return "";
224
- let out = "";
225
- for (const op of ops) out += openForOp(op);
226
- return out;
227
- };
228
- const applyOpsToText = (ops, input) => {
229
- const text = String(input);
230
- if (CURRENT_LEVEL === 0 || ops.length === 0 || text.length === 0) return text;
231
- const open = opsToOpen(ops);
232
- if (!text.includes("\n")) {
233
- return `${open}${text}${RESET}`;
234
- }
235
- const lines = text.split("\n");
236
- const result = new Array(lines.length);
237
- for (let i = 0; i < lines.length; i++) {
238
- const line = lines[i];
239
- if (line.endsWith("\r")) {
240
- result[i] = `${open}${line.slice(0, -1)}\r${RESET}`;
241
- } else {
242
- result[i] = `${open}${line}${RESET}`;
243
- }
244
- }
245
- return result.join("\n");
246
- };
247
- const mkFgOpsFromRgb = (rgb2, wantBright = false) => {
248
- if (CURRENT_LEVEL === 1) {
249
- const idx = nearestBasicIndex(rgb2);
250
- return [{ kind: "fg-basic", idx, bright: wantBright }];
251
- }
252
- if (CURRENT_LEVEL === 2) {
253
- return [{ kind: "fg-256", code: rgbToAnsi256(rgb2) }];
254
- }
255
- return [{ kind: "fg-true", rgb: rgb2 }];
256
- };
257
- const mkBgOpsFromRgb = (rgb2, wantBright = false) => {
258
- if (CURRENT_LEVEL === 1) {
259
- const idx = nearestBasicIndex(rgb2);
260
- return [{ kind: "bg-basic", idx, bright: wantBright }];
261
- }
262
- if (CURRENT_LEVEL === 2) {
263
- return [{ kind: "bg-256", code: rgbToAnsi256(rgb2) }];
264
- }
265
- return [{ kind: "bg-true", rgb: rgb2 }];
266
- };
267
- const STYLE_TABLE = {
268
- reset: { kind: "style", open: [0] },
269
- bold: { kind: "style", open: [1] },
270
- dim: { kind: "style", open: [2] },
271
- italic: { kind: "style", open: [3] },
272
- underline: { kind: "style", open: [4] },
273
- inverse: { kind: "style", open: [7] },
274
- hidden: { kind: "style", open: [8] },
275
- strikethrough: { kind: "style", open: [9] }
276
- };
277
- const STYLE_KEYS = /* @__PURE__ */ new Set([
278
- "reset",
279
- "bold",
280
- "dim",
281
- "italic",
282
- "underline",
283
- "inverse",
284
- "hidden",
285
- "strikethrough"
286
- ]);
287
- const DYNAMIC_KEYS = /* @__PURE__ */ new Set(["rgb", "hex", "hsl", "bgRgb", "bgHex", "bgHsl"]);
288
- let COLOR_KEY_CACHE;
289
- let BG_KEY_CACHE;
290
- const getColorKeys = () => {
291
- if (COLOR_KEY_CACHE) return COLOR_KEY_CACHE;
292
- const baseColors = [
293
- "black",
294
- "red",
295
- "green",
296
- "yellow",
297
- "blue",
298
- "magenta",
299
- "cyan",
300
- "white",
301
- "gray"
302
- ];
303
- const extendedColors = [
304
- "orange",
305
- "pink",
306
- "purple",
307
- "teal",
308
- "lime",
309
- "brown",
310
- "navy",
311
- "maroon",
312
- "olive",
313
- "silver"
314
- ];
315
- const grayNames = Array.from({ length: 9 }, (_, i) => `gray${(i + 1) * 10}`);
316
- COLOR_KEY_CACHE = /* @__PURE__ */ new Set([
317
- ...baseColors,
318
- ...extendedColors,
319
- ...grayNames,
320
- ...baseColors.map((c) => `${c}Bright`),
321
- ...extendedColors.map((c) => `${c}Bright`),
322
- ...baseColors.map((c) => `${c}Pastel`),
323
- ...extendedColors.map((c) => `${c}Pastel`),
324
- "grayPastel"
325
- ]);
326
- return COLOR_KEY_CACHE;
327
- };
328
- const getBgKeys = () => {
329
- if (BG_KEY_CACHE) return BG_KEY_CACHE;
330
- BG_KEY_CACHE = /* @__PURE__ */ new Set();
331
- for (const colorKey of getColorKeys()) {
332
- const capitalizedKey = colorKey.charAt(0).toUpperCase() + colorKey.slice(1);
333
- BG_KEY_CACHE.add(`bg${capitalizedKey}`);
334
- }
335
- return BG_KEY_CACHE;
336
- };
337
- const callableFromOps = (ops) => {
338
- const fn = ((input) => applyOpsToText(ops, input));
339
- Object.defineProperty(fn, OP_SYMBOL, {
340
- value: ops,
341
- enumerable: false,
342
- configurable: false,
343
- writable: false
344
- });
345
- return fn;
346
- };
347
- const appendOps = (base, extra) => {
348
- const out = [];
349
- for (const op of base) out.push(op);
350
- for (const op of extra) out.push(op);
351
- return out;
352
- };
353
- const mkRgbMethod = (ops, isBg) => (r, g, b) => {
354
- const rgb2 = { r: clampByte(r), g: clampByte(g), b: clampByte(b) };
355
- const add = isBg ? mkBgOpsFromRgb(rgb2) : mkFgOpsFromRgb(rgb2);
356
- return callableProxy(appendOps(ops, add));
357
- };
358
- const mkHexMethod = (ops, isBg) => (hex2) => {
359
- const rgb2 = hexToRgb(hex2);
360
- if (!rgb2) return callableProxy(ops);
361
- const add = isBg ? mkBgOpsFromRgb(rgb2) : mkFgOpsFromRgb(rgb2);
362
- return callableProxy(appendOps(ops, add));
363
- };
364
- const mkHslMethod = (ops, isBg) => (h, s, l) => {
365
- const rgb2 = hslToRgb(h, s, l);
366
- const add = isBg ? mkBgOpsFromRgb(rgb2) : mkFgOpsFromRgb(rgb2);
367
- return callableProxy(appendOps(ops, add));
368
- };
369
- const callableProxy = (ops) => {
370
- const base = callableFromOps(ops);
371
- return new Proxy(base, {
372
- apply(_target, _thisArg, argArray) {
373
- const [input] = argArray;
374
- return applyOpsToText(ops, input);
375
- },
376
- get(_target, prop) {
377
- const key = String(prop);
378
- if (prop === OP_SYMBOL) return ops;
379
- if (STYLE_KEYS.has(key)) {
380
- const op = STYLE_TABLE[key];
381
- return callableProxy(appendOps(ops, [op]));
382
- }
383
- if (DYNAMIC_KEYS.has(key)) {
384
- const isBg = key.startsWith("bg");
385
- switch (key) {
386
- case "rgb":
387
- return mkRgbMethod(ops, false);
388
- case "hex":
389
- return mkHexMethod(ops, false);
390
- case "hsl":
391
- return mkHslMethod(ops, false);
392
- case "bgRgb":
393
- return mkRgbMethod(ops, true);
394
- case "bgHex":
395
- return mkHexMethod(ops, true);
396
- case "bgHsl":
397
- return mkHslMethod(ops, true);
398
- }
399
- }
400
- const bgKeys = getBgKeys();
401
- const colorKeys = getColorKeys();
402
- if (bgKeys.has(key)) {
403
- const raw = key.slice(2);
404
- const colorName = raw.charAt(0).toLowerCase() + raw.slice(1);
405
- const { rgb: rgb2, wantBright } = parseColorName(colorName);
406
- return callableProxy(appendOps(ops, mkBgOpsFromRgb(rgb2, wantBright)));
407
- }
408
- if (colorKeys.has(key)) {
409
- const { rgb: rgb2, wantBright } = parseColorName(key);
410
- return callableProxy(appendOps(ops, mkFgOpsFromRgb(rgb2, wantBright)));
411
- }
412
- return callableProxy(ops);
413
- }
414
- });
415
- };
416
- export const re = callableProxy([]);
417
- export const rgb = (r, g, b) => re.rgb(r, g, b);
418
- export const hex = (h) => re.hex(h);
419
- export const hsl = (h, s, l) => re.hsl(h, s, l);
420
- export const bgRgb = (r, g, b) => re.bgRgb(r, g, b);
421
- export const bgHex = (h) => re.bgHex(h);
422
- export const bgHsl = (h, s, l) => re.bgHsl(h, s, l);
423
- export const chain = (...parts) => {
424
- const collected = [];
425
- for (const p of parts) {
426
- const ops = p[OP_SYMBOL];
427
- if (ops && ops.length > 0) {
428
- for (const op of ops) collected.push(op);
429
- }
430
- }
431
- return callableProxy(collected);
432
- };
433
- export const parseRgb = (value) => {
434
- const v = value.trim();
435
- if (!v.toLowerCase().startsWith("rgb(") || !v.endsWith(")")) return null;
436
- const inner = v.slice(4, -1);
437
- const parts = inner.split(",").map((s) => s.trim());
438
- if (parts.length !== 3) return null;
439
- const nums = parts.map((p) => Number.parseInt(p, 10));
440
- if (!Number.isFinite(nums[0]) || !Number.isFinite(nums[1]) || !Number.isFinite(nums[2]))
441
- return null;
442
- return { r: clampByte(nums[0]), g: clampByte(nums[1]), b: clampByte(nums[2]) };
443
- };
444
- export const parseHsl = (value) => {
445
- const v = value.trim().toLowerCase();
446
- if (!v.startsWith("hsl(") || !v.endsWith(")")) return null;
447
- const inner = v.slice(4, -1);
448
- const parts = inner.split(",").map((s2) => s2.trim().replace(/%$/u, ""));
449
- if (parts.length !== 3) return null;
450
- const h = Number.parseFloat(parts[0]);
451
- const s = Number.parseFloat(parts[1]);
452
- const l = Number.parseFloat(parts[2]);
453
- if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(l)) return null;
454
- return { h, s, l };
455
- };