@uselazuli/cli 0.0.0-stage → 1.0.2
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/CHANGELOG.md +120 -0
- package/LICENSE +21 -0
- package/README.md +202 -2
- package/agents/lazuli-design-reviewer.md +30 -0
- package/agents/lazuli-first-look.md +22 -0
- package/agents/lazuli-task-tester.md +25 -0
- package/bin/lazuli.mjs +747 -0
- package/package.json +11 -4
- package/rules/accessibility/HYPOTHESES.md +8 -0
- package/rules/accessibility/_area.yaml +7 -0
- package/rules/accessibility/operable.yaml +100 -0
- package/rules/accessibility/perceivable.yaml +153 -0
- package/rules/accessibility/robust.yaml +24 -0
- package/rules/accessibility/understandable.yaml +72 -0
- package/rules/color/HYPOTHESES.md +27 -0
- package/rules/color/_area.yaml +10 -0
- package/rules/color/adapt.yaml +183 -0
- package/rules/color/create.yaml +259 -0
- package/rules/color/review.yaml +215 -0
- package/rules/color/signal.yaml +196 -0
- package/rules/content/HYPOTHESES.md +19 -0
- package/rules/content/_area.yaml +8 -0
- package/rules/content/review.yaml +378 -0
- package/rules/content/voice.yaml +144 -0
- package/rules/content/writing.yaml +202 -0
- package/rules/data/HYPOTHESES.md +11 -0
- package/rules/data/_area.yaml +7 -0
- package/rules/data/charts.yaml +189 -0
- package/rules/data/dashboards.yaml +141 -0
- package/rules/data/review.yaml +159 -0
- package/rules/graphic/HYPOTHESES.md +18 -0
- package/rules/graphic/_area.yaml +8 -0
- package/rules/graphic/identity.yaml +141 -0
- package/rules/graphic/pieces.yaml +164 -0
- package/rules/graphic/review.yaml +271 -0
- package/rules/graphic/symbol.yaml +141 -0
- package/rules/motion/HYPOTHESES.md +19 -0
- package/rules/motion/_area.yaml +7 -0
- package/rules/motion/micro.yaml +143 -0
- package/rules/motion/review.yaml +347 -0
- package/rules/motion/transitions.yaml +169 -0
- package/rules/typography/HYPOTHESES.md +12 -0
- package/rules/typography/_area.yaml +8 -0
- package/rules/typography/adapt.yaml +45 -0
- package/rules/typography/create.yaml +126 -0
- package/rules/typography/review.yaml +127 -0
- package/rules/ui/HYPOTHESES.md +23 -0
- package/rules/ui/_area.yaml +9 -0
- package/rules/ui/compose.yaml +246 -0
- package/rules/ui/grid.yaml +75 -0
- package/rules/ui/review.yaml +400 -0
- package/rules/ui/states.yaml +257 -0
- package/rules/ux/HYPOTHESES.md +13 -0
- package/rules/ux/_area.yaml +8 -0
- package/rules/ux/flows.yaml +164 -0
- package/rules/ux/forms.yaml +142 -0
- package/rules/ux/review.yaml +186 -0
- package/skills/lazuli/SKILL.md +183 -0
- package/skills/lazuli/procedures/create.md +29 -0
- package/skills/lazuli/procedures/explain-decisions.md +22 -0
- package/skills/lazuli/procedures/improve.md +30 -0
- package/skills/lazuli/procedures/remember-this.md +41 -0
- package/skills/lazuli/procedures/review-design.md +36 -0
- package/skills/lazuli/procedures/show-options.md +33 -0
- package/skills/lazuli-accessibility-check/SKILL.md +68 -0
- package/skills/lazuli-color-context-and-culture/SKILL.md +53 -0
- package/skills/lazuli-color-context-and-culture/references/sectors.md +26 -0
- package/skills/lazuli-color-palette-by-intent/SKILL.md +54 -0
- package/skills/lazuli-color-palette-by-intent/references/intent-chords.md +51 -0
- package/skills/lazuli-color-screen-review/SKILL.md +51 -0
- package/skills/lazuli-color-states-and-signals/SKILL.md +53 -0
- package/skills/lazuli-content-interface-writing/SKILL.md +53 -0
- package/skills/lazuli-content-voice/SKILL.md +40 -0
- package/skills/lazuli-data-charts-and-dashboards/SKILL.md +59 -0
- package/skills/lazuli-graphic-derived-pieces/SKILL.md +40 -0
- package/skills/lazuli-graphic-derived-pieces/references/piece-template.html +24 -0
- package/skills/lazuli-graphic-identity/SKILL.md +45 -0
- package/skills/lazuli-motion-micro-and-transitions/SKILL.md +49 -0
- package/skills/lazuli-typography-choose-and-pair/SKILL.md +50 -0
- package/skills/lazuli-typography-screen-review/SKILL.md +51 -0
- package/skills/lazuli-ui-composition/SKILL.md +42 -0
- package/skills/lazuli-ui-composition/references/option-template.html +34 -0
- package/skills/lazuli-ui-layout-review/SKILL.md +47 -0
- package/skills/lazuli-ui-states-and-system/SKILL.md +54 -0
- package/skills/lazuli-ux-flows-and-forms/SKILL.md +51 -0
- package/skills/lazuli-ux-task-test/SKILL.md +44 -0
- package/src/browser.mjs +162 -0
- package/src/commands/capture.mjs +313 -0
- package/src/commands/catalog.mjs +37 -0
- package/src/commands/check-design.mjs +463 -0
- package/src/commands/check-goals.mjs +57 -0
- package/src/commands/compare-designs.mjs +160 -0
- package/src/commands/draft-memo.mjs +162 -0
- package/src/commands/hooks.mjs +345 -0
- package/src/commands/install-hooks.mjs +120 -0
- package/src/commands/install.mjs +132 -0
- package/src/commands/list-rules.mjs +49 -0
- package/src/commands/map-page.mjs +594 -0
- package/src/commands/record-answers.mjs +79 -0
- package/src/commands/record-log.mjs +325 -0
- package/src/commands/record-task.mjs +250 -0
- package/src/commands/render-options.mjs +227 -0
- package/src/commands/start-project.mjs +32 -0
- package/src/measure/a11y.mjs +391 -0
- package/src/measure/asset.mjs +206 -0
- package/src/measure/at-rest.mjs +49 -0
- package/src/measure/cases.mjs +566 -0
- package/src/measure/chart-read.mjs +401 -0
- package/src/measure/chart.mjs +264 -0
- package/src/measure/coherence.mjs +145 -0
- package/src/measure/color.mjs +86 -0
- package/src/measure/content.mjs +82 -0
- package/src/measure/cvd.mjs +24 -0
- package/src/measure/extreme.mjs +241 -0
- package/src/measure/forms.mjs +150 -0
- package/src/measure/in-page/a11y.js +522 -0
- package/src/measure/in-page/color.js +348 -0
- package/src/measure/in-page/composition.js +1496 -0
- package/src/measure/in-page/contrast.js +15 -0
- package/src/measure/in-page/cvd.js +173 -0
- package/src/measure/in-page/extreme.js +377 -0
- package/src/measure/in-page/frames.js +781 -0
- package/src/measure/in-page/look.js +28 -0
- package/src/measure/in-page/motion-probe.js +171 -0
- package/src/measure/in-page/style.js +107 -0
- package/src/measure/in-page/svg-strokes.js +112 -0
- package/src/measure/in-page/svg-uses.js +50 -0
- package/src/measure/in-page/symbol.js +123 -0
- package/src/measure/in-page/type.js +269 -0
- package/src/measure/in-page/weight.js +207 -0
- package/src/measure/layout.mjs +100 -0
- package/src/measure/motion.mjs +509 -0
- package/src/measure/piece.mjs +195 -0
- package/src/measure/registry.mjs +253 -0
- package/src/measure/rhythm.mjs +81 -0
- package/src/measure/signature.mjs +65 -0
- package/src/measure/states.mjs +240 -0
- package/src/measure/style.mjs +13 -0
- package/src/measure/symbol.mjs +268 -0
- package/src/measure/terms.mjs +153 -0
- package/src/measure/tiles.mjs +20 -0
- package/src/measure/timeline.mjs +621 -0
- package/src/measure/type.mjs +167 -0
- package/src/measure/weight.mjs +18 -0
- package/src/private-strings.mjs +81 -0
- package/src/project/blind.mjs +70 -0
- package/src/project/edits.mjs +154 -0
- package/src/project/gate.mjs +168 -0
- package/src/project/lazuli-md.mjs +521 -0
- package/src/project/log.mjs +335 -0
- package/src/project/match.mjs +200 -0
- package/src/project/records.mjs +380 -0
- package/src/project/tasks.mjs +153 -0
- package/src/report/evaluate.mjs +27 -0
- package/src/report/evaluators/a11y.mjs +227 -0
- package/src/report/evaluators/chart.mjs +151 -0
- package/src/report/evaluators/coherence.mjs +103 -0
- package/src/report/evaluators/color.mjs +133 -0
- package/src/report/evaluators/content.mjs +112 -0
- package/src/report/evaluators/extreme.mjs +87 -0
- package/src/report/evaluators/forms.mjs +116 -0
- package/src/report/evaluators/layout.mjs +103 -0
- package/src/report/evaluators/motion.mjs +280 -0
- package/src/report/evaluators/piece.mjs +69 -0
- package/src/report/evaluators/rhythm.mjs +99 -0
- package/src/report/evaluators/states.mjs +39 -0
- package/src/report/evaluators/style.mjs +35 -0
- package/src/report/evaluators/symbol.mjs +110 -0
- package/src/report/evaluators/terms.mjs +49 -0
- package/src/report/evaluators/type.mjs +166 -0
- package/src/report/evaluators/weight.mjs +51 -0
- package/src/report/evidence.mjs +4 -0
- package/src/report/finding.mjs +11 -0
- package/src/report/format.mjs +215 -0
- package/src/rules/hypotheses.mjs +28 -0
- package/src/rules/load.mjs +274 -0
- package/src/rules/public-text.mjs +56 -0
- package/src/rules/schema.mjs +220 -0
- package/templates/LAZULI.md +88 -0
|
@@ -0,0 +1,781 @@
|
|
|
1
|
+
// Evaluated as one function expression, (contrast) => ({ createAnalyzer, bandsOf }), where contrast is the value
|
|
2
|
+
// of in-page/contrast.js ({ lin, lum, ratio }); compose it as `(${framesSource})(${contrastSource})`. Plain
|
|
3
|
+
// script, no imports, no trailing semicolon. It runs in the motion analysis page and, in tests, under node:vm.
|
|
4
|
+
//
|
|
5
|
+
// Images are { width, height, data } with data as RGBA bytes (an ImageData). The library never decodes a PNG:
|
|
6
|
+
// Node has no decoder, so frames are decoded in the analysis page (createImageBitmap) and come
|
|
7
|
+
// here as raw pixels. push() keeps a reference to the image it is given (no copy): the caller must not change it.
|
|
8
|
+
//
|
|
9
|
+
// createAnalyzer(options) -> { push(img, t), result() }, frames pushed in time order, t in ms on the clock of
|
|
10
|
+
// options.loadMs. options.scrolls ([{ fromMs, toMs }], default none) and options.scrollSettleMs mark the
|
|
11
|
+
// scripted scrolls whose frames are not read for flashes; result().notes says so when any frame was left out.
|
|
12
|
+
// options.maxShiftPx bounds the movement the flash reading looks for. result().flashes = { events,
|
|
13
|
+
// maxPerSecond, at, inPlace: { maxPerSecond, at }, closings: { t, red, moved, box, inPlace }[] }: a closing is
|
|
14
|
+
// in place when its cells, less those where movement was found and those meeting result({ movers }) (boxes in
|
|
15
|
+
// CSS px of content the caller reads as moving), still cover the flash area; inPlace counts only those. box
|
|
16
|
+
// (CSS px) bounds every closing cell; moved says whether movement was found in any of them.
|
|
17
|
+
// bandsOf(img, { bandLevels }) reads the bands of one gradient shot.
|
|
18
|
+
(contrast) => {
|
|
19
|
+
// Relative luminance through per-channel tables built from contrast.lum (it is a weighted sum of the
|
|
20
|
+
// linearized channels), so no per-pixel pow and no object per pixel.
|
|
21
|
+
const LR = new Float64Array(256);
|
|
22
|
+
const LG = new Float64Array(256);
|
|
23
|
+
const LB = new Float64Array(256);
|
|
24
|
+
const LIN = new Float64Array(256);
|
|
25
|
+
for (let v = 0; v < 256; v++) {
|
|
26
|
+
LR[v] = contrast.lum({ r: v, g: 0, b: 0 });
|
|
27
|
+
LG[v] = contrast.lum({ r: 0, g: v, b: 0 });
|
|
28
|
+
LB[v] = contrast.lum({ r: 0, g: 0, b: v });
|
|
29
|
+
LIN[v] = contrast.lin(v);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// CIE 1976 UCS chromaticity (u', v') of an sRGB color: linear sRGB to CIE XYZ (the sRGB matrix of
|
|
33
|
+
// IEC 61966-2-1, standard colorimetry, not a WCAG value), then u' = 4X / (X + 15Y + 3Z), v' = 9Y / (same).
|
|
34
|
+
// Black has no chromaticity (0 / 0); our reading gives it the chromaticity of white (D65), so a change to or
|
|
35
|
+
// from black is read as a change to or from neutral.
|
|
36
|
+
// Colors are whole sRGB levels (a cell's mean color is rounded first); u'v' is cached per color, one
|
|
37
|
+
// object per distinct color and none per pixel.
|
|
38
|
+
const UV = new Map();
|
|
39
|
+
const uvOf = (r, g, b) => {
|
|
40
|
+
const key = (r << 16) | (g << 8) | b;
|
|
41
|
+
let hit = UV.get(key);
|
|
42
|
+
if (hit === undefined) {
|
|
43
|
+
const R = LIN[r];
|
|
44
|
+
const G = LIN[g];
|
|
45
|
+
const B = LIN[b];
|
|
46
|
+
const X = 0.4124 * R + 0.3576 * G + 0.1805 * B;
|
|
47
|
+
const Y = 0.2126 * R + 0.7152 * G + 0.0722 * B;
|
|
48
|
+
const Z = 0.0193 * R + 0.1192 * G + 0.9505 * B;
|
|
49
|
+
const d = X + 15 * Y + 3 * Z;
|
|
50
|
+
hit = d > 0 ? { u: (4 * X) / d, v: (9 * Y) / d } : null;
|
|
51
|
+
UV.set(key, hit);
|
|
52
|
+
}
|
|
53
|
+
return hit ?? uvOf(255, 255, 255);
|
|
54
|
+
};
|
|
55
|
+
const NO_UV = { u: 0, v: 0 };
|
|
56
|
+
// R / (R + G + B) on the captured sRGB channel values (our reading: the definition does not say encoded or
|
|
57
|
+
// linear values); 0 for black, which is not a saturated red.
|
|
58
|
+
const redShare = (r, g, b) => {
|
|
59
|
+
const s = r + g + b;
|
|
60
|
+
return s > 0 ? r / s : 0;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const median = (xs) => {
|
|
64
|
+
if (xs.length === 0) return null;
|
|
65
|
+
const s = [...xs].sort((a, b) => a - b);
|
|
66
|
+
const m = s.length >> 1;
|
|
67
|
+
return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// The largest sum of weights inside any ww by wh window of a w by h grid, by a summed-area table.
|
|
71
|
+
const windowMax = (mask, w, h, ww, wh) => {
|
|
72
|
+
const W = w + 1;
|
|
73
|
+
const sat = new Float64Array(W * (h + 1));
|
|
74
|
+
for (let y = 0; y < h; y++) {
|
|
75
|
+
let row = 0;
|
|
76
|
+
for (let x = 0; x < w; x++) {
|
|
77
|
+
row += mask[y * w + x];
|
|
78
|
+
sat[(y + 1) * W + x + 1] = sat[y * W + x + 1] + row;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
let best = 0;
|
|
82
|
+
for (let y = 0; y + wh <= h; y++) {
|
|
83
|
+
for (let x = 0; x + ww <= w; x++) {
|
|
84
|
+
const s = sat[(y + wh) * W + x + ww] - sat[y * W + x + ww] - sat[(y + wh) * W + x] + sat[y * W + x];
|
|
85
|
+
if (s > best) best = s;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return best;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// Flashes as WCAG 2.2 "general flash and red flash thresholds" and Understanding 2.3.1 read
|
|
92
|
+
// (https://www.w3.org/TR/WCAG22/ and https://www.w3.org/WAI/WCAG22/Understanding/three-flashes-or-below-threshold.html,
|
|
93
|
+
// opened while these rules were written; values come in through `flash` from rule data, never from this file):
|
|
94
|
+
// - transition: the change between adjacent peaks and valleys of relative luminance over time (Note 2). Each
|
|
95
|
+
// cell of cellPx frame px keeps the luminance of its last peak or valley (the mean relative luminance of
|
|
96
|
+
// its pixels); a general transition is counted when the swing from that extreme reaches minLumaChange
|
|
97
|
+
// ("10% or more of the maximum relative luminance") with the darker side below maxDarkLuma ("below
|
|
98
|
+
// 0.80"), and the extreme then resets to the current value. Small per-frame steps add up, also steps
|
|
99
|
+
// within pixelNoise (every frame is read); a blink eased over two frames is one transition;
|
|
100
|
+
// - red transition: one state with R / (R + G + B) of redRatio or more ("greater than or equal to 0.8") and a
|
|
101
|
+
// difference between the states of more than redChange in CIE 1976 u'v' ("more than 0.2"), read on the
|
|
102
|
+
// cell's mean color against its color at the last red transition. The page gives no luminance floor for a
|
|
103
|
+
// red state (the 0.80 darker condition belongs to the general flash only), so none is applied; a dark red
|
|
104
|
+
// is a red state;
|
|
105
|
+
// - flash: a pair of opposing transitions of the same cell (an increase then a decrease, or the reverse); a
|
|
106
|
+
// transition that closes a flash never opens the next;
|
|
107
|
+
// - area, concurrent ("the combined area of flashes occurring concurrently"): a frame closes a flash of the
|
|
108
|
+
// field when the pixels of the cells closing a flash in that frame cover more than minAreaShare of some
|
|
109
|
+
// field ("no more than ... 25% of any 10 degree visual field" passes); a cell counts its pixels that
|
|
110
|
+
// changed by more than pixelNoise since its last extreme, not its whole area. maxPerSecond is the most such
|
|
111
|
+
// frames in a half-open span [s, s + 1000). The field is 341 by 256 px at the 1024 by 768 reference;
|
|
112
|
+
// Understanding 2.3.1 substitutes CSS pixels, so it is read as fieldWidth by fieldHeight CSS px of the
|
|
113
|
+
// recorded viewport, times `scale` in frame px, rounded (our reading that this applies at any viewport
|
|
114
|
+
// width); the window is that field rounded to whole cells, against the field area in px.
|
|
115
|
+
// Movement is not flashing (our reading: a translation is not a change of luminance in place). Per band of
|
|
116
|
+
// one cell row the horizontal shift, and per band of one cell column the vertical shift, of up to maxShiftPx
|
|
117
|
+
// is estimated from the luminance profiles of the band in the previous and the current frame; a shift is
|
|
118
|
+
// taken when it explains the changed part of the profile to within minLumaChange per pixel. A changed cell
|
|
119
|
+
// whose mean luminance (and, near a red state, u'v') matches the previous frame moved by its band's shift
|
|
120
|
+
// within minLumaChange (and redChange) moved: no transition, its extremes restart from the current frame.
|
|
121
|
+
// A flash closed by cells where a shift was found since their last flash is marked `moved`: the flashes
|
|
122
|
+
// reading keeps inPlace apart (closings that are a flash of the field without those cells and without the
|
|
123
|
+
// caller's movers), so a flash count that involves movement can be sent to the reviewer instead of failing. Frames inside a scripted scroll (options.scrolls, plus
|
|
124
|
+
// scrollSettleMs after each) are not read for flashes and the cells restart after it.
|
|
125
|
+
// Our reading beyond the pages: general and red flashes are counted apart and the larger count is reported
|
|
126
|
+
// (a red transition pairs only with a red one); a red transition's sign is plus toward the saturated red
|
|
127
|
+
// state and minus away from it. The exception for a fine balanced pattern (white noise, a checkerboard under
|
|
128
|
+
// 0.1 degree, about 3.4 CSS px by the page's 341 px per 10 degrees) is not applied as such, but the cell mean
|
|
129
|
+
// cancels any balanced inversion inside one cell (cellPx / scale CSS px, 32 at scale 0.5), so balanced
|
|
130
|
+
// patterns up to that size read as no flash, wider than the exception (a false pass the MR12 text states). A
|
|
131
|
+
// frame of another size restarts the cells and drops the flashes read before it.
|
|
132
|
+
const createAnalyzer = (o) => {
|
|
133
|
+
let prev = null;
|
|
134
|
+
let prevChanged = false;
|
|
135
|
+
let prevT = null;
|
|
136
|
+
let lastT = null;
|
|
137
|
+
let count = 0;
|
|
138
|
+
const changedAt = [];
|
|
139
|
+
const intervals = [];
|
|
140
|
+
const film = o.filmOffsets.map((offset) => ({ offset, t: null, frame: null }));
|
|
141
|
+
const events = [];
|
|
142
|
+
const closings = [];
|
|
143
|
+
const closingCells = [];
|
|
144
|
+
// Cells of the first frame's size: first and last change time and the distinct whole seconds changed in.
|
|
145
|
+
let grid = null;
|
|
146
|
+
// The cells changed beyond pixelNoise between the previous frame and this one (grid geometry), or null.
|
|
147
|
+
let hit = null;
|
|
148
|
+
|
|
149
|
+
const push = (img, t) => {
|
|
150
|
+
count++;
|
|
151
|
+
for (const f of film) {
|
|
152
|
+
if (t <= o.loadMs + f.offset) {
|
|
153
|
+
f.t = t;
|
|
154
|
+
f.frame = img;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
let changed = false;
|
|
158
|
+
hit = null;
|
|
159
|
+
if (prev !== null) {
|
|
160
|
+
if (prev.width !== img.width || prev.height !== img.height) {
|
|
161
|
+
// A frame of another size is a change of the whole frame; it is not compared pixel by pixel.
|
|
162
|
+
changed = true;
|
|
163
|
+
} else {
|
|
164
|
+
changed = compare(prev, img, t);
|
|
165
|
+
}
|
|
166
|
+
if (changed) {
|
|
167
|
+
changedAt.push(t);
|
|
168
|
+
if (prevChanged) intervals.push(t - prevT);
|
|
169
|
+
}
|
|
170
|
+
} else {
|
|
171
|
+
const cols = Math.ceil(img.width / o.cellPx);
|
|
172
|
+
const rows = Math.ceil(img.height / o.cellPx);
|
|
173
|
+
grid = { cols, rows, width: img.width, height: img.height, first: new Float64Array(cols * rows).fill(NaN), last: new Float64Array(cols * rows).fill(NaN), seconds: new Array(cols * rows).fill(null) };
|
|
174
|
+
}
|
|
175
|
+
if (o.flash) {
|
|
176
|
+
if (inScroll(t)) {
|
|
177
|
+
skipped++;
|
|
178
|
+
restart = true;
|
|
179
|
+
} else {
|
|
180
|
+
readFlash(img, t);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
prev = img;
|
|
184
|
+
prevChanged = changed;
|
|
185
|
+
prevT = t;
|
|
186
|
+
lastT = t;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const compare = (a, b, t) => {
|
|
190
|
+
const { width: w, height: h } = b;
|
|
191
|
+
const A = a.data;
|
|
192
|
+
const B = b.data;
|
|
193
|
+
const noise = o.pixelNoise;
|
|
194
|
+
const sameGrid = grid !== null && grid.width === w && grid.height === h;
|
|
195
|
+
const cellHit = sameGrid ? new Uint8Array(grid.cols * grid.rows) : null;
|
|
196
|
+
let any = false;
|
|
197
|
+
for (let y = 0; y < h; y++) {
|
|
198
|
+
for (let x = 0; x < w; x++) {
|
|
199
|
+
const i = (y * w + x) * 4;
|
|
200
|
+
if (Math.abs(B[i] - A[i]) <= noise && Math.abs(B[i + 1] - A[i + 1]) <= noise && Math.abs(B[i + 2] - A[i + 2]) <= noise) continue;
|
|
201
|
+
any = true;
|
|
202
|
+
if (cellHit) cellHit[((y / o.cellPx) | 0) * grid.cols + ((x / o.cellPx) | 0)] = 1;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (any && cellHit) {
|
|
206
|
+
const second = Math.floor(t / 1000);
|
|
207
|
+
for (let c = 0; c < cellHit.length; c++) {
|
|
208
|
+
if (!cellHit[c]) continue;
|
|
209
|
+
if (Number.isNaN(grid.first[c])) grid.first[c] = t;
|
|
210
|
+
grid.last[c] = t;
|
|
211
|
+
(grid.seconds[c] ??= new Set()).add(second);
|
|
212
|
+
}
|
|
213
|
+
hit = cellHit;
|
|
214
|
+
}
|
|
215
|
+
return any;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// Flash cells (see the definition comment above). fl is made on the first frame read for flashes.
|
|
219
|
+
let fl = null;
|
|
220
|
+
let skipped = 0;
|
|
221
|
+
let restart = true;
|
|
222
|
+
const inScroll = (t) => (o.scrolls ?? []).some((s) => t >= s.fromMs && t <= s.toMs + (o.scrollSettleMs ?? 0));
|
|
223
|
+
const makeFlashCells = (w, h) => {
|
|
224
|
+
const f = o.flash;
|
|
225
|
+
const cols = Math.ceil(w / o.cellPx);
|
|
226
|
+
const rows = Math.ceil(h / o.cellPx);
|
|
227
|
+
const n = cols * rows;
|
|
228
|
+
const fw = Math.max(1, Math.round(f.fieldWidth * o.scale));
|
|
229
|
+
const fh = Math.max(1, Math.round(f.fieldHeight * o.scale));
|
|
230
|
+
return {
|
|
231
|
+
w, h, cols, rows, n,
|
|
232
|
+
needed: f.minAreaShare * fw * fh,
|
|
233
|
+
wc: Math.min(cols, Math.max(1, Math.round(fw / o.cellPx))),
|
|
234
|
+
hc: Math.min(rows, Math.max(1, Math.round(fh / o.cellPx))),
|
|
235
|
+
area: new Float64Array(n),
|
|
236
|
+
sumL: new Float64Array(n), sumR: new Float64Array(n), sumG: new Float64Array(n), sumB: new Float64Array(n),
|
|
237
|
+
// Luminance profiles: per cell row a column profile (w), per cell column a row profile (h); this frame's
|
|
238
|
+
// and the previous one's.
|
|
239
|
+
hprof: new Float64Array(rows * w), vprof: new Float64Array(cols * h),
|
|
240
|
+
hprev: new Float64Array(rows * w), vprev: new Float64Array(cols * h),
|
|
241
|
+
hshift: new Int32Array(rows), vshift: new Int32Array(cols),
|
|
242
|
+
// General: last extreme, its sign (0 before the first transition) and the low and high seen before it.
|
|
243
|
+
ext: new Float64Array(n), sign: new Int8Array(n), lo: new Float64Array(n), hi: new Float64Array(n), open: new Uint8Array(n),
|
|
244
|
+
// Red: color at the last red transition, whether it was a saturated red, its red share, sign, open flash.
|
|
245
|
+
ru: new Float64Array(n), rv: new Float64Array(n), rred: new Uint8Array(n), rshare: new Float64Array(n), rsign: new Int8Array(n), ropen: new Int8Array(n),
|
|
246
|
+
// The cell's pixels at its last extreme (RGB), and whether a shift was found since its last flash.
|
|
247
|
+
snap: new Uint8Array(w * h * 3), moved: new Uint8Array(n),
|
|
248
|
+
};
|
|
249
|
+
};
|
|
250
|
+
const cellBounds = (c) => {
|
|
251
|
+
const cx = c % fl.cols;
|
|
252
|
+
const cy = (c - cx) / fl.cols;
|
|
253
|
+
return [cx * o.cellPx, cy * o.cellPx, Math.min((cx + 1) * o.cellPx, fl.w), Math.min((cy + 1) * o.cellPx, fl.h)];
|
|
254
|
+
};
|
|
255
|
+
const snapshot = (c, D) => {
|
|
256
|
+
const [x0, y0, x1, y1] = cellBounds(c);
|
|
257
|
+
for (let y = y0; y < y1; y++) {
|
|
258
|
+
for (let x = x0; x < x1; x++) {
|
|
259
|
+
const p = y * fl.w + x;
|
|
260
|
+
fl.snap[p * 3] = D[p * 4];
|
|
261
|
+
fl.snap[p * 3 + 1] = D[p * 4 + 1];
|
|
262
|
+
fl.snap[p * 3 + 2] = D[p * 4 + 2];
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
// Pixels of the cell changed by more than pixelNoise since its last extreme (the area the cell flashes
|
|
267
|
+
// with), counted while the snapshot takes the current pixels: one pass.
|
|
268
|
+
const changedSinceAndSnap = (c, D) => {
|
|
269
|
+
const [x0, y0, x1, y1] = cellBounds(c);
|
|
270
|
+
const noise = o.pixelNoise;
|
|
271
|
+
const S = fl.snap;
|
|
272
|
+
let k = 0;
|
|
273
|
+
for (let y = y0; y < y1; y++) {
|
|
274
|
+
for (let p = y * fl.w + x0, end = y * fl.w + x1; p < end; p++) {
|
|
275
|
+
const i = p * 4;
|
|
276
|
+
const j = p * 3;
|
|
277
|
+
const r = D[i], g = D[i + 1], b = D[i + 2];
|
|
278
|
+
if (Math.abs(r - S[j]) > noise || Math.abs(g - S[j + 1]) > noise || Math.abs(b - S[j + 2]) > noise) k++;
|
|
279
|
+
S[j] = r;
|
|
280
|
+
S[j + 1] = g;
|
|
281
|
+
S[j + 2] = b;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return k;
|
|
285
|
+
};
|
|
286
|
+
// The shift (of up to maxShiftPx) that best explains the changed part of a band profile, or 0 when none
|
|
287
|
+
// explains it to within minLumaChange per pixel. len: profile length; depth: pixels summed per entry.
|
|
288
|
+
const bandShift = (cur, prv, off, len, depth) => {
|
|
289
|
+
const changedPos = [];
|
|
290
|
+
let mn = Infinity;
|
|
291
|
+
let mx = -Infinity;
|
|
292
|
+
for (let x = 0; x < len; x++) {
|
|
293
|
+
const v = cur[off + x];
|
|
294
|
+
if (v < mn) mn = v;
|
|
295
|
+
if (v > mx) mx = v;
|
|
296
|
+
if (Math.abs(v - prv[off + x]) > 1e-9) changedPos.push(x);
|
|
297
|
+
}
|
|
298
|
+
// Nothing changed, or a flat profile: no shift can be read.
|
|
299
|
+
if (changedPos.length === 0 || mx - mn <= 1e-9) return 0;
|
|
300
|
+
const R = o.maxShiftPx ?? 0;
|
|
301
|
+
// Mean residual per pixel over the changed positions for shift d, or Infinity when under half of them
|
|
302
|
+
// have a source inside the band. `step` reads every step-th changed position (the coarse pass).
|
|
303
|
+
const meanAt = (d, step) => {
|
|
304
|
+
let sum = 0;
|
|
305
|
+
let k = 0;
|
|
306
|
+
let seen = 0;
|
|
307
|
+
for (let j = 0; j < changedPos.length; j += step) {
|
|
308
|
+
seen++;
|
|
309
|
+
const x = changedPos[j];
|
|
310
|
+
const s = x - d;
|
|
311
|
+
if (s < 0 || s >= len) continue;
|
|
312
|
+
sum += Math.abs(cur[off + x] - prv[off + s]);
|
|
313
|
+
k++;
|
|
314
|
+
}
|
|
315
|
+
return k * 2 < seen ? Infinity : sum / (k * depth);
|
|
316
|
+
};
|
|
317
|
+
// Coarse to fine (speed only): every even shift on every fourth changed position, then the odd
|
|
318
|
+
// neighbors of the best at full detail.
|
|
319
|
+
let coarse = 0;
|
|
320
|
+
let coarseMean = Infinity;
|
|
321
|
+
for (let d = -R; d <= R; d += 2) {
|
|
322
|
+
const m = meanAt(d, 4);
|
|
323
|
+
if (m < coarseMean) {
|
|
324
|
+
coarseMean = m;
|
|
325
|
+
coarse = d;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const mean0 = meanAt(0, 1);
|
|
329
|
+
let best = 0;
|
|
330
|
+
let bestMean = mean0;
|
|
331
|
+
for (let d = Math.max(-R, coarse - 2); d <= Math.min(R, coarse + 2); d++) {
|
|
332
|
+
const m = meanAt(d, 1);
|
|
333
|
+
if (m < bestMean) {
|
|
334
|
+
bestMean = m;
|
|
335
|
+
best = d;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (best === 0 || bestMean >= o.flash.minLumaChange) return 0;
|
|
339
|
+
// Periodic content cannot tell movement from inversion: an inverted stripe or checker pattern equals
|
|
340
|
+
// itself moved by half its period either way. A shift whose mirror also explains the profile is not taken
|
|
341
|
+
// (content of period P moving exactly P / 2 a frame then reads as a flash, a strobe-like case).
|
|
342
|
+
if (meanAt(-best, 1) < o.flash.minLumaChange) return 0;
|
|
343
|
+
return best;
|
|
344
|
+
};
|
|
345
|
+
// Whether the cell's change is the previous frame moved by (dx, dy): its mean luminance (and, near a red
|
|
346
|
+
// state, u'v') within minLumaChange (and redChange) of the moved pixels. Pixels moved in from outside the
|
|
347
|
+
// frame are left out; a cell with none left is read as moved.
|
|
348
|
+
const movedBy = (c, D, P, dx, dy) => {
|
|
349
|
+
const f = o.flash;
|
|
350
|
+
const [x0, y0, x1, y1] = cellBounds(c);
|
|
351
|
+
let lc = 0, lp = 0, rc = 0, gc = 0, bc = 0, rp = 0, gp = 0, bp = 0, k = 0;
|
|
352
|
+
for (let y = y0; y < y1; y++) {
|
|
353
|
+
const sy = y - dy;
|
|
354
|
+
if (sy < 0 || sy >= fl.h) continue;
|
|
355
|
+
for (let x = x0; x < x1; x++) {
|
|
356
|
+
const sx = x - dx;
|
|
357
|
+
if (sx < 0 || sx >= fl.w) continue;
|
|
358
|
+
const i = (y * fl.w + x) * 4;
|
|
359
|
+
const j = (sy * fl.w + sx) * 4;
|
|
360
|
+
lc += LR[D[i]] + LG[D[i + 1]] + LB[D[i + 2]];
|
|
361
|
+
lp += LR[P[j]] + LG[P[j + 1]] + LB[P[j + 2]];
|
|
362
|
+
rc += D[i]; gc += D[i + 1]; bc += D[i + 2];
|
|
363
|
+
rp += P[j]; gp += P[j + 1]; bp += P[j + 2];
|
|
364
|
+
k++;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (k === 0) return true;
|
|
368
|
+
if (Math.abs(lc - lp) / k >= f.minLumaChange) return false;
|
|
369
|
+
const c1 = [Math.round(rc / k), Math.round(gc / k), Math.round(bc / k)];
|
|
370
|
+
const c0 = [Math.round(rp / k), Math.round(gp / k), Math.round(bp / k)];
|
|
371
|
+
if (redShare(...c1) >= f.redRatio || redShare(...c0) >= f.redRatio) {
|
|
372
|
+
const a = uvOf(...c1);
|
|
373
|
+
const b = uvOf(...c0);
|
|
374
|
+
if (Math.hypot(a.u - b.u, a.v - b.v) > f.redChange) return false;
|
|
375
|
+
}
|
|
376
|
+
return true;
|
|
377
|
+
};
|
|
378
|
+
const readFlash = (img, t) => {
|
|
379
|
+
const f = o.flash;
|
|
380
|
+
if (fl === null || fl.w !== img.width || fl.h !== img.height) {
|
|
381
|
+
fl = makeFlashCells(img.width, img.height);
|
|
382
|
+
restart = true;
|
|
383
|
+
}
|
|
384
|
+
const { w, h, cols, rows, n, area, sumL, sumR, sumG, sumB } = fl;
|
|
385
|
+
[fl.hprof, fl.hprev] = [fl.hprev, fl.hprof];
|
|
386
|
+
[fl.vprof, fl.vprev] = [fl.vprev, fl.vprof];
|
|
387
|
+
const { hprof, vprof } = fl;
|
|
388
|
+
sumL.fill(0);
|
|
389
|
+
sumR.fill(0);
|
|
390
|
+
sumG.fill(0);
|
|
391
|
+
sumB.fill(0);
|
|
392
|
+
hprof.fill(0);
|
|
393
|
+
vprof.fill(0);
|
|
394
|
+
area.fill(0);
|
|
395
|
+
const D = img.data;
|
|
396
|
+
for (let y = 0, i = 0; y < h; y++) {
|
|
397
|
+
const cy = (y / o.cellPx) | 0;
|
|
398
|
+
const row = cy * cols;
|
|
399
|
+
const hoff = cy * w;
|
|
400
|
+
for (let cx = 0; cx < cols; cx++) {
|
|
401
|
+
const end = Math.min((cx + 1) * o.cellPx, w);
|
|
402
|
+
let l = 0, sr = 0, sg = 0, sb = 0;
|
|
403
|
+
for (let x = cx * o.cellPx; x < end; x++, i += 4) {
|
|
404
|
+
const r = D[i], g = D[i + 1], b = D[i + 2];
|
|
405
|
+
const px = LR[r] + LG[g] + LB[b];
|
|
406
|
+
l += px;
|
|
407
|
+
hprof[hoff + x] += px;
|
|
408
|
+
sr += r;
|
|
409
|
+
sg += g;
|
|
410
|
+
sb += b;
|
|
411
|
+
}
|
|
412
|
+
const c = row + cx;
|
|
413
|
+
sumL[c] += l;
|
|
414
|
+
sumR[c] += sr;
|
|
415
|
+
sumG[c] += sg;
|
|
416
|
+
sumB[c] += sb;
|
|
417
|
+
area[c] += end - cx * o.cellPx;
|
|
418
|
+
vprof[cx * h + y] += l;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
// Band shifts, only where cells changed beyond pixelNoise (hit has the same geometry when sizes agree).
|
|
422
|
+
const cellHit = !restart && prev !== null && hit !== null && grid.width === w && grid.height === h ? hit : null;
|
|
423
|
+
fl.hshift.fill(0);
|
|
424
|
+
fl.vshift.fill(0);
|
|
425
|
+
if (cellHit) {
|
|
426
|
+
for (let r = 0; r < rows; r++) {
|
|
427
|
+
let any = false;
|
|
428
|
+
for (let cx = 0; cx < cols && !any; cx++) any = cellHit[r * cols + cx] === 1;
|
|
429
|
+
if (any) fl.hshift[r] = bandShift(hprof, fl.hprev, r * w, w, Math.min(o.cellPx, h - r * o.cellPx));
|
|
430
|
+
}
|
|
431
|
+
for (let cx = 0; cx < cols; cx++) {
|
|
432
|
+
let any = false;
|
|
433
|
+
for (let r = 0; r < rows && !any; r++) any = cellHit[r * cols + cx] === 1;
|
|
434
|
+
if (any) fl.vshift[cx] = bandShift(vprof, fl.vprev, cx * h, h, Math.min(o.cellPx, w - cx * o.cellPx));
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
const fired = [new Float64Array(n), new Float64Array(n), new Float64Array(n), new Float64Array(n)];
|
|
438
|
+
const firedAny = [false, false, false, false];
|
|
439
|
+
const closeW = [new Float64Array(n), new Float64Array(n)];
|
|
440
|
+
const closeAny = [false, false];
|
|
441
|
+
for (let c = 0; c < n; c++) {
|
|
442
|
+
const L = sumL[c] / area[c];
|
|
443
|
+
const r = Math.round(sumR[c] / area[c]);
|
|
444
|
+
const g = Math.round(sumG[c] / area[c]);
|
|
445
|
+
const b = Math.round(sumB[c] / area[c]);
|
|
446
|
+
const share = redShare(r, g, b);
|
|
447
|
+
const red = share >= f.redRatio;
|
|
448
|
+
// u'v' only where a red state is involved, so the color cache stays small on a video.
|
|
449
|
+
let uv = restart || red || fl.rred[c] ? uvOf(r, g, b) : NO_UV;
|
|
450
|
+
let reset = restart;
|
|
451
|
+
if (!reset && cellHit && cellHit[c]) {
|
|
452
|
+
const cx = c % cols;
|
|
453
|
+
const cy = (c - cx) / cols;
|
|
454
|
+
const dx = fl.hshift[cy];
|
|
455
|
+
const dy = fl.vshift[cx];
|
|
456
|
+
if (dx !== 0 || dy !== 0) {
|
|
457
|
+
fl.moved[c] = 1;
|
|
458
|
+
if ((dx !== 0 && movedBy(c, D, prev.data, dx, 0)) || (dy !== 0 && movedBy(c, D, prev.data, 0, dy))) {
|
|
459
|
+
reset = true;
|
|
460
|
+
uv = uvOf(r, g, b);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
if (reset) {
|
|
465
|
+
fl.ext[c] = fl.lo[c] = fl.hi[c] = L;
|
|
466
|
+
fl.sign[c] = 0;
|
|
467
|
+
fl.open[c] = 0;
|
|
468
|
+
fl.ru[c] = uv.u;
|
|
469
|
+
fl.rv[c] = uv.v;
|
|
470
|
+
fl.rred[c] = red ? 1 : 0;
|
|
471
|
+
fl.rshare[c] = share;
|
|
472
|
+
fl.rsign[c] = 0;
|
|
473
|
+
fl.ropen[c] = 0;
|
|
474
|
+
if (restart) fl.moved[c] = 0;
|
|
475
|
+
snapshot(c, D);
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
// General: follow the current peak or valley; a swing back of minLumaChange is a transition.
|
|
479
|
+
let fire = 0;
|
|
480
|
+
let follow = false;
|
|
481
|
+
const s = fl.sign[c];
|
|
482
|
+
if (s === 0) {
|
|
483
|
+
if (L > fl.hi[c]) fl.hi[c] = L;
|
|
484
|
+
if (L < fl.lo[c]) fl.lo[c] = L;
|
|
485
|
+
if (L - fl.lo[c] >= f.minLumaChange && fl.lo[c] < f.maxDarkLuma) fire = 1;
|
|
486
|
+
else if (fl.hi[c] - L >= f.minLumaChange && L < f.maxDarkLuma) fire = -1;
|
|
487
|
+
} else if (s === 1) {
|
|
488
|
+
if (L > fl.ext[c]) {
|
|
489
|
+
fl.ext[c] = L;
|
|
490
|
+
follow = true;
|
|
491
|
+
} else if (fl.ext[c] - L >= f.minLumaChange && L < f.maxDarkLuma) fire = -1;
|
|
492
|
+
} else {
|
|
493
|
+
if (L < fl.ext[c]) {
|
|
494
|
+
fl.ext[c] = L;
|
|
495
|
+
follow = true;
|
|
496
|
+
} else if (L - fl.ext[c] >= f.minLumaChange && fl.ext[c] < f.maxDarkLuma) fire = 1;
|
|
497
|
+
}
|
|
498
|
+
// Red: a change to or from a saturated red state of more than redChange in u'v'.
|
|
499
|
+
let rs = 0;
|
|
500
|
+
if ((fl.rred[c] || red) && Math.hypot(uv.u - fl.ru[c], uv.v - fl.rv[c]) > f.redChange) {
|
|
501
|
+
rs = red && !fl.rred[c] ? 1 : fl.rred[c] && !red ? -1 : share >= fl.rshare[c] ? 1 : -1;
|
|
502
|
+
}
|
|
503
|
+
const weight = fire !== 0 || rs !== 0 ? changedSinceAndSnap(c, D) : 0;
|
|
504
|
+
if (fire !== 0) {
|
|
505
|
+
fl.sign[c] = fire;
|
|
506
|
+
fl.ext[c] = L;
|
|
507
|
+
fired[fire === 1 ? 0 : 1][c] = weight;
|
|
508
|
+
firedAny[fire === 1 ? 0 : 1] = true;
|
|
509
|
+
if (fl.open[c]) {
|
|
510
|
+
closeW[0][c] = weight;
|
|
511
|
+
closeAny[0] = true;
|
|
512
|
+
fl.open[c] = 0;
|
|
513
|
+
} else {
|
|
514
|
+
fl.open[c] = 1;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
if (rs !== 0) {
|
|
518
|
+
fl.ru[c] = uv.u;
|
|
519
|
+
fl.rv[c] = uv.v;
|
|
520
|
+
fl.rred[c] = red ? 1 : 0;
|
|
521
|
+
fl.rshare[c] = share;
|
|
522
|
+
fired[rs === 1 ? 2 : 3][c] = weight;
|
|
523
|
+
firedAny[rs === 1 ? 2 : 3] = true;
|
|
524
|
+
if (rs !== fl.rsign[c]) {
|
|
525
|
+
if (fl.ropen[c] !== 0 && fl.ropen[c] !== rs) {
|
|
526
|
+
closeW[1][c] = weight;
|
|
527
|
+
closeAny[1] = true;
|
|
528
|
+
fl.ropen[c] = 0;
|
|
529
|
+
} else {
|
|
530
|
+
fl.ropen[c] = rs;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
fl.rsign[c] = rs;
|
|
534
|
+
}
|
|
535
|
+
if (fire === 0 && rs === 0 && follow) snapshot(c, D);
|
|
536
|
+
}
|
|
537
|
+
restart = false;
|
|
538
|
+
// Events: the cells that made a transition of one kind and sign in this frame cover more than the share.
|
|
539
|
+
for (let k = 0; k < 4; k++) {
|
|
540
|
+
if (firedAny[k] && windowMax(fired[k], cols, rows, fl.wc, fl.hc) > fl.needed) {
|
|
541
|
+
events.push({ t, sign: k % 2 === 0 ? 1 : -1, red: k >= 2 });
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
// A flash of the field closes in this frame when the cells closing one cover more than the share.
|
|
545
|
+
for (let k = 0; k < 2; k++) {
|
|
546
|
+
if (!closeAny[k] || windowMax(closeW[k], cols, rows, fl.wc, fl.hc) <= fl.needed) continue;
|
|
547
|
+
let moved = false;
|
|
548
|
+
let c0 = Infinity, c1 = -1, r0 = Infinity, r1 = -1;
|
|
549
|
+
for (let c = 0; c < n; c++) {
|
|
550
|
+
if (closeW[k][c] === 0) continue;
|
|
551
|
+
const cx = c % cols;
|
|
552
|
+
const cy = (c - cx) / cols;
|
|
553
|
+
c0 = Math.min(c0, cx);
|
|
554
|
+
c1 = Math.max(c1, cx);
|
|
555
|
+
r0 = Math.min(r0, cy);
|
|
556
|
+
r1 = Math.max(r1, cy);
|
|
557
|
+
if (fl.moved[c]) moved = true;
|
|
558
|
+
}
|
|
559
|
+
const x0 = c0 * o.cellPx;
|
|
560
|
+
const y0 = r0 * o.cellPx;
|
|
561
|
+
const x1 = Math.min((c1 + 1) * o.cellPx, w);
|
|
562
|
+
const y1 = Math.min((r1 + 1) * o.cellPx, h);
|
|
563
|
+
// The closing cells (index, weight, moved), kept for the in-place reading of result().
|
|
564
|
+
const cells = [];
|
|
565
|
+
for (let c = 0; c < n; c++) if (closeW[k][c] > 0) cells.push(c, closeW[k][c], fl.moved[c]);
|
|
566
|
+
closingCells.push({ cols, rows, wc: fl.wc, hc: fl.hc, needed: fl.needed, w, h, cells });
|
|
567
|
+
closings.push({
|
|
568
|
+
t,
|
|
569
|
+
red: k === 1,
|
|
570
|
+
moved,
|
|
571
|
+
box: { x: Math.round(x0 / o.scale), y: Math.round(y0 / o.scale), width: Math.round((x1 - x0) / o.scale), height: Math.round((y1 - y0) / o.scale) },
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
for (let c = 0; c < n; c++) if (closeW[0][c] > 0 || closeW[1][c] > 0) fl.moved[c] = 0;
|
|
575
|
+
};
|
|
576
|
+
// The most of the given closing times in any half-open span [s, s + 1000), and the start s of the first.
|
|
577
|
+
const perSecond = (times) => {
|
|
578
|
+
let best = 0;
|
|
579
|
+
let at = null;
|
|
580
|
+
for (let i = 0, j = 0; i < times.length; i++) {
|
|
581
|
+
while (j < times.length && times[j] < times[i] + 1000) j++;
|
|
582
|
+
if (j - i > best) {
|
|
583
|
+
best = j - i;
|
|
584
|
+
at = times[i];
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
return { best, at };
|
|
588
|
+
};
|
|
589
|
+
const topOf = (list) => {
|
|
590
|
+
const general = perSecond(list.filter((x) => !x.red).map((x) => x.t));
|
|
591
|
+
const red = perSecond(list.filter((x) => x.red).map((x) => x.t));
|
|
592
|
+
const top = red.best > general.best ? red : general;
|
|
593
|
+
return { maxPerSecond: top.best, at: top.at };
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
// Whether a closing is a flash in place (per closing box): its cells that moved
|
|
597
|
+
// (a shift found since their last flash) or that meet a box of movers (CSS px: elements the caller knows
|
|
598
|
+
// move) are left out, and what is left must still cover more than the share of some field.
|
|
599
|
+
const meets = (a, b) => a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
|
|
600
|
+
const inPlaceOf = (k, movers) => {
|
|
601
|
+
const g = closingCells[k];
|
|
602
|
+
const mask = new Float64Array(g.cols * g.rows);
|
|
603
|
+
for (let j = 0; j < g.cells.length; j += 3) {
|
|
604
|
+
const c = g.cells[j];
|
|
605
|
+
if (g.cells[j + 2]) continue;
|
|
606
|
+
const cx = c % g.cols;
|
|
607
|
+
const cy = (c - cx) / g.cols;
|
|
608
|
+
const x0 = cx * o.cellPx;
|
|
609
|
+
const y0 = cy * o.cellPx;
|
|
610
|
+
const cell = { x: x0 / o.scale, y: y0 / o.scale, width: (Math.min(x0 + o.cellPx, g.w) - x0) / o.scale, height: (Math.min(y0 + o.cellPx, g.h) - y0) / o.scale };
|
|
611
|
+
if (movers.some((m) => meets(cell, m))) continue;
|
|
612
|
+
mask[c] = g.cells[j + 1];
|
|
613
|
+
}
|
|
614
|
+
return windowMax(mask, g.cols, g.rows, g.wc, g.hc) > g.needed;
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
// opts.movers: boxes in CSS px of content the caller knows moves (elements whose animation changes a
|
|
618
|
+
// move property); flashes.inPlace counts only closings that are flashes without them.
|
|
619
|
+
// opts.settleIgnore: boxes in CSS px of areas that keep moving (moving content, endless animations);
|
|
620
|
+
// settle leaves out the changed cells that meet them, and settle.ignoredAreas counts the boxes that met
|
|
621
|
+
// any cell changed after load.
|
|
622
|
+
const result = (opts = {}) => {
|
|
623
|
+
const movers = opts.movers ?? [];
|
|
624
|
+
const ignore = opts.settleIgnore ?? [];
|
|
625
|
+
const moving = [];
|
|
626
|
+
let settle = null;
|
|
627
|
+
if (grid !== null) {
|
|
628
|
+
const n = grid.cols * grid.rows;
|
|
629
|
+
const moves = new Uint8Array(n);
|
|
630
|
+
const used = new Uint8Array(ignore.length);
|
|
631
|
+
let lastChange = null;
|
|
632
|
+
let leftOut = 0;
|
|
633
|
+
for (let c = 0; c < n; c++) {
|
|
634
|
+
if (Number.isNaN(grid.first[c])) continue;
|
|
635
|
+
const isMoving = grid.seconds[c].size >= o.minMovingSeconds && grid.last[c] - grid.first[c] > o.maxMovingMs;
|
|
636
|
+
if (isMoving) moves[c] = 1;
|
|
637
|
+
if (grid.last[c] < o.loadMs) continue;
|
|
638
|
+
// A cell changed after load that is moving content, or that meets an area given as one that keeps
|
|
639
|
+
// moving, is left out of settle and counts toward settle.ignoredAreas.
|
|
640
|
+
let skip = isMoving;
|
|
641
|
+
if (ignore.length > 0) {
|
|
642
|
+
const cx = c % grid.cols;
|
|
643
|
+
const cy = (c - cx) / grid.cols;
|
|
644
|
+
const x0 = cx * o.cellPx;
|
|
645
|
+
const y0 = cy * o.cellPx;
|
|
646
|
+
const cell = { x: x0 / o.scale, y: y0 / o.scale, width: (Math.min(x0 + o.cellPx, grid.width) - x0) / o.scale, height: (Math.min(y0 + o.cellPx, grid.height) - y0) / o.scale };
|
|
647
|
+
ignore.forEach((b, k) => {
|
|
648
|
+
if (meets(cell, b)) {
|
|
649
|
+
used[k] = 1;
|
|
650
|
+
skip = true;
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
if (skip) {
|
|
655
|
+
leftOut++;
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
if (lastChange === null || grid.last[c] > lastChange) lastChange = grid.last[c];
|
|
659
|
+
}
|
|
660
|
+
const ignoredAreas = used.reduce((s, u) => s + u, 0);
|
|
661
|
+
// Every cell changed after load was left out as an area that keeps moving: nothing was compared.
|
|
662
|
+
settle = lastChange === null
|
|
663
|
+
? { lastChangeMs: 0, unsettled: false, ignoredAreas, ...(leftOut > 0 ? { allLeftOut: true } : {}) }
|
|
664
|
+
: { lastChangeMs: lastChange - o.loadMs, unsettled: lastChange > lastT - o.quietMs, ignoredAreas };
|
|
665
|
+
// Touching moving cells (edges or corners) merge into one box.
|
|
666
|
+
const seen = new Uint8Array(n);
|
|
667
|
+
for (let c = 0; c < n; c++) {
|
|
668
|
+
if (!moves[c] || seen[c]) continue;
|
|
669
|
+
const stack = [c];
|
|
670
|
+
seen[c] = 1;
|
|
671
|
+
let c0 = Infinity, c1 = -1, r0 = Infinity, r1 = -1, firstMs = Infinity, lastMs = -Infinity;
|
|
672
|
+
const seconds = new Set();
|
|
673
|
+
while (stack.length) {
|
|
674
|
+
const k = stack.pop();
|
|
675
|
+
const cx = k % grid.cols;
|
|
676
|
+
const cy = (k - cx) / grid.cols;
|
|
677
|
+
c0 = Math.min(c0, cx);
|
|
678
|
+
c1 = Math.max(c1, cx);
|
|
679
|
+
r0 = Math.min(r0, cy);
|
|
680
|
+
r1 = Math.max(r1, cy);
|
|
681
|
+
firstMs = Math.min(firstMs, grid.first[k]);
|
|
682
|
+
lastMs = Math.max(lastMs, grid.last[k]);
|
|
683
|
+
for (const s of grid.seconds[k]) seconds.add(s);
|
|
684
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
685
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
686
|
+
const nx = cx + dx;
|
|
687
|
+
const ny = cy + dy;
|
|
688
|
+
if (nx < 0 || ny < 0 || nx >= grid.cols || ny >= grid.rows) continue;
|
|
689
|
+
const m = ny * grid.cols + nx;
|
|
690
|
+
if (moves[m] && !seen[m]) {
|
|
691
|
+
seen[m] = 1;
|
|
692
|
+
stack.push(m);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
const x0 = c0 * o.cellPx;
|
|
698
|
+
const y0 = r0 * o.cellPx;
|
|
699
|
+
const x1 = Math.min((c1 + 1) * o.cellPx, grid.width);
|
|
700
|
+
const y1 = Math.min((r1 + 1) * o.cellPx, grid.height);
|
|
701
|
+
moving.push({
|
|
702
|
+
box: { x: Math.round(x0 / o.scale), y: Math.round(y0 / o.scale), width: Math.round((x1 - x0) / o.scale), height: Math.round((y1 - y0) / o.scale) },
|
|
703
|
+
firstMs,
|
|
704
|
+
lastMs,
|
|
705
|
+
seconds: seconds.size,
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
moving.sort((p, q) => p.firstMs - q.firstMs || p.box.y - q.box.y || p.box.x - q.box.x);
|
|
709
|
+
}
|
|
710
|
+
let flashes = null;
|
|
711
|
+
if (o.flash) {
|
|
712
|
+
const all = closings.map((x, k) => ({ ...x, box: { ...x.box }, inPlace: inPlaceOf(k, movers) }));
|
|
713
|
+
flashes = {
|
|
714
|
+
events: events.map((e) => ({ ...e })),
|
|
715
|
+
...topOf(all),
|
|
716
|
+
inPlace: topOf(all.filter((x) => x.inPlace)),
|
|
717
|
+
closings: all,
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
const notes = skipped > 0 ? ["Flashes are not read while the page scrolls."] : [];
|
|
721
|
+
return {
|
|
722
|
+
count,
|
|
723
|
+
changeIntervalMs: median(intervals),
|
|
724
|
+
changedAt: [...changedAt],
|
|
725
|
+
settle,
|
|
726
|
+
moving,
|
|
727
|
+
flashes,
|
|
728
|
+
film: film.filter((f) => f.frame !== null).map((f) => ({ offset: f.offset, t: f.t, frame: f.frame })),
|
|
729
|
+
notes,
|
|
730
|
+
};
|
|
731
|
+
};
|
|
732
|
+
|
|
733
|
+
return { push, result };
|
|
734
|
+
};
|
|
735
|
+
|
|
736
|
+
// Bands of one gradient shot. On the rows and the columns at 25, 50 and 75 percent, runs of identical color;
|
|
737
|
+
// an interior run is a band when both neighbors differ from it by at most bandLevels in every channel. Per
|
|
738
|
+
// line: the widest band, the steps (runs) of the gradient that holds it (neighbors at most bandLevels apart)
|
|
739
|
+
// and levelStep, the largest channel difference between neighbor runs there. The element reading is the
|
|
740
|
+
// median line by widest band (the upper one of an even count) with its axis; null when no line has a band.
|
|
741
|
+
const bandsOf = (img, { bandLevels }) => {
|
|
742
|
+
const { width: w, height: h, data } = img;
|
|
743
|
+
const near = (a, b) => Math.abs(a[0] - b[0]) <= bandLevels && Math.abs(a[1] - b[1]) <= bandLevels && Math.abs(a[2] - b[2]) <= bandLevels;
|
|
744
|
+
const gap = (a, b) => Math.max(Math.abs(a[0] - b[0]), Math.abs(a[1] - b[1]), Math.abs(a[2] - b[2]));
|
|
745
|
+
const readLine = (axis, n, at) => {
|
|
746
|
+
const runs = [];
|
|
747
|
+
for (let k = 0; k < n; k++) {
|
|
748
|
+
const i = (axis === "row" ? at * w + k : k * w + at) * 4;
|
|
749
|
+
const c = [data[i], data[i + 1], data[i + 2]];
|
|
750
|
+
const last = runs[runs.length - 1];
|
|
751
|
+
if (last && last.c[0] === c[0] && last.c[1] === c[1] && last.c[2] === c[2]) last.len++;
|
|
752
|
+
else runs.push({ c, len: 1 });
|
|
753
|
+
}
|
|
754
|
+
let widest = -1;
|
|
755
|
+
for (let k = 1; k < runs.length - 1; k++) {
|
|
756
|
+
if (near(runs[k - 1].c, runs[k].c) && near(runs[k].c, runs[k + 1].c) && (widest < 0 || runs[k].len > runs[widest].len)) widest = k;
|
|
757
|
+
}
|
|
758
|
+
if (widest < 0) return null;
|
|
759
|
+
let a = widest;
|
|
760
|
+
let b = widest;
|
|
761
|
+
while (a > 0 && near(runs[a - 1].c, runs[a].c)) a--;
|
|
762
|
+
while (b < runs.length - 1 && near(runs[b].c, runs[b + 1].c)) b++;
|
|
763
|
+
let levelStep = 0;
|
|
764
|
+
for (let k = a; k < b; k++) levelStep = Math.max(levelStep, gap(runs[k].c, runs[k + 1].c));
|
|
765
|
+
return { axis, steps: b - a + 1, widestBandPx: runs[widest].len, levelStep };
|
|
766
|
+
};
|
|
767
|
+
const lines = [];
|
|
768
|
+
for (const [axis, n, size] of [["row", w, h], ["column", h, w]]) {
|
|
769
|
+
const at = [...new Set([0.25, 0.5, 0.75].map((p) => Math.min(size - 1, Math.floor(size * p))))];
|
|
770
|
+
for (const k of at) {
|
|
771
|
+
const r = readLine(axis, n, k);
|
|
772
|
+
if (r) lines.push(r);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
if (lines.length === 0) return null;
|
|
776
|
+
const sorted = lines.map((r, i) => ({ r, i })).sort((p, q) => p.r.widestBandPx - q.r.widestBandPx || p.i - q.i);
|
|
777
|
+
return { ...sorted[sorted.length >> 1].r };
|
|
778
|
+
};
|
|
779
|
+
|
|
780
|
+
return { createAnalyzer, bandsOf };
|
|
781
|
+
}
|