qrdx 0.0.58 → 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.
Files changed (73) hide show
  1. package/README.md +3 -3
  2. package/dist/core.js +7 -7
  3. package/dist/core.js.map +1 -1
  4. package/dist/core.mjs +7 -7
  5. package/dist/core.mjs.map +1 -1
  6. package/dist/element.browser.mjs +6 -6
  7. package/dist/element.browser.mjs.map +1 -1
  8. package/dist/index.js +7 -7
  9. package/dist/index.js.map +1 -1
  10. package/dist/index.mjs +7 -7
  11. package/dist/index.mjs.map +1 -1
  12. package/dist/qr-code-canvas.js +1 -1
  13. package/dist/qr-code-canvas.js.map +1 -1
  14. package/dist/qr-code-canvas.mjs +1 -1
  15. package/dist/qr-code-canvas.mjs.map +1 -1
  16. package/dist/schemas.js +1 -1
  17. package/dist/schemas.js.map +1 -1
  18. package/dist/schemas.mjs +1 -1
  19. package/dist/schemas.mjs.map +1 -1
  20. package/dist/src/corner-dot.d.ts.map +1 -1
  21. package/dist/src/corner-eye.d.ts.map +1 -1
  22. package/dist/src/engine.d.ts +1 -1
  23. package/dist/src/engine.d.ts.map +1 -1
  24. package/dist/src/looks.d.ts +45 -17
  25. package/dist/src/looks.d.ts.map +1 -1
  26. package/dist/src/qr-code-props.d.ts +10 -1
  27. package/dist/src/qr-code-props.d.ts.map +1 -1
  28. package/dist/src/qr-code.d.ts.map +1 -1
  29. package/dist/src/scan-risk.d.ts.map +1 -1
  30. package/dist/src/scan-score.d.ts.map +1 -1
  31. package/dist/src/shapes.d.ts +19 -0
  32. package/dist/src/shapes.d.ts.map +1 -1
  33. package/dist/src/utils/oklab.d.ts +16 -0
  34. package/dist/src/utils/oklab.d.ts.map +1 -0
  35. package/dist/src/utils.d.ts.map +1 -1
  36. package/dist/types/constants.d.ts +40 -5
  37. package/dist/types/constants.d.ts.map +1 -1
  38. package/dist/types/corner-dot.d.ts +11 -6
  39. package/dist/types/corner-dot.d.ts.map +1 -1
  40. package/dist/types/corner-eye.d.ts +21 -8
  41. package/dist/types/corner-eye.d.ts.map +1 -1
  42. package/dist/types/frame-spec.d.ts +1 -1
  43. package/dist/types/frame.d.ts +2 -2
  44. package/dist/types/image-pattern.d.ts +3 -3
  45. package/dist/types/image-pattern.d.ts.map +1 -1
  46. package/dist/types/index.js +2 -2
  47. package/dist/types/index.js.map +1 -1
  48. package/dist/types/index.mjs +2 -2
  49. package/dist/types/index.mjs.map +1 -1
  50. package/dist/types/plain.d.ts +1 -1
  51. package/dist/types/plain.d.ts.map +1 -1
  52. package/dist/types/qr.d.ts +96 -46
  53. package/dist/types/qr.d.ts.map +1 -1
  54. package/dist/vanilla.browser.mjs +29 -29
  55. package/dist/vanilla.browser.mjs.map +1 -1
  56. package/package.json +1 -1
  57. package/src/corner-dot.tsx +15 -0
  58. package/src/corner-eye.ts +163 -72
  59. package/src/engine.ts +4 -0
  60. package/src/looks.ts +272 -69
  61. package/src/qr-code-props.ts +38 -25
  62. package/src/qr-code.tsx +7 -39
  63. package/src/scan-risk.ts +19 -3
  64. package/src/scan-score.ts +11 -1
  65. package/src/shapes.ts +123 -0
  66. package/src/utils/oklab.ts +93 -0
  67. package/src/utils.tsx +20 -4
  68. package/types/constants.ts +59 -3
  69. package/types/corner-dot.ts +17 -7
  70. package/types/corner-eye.ts +26 -8
  71. package/types/image-pattern.ts +11 -4
  72. package/types/plain.ts +4 -0
  73. 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: own?.outer ?? props.cornerEyePattern ?? DEFAULT_EYE,
66
- inner: own?.inner ?? props.cornerEyeDotPattern ?? DEFAULT_DOT,
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 decoders, 0 to 255: the darker of the two
318
- * grey conversions they use (Rec. 709 weights, as jsQR, and Rec. 601, as
319
- * ZXing), so a saturated color is judged as its darkest reading.
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
- const decoderLuma = ({ r, g, b }: RGBAColor) =>
322
- Math.min(
323
- 0.2126 * r + 0.7152 * g + 0.0722 * b,
324
- 0.299 * r + 0.587 * g + 0.114 * b
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 HALO_LIGHT_LUMA = decoderLuma(parseColor(HALO_LIGHT_MIN) as RGBAColor);
339
+ const HALO_LIGHT_LUMAS = decoderLumas(parseColor(HALO_LIGHT_MIN) as RGBAColor);
328
340
 
329
- /** How light a color reads (decoderLuma), or null when it can't be read. */
330
- const lumaOf = (color: string | undefined) => {
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
- /** The rim color as drawn: opaque, the default when it can't be read. */
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
- ? `#${[parsed.r, parsed.g, parsed.b]
340
- .map((channel) => Math.round(channel).toString(16).padStart(2, "0"))
341
- .join("")}`
342
- : HALO_COLOR_DEFAULT;
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 HaloRisk {
346
- /** Which part of the design refuses it */
347
- reason: "see-through" | "picture" | "dark-background" | "rim";
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
- * Why a halo isn't drawn on this code, or null when it is (or there is
354
- * none). A halo is drawn only where it was measured never to cost a decode
355
- * (tests/halo-decode.test.ts): on an opaque background, in a rim at least
356
- * as light as HALO_LIGHT_MIN and no lighter than any background color (the
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
- export function getHaloRisk(
367
- props: Pick<QRProps, "halo" | "bgColor">
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, ${off}`,
478
+ fix: `Use a background color, ${TURN_OFF}`,
380
479
  };
381
480
  }
382
- if (isSeeThroughBackground(props.bgColor)) {
481
+ if (isSeeThroughBackground(bgColor)) {
383
482
  return {
384
483
  reason: "see-through",
385
484
  message:
386
- "A halo isn't drawn on a see-through background: on a grey surface under the code the rim makes it unreadable",
387
- fix: `Give the code an opaque background, ${off}`,
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 grounds = (
489
+ const colors = (
391
490
  paint.type === "solid"
392
491
  ? [paint.color]
393
492
  : paint.stops.map((stop) => stop.color)
394
- ).map(lumaOf);
395
- const ground = grounds.includes(null)
396
- ? null
397
- : Math.min(...(grounds as number[]));
398
- if (ground === null || ground < HALO_LIGHT_LUMA - 1e-9) {
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 is drawn only on a background at least as light as ${HALO_LIGHT_MIN}: on a darker one the rim makes the code unreadable`,
402
- fix: `Lighten the background to ${HALO_LIGHT_MIN} or lighter, ${off}`,
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
- const rim = lumaOf(haloColorOf(props.halo)) as number;
406
- if (rim < HALO_LIGHT_LUMA - 1e-9 || rim > ground + 1e-9) {
407
- return {
408
- reason: "rim",
409
- message: `A halo is drawn only in a color between ${HALO_LIGHT_MIN} and the background's own: a rim lighter than the background, or darker than ${HALO_LIGHT_MIN}, makes the code unreadable`,
410
- fix: `Pick a halo color no lighter than the background and at least as light as ${HALO_LIGHT_MIN}, ${off}`,
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 null;
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, or when `code` is given and the halo is
419
- * refused on it (getHaloRisk).
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 || (code && getHaloRisk({ halo, bgColor: code.bgColor }))) {
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
  /**
@@ -87,32 +87,45 @@ function rootAttributes(props: QRCodeOptions): QRSVGAttributes {
87
87
 
88
88
  type QRData = ReturnType<typeof getQRData>;
89
89
 
90
- /** What `getQRData` lays out for `QRCode`'s props. */
90
+ /**
91
+ * Every prop `getQRCodeData` lays the code out from, in one list. React's
92
+ * `QRCode` keeps its layout until one of these changes, so a prop the layout
93
+ * reads can't be left out of that check: the looks once were (the per-eye
94
+ * shapes, the eyes' gradient span, the rounded background and its backdrop,
95
+ * the halo and the shadow), and a code drawn without them stayed on screen
96
+ * after one was turned on.
97
+ */
98
+ export const QR_CODE_DATA_KEYS = [
99
+ "value",
100
+ "fgColor",
101
+ "hideLogo",
102
+ "bgColor",
103
+ "eyeColor",
104
+ "dotColor",
105
+ "bodyPattern",
106
+ "cornerEyePattern",
107
+ "cornerEyeDotPattern",
108
+ "level",
109
+ "templateId",
110
+ "logo",
111
+ "logoOptions",
112
+ "margin",
113
+ "autoLevel",
114
+ "shape",
115
+ "eyeShapes",
116
+ "eyeGradient",
117
+ "bgRadius",
118
+ "backdrop",
119
+ "halo",
120
+ "shadow",
121
+ ] as const satisfies readonly (keyof QRCodeProps)[];
122
+
123
+ /** What `getQRData` lays out for `QRCode`'s props (QR_CODE_DATA_KEYS). */
91
124
  export function getQRCodeData(props: QRCodeProps): QRData {
92
- return getQRData({
93
- value: props.value,
94
- fgColor: props.fgColor,
95
- hideLogo: props.hideLogo,
96
- bgColor: props.bgColor,
97
- eyeColor: props.eyeColor,
98
- dotColor: props.dotColor,
99
- bodyPattern: props.bodyPattern,
100
- cornerEyePattern: props.cornerEyePattern,
101
- cornerEyeDotPattern: props.cornerEyeDotPattern,
102
- level: props.level,
103
- templateId: props.templateId,
104
- logo: props.logo,
105
- logoOptions: props.logoOptions,
106
- margin: props.margin ?? DEFAULT_MARGIN,
107
- autoLevel: props.autoLevel,
108
- shape: props.shape,
109
- eyeShapes: props.eyeShapes,
110
- eyeGradient: props.eyeGradient,
111
- bgRadius: props.bgRadius,
112
- backdrop: props.backdrop,
113
- halo: props.halo,
114
- shadow: props.shadow,
115
- });
125
+ const read = Object.fromEntries(
126
+ QR_CODE_DATA_KEYS.map((key) => [key, props[key]])
127
+ ) as Pick<QRCodeProps, (typeof QR_CODE_DATA_KEYS)[number]>;
128
+ return getQRData({ ...read, margin: props.margin ?? DEFAULT_MARGIN });
116
129
  }
117
130
 
118
131
  /**
package/src/qr-code.tsx CHANGED
@@ -2,8 +2,11 @@
2
2
  // on the server; only QRCodeCanvas needs a client boundary.
3
3
  import { memo, useMemo } from "react";
4
4
  import type { QRCodePropsSVG } from "../types/react";
5
- import { DEFAULT_MARGIN } from "./constants";
6
- import { getQRCodeData, getQRCodeSVGProps } from "./qr-code-props";
5
+ import {
6
+ getQRCodeData,
7
+ getQRCodeSVGProps,
8
+ QR_CODE_DATA_KEYS,
9
+ } from "./qr-code-props";
7
10
  import { QRCodeSVG } from "./qr-code-svg";
8
11
 
9
12
  /**
@@ -12,45 +15,10 @@ import { QRCodeSVG } from "./qr-code-svg";
12
15
  * goes on the `<svg>`, as with `QRCodeSVG`.
13
16
  */
14
17
  export const QRCode = memo((props: QRCodePropsSVG) => {
15
- const {
16
- value,
17
- fgColor,
18
- hideLogo,
19
- logo,
20
- logoOptions,
21
- bgColor,
22
- eyeColor,
23
- dotColor,
24
- bodyPattern,
25
- cornerEyePattern,
26
- cornerEyeDotPattern,
27
- level,
28
- margin = DEFAULT_MARGIN,
29
- templateId,
30
- autoLevel,
31
- shape,
32
- } = props;
33
- // biome-ignore lint/correctness/useExhaustiveDependencies: the layout depends on exactly these props
34
18
  const qrData = useMemo(
35
19
  () => getQRCodeData(props),
36
- [
37
- value,
38
- fgColor,
39
- hideLogo,
40
- logo,
41
- logoOptions,
42
- margin,
43
- autoLevel,
44
- shape,
45
- bgColor,
46
- eyeColor,
47
- dotColor,
48
- bodyPattern,
49
- cornerEyePattern,
50
- cornerEyeDotPattern,
51
- level,
52
- templateId,
53
- ]
20
+ // biome-ignore lint/correctness/useExhaustiveDependencies: the layout reads exactly these props (QR_CODE_DATA_KEYS, the list getQRCodeData lays out from)
21
+ QR_CODE_DATA_KEYS.map((key) => props[key])
54
22
  );
55
23
 
56
24
  return <QRCodeSVG {...getQRCodeSVGProps(props, qrData)} />;
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
- const body = props.bodyPattern ?? "circle";
159
- const eye = props.cornerEyePattern ?? "gear";
160
- const dot = props.cornerEyeDotPattern ?? "circle";
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
- /** A halo the engine refuses to draw (looks.ts getHaloRisk): the design asked for a look it can't have. */
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,