@whzxc/agent-design-gallery 0.7.0 → 0.8.0

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.
@@ -0,0 +1,103 @@
1
+ /** The same comparator runs in Node tests and as a browser function handle. */
2
+ export function pixelComparator() {
3
+ const color = (image, offset) => image.data[offset + 3] === 0 ? [0, 0, 0, 0] : [...image.data.subarray(offset, offset + 4)];
4
+ const delta = (a, b) => Math.max(...a.map((value, channel) => Math.abs(value - b[channel])));
5
+ const blend = (value, a, b) => {
6
+ let channel = 0;
7
+ for (let index = 1; index < 4; index++) if (Math.abs(b[index] - a[index]) > Math.abs(b[channel] - a[channel])) channel = index;
8
+ const t = (value[channel] - a[channel]) / (b[channel] - a[channel]);
9
+ return t >= 0 && t <= 1 && value.every((number, index) => Math.abs(number - a[index] - t * (b[index] - a[index])) <= 2)
10
+ ? delta(value, a) > 2 && delta(value, b) > 2 : null;
11
+ };
12
+ const antialias = (before, after, x, y, a, b, minimum) => {
13
+ const anchors = new Map();
14
+ for (let dy = Math.max(0, y - 1); dy <= Math.min(before.height - 1, y + 1); dy++) for (let dx = Math.max(0, x - 1); dx <= Math.min(before.width - 1, x + 1); dx++) {
15
+ const offset = (dy * before.width + dx) * 4, old = color(before, offset), current = color(after, offset);
16
+ if (delta(old, current)) continue;
17
+ const key = old.join(','), entry = anchors.get(key) ?? { color: old, count: 0 };
18
+ entry.count++; anchors.set(key, entry);
19
+ }
20
+ const colors = [...anchors.values()].filter(anchor => anchor.count >= 3).map(anchor => anchor.color);
21
+ const luminance = rgba => rgba[0] * 0.2126 + rgba[1] * 0.7152 + rgba[2] * 0.0722;
22
+ for (let i = 0; i < colors.length; i++) for (let j = i + 1; j < colors.length; j++) {
23
+ if (delta(colors[i], colors[j]) <= minimum || luminance(colors[i]) === luminance(colors[j])) continue;
24
+ const old = blend(a, colors[i], colors[j]), current = blend(b, colors[i], colors[j]);
25
+ if (old !== null && current !== null && (old || current)) return true;
26
+ }
27
+ return false;
28
+ };
29
+ return (before, after, evidence = null, isolated = null) => {
30
+ const width = Math.max(before?.width ?? 0, after?.width ?? 0);
31
+ const height = Math.max(before?.height ?? 0, after?.height ?? 0);
32
+ const difference = new Uint8ClampedArray(width * height * 4);
33
+ const bleed = isolated ? new Uint8ClampedArray(width * height * 4) : null;
34
+ const classes = Object.fromEntries(['imperceptible', 'antialiasing', 'boundary_bleed', 'bitmap_resampling', 'other'].map(name => [name, { pixels: 0, ratio: 0, maxChannelDelta: 0, withinThreshold: true }]));
35
+ const palette = { imperceptible: [100, 116, 139], antialiasing: [245, 158, 11], boundary_bleed: [14, 165, 233], bitmap_resampling: [168, 85, 247], other: [255, 0, 255] };
36
+ const raster = new Set(evidence?.rasterMask ?? []), policy = evidence?.thresholds;
37
+ const eligible = evidence?.snapshotsMatch && before?.width === after?.width && before?.height === after?.height;
38
+ let differentPixels = 0, needsIsolation = false;
39
+ for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
40
+ const hasBefore = before && x < before.width && y < before.height;
41
+ const hasAfter = after && x < after.width && y < after.height;
42
+ let delta = 0;
43
+ if (!!hasBefore !== !!hasAfter) delta = 255;
44
+ else if (hasBefore && hasAfter) {
45
+ const a = (y * before.width + x) * 4, b = (y * after.width + x) * 4;
46
+ for (let channel = 0; channel < 4; channel++) {
47
+ const old = channel < 3 && before.data[a + 3] === 0 ? 0 : before.data[a + channel];
48
+ const current = channel < 3 && after.data[b + 3] === 0 ? 0 : after.data[b + channel];
49
+ delta = Math.max(delta, Math.abs(old - current));
50
+ }
51
+ }
52
+ if (!delta) continue;
53
+ differentPixels++;
54
+ const offset = (y * width + x) * 4;
55
+ let name = 'other';
56
+ if (policy && delta <= policy.imperceptible.maxChannelDelta) name = 'imperceptible';
57
+ else if (eligible) {
58
+ const old = color(before, offset), current = color(after, offset), alone = isolated && color(isolated, offset);
59
+ const external = alone && old.some((value, channel) => value !== alone[channel]);
60
+ if (!isolated) needsIsolation = true;
61
+ const edge = policy.boundaryBleed.edgeDevicePixels;
62
+ if (external) {
63
+ if ((x < edge || y < edge || x >= width - edge || y >= height - edge) && current.every((value, channel) => value === alone[channel])) name = 'boundary_bleed';
64
+ } else if (raster.has(y * width + x)) name = 'bitmap_resampling';
65
+ else if (delta <= policy.antialiasing.maxChannelDelta && evidence.edgeBoxes.some(box => x >= box.x && y >= box.y && x < box.x + box.width && y < box.y + box.height) && antialias(before, after, x, y, old, current, policy.imperceptible.maxChannelDelta)) name = 'antialiasing';
66
+ }
67
+ const stat = classes[name]; stat.pixels++; stat.maxChannelDelta = Math.max(stat.maxChannelDelta, delta);
68
+ difference.set(palette[name], offset); difference[offset + 3] = 255;
69
+ if (bleed && name === 'boundary_bleed') { bleed.set([255, 255, 255, 255], offset); }
70
+ }
71
+ const areaPixels = width * height;
72
+ for (const stat of Object.values(classes)) stat.ratio = areaPixels ? stat.pixels / areaPixels : 0;
73
+ classes.other.withinThreshold = classes.other.pixels === 0;
74
+ if (policy) {
75
+ classes.antialiasing.withinThreshold = !classes.antialiasing.pixels || classes.antialiasing.ratio < policy.antialiasing.maxRatioExclusive;
76
+ classes.bitmap_resampling.withinThreshold = !classes.bitmap_resampling.pixels || classes.bitmap_resampling.ratio < policy.bitmapResampling.maxRatioExclusive;
77
+ }
78
+ return { width, height, difference, bleed, needsIsolation, pixels: { areaPixels, equalPixels: areaPixels - differentPixels, differentPixels, classes } };
79
+ };
80
+ }
81
+
82
+ export async function imageDifference({ before, after, compare, crop = null, isolated = null, evidence = null }) {
83
+ const read = async (source, clip = null) => {
84
+ if (source === null) return null;
85
+ const image = new Image(); image.src = source;
86
+ await image.decode();
87
+ const canvas = document.createElement('canvas');
88
+ canvas.width = image.naturalWidth; canvas.height = image.naturalHeight;
89
+ const context = canvas.getContext('2d', { colorSpace: 'srgb', willReadFrequently: true });
90
+ context.drawImage(image, 0, 0);
91
+ return clip ? context.getImageData(clip.x, clip.y, clip.width, clip.height) : context.getImageData(0, 0, canvas.width, canvas.height);
92
+ };
93
+ const first = await read(before, crop), second = await read(after), alone = await read(isolated, crop);
94
+ const result = compare(first, second, evidence, alone);
95
+ const encode = image => {
96
+ const canvas = document.createElement('canvas'); canvas.width = image.width; canvas.height = image.height;
97
+ canvas.getContext('2d').putImageData(image, 0, 0);
98
+ return canvas.toDataURL('image/png').split(',')[1];
99
+ };
100
+ return { pixels: result.pixels, png: encode(new ImageData(result.difference, result.width, result.height)),
101
+ beforePng: crop && encode(first), isolatedPng: alone && encode(alone),
102
+ bleedPng: result.bleed && encode(new ImageData(result.bleed, result.width, result.height)), needsIsolation: result.needsIsolation };
103
+ }
@@ -19,7 +19,7 @@ import { CliError, classifyError } from '../cli-output.mjs';
19
19
  export function commandFailure(error, data, warnings, fallback = 'INTERNAL_ERROR') {
20
20
  const classified = classifyError(error);
21
21
  const failure = error instanceof CliError ? error : new CliError(classified.error.code === 'INTERNAL_ERROR' ? fallback : classified.error.code, classified.error.message, { cause: error });
22
- if (!['CHECK_FAILED', 'COMPARE_FAILED'].includes(failure.code)) data.complete = false;
22
+ if (!['CHECK_FAILED', 'COMPARE_FAILED', 'DIFF_FAILED'].includes(failure.code)) data.complete = false;
23
23
  failure.data = data;
24
24
  failure.warnings = [...new Set([...warnings, ...failure.warnings])];
25
25
  return failure;
@@ -1,6 +1,7 @@
1
1
  export type Content = unknown;
2
2
  export type Capture = ({ region: string; selector?: never; page?: never } | { selector: string; region?: never; page?: never } | { page: true; region?: never; selector?: never }) & { viewport?: string; origin?: string; clicks?: string[] };
3
- export interface FrameProps { name: string; width?: number; height?: number; fixture?: string; capture?: Capture; note?: string; children?: Content }
3
+ export interface FrameParent { source: string; frame: string; selector: string }
4
+ export interface FrameProps { name: string; width?: number; height?: number; fixture?: string; capture?: Capture; parent?: FrameParent; note?: string; children?: Content }
4
5
  export interface IconProps { name: string; size?: number; class?: string }
5
6
  export function Frame(props: FrameProps): JSX.Element;
6
7
  export function Icon(props: IconProps): JSX.Element;