deno.land / x / skia_canvas@0.5.8 / src / filter.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
export enum FilterType { Blur, Brightness, Contrast, DropShadow, Grayscale, HueRotate, Invert, Opacity, Saturate, Sepia,}
interface FilterBase { type: FilterType;}
interface BlurFilter extends FilterBase { type: FilterType.Blur; value: number;}
interface BrightnessFilter extends FilterBase { type: FilterType.Brightness; value: number;}
interface ContrastFilter extends FilterBase { type: FilterType.Contrast; value: number;}
interface DropShadowFilter extends FilterBase { type: FilterType.DropShadow; dx: number; dy: number; radius: number; color: string;}
interface GrayscaleFilter extends FilterBase { type: FilterType.Grayscale; value: number;}
interface HueRotateFilter extends FilterBase { type: FilterType.HueRotate; value: number;}
interface InvertFilter extends FilterBase { type: FilterType.Invert; value: number;}
interface OpacityFilter extends FilterBase { type: FilterType.Opacity; value: number;}
interface SaturateFilter extends FilterBase { type: FilterType.Saturate; value: number;}
interface SepiaFilter extends FilterBase { type: FilterType.Sepia; value: number;}
export type Filter = | BlurFilter | BrightnessFilter | ContrastFilter | DropShadowFilter | GrayscaleFilter | HueRotateFilter | InvertFilter | OpacityFilter | SaturateFilter | SepiaFilter;
const FILTER_NAMES = [ "blur", "brightness", "contrast", "drop-shadow", "grayscale", "hue-rotate", "invert", "opacity", "saturate", "sepia",];
export function parseFilterString(filter: string): Filter[] { const filters: Filter[] = [];
let state: "fn" | "args" = "fn"; let fn = ""; let argc = 0; // deno-lint-ignore no-explicit-any let argv: any[] = [];
let i = 0; while (i < filter.length) { let ch = filter[i]; if (state === "fn") { if (ch === "(") { if (!FILTER_NAMES.includes(fn.toLowerCase())) { throw new Error(`Invalid filter name: ${fn}`); } fn = fn.toLowerCase(); state = "args"; } else if (ch.match(/[a-zA-Z0-9\-]+/)) { fn += ch; } else if (ch.match(/\s/) && fn.length > 0) { throw new Error(`Unexpected whitespace in filter function`); } else if (!ch.match(/\s/)) { throw new Error(`Unexpected character in filter function: ${ch}`); } } else if (state === "args") { if (argc === 1 && fn !== "drop-shadow") { throw new Error(`Function ${fn} only takes one argument`); } else if (argc === 4 && fn === "drop-shadow") { throw new Error(`Function ${fn} only takes four arguments`); }
let arg = ""; while (ch !== ")" && ch !== "," && ch !== " ") { arg += ch; i++; ch = filter[i]; }
if (arg === "" && ch === " ") { i++; continue; }
// deno-lint-ignore no-inner-declarations function parsePixel() { let numPart = ""; let unitPart = ""; for (let j = 0; j < arg.length; j++) { const ch = arg[j]; if (ch === " ") continue; if ( (ch.match(/[0-9]/) || (ch === "." && !numPart.includes("."))) && !unitPart ) { numPart += ch; } else { unitPart += ch; } } const num = parseFloat(numPart); if (unitPart === "px") { return num; } else if (unitPart === "%") { return num * 16 / 100; } else if ( unitPart === "em" || unitPart === "rem" || unitPart === "pc" ) { return num * 16; } else if (unitPart === "pt") { return num * 4 / 3; } else if (unitPart === "") { if (num === 0) { return 0; } else { throw new Error(`Invalid unitless number: ${num} in ${arg}`); } } else if (unitPart === "in") { return num * 96; } else if (unitPart === "cm") { return num * 96 / 2.54; } else if (unitPart === "mm") { return num * 96 / 25.4; } else if (unitPart === "q") { return num * 96.0 / 25.4 / 4.0; } else { throw new Error(`Invalid unit: ${unitPart} in ${arg}`); } }
// deno-lint-ignore no-inner-declarations function parsePercentage() { if (!arg.match(/^[0-9\.]+%?$/)) { throw new Error(`Invalid percentage: ${arg}`); }
const num = parseFloat( arg.endsWith("%") ? arg.slice(0, arg.length - 1) : arg, ); if (num < 0 || isNaN(num)) { throw new Error(`Invalid percentage: ${arg}`); }
return num / 100; }
// deno-lint-ignore no-inner-declarations function parseAngle() { if (arg.endsWith("deg")) { const num = parseFloat(arg.slice(0, arg.length - 3)); if (isNaN(num)) { throw new Error(`Invalid angle: ${arg}`); } return num; } else if (arg.endsWith("grad")) { const num = parseFloat(arg.slice(0, arg.length - 4)); if (isNaN(num)) { throw new Error(`Invalid angle: ${arg}`); } return num * 360 / 400; } else if (arg.endsWith("rad")) { const num = parseFloat(arg.slice(0, arg.length - 3)); if (isNaN(num)) { throw new Error(`Invalid angle: ${arg}`); } return num * 180 / Math.PI; } else if (arg.endsWith("turn")) { const num = parseFloat(arg.slice(0, arg.length - 4)); if (isNaN(num)) { throw new Error(`Invalid angle: ${arg}`); } return num * 360; } else { throw new Error(`Invalid angle: ${arg}`); } }
// deno-lint-ignore no-explicit-any let v: any = undefined;
switch (fn) { case "blur": v = parsePixel(); break;
case "brightness": case "contrast": case "grayscale": case "invert": case "opacity": case "saturate": case "sepia": v = parsePercentage(); break;
case "drop-shadow": if (argc === 0) { v = parsePixel(); } else if (argc === 1) { v = parsePixel(); } else if (argc === 2) { v = parseFloat(arg); if (isNaN(v)) { throw new Error(`Invalid number: ${arg}`); } } else if (argc === 3) { // Pass color string as is, C++ csscolorparser will parse it v = arg; if (!v) { throw new Error(`Invalid color: ${arg}`); } } break;
case "hue-rotate": v = parseAngle(); break;
default: throw new Error(`Unknown filter function: ${fn}`); }
argc++; if (v !== undefined) argv.push(v); else throw new Error(`Invalid argument: ${arg}`);
if (ch === ")") { if (argc === 0) { throw new Error(`Function ${fn} requires arguments`); }
if (fn === "drop-shadow" && argc !== 4) { throw new Error(`Function ${fn} requires four arguments`); } else if (fn !== "drop-shadow" && argc !== 1) { throw new Error(`Function ${fn} requires one argument`); }
switch (fn) { case "blur": filters.push({ type: FilterType.Blur, value: argv[0], }); break;
case "brightness": filters.push({ type: FilterType.Brightness, value: argv[0], }); break;
case "contrast": filters.push({ type: FilterType.Contrast, value: argv[0], }); break;
case "drop-shadow": filters.push({ type: FilterType.DropShadow, dx: argv[0], dy: argv[1], radius: argv[2], color: argv[3], }); break;
case "grayscale": filters.push({ type: FilterType.Grayscale, value: argv[0], }); break;
case "hue-rotate": filters.push({ type: FilterType.HueRotate, value: argv[0], }); break;
case "invert": filters.push({ type: FilterType.Invert, value: argv[0], }); break;
case "opacity": filters.push({ type: FilterType.Opacity, value: argv[0], }); break;
case "saturate": filters.push({ type: FilterType.Saturate, value: argv[0], }); break;
case "sepia": filters.push({ type: FilterType.Sepia, value: argv[0], }); break;
default: throw new Error(`Unknown filter function: ${fn}`); }
fn = ""; argc = 0; argv = []; state = "fn"; } }
i++; }
if ((state === "fn" && fn !== "") || state === "args") { throw new Error(`Unexpected end of filter string`); }
return filters;}
skia_canvas

Version Info

Tagged at
8 months ago