1 | import { FORE_INI, FORE_DEF, CSI, SGR, BOL_ON, BOL_OFF, DIM_ON, DIM_OFF, ITA_ON, ITA_OFF, UND_ON, UND_OFF, BLI_ON, BLI_OFF, INV_ON, INV_OFF, HID_ON, HID_OFF, CRO_ON, CRO_OFF } from '@palett/enum-ansi-codes';
|
2 | import { SC } from '@palett/util-ansi';
|
3 | import '@ject/oneself';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const round = x => x + (x > 0 ? 0.5 : -0.5) << 0;
|
13 |
|
14 | function hexAt(tx, i) {
|
15 | let n = tx.charCodeAt(i);
|
16 | return n >> 5 <= 1 ? n & 0xf : (n & 0x7) + 9;
|
17 | }
|
18 |
|
19 | const prolif = n => n << 4 | n;
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | const hf = (n, h, a, l) => {
|
31 | const k = (n + h / 30) % 12;
|
32 | return l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
33 | };
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | function hexToInt(hex) {
|
41 | let lo = 0,
|
42 | hi = hex === null || hex === void 0 ? void 0 : hex.length;
|
43 | if (hi === 7) lo++, hi--;
|
44 |
|
45 | if (hi === 6) {
|
46 | const r = hexAt(hex, lo++) << 4 | hexAt(hex, lo++);
|
47 | const g = hexAt(hex, lo++) << 4 | hexAt(hex, lo++);
|
48 | const b = hexAt(hex, lo++) << 4 | hexAt(hex, lo++);
|
49 | return r << 16 | g << 8 | b;
|
50 | }
|
51 |
|
52 | if (hi === 4) lo++, hi--;
|
53 |
|
54 | if (hi === 3) {
|
55 | return prolif(hexAt(hex, lo++)) << 16 | prolif(hexAt(hex, lo++)) << 8 | prolif(hexAt(hex, lo++));
|
56 | }
|
57 |
|
58 | return 0;
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function hslToInt([h, s, l]) {
|
70 | s /= 100, l /= 100;
|
71 | const a = s * Math.min(l, 1 - l),
|
72 | r = hf(0, h, a, l),
|
73 | g = hf(8, h, a, l),
|
74 | b = hf(4, h, a, l);
|
75 | return ((round(r * 0xFF) & 0xFF) << 16) + ((round(g * 0xFF) & 0xFF) << 8) + (round(b * 0xFF) & 0xFF);
|
76 | }
|
77 |
|
78 | class Dye {
|
79 | head;
|
80 | tail;
|
81 |
|
82 | constructor(head, tail) {
|
83 | this.head = head ?? '';
|
84 | this.tail = tail ?? '';
|
85 | }
|
86 |
|
87 | static of(head, tail) {
|
88 | return new Dye(head, tail);
|
89 | }
|
90 |
|
91 | static from(dye) {
|
92 | return new Dye(dye === null || dye === void 0 ? void 0 : dye.head, dye === null || dye === void 0 ? void 0 : dye.tail);
|
93 | }
|
94 |
|
95 | static prep(...style) {
|
96 | return new Dye().style(style);
|
97 | }
|
98 |
|
99 | static hex(color) {
|
100 | return HexDye.init(this).make(color);
|
101 | }
|
102 |
|
103 | static hsl(color) {
|
104 | return HslDye.init(this).make(color);
|
105 | }
|
106 |
|
107 | static int(color) {
|
108 | return IntDye.init(this).make(color);
|
109 | }
|
110 |
|
111 | static rgb(color) {
|
112 | return RgbDye.init(this).make(color);
|
113 | }
|
114 |
|
115 | load(r, g, b) {
|
116 | if (this.head.length) this.head += ';';
|
117 | if (this.tail.length) this.tail += ';';
|
118 | this.head += FORE_INI + SC + r + SC + g + SC + b;
|
119 | this.tail += FORE_DEF;
|
120 | return this;
|
121 | }
|
122 |
|
123 | repl(r, g, b) {
|
124 | const ctx = {
|
125 | head: (this === null || this === void 0 ? void 0 : this.head) ?? '',
|
126 | tail: (this === null || this === void 0 ? void 0 : this.tail) ?? ''
|
127 | };
|
128 | return load.call(ctx, r, g, b);
|
129 | }
|
130 |
|
131 | into([r, g, b]) {
|
132 | return this.repl(r, g, b);
|
133 | }
|
134 |
|
135 | draw(text) {
|
136 | return CSI + this.head + SGR + text + CSI + this.tail + SGR;
|
137 | }
|
138 |
|
139 | make(color) {
|
140 | return draw.bind(this.into(color));
|
141 | }
|
142 |
|
143 | render(color, text) {
|
144 | return draw.call(this.into(color), text);
|
145 | }
|
146 |
|
147 | clear() {
|
148 | return this.head = '', this.tail = '', this;
|
149 | }
|
150 |
|
151 | slice() {
|
152 | return new Dye(this.head, this.tail);
|
153 | }
|
154 |
|
155 |
|
156 |
|
157 | style(style) {
|
158 | if (!(style !== null && style !== void 0 && style.length)) return this;
|
159 | if (this.head.length) this.head += ';';
|
160 | if (this.tail.length) this.tail += ';';
|
161 |
|
162 | for (let t of style) {
|
163 | const [c] = t;
|
164 | c === 'b' ? (this.head += BOL_ON, this.tail += BOL_OFF
|
165 | ) : c === 'd' ? (this.head += DIM_ON, this.tail += DIM_OFF
|
166 | ) : c === 'i' && t[1] === 't' ? (this.head += ITA_ON, this.tail += ITA_OFF
|
167 | ) : c === 'u' ? (this.head += UND_ON, this.tail += UND_OFF
|
168 | ) : c === 'b' ? (this.head += BLI_ON, this.tail += BLI_OFF
|
169 | ) : c === 'i' ? (this.head += INV_ON, this.tail += INV_OFF
|
170 | ) : c === 'h' ? (this.head += HID_ON, this.tail += HID_OFF
|
171 | ) : c === 's' ? (this.head += CRO_ON, this.tail += CRO_OFF
|
172 | ) : void 0;
|
173 | }
|
174 |
|
175 | return this;
|
176 | }
|
177 |
|
178 | }
|
179 | const {
|
180 | load,
|
181 | repl,
|
182 | draw
|
183 | } = Dye.prototype;
|
184 | class HexDye extends Dye {
|
185 | constructor(h, t) {
|
186 | super(h, t);
|
187 | }
|
188 |
|
189 | static init(ctx) {
|
190 | return new HexDye(ctx === null || ctx === void 0 ? void 0 : ctx.head, ctx === null || ctx === void 0 ? void 0 : ctx.tail);
|
191 | }
|
192 |
|
193 | into(c) {
|
194 | return c = hexToInt(c), repl.call(this, c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF);
|
195 | }
|
196 |
|
197 | make(c) {
|
198 | return draw.bind(HexDye.prototype.into.call(this, c));
|
199 | }
|
200 |
|
201 | render(c, tx) {
|
202 | return draw.call(HexDye.prototype.into.call(this, c), tx);
|
203 | }
|
204 |
|
205 | }
|
206 | class HslDye extends Dye {
|
207 | constructor(h, t) {
|
208 | super(h, t);
|
209 | }
|
210 |
|
211 | static init(ctx) {
|
212 | return new HslDye(ctx === null || ctx === void 0 ? void 0 : ctx.head, ctx === null || ctx === void 0 ? void 0 : ctx.tail);
|
213 | }
|
214 |
|
215 | into(c) {
|
216 | return c = hslToInt(c), repl.call(this, c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF);
|
217 | }
|
218 |
|
219 | make(c) {
|
220 | return draw.bind(HslDye.prototype.into.call(this, c));
|
221 | }
|
222 |
|
223 | render(c, tx) {
|
224 | return draw.call(HslDye.prototype.into.call(this, c), tx);
|
225 | }
|
226 |
|
227 | }
|
228 | class IntDye extends Dye {
|
229 | constructor(h, t) {
|
230 | super(h, t);
|
231 | }
|
232 |
|
233 | static init(ctx) {
|
234 | return new IntDye(ctx === null || ctx === void 0 ? void 0 : ctx.head, ctx === null || ctx === void 0 ? void 0 : ctx.tail);
|
235 | }
|
236 |
|
237 | into(c) {
|
238 | return repl.call(this, c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF);
|
239 | }
|
240 |
|
241 | make(c) {
|
242 | return draw.bind(IntDye.prototype.into.call(this, c));
|
243 | }
|
244 |
|
245 | render(c, tx) {
|
246 | return draw.call(IntDye.prototype.into.call(this, c), tx);
|
247 | }
|
248 |
|
249 | }
|
250 | class RgbDye extends Dye {
|
251 | constructor(h, t) {
|
252 | super(h, t);
|
253 | }
|
254 |
|
255 | static init(ctx) {
|
256 | return new RgbDye(ctx === null || ctx === void 0 ? void 0 : ctx.head, ctx === null || ctx === void 0 ? void 0 : ctx.tail);
|
257 | }
|
258 |
|
259 | into([r, g, b]) {
|
260 | return repl.call(this, r, g, b);
|
261 | }
|
262 |
|
263 | make(c) {
|
264 | return draw.bind(RgbDye.prototype.into.call(this, c));
|
265 | }
|
266 |
|
267 | render(c, tx) {
|
268 | return draw.call(RgbDye.prototype.into.call(this, c), tx);
|
269 | }
|
270 |
|
271 | }
|
272 |
|
273 | class DyeFab {
|
274 |
|
275 | base;
|
276 |
|
277 | constructor(dye) {
|
278 | this.base = dye;
|
279 | }
|
280 |
|
281 | static build(space, style) {
|
282 | return (DyeFab[space] ?? DyeFab.rgb).apply(null, style);
|
283 | }
|
284 |
|
285 | static prep(space, ...style) {
|
286 | return (DyeFab[space] ?? DyeFab.rgb).apply(null, style);
|
287 | }
|
288 |
|
289 | static hex(...style) {
|
290 | return new HexDye().style(style);
|
291 | }
|
292 |
|
293 | static hsl(...style) {
|
294 | return new HslDye().style(style);
|
295 | }
|
296 |
|
297 | static int(...style) {
|
298 | return new IntDye().style(style);
|
299 | }
|
300 |
|
301 | static rgb(...style) {
|
302 | return new RgbDye().style(style);
|
303 | }
|
304 |
|
305 | }
|
306 |
|
307 | export { Dye, DyeFab, DyeFab as DyeFactory, HexDye, HslDye, IntDye, RgbDye, draw, load, repl };
|