qrdx 0.0.59 → 0.0.61
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/README.md +4 -4
- package/dist/core.js +7 -7
- package/dist/core.js.map +1 -1
- package/dist/core.mjs +7 -7
- package/dist/core.mjs.map +1 -1
- package/dist/element.browser.mjs +6 -6
- package/dist/element.browser.mjs.map +1 -1
- package/dist/index.js +7 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7 -7
- package/dist/index.mjs.map +1 -1
- package/dist/node.js +2 -2
- package/dist/node.js.map +1 -1
- package/dist/node.mjs +2 -2
- package/dist/node.mjs.map +1 -1
- package/dist/qr-code-canvas.js +1 -1
- package/dist/qr-code-canvas.js.map +1 -1
- package/dist/qr-code-canvas.mjs +1 -1
- package/dist/qr-code-canvas.mjs.map +1 -1
- package/dist/schemas.js +1 -1
- package/dist/schemas.js.map +1 -1
- package/dist/schemas.mjs +1 -1
- package/dist/schemas.mjs.map +1 -1
- package/dist/src/api.d.ts +2 -3
- package/dist/src/api.d.ts.map +1 -1
- package/dist/src/corner-dot.d.ts.map +1 -1
- package/dist/src/corner-eye.d.ts.map +1 -1
- package/dist/src/engine.d.ts +1 -1
- package/dist/src/engine.d.ts.map +1 -1
- package/dist/src/looks.d.ts +45 -17
- package/dist/src/looks.d.ts.map +1 -1
- package/dist/src/qr-code-canvas.d.ts +2 -8
- package/dist/src/qr-code-canvas.d.ts.map +1 -1
- package/dist/src/qr-code-svg.d.ts +2 -3
- package/dist/src/qr-code-svg.d.ts.map +1 -1
- package/dist/src/qr-code.d.ts +2 -2
- package/dist/src/qr-code.d.ts.map +1 -1
- package/dist/src/qr-frame-svg.d.ts +2 -3
- package/dist/src/qr-frame-svg.d.ts.map +1 -1
- package/dist/src/rasterize.d.ts +8 -0
- package/dist/src/rasterize.d.ts.map +1 -1
- package/dist/src/react-elements.d.ts.map +1 -1
- package/dist/src/scan-risk.d.ts.map +1 -1
- package/dist/src/scan-score.d.ts +34 -0
- package/dist/src/scan-score.d.ts.map +1 -1
- package/dist/src/shapes.d.ts +19 -0
- package/dist/src/shapes.d.ts.map +1 -1
- package/dist/src/utils/oklab.d.ts +16 -0
- package/dist/src/utils/oklab.d.ts.map +1 -0
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/types/constants.d.ts +40 -5
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/corner-dot.d.ts +11 -6
- package/dist/types/corner-dot.d.ts.map +1 -1
- package/dist/types/corner-eye.d.ts +21 -8
- package/dist/types/corner-eye.d.ts.map +1 -1
- package/dist/types/frame-spec.d.ts +1 -1
- package/dist/types/frame.d.ts +2 -2
- package/dist/types/image-pattern.d.ts +3 -3
- package/dist/types/image-pattern.d.ts.map +1 -1
- package/dist/types/index.js +2 -2
- package/dist/types/index.js.map +1 -1
- package/dist/types/index.mjs +2 -2
- package/dist/types/index.mjs.map +1 -1
- package/dist/types/plain.d.ts +1 -1
- package/dist/types/plain.d.ts.map +1 -1
- package/dist/types/qr.d.ts +96 -46
- package/dist/types/qr.d.ts.map +1 -1
- package/dist/types/react.d.ts +21 -3
- package/dist/types/react.d.ts.map +1 -1
- package/dist/vanilla.browser.mjs +29 -29
- package/dist/vanilla.browser.mjs.map +1 -1
- package/package.json +4 -6
- package/src/api.tsx +3 -4
- package/src/corner-dot.tsx +15 -0
- package/src/corner-eye.ts +163 -72
- package/src/engine.ts +4 -0
- package/src/looks.ts +272 -69
- package/src/qr-code-canvas.tsx +34 -27
- package/src/qr-code-svg.tsx +3 -4
- package/src/qr-code.tsx +7 -5
- package/src/qr-frame-svg.tsx +3 -4
- package/src/rasterize.ts +14 -1
- package/src/react-elements.ts +40 -2
- package/src/scan-risk.ts +19 -3
- package/src/scan-score.ts +231 -48
- package/src/shapes.ts +123 -0
- package/src/utils/oklab.ts +93 -0
- package/src/utils.tsx +20 -4
- package/types/constants.ts +59 -3
- package/types/corner-dot.ts +17 -7
- package/types/corner-eye.ts +26 -8
- package/types/image-pattern.ts +11 -4
- package/types/plain.ts +4 -0
- package/types/qr.ts +5 -3
- package/types/react.ts +22 -3
package/src/scan-risk.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type { QRProps } from "../types";
|
|
2
|
+
import {
|
|
3
|
+
WITHDRAWN_BODY_PATTERNS,
|
|
4
|
+
WITHDRAWN_CORNER_EYE_DOT_PATTERNS,
|
|
5
|
+
WITHDRAWN_CORNER_EYE_PATTERNS,
|
|
6
|
+
withdrawnFallback,
|
|
7
|
+
} from "../types/constants";
|
|
2
8
|
import type { CornerEyeDotPattern } from "../types/corner-dot";
|
|
3
9
|
import type { CornerEyePattern } from "../types/corner-eye";
|
|
4
10
|
import type { BodyPattern } from "../types/image-pattern";
|
|
@@ -155,9 +161,19 @@ export function getStylePairRisk(
|
|
|
155
161
|
"bodyPattern" | "cornerEyePattern" | "cornerEyeDotPattern"
|
|
156
162
|
>
|
|
157
163
|
): StylePairRisk | null {
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
164
|
+
// A withdrawn shape is judged as the one it draws as
|
|
165
|
+
const body = withdrawnFallback(
|
|
166
|
+
WITHDRAWN_BODY_PATTERNS,
|
|
167
|
+
props.bodyPattern ?? "circle"
|
|
168
|
+
);
|
|
169
|
+
const eye = withdrawnFallback(
|
|
170
|
+
WITHDRAWN_CORNER_EYE_PATTERNS,
|
|
171
|
+
props.cornerEyePattern ?? "gear"
|
|
172
|
+
);
|
|
173
|
+
const dot = withdrawnFallback(
|
|
174
|
+
WITHDRAWN_CORNER_EYE_DOT_PATTERNS,
|
|
175
|
+
props.cornerEyeDotPattern ?? "circle"
|
|
176
|
+
);
|
|
161
177
|
if (WEAK_DOT_EYES[dot]?.includes(eye)) {
|
|
162
178
|
return {
|
|
163
179
|
kind: "dot-eye",
|
package/src/scan-score.ts
CHANGED
|
@@ -110,21 +110,75 @@ function matches(result: JsQRResult | null, value: string): boolean {
|
|
|
110
110
|
|
|
111
111
|
/**
|
|
112
112
|
* Camera-like degradation, in place: contrast loss plus a blur of about a
|
|
113
|
-
* quarter module (two box-blur passes approximate a Gaussian).
|
|
113
|
+
* quarter module (two box-blur passes approximate a Gaussian). Each channel
|
|
114
|
+
* is rounded to a byte after every step, as drawing would.
|
|
114
115
|
*/
|
|
115
116
|
function simulateCamera(image: ImageData, modulePx: number, contrast = 0.7) {
|
|
116
117
|
const { data, width, height } = image;
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
118
|
+
const count = width * height;
|
|
119
|
+
const radius = Math.max(1, Math.round(modulePx * 0.25));
|
|
120
|
+
// Grey artwork (black on white, anti-aliased to greys) has one channel's
|
|
121
|
+
// worth of values: blur it once and copy it out, a third of the work. The
|
|
122
|
+
// bytes are exactly those blurring red, green and blue gives.
|
|
123
|
+
const grey = greyChannel(data, count);
|
|
124
|
+
if (grey) {
|
|
125
|
+
for (let p = 0; p < count; p++) {
|
|
126
|
+
grey[p] = 128 + ((grey[p] as number) - 128) * contrast;
|
|
127
|
+
}
|
|
128
|
+
boxBlur(grey, width, height, radius);
|
|
129
|
+
for (let p = 0, i = 0; p < count; p++, i += 4) {
|
|
130
|
+
const value = grey[p] as number;
|
|
131
|
+
data[i] = value;
|
|
132
|
+
data[i + 1] = value;
|
|
133
|
+
data[i + 2] = value;
|
|
134
|
+
}
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
// Colours: each channel on its own plane (the same arithmetic, read and
|
|
138
|
+
// written in order rather than every fourth byte).
|
|
139
|
+
const plane = new Uint8ClampedArray(count);
|
|
140
|
+
for (let c = 0; c < 3; c++) {
|
|
141
|
+
for (let p = 0, i = c; p < count; p++, i += 4) {
|
|
142
|
+
plane[p] = 128 + ((data[i] as number) - 128) * contrast;
|
|
143
|
+
}
|
|
144
|
+
boxBlur(plane, width, height, radius);
|
|
145
|
+
for (let p = 0, i = c; p < count; p++, i += 4) {
|
|
146
|
+
data[i] = plane[p] as number;
|
|
120
147
|
}
|
|
121
148
|
}
|
|
122
|
-
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The red channel when every pixel is grey (red = green = blue), else null. */
|
|
152
|
+
function greyChannel(
|
|
153
|
+
data: Uint8ClampedArray,
|
|
154
|
+
count: number
|
|
155
|
+
): Uint8ClampedArray | null {
|
|
156
|
+
const grey = new Uint8ClampedArray(count);
|
|
157
|
+
for (let p = 0, i = 0; p < count; p++, i += 4) {
|
|
158
|
+
const red = data[i] as number;
|
|
159
|
+
if (red !== data[i + 1] || red !== data[i + 2]) {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
grey[p] = red;
|
|
163
|
+
}
|
|
164
|
+
return grey;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Two passes of a box blur `2 * radius + 1` wide, across then down, on one
|
|
169
|
+
* channel in place, rounding to a byte after each. Sliding window with
|
|
170
|
+
* clamped edges: pixel (line, position) is `line * lineStep + position *
|
|
171
|
+
* step`, and the sum is a plain local (this runs up to nine times a score,
|
|
172
|
+
* on every pixel).
|
|
173
|
+
*/
|
|
174
|
+
function boxBlur(
|
|
175
|
+
channel: Uint8ClampedArray,
|
|
176
|
+
width: number,
|
|
177
|
+
height: number,
|
|
178
|
+
radius: number
|
|
179
|
+
) {
|
|
123
180
|
const span = 2 * radius + 1;
|
|
124
|
-
const buffer = new Float32Array(width * height
|
|
125
|
-
// Sliding window with clamped edges. Pixel (line, position) is
|
|
126
|
-
// `line * lineStep + position * step`; the sums are plain locals (this
|
|
127
|
-
// runs nine times a score, on every pixel).
|
|
181
|
+
const buffer = new Float32Array(width * height);
|
|
128
182
|
const blur = (horizontal: boolean) => {
|
|
129
183
|
const lines = horizontal ? height : width;
|
|
130
184
|
const length = horizontal ? width : height;
|
|
@@ -132,33 +186,24 @@ function simulateCamera(image: ImageData, modulePx: number, contrast = 0.7) {
|
|
|
132
186
|
const step = horizontal ? 1 : width;
|
|
133
187
|
for (let line = 0; line < lines; line++) {
|
|
134
188
|
const start = line * lineStep;
|
|
135
|
-
let
|
|
136
|
-
let green = 0;
|
|
137
|
-
let blue = 0;
|
|
189
|
+
let sum = 0;
|
|
138
190
|
for (let k = -radius; k <= radius; k++) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
blue += data[p + 2] as number;
|
|
191
|
+
sum += channel[
|
|
192
|
+
start + Math.min(Math.max(k, 0), length - 1) * step
|
|
193
|
+
] as number;
|
|
143
194
|
}
|
|
144
195
|
for (let position = 0; position < length; position++) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
red = red - (data[out] as number) + (data[into] as number);
|
|
153
|
-
green = green - (data[out + 1] as number) + (data[into + 1] as number);
|
|
154
|
-
blue = blue - (data[out + 2] as number) + (data[into + 2] as number);
|
|
196
|
+
buffer[start + position * step] = sum / span;
|
|
197
|
+
sum =
|
|
198
|
+
sum -
|
|
199
|
+
(channel[start + Math.max(position - radius, 0) * step] as number) +
|
|
200
|
+
(channel[
|
|
201
|
+
start + Math.min(position + radius + 1, length - 1) * step
|
|
202
|
+
] as number);
|
|
155
203
|
}
|
|
156
204
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
data[o + 1] = buffer[i + 1] as number;
|
|
160
|
-
data[o + 2] = buffer[i + 2] as number;
|
|
161
|
-
}
|
|
205
|
+
// Rounds and clamps each value to a byte, as assigning one does
|
|
206
|
+
channel.set(buffer);
|
|
162
207
|
};
|
|
163
208
|
for (let pass = 0; pass < 2; pass++) {
|
|
164
209
|
blur(true);
|
|
@@ -182,10 +227,16 @@ function createCanvas(width: number, height: number) {
|
|
|
182
227
|
// A phone tries many frames at slightly different distances.
|
|
183
228
|
const ATTEMPT_SCALES = [0.9, 1, 1.1];
|
|
184
229
|
|
|
230
|
+
/** Code widths (px) `decodeAtSize` draws for `size`, in the order it tries them. */
|
|
231
|
+
function attemptWidths(size: number): number[] {
|
|
232
|
+
return ATTEMPT_SCALES.map((scale) => size * scale);
|
|
233
|
+
}
|
|
234
|
+
|
|
185
235
|
/**
|
|
186
236
|
* Decodes the code at `size` px wide a few times at slightly different
|
|
187
237
|
* scales through the camera simulation. `draw` returns the pixels with the
|
|
188
|
-
* code `codeWidth` px wide.
|
|
238
|
+
* code `codeWidth` px wide. Two hits are a pass whatever the rest would
|
|
239
|
+
* read, so it stops there.
|
|
189
240
|
*/
|
|
190
241
|
function decodeAtSize(
|
|
191
242
|
jsQR: Decoder,
|
|
@@ -195,12 +246,14 @@ function decodeAtSize(
|
|
|
195
246
|
expected: (result: JsQRResult | null) => boolean
|
|
196
247
|
): DecodeState {
|
|
197
248
|
let hits = 0;
|
|
198
|
-
for (const
|
|
199
|
-
const codeWidth = size * scale;
|
|
249
|
+
for (const codeWidth of attemptWidths(size)) {
|
|
200
250
|
const image = draw(codeWidth);
|
|
201
251
|
simulateCamera(image, codeWidth / moduleCount);
|
|
202
252
|
if (expected(jsQR(image.data, image.width, image.height))) {
|
|
203
253
|
hits++;
|
|
254
|
+
if (hits >= 2) {
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
204
257
|
}
|
|
205
258
|
}
|
|
206
259
|
if (hits >= 2) {
|
|
@@ -793,7 +846,11 @@ function quietZoneCheck(props: QRProps): { check: ScanCheck; penalty: number } {
|
|
|
793
846
|
};
|
|
794
847
|
}
|
|
795
848
|
|
|
796
|
-
/**
|
|
849
|
+
/**
|
|
850
|
+
* A halo the engine refuses to draw (looks.ts getHaloRisk): the design
|
|
851
|
+
* asked for a look it can't have. A rim drawn in another color than the
|
|
852
|
+
* one picked, to stay readable, passes and says which.
|
|
853
|
+
*/
|
|
797
854
|
function haloCheck(props: QRProps): {
|
|
798
855
|
check: ScanCheck | null;
|
|
799
856
|
penalty: number;
|
|
@@ -802,6 +859,12 @@ function haloCheck(props: QRProps): {
|
|
|
802
859
|
if (!risk) {
|
|
803
860
|
return { check: null, penalty: 0 };
|
|
804
861
|
}
|
|
862
|
+
if (risk.reason === "adjusted") {
|
|
863
|
+
return {
|
|
864
|
+
check: { id: "halo", passed: true, message: risk.message },
|
|
865
|
+
penalty: 0,
|
|
866
|
+
};
|
|
867
|
+
}
|
|
805
868
|
return {
|
|
806
869
|
check: { id: "halo", passed: false, message: risk.message, fix: risk.fix },
|
|
807
870
|
penalty: 15,
|
|
@@ -1043,6 +1106,97 @@ export async function getScanScore(
|
|
|
1043
1106
|
options: ScanScoreOptions = {}
|
|
1044
1107
|
): Promise<ScanScoreResult> {
|
|
1045
1108
|
assertBrowser();
|
|
1109
|
+
const { draw, sizes, imageTones } = await prepareScan(props, options);
|
|
1110
|
+
return await scoreArtwork(props, draw, sizes, options.signal, imageTones);
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
/**
|
|
1114
|
+
* One frame `getScanScore` decodes: the artwork on white with the code
|
|
1115
|
+
* `codeWidth` px wide, before the camera model.
|
|
1116
|
+
*/
|
|
1117
|
+
export interface ScanFrame {
|
|
1118
|
+
codeWidth: number;
|
|
1119
|
+
width: number;
|
|
1120
|
+
height: number;
|
|
1121
|
+
/** RGBA bytes, `width × height × 4` */
|
|
1122
|
+
data: Uint8ClampedArray;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
export interface ScanFrames {
|
|
1126
|
+
/** The test sizes, smallest first */
|
|
1127
|
+
sizes: number[];
|
|
1128
|
+
/** Every frame the score may decode, smallest code first */
|
|
1129
|
+
frames: ScanFrame[];
|
|
1130
|
+
/** Tones of the images the design paints with, for the contrast checks */
|
|
1131
|
+
imageTones: ImageToneMap;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
/**
|
|
1135
|
+
* The frames `getScanScore` decodes, drawn on the page's canvas (browser
|
|
1136
|
+
* only), for `getScanScoreFromFrames` to score somewhere else, such as a Web
|
|
1137
|
+
* Worker: drawing is quick native work, and the camera model and decoding
|
|
1138
|
+
* are what keep the page busy. It can be posted as it is (transfer each
|
|
1139
|
+
* frame's `data.buffer` to skip the copy).
|
|
1140
|
+
*/
|
|
1141
|
+
export async function drawScanFrames(
|
|
1142
|
+
props: QRProps,
|
|
1143
|
+
options: ScanScoreOptions = {}
|
|
1144
|
+
): Promise<ScanFrames> {
|
|
1145
|
+
assertBrowser();
|
|
1146
|
+
const { draw, sizes, imageTones } = await prepareScan(props, options);
|
|
1147
|
+
const frames = sizes.flatMap((size) =>
|
|
1148
|
+
attemptWidths(size).map((codeWidth) => {
|
|
1149
|
+
const { data, width, height } = draw(codeWidth);
|
|
1150
|
+
return { codeWidth, width, height, data };
|
|
1151
|
+
})
|
|
1152
|
+
);
|
|
1153
|
+
return { sizes, frames, imageTones };
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
/**
|
|
1157
|
+
* `getScanScore`'s result from the frames `drawScanFrames` drew for the same
|
|
1158
|
+
* props. Needs no DOM, so it runs in a Web Worker. The frames aren't changed
|
|
1159
|
+
* (each is copied before the camera model blurs it), so they can be scored
|
|
1160
|
+
* again.
|
|
1161
|
+
*/
|
|
1162
|
+
export async function getScanScoreFromFrames(
|
|
1163
|
+
props: QRProps,
|
|
1164
|
+
scan: ScanFrames,
|
|
1165
|
+
options: Pick<ScanScoreOptions, "signal"> = {}
|
|
1166
|
+
): Promise<ScanScoreResult> {
|
|
1167
|
+
const byWidth = new Map(scan.frames.map((frame) => [frame.codeWidth, frame]));
|
|
1168
|
+
const draw = (codeWidth: number) => {
|
|
1169
|
+
const frame = byWidth.get(codeWidth);
|
|
1170
|
+
if (!frame || frame.data.length < frame.width * frame.height * 4) {
|
|
1171
|
+
throw new Error(
|
|
1172
|
+
`No scan frame with the code ${codeWidth} px wide: draw the frames with drawScanFrames for these props and sizes`
|
|
1173
|
+
);
|
|
1174
|
+
}
|
|
1175
|
+
return {
|
|
1176
|
+
data: new Uint8ClampedArray(frame.data),
|
|
1177
|
+
width: frame.width,
|
|
1178
|
+
height: frame.height,
|
|
1179
|
+
colorSpace: "srgb",
|
|
1180
|
+
} as ImageData;
|
|
1181
|
+
};
|
|
1182
|
+
return await scoreArtwork(
|
|
1183
|
+
props,
|
|
1184
|
+
draw,
|
|
1185
|
+
scan.sizes,
|
|
1186
|
+
options.signal,
|
|
1187
|
+
scan.imageTones
|
|
1188
|
+
);
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
/**
|
|
1192
|
+
* What `getScanScore` does on the page before decoding: logos and image
|
|
1193
|
+
* fills inlined, the images' tones read and the artwork loaded, with a
|
|
1194
|
+
* `draw` that puts it on white at any code width.
|
|
1195
|
+
*/
|
|
1196
|
+
async function prepareScan(
|
|
1197
|
+
props: QRProps,
|
|
1198
|
+
options: ScanScoreOptions
|
|
1199
|
+
): Promise<{ draw: DrawArtwork; sizes: number[]; imageTones: ImageToneMap }> {
|
|
1046
1200
|
const { signal } = options;
|
|
1047
1201
|
const sizes = testSizes(options);
|
|
1048
1202
|
|
|
@@ -1086,7 +1240,7 @@ export async function getScanScore(
|
|
|
1086
1240
|
canvas.remove();
|
|
1087
1241
|
return image;
|
|
1088
1242
|
};
|
|
1089
|
-
return
|
|
1243
|
+
return { draw: drawArtwork, sizes, imageTones };
|
|
1090
1244
|
}
|
|
1091
1245
|
|
|
1092
1246
|
/**
|
|
@@ -1124,9 +1278,10 @@ export async function getScanScoreFromPixels(
|
|
|
1124
1278
|
const sourceCodeWidth =
|
|
1125
1279
|
(width * size * getTemplateCodeShare(props)) / artwork.width;
|
|
1126
1280
|
const source = { data: onWhite(pixels), width, height };
|
|
1281
|
+
const grey = isGrey(source.data);
|
|
1127
1282
|
|
|
1128
1283
|
const drawArtwork = (codeWidth: number) => {
|
|
1129
|
-
const art = resizePixels(source, codeWidth / sourceCodeWidth);
|
|
1284
|
+
const art = resizePixels(source, codeWidth / sourceCodeWidth, grey);
|
|
1130
1285
|
const pad = Math.max(8, Math.round(codeWidth * 0.12));
|
|
1131
1286
|
return padWhite(art, pad);
|
|
1132
1287
|
};
|
|
@@ -1559,10 +1714,26 @@ export interface PixelScanResult {
|
|
|
1559
1714
|
/** Code widths (px) `scanQRPixels` checks through the camera model. */
|
|
1560
1715
|
export const PIXEL_SCAN_SIZES = [320] as const;
|
|
1561
1716
|
|
|
1562
|
-
/**
|
|
1717
|
+
/** True when every pixel is grey (red = green = blue). */
|
|
1718
|
+
function isGrey(data: Uint8ClampedArray): boolean {
|
|
1719
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
1720
|
+
if (data[i] !== data[i + 1] || data[i] !== data[i + 2]) {
|
|
1721
|
+
return false;
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
return true;
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
/**
|
|
1728
|
+
* Area-average resize of RGBA pixels by `scale` (at most 1), for decoding:
|
|
1729
|
+
* the result is opaque (alpha isn't averaged; jsQR doesn't read it). With
|
|
1730
|
+
* `grey` (every source pixel grey, see `isGrey`) only red is averaged and
|
|
1731
|
+
* copied to green and blue, which is what averaging each gives.
|
|
1732
|
+
*/
|
|
1563
1733
|
function resizePixels(
|
|
1564
1734
|
source: { data: Uint8ClampedArray; width: number; height: number },
|
|
1565
|
-
scale: number
|
|
1735
|
+
scale: number,
|
|
1736
|
+
grey = false
|
|
1566
1737
|
): ImageData {
|
|
1567
1738
|
const factor = Math.min(1, scale);
|
|
1568
1739
|
const width = Math.max(1, Math.round(source.width * factor));
|
|
@@ -1570,6 +1741,7 @@ function resizePixels(
|
|
|
1570
1741
|
const out = new Uint8ClampedArray(width * height * 4);
|
|
1571
1742
|
const stepX = source.width / width;
|
|
1572
1743
|
const stepY = source.height / height;
|
|
1744
|
+
const { data } = source;
|
|
1573
1745
|
for (let y = 0; y < height; y++) {
|
|
1574
1746
|
const y0 = Math.floor(y * stepY);
|
|
1575
1747
|
const y1 = Math.max(
|
|
@@ -1582,26 +1754,36 @@ function resizePixels(
|
|
|
1582
1754
|
x0 + 1,
|
|
1583
1755
|
Math.min(source.width, Math.floor((x + 1) * stepX))
|
|
1584
1756
|
);
|
|
1757
|
+
const count = (y1 - y0) * (x1 - x0);
|
|
1758
|
+
const o = (y * width + x) * 4;
|
|
1585
1759
|
// Plain locals, not an array: this runs on every output pixel
|
|
1586
1760
|
let red = 0;
|
|
1761
|
+
if (grey) {
|
|
1762
|
+
for (let sy = y0; sy < y1; sy++) {
|
|
1763
|
+
for (let sx = x0; sx < x1; sx++) {
|
|
1764
|
+
red += data[(sy * source.width + sx) * 4] as number;
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
out[o] = red / count;
|
|
1768
|
+
out[o + 1] = out[o] as number;
|
|
1769
|
+
out[o + 2] = out[o] as number;
|
|
1770
|
+
out[o + 3] = 255;
|
|
1771
|
+
continue;
|
|
1772
|
+
}
|
|
1587
1773
|
let green = 0;
|
|
1588
1774
|
let blue = 0;
|
|
1589
|
-
let alpha = 0;
|
|
1590
1775
|
for (let sy = y0; sy < y1; sy++) {
|
|
1591
1776
|
for (let sx = x0; sx < x1; sx++) {
|
|
1592
1777
|
const i = (sy * source.width + sx) * 4;
|
|
1593
|
-
red +=
|
|
1594
|
-
green +=
|
|
1595
|
-
blue +=
|
|
1596
|
-
alpha += source.data[i + 3] as number;
|
|
1778
|
+
red += data[i] as number;
|
|
1779
|
+
green += data[i + 1] as number;
|
|
1780
|
+
blue += data[i + 2] as number;
|
|
1597
1781
|
}
|
|
1598
1782
|
}
|
|
1599
|
-
const count = (y1 - y0) * (x1 - x0);
|
|
1600
|
-
const o = (y * width + x) * 4;
|
|
1601
1783
|
out[o] = red / count;
|
|
1602
1784
|
out[o + 1] = green / count;
|
|
1603
1785
|
out[o + 2] = blue / count;
|
|
1604
|
-
out[o + 3] =
|
|
1786
|
+
out[o + 3] = 255;
|
|
1605
1787
|
}
|
|
1606
1788
|
}
|
|
1607
1789
|
return { data: out, width, height, colorSpace: "srgb" } as ImageData;
|
|
@@ -1660,10 +1842,11 @@ export async function scanQRPixels(
|
|
|
1660
1842
|
distance(location.topLeftCorner, location.bottomLeftCorner) +
|
|
1661
1843
|
distance(location.topRightCorner, location.bottomRightCorner)) /
|
|
1662
1844
|
4;
|
|
1845
|
+
const grey = isGrey(source.data);
|
|
1663
1846
|
for (const size of options.sizes ?? PIXEL_SCAN_SIZES) {
|
|
1664
1847
|
const state = decodeAtSize(
|
|
1665
1848
|
jsQR,
|
|
1666
|
-
(target) => resizePixels(source, target / codeWidth),
|
|
1849
|
+
(target) => resizePixels(source, target / codeWidth, grey),
|
|
1667
1850
|
size,
|
|
1668
1851
|
dimension,
|
|
1669
1852
|
(result) => matches(result, expected)
|
package/src/shapes.ts
CHANGED
|
@@ -57,6 +57,25 @@ export function roundedRectPath(
|
|
|
57
57
|
);
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/** Square with `cut` chamfered off each corner, clockwise from the top. */
|
|
61
|
+
export function octagonPoints(
|
|
62
|
+
x: number,
|
|
63
|
+
y: number,
|
|
64
|
+
size: number,
|
|
65
|
+
cut: number
|
|
66
|
+
): Point[] {
|
|
67
|
+
return [
|
|
68
|
+
[x + cut, y],
|
|
69
|
+
[x + size - cut, y],
|
|
70
|
+
[x + size, y + cut],
|
|
71
|
+
[x + size, y + size - cut],
|
|
72
|
+
[x + size - cut, y + size],
|
|
73
|
+
[x + cut, y + size],
|
|
74
|
+
[x, y + size - cut],
|
|
75
|
+
[x, y + cut],
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
|
|
60
79
|
/** Points of a regular polygon (clockwise), first vertex at `rotation` degrees. */
|
|
61
80
|
export function regularPolygon(
|
|
62
81
|
cx: number,
|
|
@@ -167,3 +186,107 @@ export function hashString(value: string): string {
|
|
|
167
186
|
}
|
|
168
187
|
return (h >>> 0).toString(36);
|
|
169
188
|
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Rectangle whose corners are scooped out by concave quarter circles of
|
|
192
|
+
* radius `scoop` (clockwise, like roundedRectPath).
|
|
193
|
+
*/
|
|
194
|
+
export function scoopedRectPath(
|
|
195
|
+
x: number,
|
|
196
|
+
y: number,
|
|
197
|
+
width: number,
|
|
198
|
+
height: number,
|
|
199
|
+
scoop: number
|
|
200
|
+
): string {
|
|
201
|
+
const s = Math.max(0, Math.min(scoop, Math.min(width, height) / 2));
|
|
202
|
+
const arc = (toX: number, toY: number) =>
|
|
203
|
+
`A${fmt(s)} ${fmt(s)} 0 0 0 ${fmt(toX)} ${fmt(toY)}`;
|
|
204
|
+
return (
|
|
205
|
+
`M${fmt(x + s)} ${fmt(y)}` +
|
|
206
|
+
`H${fmt(x + width - s)}${arc(x + width, y + s)}` +
|
|
207
|
+
`V${fmt(y + height - s)}${arc(x + width - s, y + height)}` +
|
|
208
|
+
`H${fmt(x + s)}${arc(x, y + height - s)}` +
|
|
209
|
+
`V${fmt(y + s)}${arc(x + s, y)}Z`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Rectangle with a square notch of side `notch` cut from each corner. */
|
|
214
|
+
export function notchedRectPath(
|
|
215
|
+
x: number,
|
|
216
|
+
y: number,
|
|
217
|
+
width: number,
|
|
218
|
+
height: number,
|
|
219
|
+
notch: number
|
|
220
|
+
): string {
|
|
221
|
+
const n = Math.max(0, Math.min(notch, Math.min(width, height) / 2));
|
|
222
|
+
return polygonPath([
|
|
223
|
+
[x + n, y],
|
|
224
|
+
[x + width - n, y],
|
|
225
|
+
[x + width - n, y + n],
|
|
226
|
+
[x + width, y + n],
|
|
227
|
+
[x + width, y + height - n],
|
|
228
|
+
[x + width - n, y + height - n],
|
|
229
|
+
[x + width - n, y + height],
|
|
230
|
+
[x + n, y + height],
|
|
231
|
+
[x + n, y + height - n],
|
|
232
|
+
[x, y + height - n],
|
|
233
|
+
[x, y + n],
|
|
234
|
+
[x + n, y + n],
|
|
235
|
+
]);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Rounded square whose sides pinch in slightly, like a cushion. Sides meet
|
|
240
|
+
* the corners tangentially, so the outline has no kinks.
|
|
241
|
+
*/
|
|
242
|
+
export function cushionPath(
|
|
243
|
+
x: number,
|
|
244
|
+
y: number,
|
|
245
|
+
size: number,
|
|
246
|
+
radius: number,
|
|
247
|
+
pinch: number
|
|
248
|
+
): string {
|
|
249
|
+
const r = radius;
|
|
250
|
+
const mid = size / 2;
|
|
251
|
+
const pull = (mid - r) * 0.55;
|
|
252
|
+
const P = (px: number, py: number) => `${fmt(x + px)} ${fmt(y + py)}`;
|
|
253
|
+
return (
|
|
254
|
+
`M${P(r, 0)}` +
|
|
255
|
+
`C${P(r + pull, 0)} ${P(mid - pull, pinch)} ${P(mid, pinch)}` +
|
|
256
|
+
`C${P(mid + pull, pinch)} ${P(size - r - pull, 0)} ${P(size - r, 0)}` +
|
|
257
|
+
`A${fmt(r)} ${fmt(r)} 0 0 1 ${P(size, r)}` +
|
|
258
|
+
`C${P(size, r + pull)} ${P(size - pinch, mid - pull)} ${P(size - pinch, mid)}` +
|
|
259
|
+
`C${P(size - pinch, mid + pull)} ${P(size, size - r - pull)} ${P(size, size - r)}` +
|
|
260
|
+
`A${fmt(r)} ${fmt(r)} 0 0 1 ${P(size - r, size)}` +
|
|
261
|
+
`C${P(size - r - pull, size)} ${P(mid + pull, size - pinch)} ${P(mid, size - pinch)}` +
|
|
262
|
+
`C${P(mid - pull, size - pinch)} ${P(r + pull, size)} ${P(r, size)}` +
|
|
263
|
+
`A${fmt(r)} ${fmt(r)} 0 0 1 ${P(0, size - r)}` +
|
|
264
|
+
`C${P(0, size - r - pull)} ${P(pinch, mid + pull)} ${P(pinch, mid)}` +
|
|
265
|
+
`C${P(pinch, mid - pull)} ${P(0, r + pull)} ${P(0, r)}` +
|
|
266
|
+
`A${fmt(r)} ${fmt(r)} 0 0 1 ${P(r, 0)}Z`
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Shield: flat top with corners of `radius`, straight sides down to
|
|
272
|
+
* `shoulder`, then curving to a point at the bottom. `m` scales the curve.
|
|
273
|
+
*/
|
|
274
|
+
export function shieldPath(
|
|
275
|
+
x: number,
|
|
276
|
+
y: number,
|
|
277
|
+
size: number,
|
|
278
|
+
radius: number,
|
|
279
|
+
shoulder: number,
|
|
280
|
+
m: number
|
|
281
|
+
): string {
|
|
282
|
+
const mid = size / 2;
|
|
283
|
+
const P = (px: number, py: number) => `${fmt(x + px)} ${fmt(y + py)}`;
|
|
284
|
+
return (
|
|
285
|
+
`M${P(radius, 0)}H${fmt(x + size - radius)}` +
|
|
286
|
+
`A${fmt(radius)} ${fmt(radius)} 0 0 1 ${P(size, radius)}` +
|
|
287
|
+
`V${fmt(y + shoulder)}` +
|
|
288
|
+
`C${P(size, shoulder + 1.55 * m)} ${P(mid + 1.6 * m, size)} ${P(mid, size)}` +
|
|
289
|
+
`C${P(mid - 1.6 * m, size)} ${P(0, shoulder + 1.55 * m)} ${P(0, shoulder)}` +
|
|
290
|
+
`V${fmt(y + radius)}A${fmt(radius)} ${fmt(radius)} 0 0 1 ${P(radius, 0)}Z`
|
|
291
|
+
);
|
|
292
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: color math */
|
|
2
|
+
// sRGB to OKLCh and back (Björn Ottosson's OKLab matrices), so a color's
|
|
3
|
+
// lightness can move while its hue stays: the halo's rim (looks.ts
|
|
4
|
+
// fitHaloColor) keeps the hue a person picked and only changes how light
|
|
5
|
+
// it is.
|
|
6
|
+
import type { RGBAColor } from "./parse-color";
|
|
7
|
+
|
|
8
|
+
/** OKLab lightness (0 to 1), chroma and hue (radians). */
|
|
9
|
+
export interface OkLch {
|
|
10
|
+
l: number;
|
|
11
|
+
c: number;
|
|
12
|
+
h: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const toLinear = (channel: number) => {
|
|
16
|
+
const value = channel / 255;
|
|
17
|
+
return value <= 0.040_45 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const toGamma = (value: number) =>
|
|
21
|
+
255 *
|
|
22
|
+
(value <= 0.003_130_8 ? 12.92 * value : 1.055 * value ** (1 / 2.4) - 0.055);
|
|
23
|
+
|
|
24
|
+
/** An sRGB color (channels 0 to 255) in OKLCh; its alpha is dropped. */
|
|
25
|
+
export function rgbToOklch({ r, g, b }: RGBAColor): OkLch {
|
|
26
|
+
const red = toLinear(r);
|
|
27
|
+
const green = toLinear(g);
|
|
28
|
+
const blue = toLinear(b);
|
|
29
|
+
const l = Math.cbrt(
|
|
30
|
+
0.412_221_470_8 * red + 0.536_332_536_3 * green + 0.051_445_992_9 * blue
|
|
31
|
+
);
|
|
32
|
+
const m = Math.cbrt(
|
|
33
|
+
0.211_903_498_2 * red + 0.680_699_545_1 * green + 0.107_396_956_6 * blue
|
|
34
|
+
);
|
|
35
|
+
const s = Math.cbrt(
|
|
36
|
+
0.088_302_461_9 * red + 0.281_718_837_6 * green + 0.629_978_700_5 * blue
|
|
37
|
+
);
|
|
38
|
+
const a = 1.977_998_495_1 * l - 2.428_592_205 * m + 0.450_593_709_9 * s;
|
|
39
|
+
const bb = 0.025_904_037_1 * l + 0.782_771_766_2 * m - 0.808_675_766 * s;
|
|
40
|
+
return {
|
|
41
|
+
l: 0.210_454_255_3 * l + 0.793_617_785 * m - 0.004_072_046_8 * s,
|
|
42
|
+
c: Math.hypot(a, bb),
|
|
43
|
+
h: Math.atan2(bb, a),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Linear sRGB (0 to 1 inside the gamut) of an OKLCh color. */
|
|
48
|
+
function toLinearRgb({ l, c, h }: OkLch): [number, number, number] {
|
|
49
|
+
const a = c * Math.cos(h);
|
|
50
|
+
const b = c * Math.sin(h);
|
|
51
|
+
const long = (l + 0.396_337_777_4 * a + 0.215_803_757_3 * b) ** 3;
|
|
52
|
+
const medium = (l - 0.105_561_345_8 * a - 0.063_854_172_8 * b) ** 3;
|
|
53
|
+
const short = (l - 0.089_484_177_5 * a - 1.291_485_548 * b) ** 3;
|
|
54
|
+
return [
|
|
55
|
+
4.076_741_662_1 * long - 3.307_711_591_3 * medium + 0.230_969_929_2 * short,
|
|
56
|
+
-1.268_438_004_6 * long +
|
|
57
|
+
2.609_757_401_1 * medium -
|
|
58
|
+
0.341_319_396_5 * short,
|
|
59
|
+
-0.004_196_086_3 * long - 0.703_418_614_7 * medium + 1.707_614_701 * short,
|
|
60
|
+
];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const GAMUT_SLACK = 1e-7;
|
|
64
|
+
const inGamut = (rgb: number[]) =>
|
|
65
|
+
rgb.every((value) => value >= -GAMUT_SLACK && value <= 1 + GAMUT_SLACK);
|
|
66
|
+
|
|
67
|
+
/** Halvings of the chroma when a color is outside sRGB: well past a channel step. */
|
|
68
|
+
const GAMUT_STEPS = 20;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* An OKLCh color as sRGB, channels rounded to whole numbers. Outside sRGB
|
|
72
|
+
* its chroma is reduced until it fits, its lightness and hue kept.
|
|
73
|
+
*/
|
|
74
|
+
export function oklchToRgb(color: OkLch): RGBAColor {
|
|
75
|
+
let linear = toLinearRgb(color);
|
|
76
|
+
if (!inGamut(linear)) {
|
|
77
|
+
let inside = 0;
|
|
78
|
+
let outside = color.c;
|
|
79
|
+
for (let step = 0; step < GAMUT_STEPS; step++) {
|
|
80
|
+
const c = (inside + outside) / 2;
|
|
81
|
+
if (inGamut(toLinearRgb({ ...color, c }))) {
|
|
82
|
+
inside = c;
|
|
83
|
+
} else {
|
|
84
|
+
outside = c;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
linear = toLinearRgb({ ...color, c: inside });
|
|
88
|
+
}
|
|
89
|
+
const [r = 0, g = 0, b = 0] = linear.map((value) =>
|
|
90
|
+
Math.round(toGamma(Math.min(1, Math.max(0, value))))
|
|
91
|
+
);
|
|
92
|
+
return { r, g, b, a: 1 };
|
|
93
|
+
}
|