qrdx 0.0.46 → 0.0.48
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/LICENSE +21 -0
- package/README.md +358 -300
- package/dist/index.js +27 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +27 -7
- package/dist/index.mjs.map +1 -1
- package/dist/qr-code-canvas.js +12 -0
- package/dist/qr-code-canvas.js.map +1 -0
- package/dist/qr-code-canvas.mjs +12 -0
- package/dist/qr-code-canvas.mjs.map +1 -0
- package/dist/src/api.d.ts.map +1 -1
- package/dist/src/art-trace.d.ts +46 -0
- package/dist/src/art-trace.d.ts.map +1 -0
- package/dist/src/art.d.ts +138 -0
- package/dist/src/art.d.ts.map +1 -0
- package/dist/src/body-patterns.d.ts +34 -0
- package/dist/src/body-patterns.d.ts.map +1 -0
- package/dist/src/codegen.d.ts +5 -5
- package/dist/src/codegen.d.ts.map +1 -1
- package/dist/src/codeword-map.d.ts +40 -0
- package/dist/src/codeword-map.d.ts.map +1 -0
- package/dist/src/constants.d.ts +4 -7
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/corner-dot.d.ts +3 -1
- package/dist/src/corner-dot.d.ts.map +1 -1
- package/dist/src/corner-eye.d.ts +5 -2
- package/dist/src/corner-eye.d.ts.map +1 -1
- package/dist/src/detection.d.ts.map +1 -1
- package/dist/src/download.d.ts +50 -6
- package/dist/src/download.d.ts.map +1 -1
- package/dist/src/eps.d.ts +62 -0
- package/dist/src/eps.d.ts.map +1 -0
- package/dist/src/frame-icons.d.ts +11 -0
- package/dist/src/frame-icons.d.ts.map +1 -0
- package/dist/src/frame-import.d.ts +61 -0
- package/dist/src/frame-import.d.ts.map +1 -0
- package/dist/src/frame-spec.d.ts +24 -0
- package/dist/src/frame-spec.d.ts.map +1 -0
- package/dist/src/frame-styles.d.ts +50 -0
- package/dist/src/frame-styles.d.ts.map +1 -0
- package/dist/src/frame.d.ts +126 -0
- package/dist/src/frame.d.ts.map +1 -0
- package/dist/src/frames/builtin.d.ts +8 -0
- package/dist/src/frames/builtin.d.ts.map +1 -0
- package/dist/src/frames/official.d.ts +18 -0
- package/dist/src/frames/official.d.ts.map +1 -0
- package/dist/src/helpers.d.ts +15 -2
- package/dist/src/helpers.d.ts.map +1 -1
- package/dist/src/index.d.ts +25 -13
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/logo-budget.d.ts +54 -0
- package/dist/src/logo-budget.d.ts.map +1 -0
- package/dist/src/logo-cleanup.d.ts +25 -0
- package/dist/src/logo-cleanup.d.ts.map +1 -0
- package/dist/src/logo.d.ts +28 -0
- package/dist/src/logo.d.ts.map +1 -0
- package/dist/src/print.d.ts +73 -0
- package/dist/src/print.d.ts.map +1 -0
- package/dist/src/qr-code-canvas.d.ts +13 -0
- package/dist/src/qr-code-canvas.d.ts.map +1 -0
- package/dist/src/qr-code.d.ts +4 -2
- package/dist/src/qr-code.d.ts.map +1 -1
- package/dist/src/qr-outline.d.ts +41 -0
- package/dist/src/qr-outline.d.ts.map +1 -0
- package/dist/src/qr-shape.d.ts +72 -0
- package/dist/src/qr-shape.d.ts.map +1 -0
- package/dist/src/scan-risk.d.ts +40 -0
- package/dist/src/scan-risk.d.ts.map +1 -0
- package/dist/src/scan-score.d.ts +154 -0
- package/dist/src/scan-score.d.ts.map +1 -0
- package/dist/src/shapes.d.ts +36 -0
- package/dist/src/shapes.d.ts.map +1 -0
- package/dist/src/silhouette.d.ts +28 -0
- package/dist/src/silhouette.d.ts.map +1 -0
- package/dist/src/utils/color.d.ts.map +1 -1
- package/dist/src/utils/fmt.d.ts +24 -0
- package/dist/src/utils/fmt.d.ts.map +1 -0
- package/dist/src/utils/paint.d.ts +80 -0
- package/dist/src/utils/paint.d.ts.map +1 -0
- package/dist/src/utils/parse-color.d.ts +21 -0
- package/dist/src/utils/parse-color.d.ts.map +1 -0
- package/dist/src/utils/path-data.d.ts +24 -0
- package/dist/src/utils/path-data.d.ts.map +1 -0
- package/dist/src/utils/png.d.ts +16 -0
- package/dist/src/utils/png.d.ts.map +1 -0
- package/dist/src/utils/polygon-clip.d.ts +26 -0
- package/dist/src/utils/polygon-clip.d.ts.map +1 -0
- package/dist/src/utils/print-shared.d.ts +20 -0
- package/dist/src/utils/print-shared.d.ts.map +1 -0
- package/dist/src/utils/svg-geometry.d.ts +49 -0
- package/dist/src/utils/svg-geometry.d.ts.map +1 -0
- package/dist/src/utils/svg-gradient.d.ts +59 -0
- package/dist/src/utils/svg-gradient.d.ts.map +1 -0
- package/dist/src/utils/svg-painter.d.ts +152 -0
- package/dist/src/utils/svg-painter.d.ts.map +1 -0
- package/dist/src/utils/webp.d.ts +25 -0
- package/dist/src/utils/webp.d.ts.map +1 -0
- package/dist/src/utils.d.ts +18 -2
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/webp.d.ts +19 -0
- package/dist/src/webp.d.ts.map +1 -0
- package/dist/types/art.d.ts +64 -0
- package/dist/types/art.d.ts.map +1 -0
- package/dist/types/color.d.ts +125 -7
- package/dist/types/color.d.ts.map +1 -1
- package/dist/types/corner-dot.d.ts +52 -2
- package/dist/types/corner-dot.d.ts.map +1 -1
- package/dist/types/corner-eye.d.ts +43 -2
- package/dist/types/corner-eye.d.ts.map +1 -1
- package/dist/types/frame-spec.d.ts +3509 -0
- package/dist/types/frame-spec.d.ts.map +1 -0
- package/dist/types/frame.d.ts +68 -0
- package/dist/types/frame.d.ts.map +1 -0
- package/dist/types/image-pattern.d.ts +15 -1
- package/dist/types/image-pattern.d.ts.map +1 -1
- package/dist/types/index.d.ts +15 -9
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +6 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/types/qr.d.ts +1684 -4
- package/dist/types/qr.d.ts.map +1 -1
- package/dist/types/template.d.ts +2 -2
- package/dist/types/template.d.ts.map +1 -1
- package/package.json +70 -25
- package/src/api.tsx +67 -17
- package/src/art-trace.ts +554 -0
- package/src/art.tsx +2226 -0
- package/src/body-patterns.tsx +389 -0
- package/src/codegen.ts +10 -5
- package/src/codeword-map.ts +265 -0
- package/src/constants.ts +4 -8
- package/src/corner-dot.tsx +209 -33
- package/src/corner-eye.ts +328 -60
- package/src/detection.ts +5 -11
- package/src/download.tsx +174 -70
- package/src/eps.ts +701 -0
- package/src/frame-icons.ts +325 -0
- package/src/frame-import.ts +1198 -0
- package/src/frame-spec.ts +518 -0
- package/src/frame-styles.ts +1095 -0
- package/src/frame.tsx +630 -0
- package/src/frames/builtin/banner-bottom.json +58 -0
- package/src/frames/builtin/circle.json +81 -0
- package/src/frames/builtin/rounded-border.json +60 -0
- package/src/frames/builtin/ticket.json +51 -0
- package/src/frames/builtin.ts +20 -0
- package/src/frames/official/attendance.json +110 -0
- package/src/frames/official/audio-guide.json +95 -0
- package/src/frames/official/book-now.json +103 -0
- package/src/frames/official/check-in.json +110 -0
- package/src/frames/official/christmas.json +131 -0
- package/src/frames/official/donate.json +77 -0
- package/src/frames/official/event-ticket.json +77 -0
- package/src/frames/official/feedback-bubble.json +73 -0
- package/src/frames/official/follow-us.json +83 -0
- package/src/frames/official/get-the-app.json +107 -0
- package/src/frames/official/halloween.json +80 -0
- package/src/frames/official/new-year.json +94 -0
- package/src/frames/official/order-at-table.json +86 -0
- package/src/frames/official/product-info.json +119 -0
- package/src/frames/official/register-product.json +77 -0
- package/src/frames/official/review-us.json +127 -0
- package/src/frames/official/rsvp-card.json +112 -0
- package/src/frames/official/save-contact.json +117 -0
- package/src/frames/official/scan-to-pay.json +94 -0
- package/src/frames/official/shop-window.json +81 -0
- package/src/frames/official/sign-rider.json +81 -0
- package/src/frames/official/table-tent.json +75 -0
- package/src/frames/official/wifi-card.json +89 -0
- package/src/frames/official/winter.json +142 -0
- package/src/frames/official.ts +77 -0
- package/src/helpers.ts +168 -11
- package/src/index.tsx +150 -172
- package/src/logo-budget.ts +226 -0
- package/src/logo-cleanup.ts +146 -0
- package/src/logo.tsx +137 -0
- package/src/print.ts +794 -0
- package/src/qr-code-canvas.tsx +241 -0
- package/src/qr-code.tsx +51 -16
- package/src/qr-outline.ts +511 -0
- package/src/qr-shape.ts +255 -0
- package/src/scan-risk.ts +197 -0
- package/src/scan-score.ts +1629 -0
- package/src/shapes.ts +169 -0
- package/src/silhouette.ts +334 -0
- package/src/utils/color.ts +10 -1
- package/src/utils/fmt.ts +59 -0
- package/src/utils/paint.ts +317 -0
- package/src/utils/parse-color.ts +135 -0
- package/src/utils/path-data.ts +182 -0
- package/src/utils/png.ts +175 -0
- package/src/utils/polygon-clip.ts +163 -0
- package/src/utils/print-shared.ts +74 -0
- package/src/utils/svg-geometry.ts +838 -0
- package/src/utils/svg-gradient.ts +407 -0
- package/src/utils/svg-painter.ts +946 -0
- package/src/utils/webp.ts +556 -0
- package/src/utils.tsx +794 -385
- package/src/webp.ts +84 -0
- package/types/art.ts +74 -0
- package/types/color.ts +132 -12
- package/types/corner-dot.ts +41 -4
- package/types/corner-eye.ts +31 -4
- package/types/frame-spec.ts +748 -0
- package/types/frame.ts +67 -0
- package/types/image-pattern.ts +15 -0
- package/types/index.ts +106 -4
- package/types/qr.ts +180 -2
- package/types/template.ts +2 -2
- package/dist/cli.js +0 -4319
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: color and wedge math */
|
|
2
|
+
// Paint helpers shared by the SVG renderer and the vector exporters: colors
|
|
3
|
+
// with alpha split into a color and an opacity (for SVG consumers that don't
|
|
4
|
+
// read 8-digit hex, such as Illustrator), compositing for formats without
|
|
5
|
+
// transparency, and the wedge fan that draws an angular (conic) gradient,
|
|
6
|
+
// which SVG, PDF and PostScript have no native shading for.
|
|
7
|
+
import type { GradientStop } from "../../types/color";
|
|
8
|
+
import { fmt, roundTo } from "./fmt";
|
|
9
|
+
import { parseColor, type RGBAColor } from "./parse-color";
|
|
10
|
+
|
|
11
|
+
const hex2 = (value: number) =>
|
|
12
|
+
Math.round(Math.min(Math.max(value, 0), 255))
|
|
13
|
+
.toString(16)
|
|
14
|
+
.padStart(2, "0");
|
|
15
|
+
|
|
16
|
+
/** "#rrggbb" for a color (alpha dropped). */
|
|
17
|
+
export function toHex6(color: RGBAColor): string {
|
|
18
|
+
return `#${hex2(color.r)}${hex2(color.g)}${hex2(color.b)}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A CSS color as an opaque color plus an opacity. Colors without alpha, fully
|
|
23
|
+
* transparent ones ("transparent" stays "transparent") and colors that don't
|
|
24
|
+
* parse come back unchanged, so they render exactly as before.
|
|
25
|
+
*/
|
|
26
|
+
export function splitAlpha(color: string): {
|
|
27
|
+
color: string;
|
|
28
|
+
opacity?: number;
|
|
29
|
+
} {
|
|
30
|
+
const parsed = parseColor(color);
|
|
31
|
+
if (!parsed || parsed.a >= 1 || parsed.a <= 0) {
|
|
32
|
+
return { color };
|
|
33
|
+
}
|
|
34
|
+
return { color: toHex6(parsed), opacity: roundTo(parsed.a, 3) };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** `color` composited over an opaque `backdrop` (straight alpha). */
|
|
38
|
+
export function flattenOver(color: RGBAColor, backdrop: RGBAColor): RGBAColor {
|
|
39
|
+
const a = Math.min(Math.max(color.a, 0), 1);
|
|
40
|
+
return {
|
|
41
|
+
r: color.r * a + backdrop.r * (1 - a),
|
|
42
|
+
g: color.g * a + backdrop.g * (1 - a),
|
|
43
|
+
b: color.b * a + backdrop.b * (1 - a),
|
|
44
|
+
a: 1,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ParsedStop {
|
|
49
|
+
/** 0–1 */
|
|
50
|
+
offset: number;
|
|
51
|
+
color: RGBAColor;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const BLACK: RGBAColor = { r: 0, g: 0, b: 0, a: 1 };
|
|
55
|
+
|
|
56
|
+
/** Stops sorted by offset, offsets clamped to 0–1, colors parsed. */
|
|
57
|
+
export function parseStops(stops: GradientStop[]): ParsedStop[] {
|
|
58
|
+
return [...stops]
|
|
59
|
+
.map((stop) => ({
|
|
60
|
+
offset: Math.min(Math.max(stop.offset / 100, 0), 1),
|
|
61
|
+
color: parseColor(stop.color) ?? BLACK,
|
|
62
|
+
}))
|
|
63
|
+
.sort((a, b) => a.offset - b.offset);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Color at `t` (0–1) along sorted stops, blended in premultiplied sRGB as
|
|
68
|
+
* CSS and SVG do, so a stop fading to transparent doesn't turn grey.
|
|
69
|
+
*/
|
|
70
|
+
export function colorAt(stops: ParsedStop[], t: number): RGBAColor {
|
|
71
|
+
const first = stops[0];
|
|
72
|
+
const last = stops.at(-1);
|
|
73
|
+
if (!(first && last)) {
|
|
74
|
+
return BLACK;
|
|
75
|
+
}
|
|
76
|
+
if (t <= first.offset) {
|
|
77
|
+
return first.color;
|
|
78
|
+
}
|
|
79
|
+
if (t >= last.offset) {
|
|
80
|
+
return last.color;
|
|
81
|
+
}
|
|
82
|
+
for (let i = 0; i + 1 < stops.length; i++) {
|
|
83
|
+
const from = stops[i] as ParsedStop;
|
|
84
|
+
const to = stops[i + 1] as ParsedStop;
|
|
85
|
+
if (t >= from.offset && t <= to.offset) {
|
|
86
|
+
const span = to.offset - from.offset;
|
|
87
|
+
const k = span > 0 ? (t - from.offset) / span : 1;
|
|
88
|
+
const a = from.color.a + (to.color.a - from.color.a) * k;
|
|
89
|
+
const mix = (c0: number, c1: number) => {
|
|
90
|
+
const premultiplied =
|
|
91
|
+
c0 * from.color.a + (c1 * to.color.a - c0 * from.color.a) * k;
|
|
92
|
+
return a > 0 ? premultiplied / a : c0 + (c1 - c0) * k;
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
r: mix(from.color.r, to.color.r),
|
|
96
|
+
g: mix(from.color.g, to.color.g),
|
|
97
|
+
b: mix(from.color.b, to.color.b),
|
|
98
|
+
a,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return last.color;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface ConicWedge {
|
|
106
|
+
/** Triangle from the centre, as SVG path data */
|
|
107
|
+
d: string;
|
|
108
|
+
/** Opaque "#rrggbb" */
|
|
109
|
+
color: string;
|
|
110
|
+
/** Set when stops differ in alpha: this wedge's own opacity */
|
|
111
|
+
opacity?: number;
|
|
112
|
+
/** Start angle, degrees clockwise from the +x axis (0–360) */
|
|
113
|
+
from: number;
|
|
114
|
+
/** Degrees the wedge covers from `from` (overlap included) */
|
|
115
|
+
sweep: number;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface ConicFan {
|
|
119
|
+
wedges: ConicWedge[];
|
|
120
|
+
/**
|
|
121
|
+
* Set when every stop shares one alpha below 1: the wedges are opaque and
|
|
122
|
+
* the whole fan is drawn at this opacity (one group, so overlaps don't
|
|
123
|
+
* add up).
|
|
124
|
+
*/
|
|
125
|
+
opacity?: number;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Widest wedge, in degrees: keeps the chord outside the area it covers. */
|
|
129
|
+
const MAX_WEDGE_DEGREES = 4;
|
|
130
|
+
/** Most wedges in one fan */
|
|
131
|
+
const MAX_WEDGES = 720;
|
|
132
|
+
/** Largest color step between neighbouring wedges, in 0–255 levels */
|
|
133
|
+
const LEVELS_PER_WEDGE = 2;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Wedges that draw an angular (conic) gradient around (`cx`, `cy`): each is
|
|
137
|
+
* a thin triangle filled with the gradient's color at its middle angle, as
|
|
138
|
+
* many as the color change needs (about 2 levels per wedge, at most 4° and
|
|
139
|
+
* 720 wedges). `angle` is where the sweep starts, clockwise from the top,
|
|
140
|
+
* as in CSS `conic-gradient(from <angle>deg, …)`; `radius` must reach every
|
|
141
|
+
* point the fan has to cover (1.003 × the farthest point is enough).
|
|
142
|
+
*
|
|
143
|
+
* Anti-aliased renderers (PDF viewers, browsers) blend the background into
|
|
144
|
+
* the seam where two shapes meet edge to edge. So when the stops share one
|
|
145
|
+
* alpha, every wedge reaches one wedge width into the next (and the first
|
|
146
|
+
* one back under the last); painted in order, each seam lies on the solid
|
|
147
|
+
* wedge before it. When stops differ in alpha, wedges carry their own
|
|
148
|
+
* opacity and meet edge to edge, so nothing is painted twice.
|
|
149
|
+
*/
|
|
150
|
+
export function conicWedges(
|
|
151
|
+
stops: GradientStop[],
|
|
152
|
+
angle: number,
|
|
153
|
+
cx: number,
|
|
154
|
+
cy: number,
|
|
155
|
+
radius: number
|
|
156
|
+
): ConicFan {
|
|
157
|
+
const parsed = parseStops(stops);
|
|
158
|
+
if (parsed.length === 0) {
|
|
159
|
+
return { wedges: [] };
|
|
160
|
+
}
|
|
161
|
+
const firstAlpha = (parsed[0] as ParsedStop).color.a;
|
|
162
|
+
const uniform = parsed.every(
|
|
163
|
+
(stop) => Math.abs(stop.color.a - firstAlpha) < 0.002
|
|
164
|
+
);
|
|
165
|
+
// Breakpoints along the turn (0–1): the stops, plus both ends
|
|
166
|
+
const breaks = [
|
|
167
|
+
...new Set([0, ...parsed.map((stop) => stop.offset), 1]),
|
|
168
|
+
].sort((a, b) => a - b);
|
|
169
|
+
|
|
170
|
+
const spans: [number, number][] = [];
|
|
171
|
+
for (let i = 0; i + 1 < breaks.length; i++) {
|
|
172
|
+
const from = breaks[i] as number;
|
|
173
|
+
const to = breaks[i + 1] as number;
|
|
174
|
+
if (to <= from) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
const c0 = colorAt(parsed, from + 1e-9);
|
|
178
|
+
const c1 = colorAt(parsed, to - 1e-9);
|
|
179
|
+
const delta = Math.max(
|
|
180
|
+
Math.abs(c0.r * c0.a - c1.r * c1.a),
|
|
181
|
+
Math.abs(c0.g * c0.a - c1.g * c1.a),
|
|
182
|
+
Math.abs(c0.b * c0.a - c1.b * c1.a),
|
|
183
|
+
Math.abs(c0.a - c1.a) * 255
|
|
184
|
+
);
|
|
185
|
+
const byColor = Math.ceil(delta / LEVELS_PER_WEDGE);
|
|
186
|
+
const byAngle = Math.ceil(((to - from) * 360) / MAX_WEDGE_DEGREES);
|
|
187
|
+
const steps = Math.max(1, byColor, byAngle);
|
|
188
|
+
for (let s = 0; s < steps; s++) {
|
|
189
|
+
spans.push([
|
|
190
|
+
from + ((to - from) * s) / steps,
|
|
191
|
+
from + ((to - from) * (s + 1)) / steps,
|
|
192
|
+
]);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// Too many: merge neighbours evenly until the fan fits
|
|
196
|
+
let merged = spans;
|
|
197
|
+
if (spans.length > MAX_WEDGES) {
|
|
198
|
+
const factor = Math.ceil(spans.length / MAX_WEDGES);
|
|
199
|
+
merged = [];
|
|
200
|
+
for (let i = 0; i < spans.length; i += factor) {
|
|
201
|
+
const group = spans.slice(i, i + factor);
|
|
202
|
+
merged.push([
|
|
203
|
+
(group[0] as [number, number])[0],
|
|
204
|
+
(group.at(-1) as [number, number])[1],
|
|
205
|
+
]);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// CSS angles start at 12 o'clock; SVG's +x axis is 3 o'clock.
|
|
210
|
+
const start = angle - 90;
|
|
211
|
+
// An extended wedge is up to 2 × 4° wide: reach a little further so its
|
|
212
|
+
// chord stays outside the area
|
|
213
|
+
const reach =
|
|
214
|
+
radius / Math.cos(((2 * MAX_WEDGE_DEGREES) / 2) * (Math.PI / 180));
|
|
215
|
+
const toPoint = (degrees: number): [string, string] => {
|
|
216
|
+
const radians = (degrees * Math.PI) / 180;
|
|
217
|
+
return [
|
|
218
|
+
fmt(cx + reach * Math.cos(radians)),
|
|
219
|
+
fmt(cy + reach * Math.sin(radians)),
|
|
220
|
+
];
|
|
221
|
+
};
|
|
222
|
+
const center = `${fmt(cx)} ${fmt(cy)}`;
|
|
223
|
+
const normalize = (degrees: number) =>
|
|
224
|
+
roundTo(((degrees % 360) + 360) % 360, 4);
|
|
225
|
+
const last = merged.length - 1;
|
|
226
|
+
|
|
227
|
+
const wedges = merged.map(([t0, t1], index) => {
|
|
228
|
+
const color = colorAt(parsed, (t0 + t1) / 2);
|
|
229
|
+
const width = (t1 - t0) * 360;
|
|
230
|
+
// Shared alpha: reach under the neighbours (see above)
|
|
231
|
+
const back = uniform && index === 0 && last > 0 ? width : 0;
|
|
232
|
+
const ahead = uniform && index < last ? width : 0;
|
|
233
|
+
const from = start + t0 * 360 - back;
|
|
234
|
+
const to = start + t1 * 360 + ahead;
|
|
235
|
+
const [x0, y0] = toPoint(from);
|
|
236
|
+
const [x1, y1] = toPoint(to);
|
|
237
|
+
return {
|
|
238
|
+
d: `M${center}L${x0} ${y0}L${x1} ${y1}Z`,
|
|
239
|
+
color: toHex6(color),
|
|
240
|
+
...(uniform || color.a >= 1 ? {} : { opacity: roundTo(color.a, 3) }),
|
|
241
|
+
from: normalize(from),
|
|
242
|
+
sweep: roundTo(to - from, 4),
|
|
243
|
+
};
|
|
244
|
+
});
|
|
245
|
+
return uniform && firstAlpha < 1
|
|
246
|
+
? { wedges, opacity: roundTo(firstAlpha, 3) }
|
|
247
|
+
: { wedges };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Angles (degrees, 0–360 from +x, clockwise) of a point around a centre. */
|
|
251
|
+
export function angleAround(
|
|
252
|
+
cx: number,
|
|
253
|
+
cy: number,
|
|
254
|
+
x: number,
|
|
255
|
+
y: number
|
|
256
|
+
): number {
|
|
257
|
+
const degrees = (Math.atan2(y - cy, x - cx) * 180) / Math.PI;
|
|
258
|
+
return ((degrees % 360) + 360) % 360;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* The arc of directions (degrees from +x, clockwise) a box covers seen from
|
|
263
|
+
* (`cx`, `cy`), or null when the centre is inside the box (every direction).
|
|
264
|
+
*/
|
|
265
|
+
export function angularSpan(
|
|
266
|
+
cx: number,
|
|
267
|
+
cy: number,
|
|
268
|
+
box: { x: number; y: number; width: number; height: number }
|
|
269
|
+
): [number, number] | null {
|
|
270
|
+
const pad = 1e-6;
|
|
271
|
+
if (
|
|
272
|
+
cx >= box.x - pad &&
|
|
273
|
+
cx <= box.x + box.width + pad &&
|
|
274
|
+
cy >= box.y - pad &&
|
|
275
|
+
cy <= box.y + box.height + pad
|
|
276
|
+
) {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
const corners = [
|
|
280
|
+
[box.x, box.y],
|
|
281
|
+
[box.x + box.width, box.y],
|
|
282
|
+
[box.x, box.y + box.height],
|
|
283
|
+
[box.x + box.width, box.y + box.height],
|
|
284
|
+
] as const;
|
|
285
|
+
const middle = angleAround(
|
|
286
|
+
cx,
|
|
287
|
+
cy,
|
|
288
|
+
box.x + box.width / 2,
|
|
289
|
+
box.y + box.height / 2
|
|
290
|
+
);
|
|
291
|
+
let low = 0;
|
|
292
|
+
let high = 0;
|
|
293
|
+
for (const [x, y] of corners) {
|
|
294
|
+
// Signed difference from the middle direction, in (-180, 180]
|
|
295
|
+
let difference = angleAround(cx, cy, x, y) - middle;
|
|
296
|
+
if (difference > 180) {
|
|
297
|
+
difference -= 360;
|
|
298
|
+
} else if (difference <= -180) {
|
|
299
|
+
difference += 360;
|
|
300
|
+
}
|
|
301
|
+
low = Math.min(low, difference);
|
|
302
|
+
high = Math.max(high, difference);
|
|
303
|
+
}
|
|
304
|
+
return [middle + low, middle + high];
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** True when the arcs [a0, a1] and [b0, b1] (degrees, a1 ≥ a0) meet. */
|
|
308
|
+
export function arcsOverlap(a: [number, number], b: [number, number]): boolean {
|
|
309
|
+
const lengthA = a[1] - a[0];
|
|
310
|
+
const lengthB = b[1] - b[0];
|
|
311
|
+
if (lengthA >= 360 || lengthB >= 360) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
// Start of b relative to the start of a, in [0, 360)
|
|
315
|
+
const offset = (((b[0] - a[0]) % 360) + 360) % 360;
|
|
316
|
+
return offset <= lengthA || offset + lengthB >= 360;
|
|
317
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: color math */
|
|
2
|
+
|
|
3
|
+
import { roundTo } from "./fmt";
|
|
4
|
+
|
|
5
|
+
export interface RGBAColor {
|
|
6
|
+
r: number;
|
|
7
|
+
g: number;
|
|
8
|
+
b: number;
|
|
9
|
+
/** 0 (transparent) to 1 (opaque) */
|
|
10
|
+
a: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const NAMED_COLORS: Record<string, [number, number, number]> = {
|
|
14
|
+
black: [0, 0, 0],
|
|
15
|
+
white: [255, 255, 255],
|
|
16
|
+
red: [255, 0, 0],
|
|
17
|
+
green: [0, 128, 0],
|
|
18
|
+
lime: [0, 255, 0],
|
|
19
|
+
blue: [0, 0, 255],
|
|
20
|
+
navy: [0, 0, 128],
|
|
21
|
+
yellow: [255, 255, 0],
|
|
22
|
+
orange: [255, 165, 0],
|
|
23
|
+
purple: [128, 0, 128],
|
|
24
|
+
pink: [255, 192, 203],
|
|
25
|
+
gray: [128, 128, 128],
|
|
26
|
+
grey: [128, 128, 128],
|
|
27
|
+
silver: [192, 192, 192],
|
|
28
|
+
maroon: [128, 0, 0],
|
|
29
|
+
olive: [128, 128, 0],
|
|
30
|
+
teal: [0, 128, 128],
|
|
31
|
+
aqua: [0, 255, 255],
|
|
32
|
+
cyan: [0, 255, 255],
|
|
33
|
+
fuchsia: [255, 0, 255],
|
|
34
|
+
magenta: [255, 0, 255],
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
function parseChannel(value: string, max: number): number {
|
|
38
|
+
const trimmed = value.trim();
|
|
39
|
+
const number = Number.parseFloat(trimmed);
|
|
40
|
+
if (Number.isNaN(number)) {
|
|
41
|
+
return Number.NaN;
|
|
42
|
+
}
|
|
43
|
+
return trimmed.endsWith("%") ? (number / 100) * max : number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Parses hex (#rgb, #rgba, #rrggbb, #rrggbbaa), rgb()/rgba(), `transparent`
|
|
48
|
+
* and common color names. Returns null for anything else.
|
|
49
|
+
*/
|
|
50
|
+
export function parseColor(input: string | null | undefined): RGBAColor | null {
|
|
51
|
+
if (!input) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
const value = input.trim().toLowerCase();
|
|
55
|
+
|
|
56
|
+
if (value === "transparent") {
|
|
57
|
+
return { r: 0, g: 0, b: 0, a: 0 };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const named = NAMED_COLORS[value];
|
|
61
|
+
if (named) {
|
|
62
|
+
return { r: named[0], g: named[1], b: named[2], a: 1 };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (value.startsWith("#")) {
|
|
66
|
+
let hex = value.slice(1);
|
|
67
|
+
if (hex.length === 3 || hex.length === 4) {
|
|
68
|
+
hex = hex
|
|
69
|
+
.split("")
|
|
70
|
+
.map((char) => char + char)
|
|
71
|
+
.join("");
|
|
72
|
+
}
|
|
73
|
+
if (!/^[0-9a-f]{6}([0-9a-f]{2})?$/.test(hex)) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
r: Number.parseInt(hex.slice(0, 2), 16),
|
|
78
|
+
g: Number.parseInt(hex.slice(2, 4), 16),
|
|
79
|
+
b: Number.parseInt(hex.slice(4, 6), 16),
|
|
80
|
+
a: hex.length === 8 ? Number.parseInt(hex.slice(6, 8), 16) / 255 : 1,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const fn = /^rgba?\(([^)]*)\)$/.exec(value);
|
|
85
|
+
if (fn) {
|
|
86
|
+
const [channels, alpha] = fn[1].split("/");
|
|
87
|
+
const parts = channels.split(/[\s,]+/).filter(Boolean);
|
|
88
|
+
// Three channels, plus an alpha either after "/" or as a fourth value.
|
|
89
|
+
if (parts.length !== (alpha === undefined && parts.length === 4 ? 4 : 3)) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const alphaPart = alpha ?? parts[3];
|
|
93
|
+
const [r, g, b] = parts.slice(0, 3).map((part) => parseChannel(part, 255));
|
|
94
|
+
const a = alphaPart === undefined ? 1 : parseChannel(alphaPart, 1);
|
|
95
|
+
if ([r, g, b, a].some((channel) => Number.isNaN(channel))) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
const clamp = (channel: number, max: number) =>
|
|
99
|
+
Math.min(Math.max(channel, 0), max);
|
|
100
|
+
return {
|
|
101
|
+
r: clamp(r, 255),
|
|
102
|
+
g: clamp(g, 255),
|
|
103
|
+
b: clamp(b, 255),
|
|
104
|
+
a: clamp(a, 1),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Naive RGB → CMYK conversion (no ICC profile): K = 1 − max(R, G, B) and
|
|
113
|
+
* C/M/Y from what is left. Pure black maps to K only. Returns 0–1 values.
|
|
114
|
+
* Ask your printer for a proper color-managed conversion when exact brand
|
|
115
|
+
* colors matter.
|
|
116
|
+
*/
|
|
117
|
+
export function rgbToCmyk(
|
|
118
|
+
r: number,
|
|
119
|
+
g: number,
|
|
120
|
+
b: number
|
|
121
|
+
): [c: number, m: number, y: number, k: number] {
|
|
122
|
+
const red = r / 255;
|
|
123
|
+
const green = g / 255;
|
|
124
|
+
const blue = b / 255;
|
|
125
|
+
const k = 1 - Math.max(red, green, blue);
|
|
126
|
+
if (k >= 1) {
|
|
127
|
+
return [0, 0, 0, 1];
|
|
128
|
+
}
|
|
129
|
+
return [
|
|
130
|
+
roundTo((1 - red - k) / (1 - k)),
|
|
131
|
+
roundTo((1 - green - k) / (1 - k)),
|
|
132
|
+
roundTo((1 - blue - k) / (1 - k)),
|
|
133
|
+
roundTo(k),
|
|
134
|
+
];
|
|
135
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: path grammar */
|
|
2
|
+
// Strict reader for the path data a frame spec may hold. Unlike
|
|
3
|
+
// `parsePathData` (which reads markup this library wrote and stops quietly at
|
|
4
|
+
// the first malformed command), this rejects anything outside the grammar:
|
|
5
|
+
// only the SVG path commands, plain decimal numbers (no exponents), spaces
|
|
6
|
+
// and commas. Output is always re-serialized from the numbers it read, so no
|
|
7
|
+
// character of the input reaches an SVG, canvas, PDF or EPS file.
|
|
8
|
+
|
|
9
|
+
import { fmt } from "./fmt";
|
|
10
|
+
|
|
11
|
+
/** One path command with its own arguments (implicit repeats are split). */
|
|
12
|
+
export interface PathCommand {
|
|
13
|
+
/** Command letter as written: upper case is absolute, lower case relative */
|
|
14
|
+
command: string;
|
|
15
|
+
values: number[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const ARITY: Record<string, number> = {
|
|
19
|
+
M: 2,
|
|
20
|
+
L: 2,
|
|
21
|
+
H: 1,
|
|
22
|
+
V: 1,
|
|
23
|
+
C: 6,
|
|
24
|
+
S: 4,
|
|
25
|
+
Q: 4,
|
|
26
|
+
T: 2,
|
|
27
|
+
A: 7,
|
|
28
|
+
Z: 0,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** Characters path data in a frame spec may contain. */
|
|
32
|
+
export const PATH_DATA_CHARSET = /^[MmLlHhVvCcSsQqTtAaZz0-9.,+\-\s]*$/;
|
|
33
|
+
|
|
34
|
+
const NUMBER = /[+-]?(?:\d+\.?\d*|\.\d+)/y;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Reads path data into commands, or returns null when it breaks the grammar
|
|
38
|
+
* (unknown characters, a missing argument, an exponent, more than
|
|
39
|
+
* `maxDecimals` decimals, or not starting with a move).
|
|
40
|
+
*/
|
|
41
|
+
export function tokenizePathData(
|
|
42
|
+
d: string,
|
|
43
|
+
maxDecimals = Number.POSITIVE_INFINITY
|
|
44
|
+
): PathCommand[] | null {
|
|
45
|
+
if (!PATH_DATA_CHARSET.test(d)) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
const commands: PathCommand[] = [];
|
|
49
|
+
let index = 0;
|
|
50
|
+
const skip = () => {
|
|
51
|
+
while (index < d.length && /[\s,]/.test(d[index] as string)) {
|
|
52
|
+
index++;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const readNumber = (): number | null => {
|
|
56
|
+
skip();
|
|
57
|
+
NUMBER.lastIndex = index;
|
|
58
|
+
const match = NUMBER.exec(d);
|
|
59
|
+
if (!match) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const text = match[0];
|
|
63
|
+
const dot = text.indexOf(".");
|
|
64
|
+
if (dot >= 0 && text.length - dot - 1 > maxDecimals) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
index = NUMBER.lastIndex;
|
|
68
|
+
const value = Number(text);
|
|
69
|
+
return Number.isFinite(value) ? value : null;
|
|
70
|
+
};
|
|
71
|
+
const readFlag = (): number | null => {
|
|
72
|
+
skip();
|
|
73
|
+
const char = d[index];
|
|
74
|
+
if (char === "0" || char === "1") {
|
|
75
|
+
index++;
|
|
76
|
+
return char === "1" ? 1 : 0;
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
};
|
|
80
|
+
const startsNumber = () => {
|
|
81
|
+
skip();
|
|
82
|
+
return index < d.length && /[0-9.+-]/.test(d[index] as string);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
skip();
|
|
86
|
+
while (index < d.length) {
|
|
87
|
+
const letter = d[index] as string;
|
|
88
|
+
const upper = letter.toUpperCase();
|
|
89
|
+
const arity = ARITY[upper];
|
|
90
|
+
if (arity === undefined) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
if (commands.length === 0 && upper !== "M") {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
index++;
|
|
97
|
+
if (arity === 0) {
|
|
98
|
+
commands.push({ command: letter, values: [] });
|
|
99
|
+
skip();
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
let command = letter;
|
|
103
|
+
let first = true;
|
|
104
|
+
while (first || startsNumber()) {
|
|
105
|
+
const values: number[] = [];
|
|
106
|
+
for (let i = 0; i < arity; i++) {
|
|
107
|
+
const value =
|
|
108
|
+
upper === "A" && (i === 3 || i === 4) ? readFlag() : readNumber();
|
|
109
|
+
if (value === null) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
values.push(value);
|
|
113
|
+
}
|
|
114
|
+
commands.push({ command, values });
|
|
115
|
+
// Extra pairs after a move are line-tos
|
|
116
|
+
if (upper === "M") {
|
|
117
|
+
command = letter === "M" ? "L" : "l";
|
|
118
|
+
}
|
|
119
|
+
first = false;
|
|
120
|
+
}
|
|
121
|
+
skip();
|
|
122
|
+
}
|
|
123
|
+
return commands;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Segments a command adds once drawn (an arc becomes up to four curves). */
|
|
127
|
+
export function countPathSegments(commands: PathCommand[]): number {
|
|
128
|
+
let count = 0;
|
|
129
|
+
for (const { command } of commands) {
|
|
130
|
+
count += command.toUpperCase() === "A" ? 4 : 1;
|
|
131
|
+
}
|
|
132
|
+
return count;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Writes commands back as path data, scaled by `scale` and moved by
|
|
137
|
+
* (dx, dy). Relative commands are only scaled; arc radii scale and arc
|
|
138
|
+
* angles and flags are kept, so the shape is exact under the uniform scale.
|
|
139
|
+
*/
|
|
140
|
+
export function writePathData(
|
|
141
|
+
commands: PathCommand[],
|
|
142
|
+
scale = 1,
|
|
143
|
+
dx = 0,
|
|
144
|
+
dy = 0
|
|
145
|
+
): string {
|
|
146
|
+
let out = "";
|
|
147
|
+
for (const { command, values } of commands) {
|
|
148
|
+
const upper = command.toUpperCase();
|
|
149
|
+
const absolute = command === upper;
|
|
150
|
+
const ox = absolute ? dx : 0;
|
|
151
|
+
const oy = absolute ? dy : 0;
|
|
152
|
+
let args: string[];
|
|
153
|
+
switch (upper) {
|
|
154
|
+
case "Z":
|
|
155
|
+
args = [];
|
|
156
|
+
break;
|
|
157
|
+
case "H":
|
|
158
|
+
args = [fmt((values[0] as number) * scale + ox)];
|
|
159
|
+
break;
|
|
160
|
+
case "V":
|
|
161
|
+
args = [fmt((values[0] as number) * scale + oy)];
|
|
162
|
+
break;
|
|
163
|
+
case "A":
|
|
164
|
+
args = [
|
|
165
|
+
fmt(Math.abs(values[0] as number) * scale),
|
|
166
|
+
fmt(Math.abs(values[1] as number) * scale),
|
|
167
|
+
fmt(values[2] as number),
|
|
168
|
+
String(values[3]),
|
|
169
|
+
String(values[4]),
|
|
170
|
+
fmt((values[5] as number) * scale + ox),
|
|
171
|
+
fmt((values[6] as number) * scale + oy),
|
|
172
|
+
];
|
|
173
|
+
break;
|
|
174
|
+
default:
|
|
175
|
+
args = values.map((value, i) =>
|
|
176
|
+
fmt(value * scale + (i % 2 === 0 ? ox : oy))
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
out += command + args.join(" ");
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|