1 |
|
2 |
|
3 |
|
4 | import { c as createCommonjsModule, a as commonjsGlobal } from './_commonjsHelpers-383fba37.js';
|
5 |
|
6 | var style = createCommonjsModule(function (module, exports) {
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | (function (global, factory) {
|
14 | factory(exports) ;
|
15 | })(commonjsGlobal, (function (exports) {
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | let re = {
|
38 | 'colorize': 'C',
|
39 | 'colorhex': '(?:[0-9a-fA-F]{3}){1,2}',
|
40 | 'colorname': '[a-zA-Z]+',
|
41 | 'padding': 'P[0-9]{2}',
|
42 | 'zoom': 'Z(?:[0-9]+(?:\\.[0-9]+)?|x)',
|
43 | 'classbase': '-?[_a-zA-Z][_a-zA-Z0-9-]{0,100}',
|
44 | 'id': '[a-zA-Z][_a-zA-Z0-9-]{0,100}'
|
45 | };
|
46 | re.colorbase = `(?:${re.colorhex}|${re.colorname})`;
|
47 | re.color = `_${re.colorbase}_`;
|
48 | re.colors = `_${re.colorbase}(?:,${re.colorbase})?_`;
|
49 | re.background = `G${re.color}`;
|
50 | re.detail = `D${re.colors}`;
|
51 | re.detailsym = `D[0-9]{2}${re.colors}`;
|
52 | re.classes = `${re.classbase}(?: ${re.classbase})*`;
|
53 | re.full = `-(${re.colorize})?(${re.padding})?(${re.background})?(${re.detail})?(${re.zoom})?(?:-((?:${re.detailsym})*))?(?:-(${re.classes})?!(?:(${re.id})!)?)?`;
|
54 |
|
55 | const prefixColor = color => {
|
56 | const regex = new RegExp(`^${re.colorhex}$`);
|
57 | return (regex.test(color) ? '#' : '') + color;
|
58 | };
|
59 |
|
60 | const definedProps = obj => Object.fromEntries(Object.entries(obj).filter(([k, v]) => v !== undefined));
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | const parse = styleString => {
|
79 | const regex = `^${re.full}`;
|
80 | const m = (typeof styleString === 'string' ? styleString.match(new RegExp(regex)) : []) || [];
|
81 | return definedProps({
|
82 | 'colorize': !m[1] ? undefined : !!m[1],
|
83 | 'padding': !m[2] ? undefined : parseInt(m[2].slice(1)),
|
84 | 'background': !m[3] ? undefined : prefixColor(m[3].slice(2, -1)),
|
85 | 'detail': !m[4] ? undefined : m[4].slice(2, -1).split(',').map(prefixColor),
|
86 | 'zoom': !m[5] ? undefined : m[5] === 'Zx' ? 'x' : parseFloat(m[5].slice(1)),
|
87 | 'detailsym': !m[6] ? undefined : m[6].match(new RegExp(re.detailsym, 'g')).map(val => {
|
88 | const parts = val.split('_');
|
89 | const detail = parts[1].split(',').map(prefixColor);
|
90 | return {
|
91 | 'index': parseInt(parts[0].slice(1)),
|
92 | 'detail': detail
|
93 | };
|
94 | }),
|
95 | 'classes': !m[7] ? undefined : m[7],
|
96 | 'id': !m[8] ? undefined : m[8]
|
97 | });
|
98 | };
|
99 |
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | const compose = styleObject => {
|
130 | if (typeof styleObject !== 'object' || styleObject === null) return undefined;
|
131 |
|
132 | let style1 = '-';
|
133 | style1 += !styleObject.colorize ? '' : 'C';
|
134 | const padding = parseInt(styleObject.padding);
|
135 | style1 += !padding || padding <= 0 || padding > 99 ? '' : 'P' + (padding > 9 ? padding : '0' + padding);
|
136 | const background = !styleObject.background || !(typeof styleObject.background === 'string') ? undefined : styleObject.background.match(re.colorbase)[0];
|
137 | style1 += !background ? '' : 'G_' + background + '_';
|
138 | const detail1 = !styleObject.detail || !styleObject.detail[0] || !(typeof styleObject.detail[0] === 'string') ? undefined : styleObject.detail[0].match(re.colorbase)[0];
|
139 | const detail2 = !styleObject.detail || !styleObject.detail[1] || !(typeof styleObject.detail[1] === 'string') ? undefined : styleObject.detail[1].match(re.colorbase)[0];
|
140 |
|
141 | if (detail1) {
|
142 | style1 += 'D_' + detail1;
|
143 |
|
144 | if (detail2) {
|
145 | style1 += ',' + detail2;
|
146 | }
|
147 |
|
148 | style1 += '_';
|
149 | }
|
150 |
|
151 | const zoom = styleObject.zoom === 'x' ? 'x' : parseFloat(styleObject.zoom);
|
152 | style1 += !zoom || zoom <= 0 ? '' : 'Z' + zoom;
|
153 | let style2 = '';
|
154 | const detailsym = !styleObject.detailsym || !Array.isArray(styleObject.detailsym) ? [] : styleObject.detailsym.map(styleObject => {
|
155 | const index = parseInt(styleObject.index);
|
156 | if (!index || index <= 0 || index > 99) return '';
|
157 | let style = 'D' + (index > 9 ? index : '0' + index);
|
158 | const detail1 = !styleObject.detail || !styleObject.detail[0] ? undefined : styleObject.detail[0].match(re.colorbase)[0];
|
159 | const detail2 = !styleObject.detail || !styleObject.detail[1] ? undefined : styleObject.detail[1].match(re.colorbase)[0];
|
160 |
|
161 | if (detail1) {
|
162 | style += '_' + detail1;
|
163 |
|
164 | if (detail2) {
|
165 | style += ',' + detail2;
|
166 | }
|
167 |
|
168 | style += '_';
|
169 | }
|
170 |
|
171 | return style;
|
172 | });
|
173 | style2 += detailsym.join('');
|
174 | let style3 = '';
|
175 | const classes = !styleObject.classes || !(typeof styleObject.classes === 'string') ? undefined : styleObject.classes.match(re.classes)[0];
|
176 | style3 += !classes ? '' : classes;
|
177 | const id = !styleObject.id || !(typeof styleObject.id === 'string') ? undefined : styleObject.id.match(re.id)[0];
|
178 | style3 += classes || id ? '!' : '';
|
179 | style3 += !id ? '' : id + '!';
|
180 | return style1 + (style2 || style3 ? '-' + style2 : '') + (style3 ? '-' + style3 : '');
|
181 | };
|
182 |
|
183 | |
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | const merge = (style1, style2) => {
|
198 | if (typeof style1 !== 'object') style1 = {};
|
199 | if (typeof style2 !== 'object') style2 = {};
|
200 | const zoom1 = 'zoom' in style1 ? style1['zoom'] : 1;
|
201 | const zoom2 = 'zoom' in style2 ? style2['zoom'] : 1;
|
202 | return { ...style1,
|
203 | ...style2,
|
204 | ...{
|
205 | zoom: zoom1 * zoom2
|
206 | }
|
207 | };
|
208 | };
|
209 |
|
210 | const rgb2arr = rgb => {
|
211 | if (typeof rgb !== 'string') return [0, 0, 0];
|
212 | return rgb.replace(/rgba?\((.+?)\)/ig, (_, values) => {
|
213 | return values;
|
214 | }).split(',').map(Number);
|
215 | };
|
216 |
|
217 | const arr2hex = arr => {
|
218 | return arr.slice(0, 3).map(num => num.toString(16).padStart(2, '0')).join('');
|
219 | };
|
220 | |
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | const rgb2hex = (rgb, tolerance = 0) => {
|
236 | const arr = rgb2arr(rgb);
|
237 |
|
238 | if (arr.length == 4 && arr[3] <= tolerance) {
|
239 | return 'transparent';
|
240 | } else {
|
241 | return arr2hex(arr);
|
242 | }
|
243 | };
|
244 | |
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 | const rgba2hex = (color, background) => {
|
258 | const bArr = rgb2arr(background);
|
259 | const cArr = rgb2arr(color);
|
260 | const alpha = cArr.length == 4 ? cArr[3] : 1;
|
261 |
|
262 | if (alpha == 0) {
|
263 | return 'transparent';
|
264 | } else {
|
265 | return arr2hex(cArr.map((v, i) => parseInt((1 - alpha) * bArr[i] + alpha * v)));
|
266 | }
|
267 | };
|
268 |
|
269 | exports.compose = compose;
|
270 | exports.merge = merge;
|
271 | exports.parse = parse;
|
272 | exports.re = re;
|
273 | exports.rgb2hex = rgb2hex;
|
274 | exports.rgba2hex = rgba2hex;
|
275 |
|
276 | Object.defineProperty(exports, '__esModule', { value: true });
|
277 |
|
278 | }));
|
279 |
|
280 |
|
281 | });
|
282 |
|
283 | export { style as s };
|