@uselazuli/cli 0.0.0-stage → 1.0.1

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 (179) hide show
  1. package/CHANGELOG.md +98 -0
  2. package/LICENSE +21 -0
  3. package/README.md +202 -2
  4. package/agents/lazuli-design-reviewer.md +30 -0
  5. package/agents/lazuli-first-look.md +22 -0
  6. package/agents/lazuli-task-tester.md +25 -0
  7. package/bin/lazuli.mjs +699 -0
  8. package/package.json +11 -4
  9. package/rules/accessibility/HYPOTHESES.md +8 -0
  10. package/rules/accessibility/_area.yaml +7 -0
  11. package/rules/accessibility/operable.yaml +100 -0
  12. package/rules/accessibility/perceivable.yaml +153 -0
  13. package/rules/accessibility/robust.yaml +24 -0
  14. package/rules/accessibility/understandable.yaml +72 -0
  15. package/rules/color/HYPOTHESES.md +27 -0
  16. package/rules/color/_area.yaml +10 -0
  17. package/rules/color/adapt.yaml +183 -0
  18. package/rules/color/create.yaml +259 -0
  19. package/rules/color/review.yaml +215 -0
  20. package/rules/color/signal.yaml +196 -0
  21. package/rules/content/HYPOTHESES.md +18 -0
  22. package/rules/content/_area.yaml +8 -0
  23. package/rules/content/review.yaml +347 -0
  24. package/rules/content/voice.yaml +144 -0
  25. package/rules/content/writing.yaml +202 -0
  26. package/rules/data/HYPOTHESES.md +11 -0
  27. package/rules/data/_area.yaml +7 -0
  28. package/rules/data/charts.yaml +189 -0
  29. package/rules/data/dashboards.yaml +141 -0
  30. package/rules/data/review.yaml +159 -0
  31. package/rules/graphic/HYPOTHESES.md +17 -0
  32. package/rules/graphic/_area.yaml +8 -0
  33. package/rules/graphic/identity.yaml +141 -0
  34. package/rules/graphic/pieces.yaml +164 -0
  35. package/rules/graphic/review.yaml +242 -0
  36. package/rules/graphic/symbol.yaml +141 -0
  37. package/rules/motion/HYPOTHESES.md +19 -0
  38. package/rules/motion/_area.yaml +7 -0
  39. package/rules/motion/micro.yaml +143 -0
  40. package/rules/motion/review.yaml +347 -0
  41. package/rules/motion/transitions.yaml +169 -0
  42. package/rules/typography/HYPOTHESES.md +12 -0
  43. package/rules/typography/_area.yaml +8 -0
  44. package/rules/typography/adapt.yaml +45 -0
  45. package/rules/typography/create.yaml +126 -0
  46. package/rules/typography/review.yaml +127 -0
  47. package/rules/ui/HYPOTHESES.md +23 -0
  48. package/rules/ui/_area.yaml +9 -0
  49. package/rules/ui/compose.yaml +219 -0
  50. package/rules/ui/grid.yaml +75 -0
  51. package/rules/ui/review.yaml +399 -0
  52. package/rules/ui/states.yaml +257 -0
  53. package/rules/ux/HYPOTHESES.md +13 -0
  54. package/rules/ux/_area.yaml +8 -0
  55. package/rules/ux/flows.yaml +164 -0
  56. package/rules/ux/forms.yaml +142 -0
  57. package/rules/ux/review.yaml +186 -0
  58. package/skills/lazuli/SKILL.md +180 -0
  59. package/skills/lazuli/procedures/create.md +29 -0
  60. package/skills/lazuli/procedures/explain-decisions.md +22 -0
  61. package/skills/lazuli/procedures/improve.md +30 -0
  62. package/skills/lazuli/procedures/remember-this.md +41 -0
  63. package/skills/lazuli/procedures/review-design.md +35 -0
  64. package/skills/lazuli/procedures/show-options.md +32 -0
  65. package/skills/lazuli-accessibility-check/SKILL.md +68 -0
  66. package/skills/lazuli-color-context-and-culture/SKILL.md +53 -0
  67. package/skills/lazuli-color-context-and-culture/references/sectors.md +26 -0
  68. package/skills/lazuli-color-palette-by-intent/SKILL.md +54 -0
  69. package/skills/lazuli-color-palette-by-intent/references/intent-chords.md +51 -0
  70. package/skills/lazuli-color-screen-review/SKILL.md +51 -0
  71. package/skills/lazuli-color-states-and-signals/SKILL.md +53 -0
  72. package/skills/lazuli-content-interface-writing/SKILL.md +53 -0
  73. package/skills/lazuli-content-voice/SKILL.md +39 -0
  74. package/skills/lazuli-data-charts-and-dashboards/SKILL.md +59 -0
  75. package/skills/lazuli-graphic-derived-pieces/SKILL.md +40 -0
  76. package/skills/lazuli-graphic-derived-pieces/references/piece-template.html +24 -0
  77. package/skills/lazuli-graphic-identity/SKILL.md +43 -0
  78. package/skills/lazuli-motion-micro-and-transitions/SKILL.md +49 -0
  79. package/skills/lazuli-typography-choose-and-pair/SKILL.md +50 -0
  80. package/skills/lazuli-typography-screen-review/SKILL.md +51 -0
  81. package/skills/lazuli-ui-composition/SKILL.md +42 -0
  82. package/skills/lazuli-ui-composition/references/option-template.html +34 -0
  83. package/skills/lazuli-ui-layout-review/SKILL.md +47 -0
  84. package/skills/lazuli-ui-states-and-system/SKILL.md +54 -0
  85. package/skills/lazuli-ux-flows-and-forms/SKILL.md +51 -0
  86. package/skills/lazuli-ux-task-test/SKILL.md +44 -0
  87. package/src/browser.mjs +161 -0
  88. package/src/commands/capture.mjs +313 -0
  89. package/src/commands/catalog.mjs +37 -0
  90. package/src/commands/check-design.mjs +428 -0
  91. package/src/commands/check-goals.mjs +57 -0
  92. package/src/commands/compare-designs.mjs +97 -0
  93. package/src/commands/draft-memo.mjs +161 -0
  94. package/src/commands/hooks.mjs +343 -0
  95. package/src/commands/install-hooks.mjs +120 -0
  96. package/src/commands/install.mjs +132 -0
  97. package/src/commands/list-rules.mjs +49 -0
  98. package/src/commands/map-page.mjs +594 -0
  99. package/src/commands/record-answers.mjs +79 -0
  100. package/src/commands/record-log.mjs +296 -0
  101. package/src/commands/record-task.mjs +250 -0
  102. package/src/commands/render-options.mjs +164 -0
  103. package/src/commands/start-project.mjs +32 -0
  104. package/src/measure/a11y.mjs +383 -0
  105. package/src/measure/asset.mjs +206 -0
  106. package/src/measure/at-rest.mjs +49 -0
  107. package/src/measure/cases.mjs +549 -0
  108. package/src/measure/chart-read.mjs +401 -0
  109. package/src/measure/chart.mjs +264 -0
  110. package/src/measure/coherence.mjs +145 -0
  111. package/src/measure/color.mjs +79 -0
  112. package/src/measure/content.mjs +82 -0
  113. package/src/measure/cvd.mjs +24 -0
  114. package/src/measure/extreme.mjs +241 -0
  115. package/src/measure/forms.mjs +150 -0
  116. package/src/measure/in-page/a11y.js +489 -0
  117. package/src/measure/in-page/color.js +346 -0
  118. package/src/measure/in-page/composition.js +1496 -0
  119. package/src/measure/in-page/contrast.js +15 -0
  120. package/src/measure/in-page/cvd.js +173 -0
  121. package/src/measure/in-page/extreme.js +355 -0
  122. package/src/measure/in-page/frames.js +781 -0
  123. package/src/measure/in-page/look.js +28 -0
  124. package/src/measure/in-page/motion-probe.js +169 -0
  125. package/src/measure/in-page/style.js +107 -0
  126. package/src/measure/in-page/svg-strokes.js +112 -0
  127. package/src/measure/in-page/svg-uses.js +50 -0
  128. package/src/measure/in-page/symbol.js +123 -0
  129. package/src/measure/in-page/type.js +266 -0
  130. package/src/measure/in-page/weight.js +207 -0
  131. package/src/measure/layout.mjs +91 -0
  132. package/src/measure/motion.mjs +508 -0
  133. package/src/measure/piece.mjs +195 -0
  134. package/src/measure/registry.mjs +252 -0
  135. package/src/measure/rhythm.mjs +81 -0
  136. package/src/measure/signature.mjs +65 -0
  137. package/src/measure/states.mjs +240 -0
  138. package/src/measure/style.mjs +13 -0
  139. package/src/measure/symbol.mjs +268 -0
  140. package/src/measure/terms.mjs +153 -0
  141. package/src/measure/tiles.mjs +20 -0
  142. package/src/measure/timeline.mjs +621 -0
  143. package/src/measure/type.mjs +167 -0
  144. package/src/measure/weight.mjs +18 -0
  145. package/src/private-strings.mjs +81 -0
  146. package/src/project/blind.mjs +70 -0
  147. package/src/project/edits.mjs +154 -0
  148. package/src/project/gate.mjs +159 -0
  149. package/src/project/lazuli-md.mjs +497 -0
  150. package/src/project/log.mjs +329 -0
  151. package/src/project/match.mjs +112 -0
  152. package/src/project/records.mjs +380 -0
  153. package/src/project/tasks.mjs +153 -0
  154. package/src/report/evaluate.mjs +27 -0
  155. package/src/report/evaluators/a11y.mjs +206 -0
  156. package/src/report/evaluators/chart.mjs +151 -0
  157. package/src/report/evaluators/coherence.mjs +103 -0
  158. package/src/report/evaluators/color.mjs +86 -0
  159. package/src/report/evaluators/content.mjs +112 -0
  160. package/src/report/evaluators/extreme.mjs +53 -0
  161. package/src/report/evaluators/forms.mjs +116 -0
  162. package/src/report/evaluators/layout.mjs +94 -0
  163. package/src/report/evaluators/motion.mjs +273 -0
  164. package/src/report/evaluators/piece.mjs +69 -0
  165. package/src/report/evaluators/rhythm.mjs +99 -0
  166. package/src/report/evaluators/states.mjs +39 -0
  167. package/src/report/evaluators/style.mjs +35 -0
  168. package/src/report/evaluators/symbol.mjs +110 -0
  169. package/src/report/evaluators/terms.mjs +49 -0
  170. package/src/report/evaluators/type.mjs +163 -0
  171. package/src/report/evaluators/weight.mjs +51 -0
  172. package/src/report/evidence.mjs +4 -0
  173. package/src/report/finding.mjs +11 -0
  174. package/src/report/format.mjs +215 -0
  175. package/src/rules/hypotheses.mjs +28 -0
  176. package/src/rules/load.mjs +256 -0
  177. package/src/rules/public-text.mjs +56 -0
  178. package/src/rules/schema.mjs +212 -0
  179. package/templates/LAZULI.md +86 -0
@@ -0,0 +1,346 @@
1
+ // Evaluated inside the page as a function expression: (shotDataUrl, shotCssWidth, contrast, tile, warn) => report,
2
+ // where contrast is the value of in-page/contrast.js ({ lin, lum, ratio }) and tile ({ offsetY, heightPx }) is the
3
+ // part of the page the screenshot shows, from offsetY down (no tile: the screenshot starts at the top). warn
4
+ // ({ roles, marks }) holds the lists of the ES5 threshold that mark a warning (no lists: no row is a warning).
5
+ // Plain script, no imports. Returns { area, contrast, textChroma, notes, uncertain }, with every contrast row (el,
6
+ // the element's key, outside, true when its box lies outside the tile, and warning) and the count of uncertain
7
+ // rows; Node merges the tiles, keeps the lowest rows and writes the uncertain note once.
8
+ async (shotDataUrl, shotCssWidth, contrast, tile, warn) => {
9
+ const offsetY = tile?.offsetY ?? 0;
10
+ const tileH = tile?.heightPx ?? Infinity;
11
+ const pageH = Math.max(document.documentElement.scrollHeight, innerHeight);
12
+ const notes = [];
13
+ const noteOnce = (n) => {
14
+ if (!notes.includes(n)) notes.push(n);
15
+ };
16
+
17
+ // ---- color normalization: any CSS color string -> {r,g,b,a} via a 1x1 canvas ----
18
+ const probe = document.createElement("canvas");
19
+ probe.width = probe.height = 1;
20
+ const pctx = probe.getContext("2d", { willReadFrequently: true });
21
+ const cache = new Map();
22
+ const toRgba = (css) => {
23
+ if (cache.has(css)) return cache.get(css);
24
+ pctx.fillStyle = "#000";
25
+ pctx.fillStyle = css;
26
+ const a1 = pctx.fillStyle;
27
+ pctx.fillStyle = "#fff";
28
+ pctx.fillStyle = css;
29
+ const valid = a1 === pctx.fillStyle;
30
+ let out = null;
31
+ if (valid) {
32
+ pctx.clearRect(0, 0, 1, 1);
33
+ pctx.fillStyle = css;
34
+ pctx.fillRect(0, 0, 1, 1);
35
+ const d = pctx.getImageData(0, 0, 1, 1).data;
36
+ out = { r: d[0], g: d[1], b: d[2], a: d[3] / 255 };
37
+ }
38
+ cache.set(css, out);
39
+ return out;
40
+ };
41
+ const hex = ({ r, g, b }) =>
42
+ "#" + [r, g, b].map((v) => Math.round(v).toString(16).padStart(2, "0")).join("");
43
+ const over = (top, bottom) => {
44
+ const a = top.a + bottom.a * (1 - top.a);
45
+ if (a === 0) return { r: 0, g: 0, b: 0, a: 0 };
46
+ const mix = (t, b) => (t * top.a + b * bottom.a * (1 - top.a)) / a;
47
+ return { r: mix(top.r, bottom.r), g: mix(top.g, bottom.g), b: mix(top.b, bottom.b), a };
48
+ };
49
+ const { ratio } = contrast;
50
+ // A warning: the element or an ancestor up to its nearest section, article, aside or body has a role of
51
+ // warn.roles or a class or id token containing a word of warn.marks, or its own text starts with a marks word
52
+ // and a colon. Compared without case or accents.
53
+ const fold = (v) => String(v).normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase();
54
+ const roles = (warn?.roles ?? []).map(fold);
55
+ const marks = (warn?.marks ?? []).map(fold);
56
+ const isWarning = (el, text) => {
57
+ const lead = fold(text);
58
+ if (marks.some((m) => lead.startsWith(m) && /^\s*:/.test(lead.slice(m.length)))) return true;
59
+ for (let e = el; e; e = e.parentElement) {
60
+ const role = (e.getAttribute("role") || "").split(/\s+/).map(fold);
61
+ if (role.some((r) => roles.includes(r))) return true;
62
+ const tokens = [...(e.getAttribute("class") || "").split(/\s+/), e.id || ""].filter(Boolean).map(fold);
63
+ if (tokens.some((t) => marks.some((m) => t.includes(m)))) return true;
64
+ if (/^(section|article|aside|body)$/i.test(e.tagName)) break;
65
+ }
66
+ return false;
67
+ };
68
+ // Hypothesis H10 (rules/color/HYPOTHESES.md) defines chromatic text: channel spread above 40.
69
+ const chromatic = ({ r, g, b }) => Math.max(r, g, b) - Math.min(r, g, b) > 40;
70
+ const hsl = ({ r, g, b }) => {
71
+ const [R, G, B] = [r, g, b].map((v) => v / 255);
72
+ const M = Math.max(R, G, B), m = Math.min(R, G, B), l = (M + m) / 2;
73
+ let h = 0, s = 0;
74
+ if (M !== m) {
75
+ const d = M - m;
76
+ s = l > 0.5 ? d / (2 - M - m) : d / (M + m);
77
+ h = M === R ? (G - B) / d + (G < B ? 6 : 0) : M === G ? (B - R) / d + 2 : (R - G) / d + 4;
78
+ h *= 60;
79
+ }
80
+ return { h: Math.round(h), s: Math.round(s * 100), l: Math.round(l * 100) };
81
+ };
82
+
83
+ // ---- 1. area from screenshot pixels ----
84
+ const img = new Image();
85
+ await new Promise((res, rej) => {
86
+ img.onload = res;
87
+ img.onerror = () => rej(new Error("screenshot decode failed"));
88
+ img.src = shotDataUrl;
89
+ });
90
+ const W = img.naturalWidth, H = img.naturalHeight;
91
+ const shot = document.createElement("canvas");
92
+ shot.width = W;
93
+ shot.height = H;
94
+ const sctx = shot.getContext("2d", { willReadFrequently: true });
95
+ sctx.drawImage(img, 0, 0);
96
+ const px = sctx.getImageData(0, 0, W, H).data;
97
+ const scaleX = W / shotCssWidth, scaleY = scaleX;
98
+ const viewH = Math.min(H, Math.round(innerHeight * scaleY));
99
+
100
+ // Quantize to 4 bits per channel, keep the mean real color of each bucket.
101
+ const buckets = new Map();
102
+ const sample = (x0, y0, x1, y1, step, into) => {
103
+ let total = 0;
104
+ for (let y = y0; y < y1; y += step) {
105
+ for (let x = x0; x < x1; x += step) {
106
+ const i = (y * W + x) * 4;
107
+ const key = ((px[i] >> 4) << 8) | ((px[i + 1] >> 4) << 4) | (px[i + 2] >> 4);
108
+ let b = into.get(key);
109
+ if (!b) into.set(key, (b = { n: 0, r: 0, g: 0, b: 0 }));
110
+ b.n++;
111
+ b.r += px[i];
112
+ b.g += px[i + 1];
113
+ b.b += px[i + 2];
114
+ total++;
115
+ }
116
+ }
117
+ return total;
118
+ };
119
+ const meanOf = (b) => ({ r: b.r / b.n, g: b.g / b.n, b: b.b / b.n, a: 1 });
120
+ const total = sample(0, 0, W, viewH, 4, buckets);
121
+ const area = [...buckets.values()]
122
+ .map((b) => {
123
+ const c = meanOf(b);
124
+ return { hex: hex(c), pct: +((100 * b.n) / total).toFixed(1), ...hsl(c) };
125
+ })
126
+ .sort((a, b) => b.pct - a.pct);
127
+ noteOnce("Area comes from the first viewport; text backgrounds over images use the full-page screenshot.");
128
+ if (document.querySelector("canvas")) {
129
+ noteOnce("Canvas content is measured from pixels only, not from styles.");
130
+ }
131
+
132
+ // Most common pixel color inside a CSS-pixel rect (text pixels are the minority).
133
+ // `avoid` is the text color: in a tight box over a gradient the glyph color can be the most
134
+ // common bucket, so buckets close to it are skipped.
135
+ const pixelBackground = (rect, avoid) => {
136
+ const x0 = Math.max(0, Math.floor((rect.left + scrollX) * scaleX));
137
+ const y0 = Math.max(0, Math.floor((rect.top + scrollY - offsetY) * scaleY));
138
+ const x1 = Math.min(W, Math.ceil((rect.right + scrollX) * scaleX));
139
+ const y1 = Math.min(H, Math.ceil((rect.bottom + scrollY - offsetY) * scaleY));
140
+ if (x1 <= x0 || y1 <= y0) return null;
141
+ const local = new Map();
142
+ sample(x0, y0, x1, y1, 1, local);
143
+ let best = null;
144
+ let top = null;
145
+ for (const b of local.values()) {
146
+ if (!top || b.n > top.n) top = b;
147
+ if (avoid && sameColor(meanOf(b), avoid)) continue;
148
+ if (!best || b.n > best.n) best = b;
149
+ }
150
+ // Everything matched the text color: fall back to the plain dominant color.
151
+ const pick = best || top;
152
+ return pick ? meanOf(pick) : null;
153
+ };
154
+
155
+ // ---- 2. contrast per text element ----
156
+ // When neither body nor html is opaque, the real canvas may differ (for example a dark
157
+ // color-scheme); such rows are also checked against the screenshot below.
158
+ const pageCanvas = (() => {
159
+ for (const el of [document.body, document.documentElement]) {
160
+ const c = toRgba(getComputedStyle(el).backgroundColor);
161
+ if (c && c.a > 0) return c;
162
+ }
163
+ return { r: 255, g: 255, b: 255, a: 1 };
164
+ })();
165
+ const sameColor = (x, y) => Math.max(Math.abs(x.r - y.r), Math.abs(x.g - y.g), Math.abs(x.b - y.b)) < 24;
166
+ // Visually hidden patterns (sr-only clip, clip-path inset of half or more).
167
+ const clippedAway = (s) => {
168
+ if (s.clip && /^rect\(\s*0(px)?[\s,]+0(px)?[\s,]+0(px)?[\s,]+0(px)?\s*\)$/.test(s.clip)) return true;
169
+ const m = /^inset\(\s*([\d.]+)%/.exec(s.clipPath || "");
170
+ return Boolean(m && parseFloat(m[1]) >= 50);
171
+ };
172
+ let uncertain = 0;
173
+
174
+ // Walk ancestors, stack translucent backgrounds, stop at the first opaque one.
175
+ const backgroundOf = (el) => {
176
+ const layers = [];
177
+ let usesImage = false;
178
+ let base = null;
179
+ for (let e = el; e; e = e.parentElement) {
180
+ const s = getComputedStyle(e);
181
+ if (s.backgroundImage && s.backgroundImage !== "none") usesImage = true;
182
+ const c = toRgba(s.backgroundColor);
183
+ if (c && c.a > 0) {
184
+ layers.push(c);
185
+ if (c.a >= 1) {
186
+ base = c;
187
+ break;
188
+ }
189
+ }
190
+ }
191
+ let bg = base || pageCanvas;
192
+ for (let i = layers.length - (base ? 2 : 1); i >= 0; i--) bg = over(layers[i], bg);
193
+ return { bg, usesImage, opaqueAncestor: Boolean(base) };
194
+ };
195
+
196
+ const rows = [];
197
+ let textTotal = 0, textChromatic = 0, longColored = 0;
198
+ const isSvgText = (el) => el instanceof SVGTextContentElement;
199
+ const candidates = document.body.querySelectorAll("*");
200
+ let index = -1;
201
+ for (const el of candidates) {
202
+ index += 1;
203
+ const svg = isSvgText(el) && !(el.parentElement instanceof SVGTextContentElement);
204
+ const own = [...el.childNodes]
205
+ .filter((n) => n.nodeType === 3)
206
+ .map((n) => n.textContent)
207
+ .join(" ")
208
+ .replace(/\s+/g, " ")
209
+ .trim();
210
+ const text = svg ? el.textContent.replace(/\s+/g, " ").trim() : own;
211
+ if (text.length < 2) continue;
212
+ if (!svg && el instanceof SVGElement) continue;
213
+ if (["SCRIPT", "STYLE", "NOSCRIPT", "TITLE"].includes(el.tagName.toUpperCase())) continue;
214
+ const rect = el.getBoundingClientRect();
215
+ if (rect.width < 2 || rect.height < 2) continue; // empty or visually hidden (sr-only)
216
+ const s = getComputedStyle(el);
217
+ if (s.visibility === "hidden" || s.display === "none") continue;
218
+ let opacity = 1;
219
+ let hidden = false;
220
+ for (let e = el; e; e = e.parentElement) {
221
+ const es = getComputedStyle(e);
222
+ opacity *= parseFloat(es.opacity);
223
+ if (clippedAway(es)) hidden = true;
224
+ }
225
+ if (opacity === 0 || hidden) continue;
226
+
227
+ let fgRaw;
228
+ if (svg) {
229
+ const paint = (c, o) => {
230
+ const v = toRgba(c);
231
+ return v && v.a > 0 ? { ...v, a: v.a * parseFloat(o) } : null;
232
+ };
233
+ fgRaw = paint(s.fill, s.fillOpacity) || paint(s.stroke, s.strokeOpacity);
234
+ if (!fgRaw) {
235
+ noteOnce("Some SVG text uses a gradient, pattern or no paint; it was not measured.");
236
+ continue;
237
+ }
238
+ } else {
239
+ fgRaw = toRgba(s.color);
240
+ if (!fgRaw) continue;
241
+ if (fgRaw.a === 0) {
242
+ noteOnce("Text with a transparent color (for example background-clip text) was not measured.");
243
+ continue;
244
+ }
245
+ }
246
+ let bgInfo = backgroundOf(el);
247
+ let bg = bgInfo.bg;
248
+ // A box on the page but outside this tile is read on the tile that shows it; here it only counts for text chroma.
249
+ const boxTop = rect.top + scrollY;
250
+ const outside = boxTop + rect.height <= offsetY || boxTop >= offsetY + tileH;
251
+ if (outside && boxTop + rect.height > 0 && boxTop < pageH) {
252
+ const styled = over({ ...fgRaw, a: fgRaw.a * opacity }, { ...bg, a: 1 });
253
+ textTotal++;
254
+ if (chromatic(styled)) {
255
+ textChromatic++;
256
+ if (text.length > 80) longColored++;
257
+ }
258
+ continue;
259
+ }
260
+ if (svg || bgInfo.usesImage) {
261
+ const est = pixelBackground(rect, fgRaw);
262
+ if (!est) {
263
+ noteOnce("Some text over an image or SVG could not be located in the screenshot and was skipped.");
264
+ continue;
265
+ }
266
+ bg = est;
267
+ noteOnce(
268
+ svg
269
+ ? "Text inside SVG: background estimated from pixels."
270
+ : "Text over an image or gradient: background estimated from pixels.",
271
+ );
272
+ }
273
+ const paint = (under) => over({ ...fgRaw, a: fgRaw.a * opacity }, { ...under, a: 1 });
274
+ let fg = paint(bg);
275
+
276
+ const size = parseFloat(s.fontSize);
277
+ const weight = parseInt(s.fontWeight, 10) || 400;
278
+ const large = size >= 24 || (size >= 18.66 && weight >= 700);
279
+ let r = ratio(fg, bg);
280
+
281
+ // Ancestors miss what is painted beside the text: a sibling image, video or overlay, or a
282
+ // default canvas that is not white. Check failing rows, and rows that fell back to the
283
+ // page canvas, against the screenshot before trusting the computed background.
284
+ // The 1.5 ratio between the two backgrounds and the 24-step color match are
285
+ // implementation tolerances for "clearly different", not rule thresholds.
286
+ const fails = r < (large ? 3 : 4.5);
287
+ if (!svg && !bgInfo.usesImage && (fails || !bgInfo.opaqueAncestor)) {
288
+ // Plain dominant color first: if it agrees with the computed background, the result
289
+ // stands (this keeps faint text that nearly matches its background a fail). Only when
290
+ // the dominant color is the glyph color itself, look past it.
291
+ const top = pixelBackground(rect);
292
+ const est = top && sameColor(top, fgRaw) ? pixelBackground(rect, fgRaw) : top;
293
+ const agrees = est && ratio(est, bg) <= 1.5;
294
+ if (!agrees) {
295
+ if (est && !sameColor(est, fgRaw)) {
296
+ bg = est;
297
+ fg = paint(bg);
298
+ r = ratio(fg, bg);
299
+ noteOnce(
300
+ "Some text sits over something that is not its ancestor (an image, video, overlay or a non-white default canvas): background estimated from pixels.",
301
+ );
302
+ } else if (!est && fails && bgInfo.opaqueAncestor) {
303
+ // Box lies outside the capture; the opaque ancestor makes the computed result reliable.
304
+ noteOnce("Some text was checked from styles only (outside the screenshot).");
305
+ } else if (fails) {
306
+ // No usable pixels (and no opaque ancestor), or only the glyph color. A box off the page is read in
307
+ // every tile, so only the first tile counts it.
308
+ if (!outside || offsetY === 0) uncertain++;
309
+ textTotal++;
310
+ if (chromatic(fg)) {
311
+ textChromatic++;
312
+ if (text.length > 80) longColored++;
313
+ }
314
+ continue;
315
+ }
316
+ }
317
+ }
318
+ rows.push({
319
+ el: `${index}:${text.slice(0, 60)}`,
320
+ ...(outside ? { outside: true } : {}),
321
+ text: text.slice(0, 60),
322
+ fg: hex(fg),
323
+ bg: hex(bg),
324
+ ratio: +r.toFixed(2),
325
+ size: large ? "large" : "normal",
326
+ pass: r >= (large ? 3 : 4.5),
327
+ warning: isWarning(el, text),
328
+ });
329
+
330
+ textTotal++;
331
+ if (chromatic(fg)) {
332
+ textChromatic++;
333
+ if (text.length > 80) longColored++;
334
+ }
335
+ }
336
+ return {
337
+ area,
338
+ contrast: rows,
339
+ textChroma: {
340
+ chromaticPct: textTotal ? +((100 * textChromatic) / textTotal).toFixed(1) : 0,
341
+ longColoredBlocks: longColored,
342
+ },
343
+ notes,
344
+ uncertain,
345
+ };
346
+ }