qrdx 0.0.59 → 0.0.60
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 +3 -3
- 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/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/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/scan-risk.d.ts.map +1 -1
- 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/vanilla.browser.mjs +29 -29
- package/dist/vanilla.browser.mjs.map +1 -1
- package/package.json +1 -1
- 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/scan-risk.ts +19 -3
- package/src/scan-score.ts +11 -1
- 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/src/looks.ts
CHANGED
|
@@ -28,6 +28,9 @@ import {
|
|
|
28
28
|
SHADOW_OPACITY_DEFAULT,
|
|
29
29
|
SHADOW_OPACITY_MAX,
|
|
30
30
|
SHADOW_OPACITY_MIN,
|
|
31
|
+
WITHDRAWN_CORNER_EYE_DOT_PATTERNS,
|
|
32
|
+
WITHDRAWN_CORNER_EYE_PATTERNS,
|
|
33
|
+
withdrawnFallback,
|
|
31
34
|
} from "../types/constants";
|
|
32
35
|
import qrcodegen from "./codegen";
|
|
33
36
|
import {
|
|
@@ -40,6 +43,7 @@ import { capImageSettings } from "./logo-budget";
|
|
|
40
43
|
import { isShapedCode } from "./qr-shape";
|
|
41
44
|
import { getStylePairRisk, type StylePairRisk } from "./scan-risk";
|
|
42
45
|
import { resolveErrorLevel } from "./utils";
|
|
46
|
+
import { oklchToRgb, rgbToOklch } from "./utils/oklab";
|
|
43
47
|
import { parseColor, type RGBAColor } from "./utils/parse-color";
|
|
44
48
|
|
|
45
49
|
type EyeShapeProps = Pick<
|
|
@@ -61,9 +65,16 @@ export function eyeShapeOf(
|
|
|
61
65
|
position: EyePosition
|
|
62
66
|
): { outer: CornerEyePattern; inner: CornerEyeDotPattern } {
|
|
63
67
|
const own = props.eyeShapes?.[position];
|
|
68
|
+
// A shape withdrawn before release draws as the one it maps to
|
|
64
69
|
return {
|
|
65
|
-
outer:
|
|
66
|
-
|
|
70
|
+
outer: withdrawnFallback(
|
|
71
|
+
WITHDRAWN_CORNER_EYE_PATTERNS,
|
|
72
|
+
own?.outer ?? props.cornerEyePattern ?? DEFAULT_EYE
|
|
73
|
+
),
|
|
74
|
+
inner: withdrawnFallback(
|
|
75
|
+
WITHDRAWN_CORNER_EYE_DOT_PATTERNS,
|
|
76
|
+
own?.inner ?? props.cornerEyeDotPattern ?? DEFAULT_DOT
|
|
77
|
+
),
|
|
67
78
|
};
|
|
68
79
|
}
|
|
69
80
|
|
|
@@ -314,115 +325,307 @@ export function isSeeThroughBackground(bgColor: QRProps["bgColor"]): boolean {
|
|
|
314
325
|
}
|
|
315
326
|
|
|
316
327
|
/**
|
|
317
|
-
* How light a color reads to
|
|
318
|
-
*
|
|
319
|
-
*
|
|
328
|
+
* How light a color reads to each decoder, 0 to 255: the two grey
|
|
329
|
+
* conversions they use, Rec. 709 weights (jsQR) and Rec. 601 (ZXing). A
|
|
330
|
+
* saturated color reads differently to each, so the halo rule holds for
|
|
331
|
+
* both.
|
|
320
332
|
*/
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
333
|
+
type Lumas = [rec709: number, rec601: number];
|
|
334
|
+
const decoderLumas = ({ r, g, b }: RGBAColor): Lumas => [
|
|
335
|
+
0.2126 * r + 0.7152 * g + 0.0722 * b,
|
|
336
|
+
0.299 * r + 0.587 * g + 0.114 * b,
|
|
337
|
+
];
|
|
326
338
|
|
|
327
|
-
const
|
|
339
|
+
const HALO_LIGHT_LUMAS = decoderLumas(parseColor(HALO_LIGHT_MIN) as RGBAColor);
|
|
328
340
|
|
|
329
|
-
/**
|
|
330
|
-
const
|
|
331
|
-
const parsed = readColor(color);
|
|
332
|
-
return parsed ? decoderLuma(parsed) : null;
|
|
333
|
-
};
|
|
341
|
+
/** Rounding slack in the halo rule's comparisons. */
|
|
342
|
+
const LUMA_SLACK = 1e-9;
|
|
334
343
|
|
|
335
|
-
/**
|
|
344
|
+
/** True when every decoder reads `lumas` between `lower` and `upper`. */
|
|
345
|
+
const lumasWithin = (lumas: Lumas, lower: Lumas, upper: Lumas) =>
|
|
346
|
+
lumas.every(
|
|
347
|
+
(luma, decoder) =>
|
|
348
|
+
luma >= lower[decoder] - LUMA_SLACK && luma <= upper[decoder] + LUMA_SLACK
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
const toHex = ({ r, g, b }: RGBAColor) =>
|
|
352
|
+
`#${[r, g, b]
|
|
353
|
+
.map((channel) => Math.round(channel).toString(16).padStart(2, "0"))
|
|
354
|
+
.join("")}`;
|
|
355
|
+
|
|
356
|
+
/** The rim color as picked: opaque, the default when it can't be read. */
|
|
336
357
|
function haloColorOf(halo: QRHalo): string {
|
|
337
358
|
const parsed = readColor(halo.color || HALO_COLOR_DEFAULT);
|
|
338
|
-
return parsed && parsed.a > 0
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
359
|
+
return parsed && parsed.a > 0 ? toHex(parsed) : HALO_COLOR_DEFAULT;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** Halvings of the lightness and of the chroma when a rim moves: well past a channel step. */
|
|
363
|
+
const FIT_STEPS = 24;
|
|
364
|
+
const CHROMA_STEPS = 12;
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* `color` with its lightness moved until every decoder reads it between
|
|
368
|
+
* `lower` and `upper`, as little as it takes: its OKLab hue and chroma are
|
|
369
|
+
* kept (the chroma reduced only where sRGB has no such color, or where no
|
|
370
|
+
* lightness fits both decoders at it). Null when not even a grey fits.
|
|
371
|
+
*/
|
|
372
|
+
function fitLightness(
|
|
373
|
+
color: RGBAColor,
|
|
374
|
+
lower: Lumas,
|
|
375
|
+
upper: Lumas
|
|
376
|
+
): RGBAColor | null {
|
|
377
|
+
if (lumasWithin(decoderLumas(color), lower, upper)) {
|
|
378
|
+
return color;
|
|
379
|
+
}
|
|
380
|
+
if (color.r === color.g && color.g === color.b) {
|
|
381
|
+
// A grey stays a grey: every decoder reads it as its own channel
|
|
382
|
+
const grey = Math.min(
|
|
383
|
+
Math.floor(Math.min(...upper) + LUMA_SLACK),
|
|
384
|
+
Math.max(color.r, Math.ceil(Math.max(...lower) - LUMA_SLACK))
|
|
385
|
+
);
|
|
386
|
+
return grey >= Math.max(...lower) - LUMA_SLACK
|
|
387
|
+
? { r: grey, g: grey, b: grey, a: 1 }
|
|
388
|
+
: null;
|
|
389
|
+
}
|
|
390
|
+
const start = rgbToOklch(color);
|
|
391
|
+
const atChroma = (c: number) => {
|
|
392
|
+
const here = oklchToRgb({ l: start.l, c, h: start.h });
|
|
393
|
+
const lumas = decoderLumas(here);
|
|
394
|
+
if (lumasWithin(lumas, lower, upper)) {
|
|
395
|
+
return here;
|
|
396
|
+
}
|
|
397
|
+
// Too dark for a decoder: lighten toward white, which clears `lower`;
|
|
398
|
+
// else darken toward black, which clears `upper`
|
|
399
|
+
const lighten = lumas.some((luma, decoder) => luma < lower[decoder]);
|
|
400
|
+
const clears = (rgb: RGBAColor) =>
|
|
401
|
+
decoderLumas(rgb).every((luma, decoder) =>
|
|
402
|
+
lighten
|
|
403
|
+
? luma >= lower[decoder] - LUMA_SLACK
|
|
404
|
+
: luma <= upper[decoder] + LUMA_SLACK
|
|
405
|
+
);
|
|
406
|
+
// The lightness nearest the color's own that clears the bound it missed
|
|
407
|
+
let missed = start.l;
|
|
408
|
+
let cleared = lighten ? 1 : 0;
|
|
409
|
+
for (let step = 0; step < FIT_STEPS; step++) {
|
|
410
|
+
const l = (missed + cleared) / 2;
|
|
411
|
+
if (clears(oklchToRgb({ l, c, h: start.h }))) {
|
|
412
|
+
cleared = l;
|
|
413
|
+
} else {
|
|
414
|
+
missed = l;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
const rgb = oklchToRgb({ l: cleared, c, h: start.h });
|
|
418
|
+
return lumasWithin(decoderLumas(rgb), lower, upper) ? rgb : null;
|
|
419
|
+
};
|
|
420
|
+
const full = atChroma(start.c);
|
|
421
|
+
if (full) {
|
|
422
|
+
return full;
|
|
423
|
+
}
|
|
424
|
+
// As much of the chroma as still fits
|
|
425
|
+
let best = atChroma(0);
|
|
426
|
+
if (!best) {
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
let fits = 0;
|
|
430
|
+
let fails = start.c;
|
|
431
|
+
for (let step = 0; step < CHROMA_STEPS; step++) {
|
|
432
|
+
const c = (fits + fails) / 2;
|
|
433
|
+
const rgb = atChroma(c);
|
|
434
|
+
if (rgb) {
|
|
435
|
+
fits = c;
|
|
436
|
+
best = rgb;
|
|
437
|
+
} else {
|
|
438
|
+
fails = c;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return best;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
export interface HaloRefusal {
|
|
445
|
+
/** Which part of the design refuses a halo */
|
|
446
|
+
reason: "see-through" | "picture" | "dark-background";
|
|
447
|
+
message: string;
|
|
448
|
+
fix: string;
|
|
343
449
|
}
|
|
344
450
|
|
|
345
|
-
export interface
|
|
346
|
-
/**
|
|
347
|
-
reason: "
|
|
451
|
+
export interface HaloAdjustment {
|
|
452
|
+
/** The rim is drawn, in another color than the one picked */
|
|
453
|
+
reason: "adjusted";
|
|
454
|
+
/** The color the rim is drawn in */
|
|
455
|
+
color: string;
|
|
348
456
|
message: string;
|
|
349
457
|
fix: string;
|
|
350
458
|
}
|
|
351
459
|
|
|
460
|
+
/** Why a halo isn't drawn on a code, or how its color was moved. */
|
|
461
|
+
export type HaloRisk = HaloRefusal | HaloAdjustment;
|
|
462
|
+
|
|
463
|
+
const TURN_OFF = "or turn the halo off";
|
|
464
|
+
|
|
352
465
|
/**
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
* color, every gradient stop). A rim lighter than the background raises
|
|
358
|
-
* the light level around every dot, and decoders then misread light
|
|
359
|
-
* modules: at every width on a grey page under a see-through code, and
|
|
360
|
-
* with grey or colored dots on a light grey background (measured 21 Nov
|
|
361
|
-
* 2026, every dot shape, widths 0.1 to 0.5, 4, 6 and 9 px per module,
|
|
362
|
-
* four rasteriser and decoder pipelines). A see-through background shows
|
|
363
|
-
* whatever is under the code, and a picture its own tones, so neither
|
|
364
|
-
* takes a halo.
|
|
466
|
+
* How light a rim may read to each decoder on this background (as light
|
|
467
|
+
* as its darkest color, every gradient stop included), or why no rim is
|
|
468
|
+
* drawn on it: a picture, any see-through paint, or a color darker than
|
|
469
|
+
* HALO_LIGHT_MIN.
|
|
365
470
|
*/
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
): HaloRisk | null {
|
|
369
|
-
if (!props.halo) {
|
|
370
|
-
return null;
|
|
371
|
-
}
|
|
372
|
-
const off = "or turn the halo off";
|
|
373
|
-
const paint = normalizeColorConfig(props.bgColor, DEFAULT_BGCOLOR);
|
|
471
|
+
function rimCeiling(bgColor: QRProps["bgColor"]): Lumas | HaloRefusal {
|
|
472
|
+
const paint = normalizeColorConfig(bgColor, DEFAULT_BGCOLOR);
|
|
374
473
|
if (paint.type === "image") {
|
|
375
474
|
return {
|
|
376
475
|
reason: "picture",
|
|
377
476
|
message:
|
|
378
477
|
"A halo isn't drawn on a picture background: on the picture's darker parts the rim makes the code unreadable",
|
|
379
|
-
fix: `Use a background color, ${
|
|
478
|
+
fix: `Use a background color, ${TURN_OFF}`,
|
|
380
479
|
};
|
|
381
480
|
}
|
|
382
|
-
if (isSeeThroughBackground(
|
|
481
|
+
if (isSeeThroughBackground(bgColor)) {
|
|
383
482
|
return {
|
|
384
483
|
reason: "see-through",
|
|
385
484
|
message:
|
|
386
|
-
"A halo
|
|
387
|
-
fix: `Give the code an opaque background, ${
|
|
485
|
+
"A halo needs an opaque background: on a grey surface under a see-through code the rim makes it unreadable",
|
|
486
|
+
fix: `Give the code an opaque background, ${TURN_OFF}`,
|
|
388
487
|
};
|
|
389
488
|
}
|
|
390
|
-
const
|
|
489
|
+
const colors = (
|
|
391
490
|
paint.type === "solid"
|
|
392
491
|
? [paint.color]
|
|
393
492
|
: paint.stops.map((stop) => stop.color)
|
|
394
|
-
).map(
|
|
395
|
-
const
|
|
396
|
-
|
|
397
|
-
:
|
|
398
|
-
|
|
493
|
+
).map(readColor);
|
|
494
|
+
const ceiling: Lumas = [Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY];
|
|
495
|
+
for (const color of colors) {
|
|
496
|
+
const lumas = color ? decoderLumas(color) : ([0, 0] as Lumas);
|
|
497
|
+
ceiling[0] = Math.min(ceiling[0], lumas[0]);
|
|
498
|
+
ceiling[1] = Math.min(ceiling[1], lumas[1]);
|
|
499
|
+
}
|
|
500
|
+
if (!lumasWithin(HALO_LIGHT_LUMAS, HALO_LIGHT_LUMAS, ceiling)) {
|
|
399
501
|
return {
|
|
400
502
|
reason: "dark-background",
|
|
401
|
-
message: `A halo
|
|
402
|
-
fix: `Lighten the background to ${HALO_LIGHT_MIN} or lighter, ${
|
|
503
|
+
message: `A halo needs a light background, at least as light as ${HALO_LIGHT_MIN}: on a darker one the rim makes the code unreadable`,
|
|
504
|
+
fix: `Lighten the background to ${HALO_LIGHT_MIN} or lighter, ${TURN_OFF}`,
|
|
403
505
|
};
|
|
404
506
|
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
507
|
+
return ceiling;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* The halo's rim on this background: the color it is drawn in, the color
|
|
512
|
+
* picked, or why none is drawn.
|
|
513
|
+
*/
|
|
514
|
+
function planRim(
|
|
515
|
+
halo: QRHalo,
|
|
516
|
+
bgColor: QRProps["bgColor"]
|
|
517
|
+
): { drawn: string; picked: string } | HaloRefusal {
|
|
518
|
+
const ceiling = rimCeiling(bgColor);
|
|
519
|
+
if (!Array.isArray(ceiling)) {
|
|
520
|
+
return ceiling;
|
|
521
|
+
}
|
|
522
|
+
const picked = haloColorOf(halo);
|
|
523
|
+
const fitted = fitLightness(
|
|
524
|
+
parseColor(picked) as RGBAColor,
|
|
525
|
+
HALO_LIGHT_LUMAS,
|
|
526
|
+
ceiling
|
|
527
|
+
);
|
|
528
|
+
// HALO_LIGHT_MIN fits wherever a halo is drawn (rimCeiling)
|
|
529
|
+
return { drawn: fitted ? toHex(fitted) : HALO_LIGHT_MIN, picked };
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* The color a halo's rim is drawn in on this background, or null where no
|
|
534
|
+
* halo is drawn (getHaloRisk says why). A rim is drawn only where it was
|
|
535
|
+
* measured never to cost a decode (tests/halo-decode.test.ts): every
|
|
536
|
+
* decoder must read it at least as light as HALO_LIGHT_MIN and no lighter
|
|
537
|
+
* than any background color (the color, every gradient stop). Any color
|
|
538
|
+
* can be picked: one outside that range is drawn in the closest color
|
|
539
|
+
* inside it, its hue and as much of its chroma as fits kept, only its
|
|
540
|
+
* lightness moved (tests/halo-colors-decode.test.ts reads those rims).
|
|
541
|
+
*/
|
|
542
|
+
export function fitHaloColor(
|
|
543
|
+
color: string | undefined,
|
|
544
|
+
bgColor: QRProps["bgColor"]
|
|
545
|
+
): string | null {
|
|
546
|
+
const plan = planRim({ color }, bgColor);
|
|
547
|
+
return "drawn" in plan ? plan.drawn : null;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Up to `count` tints of `color` a halo draws as they are on this
|
|
552
|
+
* background: its hue at evenly spaced lightness from the darkest rim the
|
|
553
|
+
* rule takes toward the background's own, darkest (the most visible)
|
|
554
|
+
* first. Empty where no halo is drawn.
|
|
555
|
+
*/
|
|
556
|
+
export function haloTints(
|
|
557
|
+
color: string | undefined,
|
|
558
|
+
bgColor: QRProps["bgColor"],
|
|
559
|
+
count = 4
|
|
560
|
+
): string[] {
|
|
561
|
+
const ceiling = rimCeiling(bgColor);
|
|
562
|
+
const base = readColor(color);
|
|
563
|
+
if (!(Array.isArray(ceiling) && base)) {
|
|
564
|
+
return [];
|
|
565
|
+
}
|
|
566
|
+
const tints = new Set<string>();
|
|
567
|
+
for (let index = 0; index < count; index++) {
|
|
568
|
+
const share = index / count;
|
|
569
|
+
const floor = HALO_LIGHT_LUMAS.map(
|
|
570
|
+
(luma, decoder) => luma + share * ((ceiling[decoder] ?? luma) - luma)
|
|
571
|
+
) as Lumas;
|
|
572
|
+
const tint = fitLightness({ ...base, a: 1 }, floor, ceiling);
|
|
573
|
+
if (tint) {
|
|
574
|
+
tints.add(toHex(tint));
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
return [...tints];
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Why a halo isn't drawn on this code, or how its color was moved: null
|
|
582
|
+
* when it is drawn as picked (or there is none). A rim lighter than the
|
|
583
|
+
* background raises the light level around every dot, and decoders then
|
|
584
|
+
* misread light modules: at every width on a grey page under a
|
|
585
|
+
* see-through code, and with grey or colored dots on a light grey
|
|
586
|
+
* background (measured 21 Nov 2026, every dot shape, widths 0.1 to 0.5, 4,
|
|
587
|
+
* 6 and 9 px per module, four rasteriser and decoder pipelines). A
|
|
588
|
+
* see-through background shows whatever is under the code, and a picture
|
|
589
|
+
* its own tones, so neither takes a halo, nor does a background darker
|
|
590
|
+
* than HALO_LIGHT_MIN; those are refusals. On any other background a
|
|
591
|
+
* color outside the rule's range is "adjusted": drawn in the closest one
|
|
592
|
+
* inside it (fitHaloColor).
|
|
593
|
+
*/
|
|
594
|
+
export function getHaloRisk(
|
|
595
|
+
props: Pick<QRProps, "halo" | "bgColor">
|
|
596
|
+
): HaloRisk | null {
|
|
597
|
+
if (!props.halo) {
|
|
598
|
+
return null;
|
|
599
|
+
}
|
|
600
|
+
const plan = planRim(props.halo, props.bgColor);
|
|
601
|
+
if (!("drawn" in plan)) {
|
|
602
|
+
return plan;
|
|
603
|
+
}
|
|
604
|
+
if (plan.drawn === plan.picked) {
|
|
605
|
+
return null;
|
|
412
606
|
}
|
|
413
|
-
return
|
|
607
|
+
return {
|
|
608
|
+
reason: "adjusted",
|
|
609
|
+
color: plan.drawn,
|
|
610
|
+
message: `Halo drawn as ${plan.drawn} instead of ${plan.picked} so the code still scans: a rim must read no lighter than the background and at least as light as ${HALO_LIGHT_MIN}`,
|
|
611
|
+
fix: `To have it drawn as picked, choose a color in that range, such as ${plan.drawn}`,
|
|
612
|
+
};
|
|
414
613
|
}
|
|
415
614
|
|
|
416
615
|
/**
|
|
417
616
|
* A halo with its defaults filled in, its width in range and its rim
|
|
418
|
-
* opaque; null without one
|
|
419
|
-
*
|
|
617
|
+
* opaque; null without one. With `code`, the rim is in the color drawn on
|
|
618
|
+
* that code's background (fitHaloColor), and null where none is drawn.
|
|
420
619
|
*/
|
|
421
620
|
export function resolveHalo(
|
|
422
621
|
halo: QRHalo | undefined,
|
|
423
622
|
code?: Pick<QRProps, "bgColor">
|
|
424
623
|
): { color: string; width: number } | null {
|
|
425
|
-
if (!halo
|
|
624
|
+
if (!halo) {
|
|
625
|
+
return null;
|
|
626
|
+
}
|
|
627
|
+
const plan = code ? planRim(halo, code.bgColor) : null;
|
|
628
|
+
if (plan && !("drawn" in plan)) {
|
|
426
629
|
return null;
|
|
427
630
|
}
|
|
428
631
|
const width = clamp(
|
|
@@ -433,7 +636,7 @@ export function resolveHalo(
|
|
|
433
636
|
);
|
|
434
637
|
// The rim is always opaque: a see-through rim would compound where rims
|
|
435
638
|
// overlap in the print PDF, which paints each shape on its own
|
|
436
|
-
return { color: haloColorOf(halo), width };
|
|
639
|
+
return { color: plan ? plan.drawn : haloColorOf(halo), width };
|
|
437
640
|
}
|
|
438
641
|
|
|
439
642
|
/**
|
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
|
@@ -793,7 +793,11 @@ function quietZoneCheck(props: QRProps): { check: ScanCheck; penalty: number } {
|
|
|
793
793
|
};
|
|
794
794
|
}
|
|
795
795
|
|
|
796
|
-
/**
|
|
796
|
+
/**
|
|
797
|
+
* A halo the engine refuses to draw (looks.ts getHaloRisk): the design
|
|
798
|
+
* asked for a look it can't have. A rim drawn in another color than the
|
|
799
|
+
* one picked, to stay readable, passes and says which.
|
|
800
|
+
*/
|
|
797
801
|
function haloCheck(props: QRProps): {
|
|
798
802
|
check: ScanCheck | null;
|
|
799
803
|
penalty: number;
|
|
@@ -802,6 +806,12 @@ function haloCheck(props: QRProps): {
|
|
|
802
806
|
if (!risk) {
|
|
803
807
|
return { check: null, penalty: 0 };
|
|
804
808
|
}
|
|
809
|
+
if (risk.reason === "adjusted") {
|
|
810
|
+
return {
|
|
811
|
+
check: { id: "halo", passed: true, message: risk.message },
|
|
812
|
+
penalty: 0,
|
|
813
|
+
};
|
|
814
|
+
}
|
|
805
815
|
return {
|
|
806
816
|
check: { id: "halo", passed: false, message: risk.message, fix: risk.fix },
|
|
807
817
|
penalty: 15,
|
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
|
+
}
|