sang-ai-kit 0.1.1 → 0.2.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.
- package/README.md +9 -8
- package/generated/sang-ai-utilities.css +120 -1
- package/package.json +3 -1
- package/src/AiAssistant.tsx +20 -33
- package/src/AiSettings.tsx +6 -3
- package/src/ModelControls.tsx +3 -11
- package/src/SessionList.tsx +4 -1
- package/src/SkillSettings.tsx +4 -2
- package/src/TokenUsageBar.tsx +4 -1
- package/src/assistant.css +2125 -1712
- package/src/assistantTypes.ts +7 -2
- package/src/background-removal/BackgroundRemovalSettings.tsx +502 -0
- package/src/background-removal/ColorMattingEditor.tsx +649 -0
- package/src/background-removal/background-removal.css +294 -0
- package/src/background-removal/color-matting.css +838 -0
- package/src/background-removal/colorMatting.ts +117 -0
- package/src/background-removal/colorMatting.worker.ts +21 -0
- package/src/background-removal/index.ts +92 -0
- package/src/background-removal/inference.worker.ts +117 -0
- package/src/background-removal/models.ts +65 -0
- package/src/background-removal/processing.ts +61 -0
- package/src/background-removal/resources.ts +114 -0
- package/src/background-removal/status.ts +23 -0
- package/src/background-removal/storage.ts +56 -0
- package/src/background-removal/tool.ts +70 -0
- package/src/background-removal/types.ts +38 -0
- package/src/background-removal/useResources.ts +38 -0
- package/src/builtInToolDefinitions.ts +68 -0
- package/src/builtInTools.ts +66 -22
- package/src/bun-env.d.ts +25 -0
- package/src/conversationUtils.ts +71 -1
- package/src/endpointConnection.ts +2 -2
- package/src/features.ts +3 -1
- package/src/imageToolHost.ts +86 -0
- package/src/index.ts +15 -6
- package/src/modelConfig.ts +49 -7
- package/src/openai.ts +56 -30
- package/src/responses.ts +14 -0
- package/src/settingsTypes.ts +6 -8
- package/src/skills.ts +1 -1
- package/src/style.css +111 -67
- package/src/useAssistant.ts +5 -38
- package/src/useResolvedTheme.ts +2 -1
|
@@ -0,0 +1,649 @@
|
|
|
1
|
+
import { useEffect, useRef, useState, type PointerEvent } from 'react';
|
|
2
|
+
import { Eye, Eraser, Grid, Moon, Paintbrush, Pipette, RotateCcw, Scissors, Sliders, Sparkles, Sun, Undo2, X, ZoomIn, ZoomOut } from 'lucide-react';
|
|
3
|
+
import { applyBrushStrokes, applyColorMatting, removeColorBackground, type BrushStroke } from './colorMatting';
|
|
4
|
+
import { download } from './resources';
|
|
5
|
+
import './color-matting.css';
|
|
6
|
+
|
|
7
|
+
export interface ColorMattingEditorProps {
|
|
8
|
+
image: Blob | string;
|
|
9
|
+
english?: boolean;
|
|
10
|
+
onApply: (result: Blob, signal: AbortSignal) => void | Promise<void>;
|
|
11
|
+
onClose: () => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type Tool = 'pick' | 'erase' | 'restore';
|
|
15
|
+
type StageBackdrop = 'checker' | 'dark' | 'light';
|
|
16
|
+
|
|
17
|
+
const PRESET_COLORS = [
|
|
18
|
+
{ hex: '#ffffff', zh: '纯白', en: 'White' },
|
|
19
|
+
{ hex: '#000000', zh: '纯黑', en: 'Black' },
|
|
20
|
+
{ hex: '#00ff00', zh: '绿幕', en: 'Green' },
|
|
21
|
+
{ hex: '#0057ff', zh: '蓝幕', en: 'Blue' },
|
|
22
|
+
{ hex: '#f1f5f9', zh: '浅灰', en: 'Gray' }
|
|
23
|
+
] as const;
|
|
24
|
+
|
|
25
|
+
const BRUSH_PRESETS = [8, 18, 36, 72] as const;
|
|
26
|
+
|
|
27
|
+
export default function ColorMattingEditor({ image, english = false, onApply, onClose }: ColorMattingEditorProps) {
|
|
28
|
+
const [color, setColor] = useState('#ffffff');
|
|
29
|
+
const [tolerance, setTolerance] = useState(25);
|
|
30
|
+
const [feather, setFeather] = useState(1.5);
|
|
31
|
+
const [contiguous, setContiguous] = useState(true);
|
|
32
|
+
const [colorMatting, setColorMatting] = useState(true);
|
|
33
|
+
const [seed, setSeed] = useState<{ x: number; y: number }>();
|
|
34
|
+
const [tool, setTool] = useState<Tool>('pick');
|
|
35
|
+
const [brushSize, setBrushSize] = useState(18);
|
|
36
|
+
const [zoom, setZoom] = useState(1);
|
|
37
|
+
const [backdrop, setBackdrop] = useState<StageBackdrop>('checker');
|
|
38
|
+
const [compareOriginal, setCompareOriginal] = useState(false);
|
|
39
|
+
const [imageSize, setImageSize] = useState<{ width: number; height: number }>({ width: 0, height: 0 });
|
|
40
|
+
const [cursor, setCursor] = useState<{ x: number; y: number }>();
|
|
41
|
+
const [strokes, setStrokes] = useState<BrushStroke[]>([]);
|
|
42
|
+
const [blob, setBlob] = useState<Blob>();
|
|
43
|
+
const [error, setError] = useState('');
|
|
44
|
+
const [busy, setBusy] = useState(false);
|
|
45
|
+
const canvas = useRef<HTMLCanvasElement>(null);
|
|
46
|
+
const source = useRef<ImageData | null>(null);
|
|
47
|
+
const working = useRef<ImageData | null>(null);
|
|
48
|
+
const dimensions = useRef({ width: 0, height: 0 });
|
|
49
|
+
const activeStroke = useRef<BrushStroke | null>(null);
|
|
50
|
+
const abort = useRef(new AbortController());
|
|
51
|
+
const text = (zh: string, en: string) => english ? en : zh;
|
|
52
|
+
const targetColor = [1, 3, 5].map(offset => parseInt(color.slice(offset, offset + 2), 16)) as [number, number, number];
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
const controller = new AbortController();
|
|
56
|
+
abort.current = controller;
|
|
57
|
+
source.current = null;
|
|
58
|
+
working.current = null;
|
|
59
|
+
setBlob(undefined);
|
|
60
|
+
setStrokes([]);
|
|
61
|
+
setSeed(undefined);
|
|
62
|
+
setCompareOriginal(false);
|
|
63
|
+
setError('');
|
|
64
|
+
setBusy(false);
|
|
65
|
+
void (async () => {
|
|
66
|
+
try {
|
|
67
|
+
const loaded = typeof image === 'string'
|
|
68
|
+
? await download(new URL(image, window.location.href).href, 50 * 1024 * 1024, controller.signal)
|
|
69
|
+
: image;
|
|
70
|
+
if (loaded.size > 50 * 1024 * 1024) throw new Error('Image exceeds size limit');
|
|
71
|
+
const bitmap = await createImageBitmap(loaded);
|
|
72
|
+
try {
|
|
73
|
+
if (controller.signal.aborted) return;
|
|
74
|
+
if (!bitmap.width || !bitmap.height || bitmap.width * bitmap.height > 40_000_000) throw new Error('Image exceeds 40 megapixels');
|
|
75
|
+
dimensions.current = { width: bitmap.width, height: bitmap.height };
|
|
76
|
+
setImageSize({ width: bitmap.width, height: bitmap.height });
|
|
77
|
+
const element = canvas.current!;
|
|
78
|
+
const scale = Math.min(1, 800 / Math.max(bitmap.width, bitmap.height));
|
|
79
|
+
element.width = Math.max(1, Math.round(bitmap.width * scale));
|
|
80
|
+
element.height = Math.max(1, Math.round(bitmap.height * scale));
|
|
81
|
+
const context = element.getContext('2d', { willReadFrequently: true });
|
|
82
|
+
if (!context) throw new Error('Canvas is unavailable');
|
|
83
|
+
context.drawImage(bitmap, 0, 0, element.width, element.height);
|
|
84
|
+
source.current = context.getImageData(0, 0, element.width, element.height);
|
|
85
|
+
setBlob(loaded);
|
|
86
|
+
} finally { bitmap.close(); }
|
|
87
|
+
} catch (reason) { if (!controller.signal.aborted) setError(String(reason)); }
|
|
88
|
+
})();
|
|
89
|
+
return () => controller.abort();
|
|
90
|
+
}, [image]);
|
|
91
|
+
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
const handleKeyDown = (event: KeyboardEvent) => {
|
|
94
|
+
if (event.key === 'Escape') { event.stopPropagation(); onClose(); }
|
|
95
|
+
};
|
|
96
|
+
window.addEventListener('keydown', handleKeyDown);
|
|
97
|
+
return () => window.removeEventListener('keydown', handleKeyDown);
|
|
98
|
+
}, [onClose]);
|
|
99
|
+
|
|
100
|
+
const previewStroke = (stroke: BrushStroke): BrushStroke => {
|
|
101
|
+
const original = dimensions.current;
|
|
102
|
+
const preview = source.current!;
|
|
103
|
+
return {
|
|
104
|
+
...stroke,
|
|
105
|
+
radius: Math.max(0.75, stroke.radius * preview.width / original.width),
|
|
106
|
+
points: stroke.points.map(point => ({ x: point.x * preview.width / original.width, y: point.y * preview.height / original.height }))
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
const original = source.current;
|
|
112
|
+
if (!blob || !original || !canvas.current) return;
|
|
113
|
+
if (compareOriginal) {
|
|
114
|
+
canvas.current.getContext('2d')?.putImageData(original, 0, 0);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const scaleX = original.width / dimensions.current.width;
|
|
118
|
+
const scaleY = original.height / dimensions.current.height;
|
|
119
|
+
const options = { targetColor, tolerance, feather: feather * scaleX, contiguous,
|
|
120
|
+
...(seed ? { startX: Math.min(original.width - 1, Math.floor(seed.x * scaleX)), startY: Math.min(original.height - 1, Math.floor(seed.y * scaleY)) } : {}) };
|
|
121
|
+
const result = colorMatting ? applyColorMatting(original, options) : new ImageData(new Uint8ClampedArray(original.data), original.width, original.height);
|
|
122
|
+
applyBrushStrokes(result, original, strokes.map(previewStroke));
|
|
123
|
+
working.current = result;
|
|
124
|
+
canvas.current.getContext('2d')?.putImageData(result, 0, 0);
|
|
125
|
+
}, [blob, color, tolerance, feather, contiguous, colorMatting, seed, strokes, compareOriginal]);
|
|
126
|
+
|
|
127
|
+
const position = (event: PointerEvent<HTMLCanvasElement>) => {
|
|
128
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
129
|
+
return {
|
|
130
|
+
x: Math.max(0, Math.min(dimensions.current.width - 1, (event.clientX - rect.left) * dimensions.current.width / rect.width)),
|
|
131
|
+
y: Math.max(0, Math.min(dimensions.current.height - 1, (event.clientY - rect.top) * dimensions.current.height / rect.height))
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const paint = (stroke: BrushStroke) => {
|
|
136
|
+
if (!source.current || !working.current || !canvas.current) return;
|
|
137
|
+
applyBrushStrokes(working.current, source.current, [previewStroke(stroke)]);
|
|
138
|
+
canvas.current.getContext('2d')?.putImageData(working.current, 0, 0);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const pointerDown = (event: PointerEvent<HTMLCanvasElement>) => {
|
|
142
|
+
if (!blob || busy || event.button !== 0 || !source.current) return;
|
|
143
|
+
if (compareOriginal) setCompareOriginal(false);
|
|
144
|
+
const point = position(event);
|
|
145
|
+
if (tool === 'pick') {
|
|
146
|
+
const x = Math.min(source.current.width - 1, Math.floor(point.x * source.current.width / dimensions.current.width));
|
|
147
|
+
const y = Math.min(source.current.height - 1, Math.floor(point.y * source.current.height / dimensions.current.height));
|
|
148
|
+
const offset = (y * source.current.width + x) * 4;
|
|
149
|
+
setColor('#' + Array.from(source.current.data.slice(offset, offset + 3), value => value.toString(16).padStart(2, '0')).join(''));
|
|
150
|
+
setSeed(point);
|
|
151
|
+
setColorMatting(true);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
155
|
+
activeStroke.current = { mode: tool, radius: brushSize, points: [point] };
|
|
156
|
+
paint(activeStroke.current);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const pointerMove = (event: PointerEvent<HTMLCanvasElement>) => {
|
|
160
|
+
if (tool !== 'pick' && blob) {
|
|
161
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
162
|
+
setCursor({ x: Math.max(0, Math.min(100, (event.clientX - rect.left) / rect.width * 100)),
|
|
163
|
+
y: Math.max(0, Math.min(100, (event.clientY - rect.top) / rect.height * 100)) });
|
|
164
|
+
}
|
|
165
|
+
const stroke = activeStroke.current;
|
|
166
|
+
if (!stroke) return;
|
|
167
|
+
const point = position(event);
|
|
168
|
+
const previous = stroke.points[stroke.points.length - 1];
|
|
169
|
+
stroke.points.push(point);
|
|
170
|
+
paint({ ...stroke, points: [previous, point] });
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const pointerUp = () => {
|
|
174
|
+
const stroke = activeStroke.current;
|
|
175
|
+
activeStroke.current = null;
|
|
176
|
+
if (stroke) setStrokes(previous => [...previous, stroke]);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const resetAllParameters = () => {
|
|
180
|
+
setColor('#ffffff');
|
|
181
|
+
setTolerance(25);
|
|
182
|
+
setFeather(1.5);
|
|
183
|
+
setContiguous(true);
|
|
184
|
+
setColorMatting(true);
|
|
185
|
+
setSeed(undefined);
|
|
186
|
+
setStrokes([]);
|
|
187
|
+
setCompareOriginal(false);
|
|
188
|
+
setZoom(1);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const apply = async () => {
|
|
192
|
+
if (!blob || busy) return;
|
|
193
|
+
const signal = abort.current.signal;
|
|
194
|
+
setBusy(true);
|
|
195
|
+
setError('');
|
|
196
|
+
try {
|
|
197
|
+
const options = { targetColor, tolerance, feather, contiguous,
|
|
198
|
+
...(seed ? { startX: Math.floor(seed.x), startY: Math.floor(seed.y) } : {}) };
|
|
199
|
+
const result = await removeColorBackground(blob, options, signal, { colorMatting, strokes });
|
|
200
|
+
signal.throwIfAborted();
|
|
201
|
+
await onApply(result, signal);
|
|
202
|
+
signal.throwIfAborted();
|
|
203
|
+
onClose();
|
|
204
|
+
} catch (reason) { if (!signal.aborted) setError(String(reason)); }
|
|
205
|
+
finally { if (!signal.aborted) setBusy(false); }
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const toolButton = (value: Tool, ariaLabel: string, shortText: string, Icon: typeof Pipette) => (
|
|
209
|
+
<button
|
|
210
|
+
type="button"
|
|
211
|
+
className="sak-matting-tool-btn"
|
|
212
|
+
title={ariaLabel}
|
|
213
|
+
aria-label={ariaLabel}
|
|
214
|
+
aria-pressed={tool === value}
|
|
215
|
+
onClick={() => {
|
|
216
|
+
setTool(value);
|
|
217
|
+
setCompareOriginal(false);
|
|
218
|
+
}}
|
|
219
|
+
disabled={!blob || busy}
|
|
220
|
+
>
|
|
221
|
+
<Icon size={15} />
|
|
222
|
+
<span>{shortText}</span>
|
|
223
|
+
</button>
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<div className="sak-matting-overlay" role="dialog" aria-modal="true" aria-label={text('颜色抠图', 'Color matting')}>
|
|
228
|
+
<div className="sak-matting">
|
|
229
|
+
{/* 顶部标题栏 */}
|
|
230
|
+
<header className="sak-matting-header">
|
|
231
|
+
<div className="sak-matting-header-left">
|
|
232
|
+
<div className="sak-matting-brand-icon" aria-hidden="true">
|
|
233
|
+
<Scissors size={17} />
|
|
234
|
+
</div>
|
|
235
|
+
<div className="sak-matting-title-wrap">
|
|
236
|
+
<div className="sak-matting-title-row">
|
|
237
|
+
<h2>{text('颜色抠图', 'Color matting')}</h2>
|
|
238
|
+
<span className="sak-matting-badge">
|
|
239
|
+
{text('色彩容差 + 画笔精修', 'Color Key + Brush Refine')}
|
|
240
|
+
</span>
|
|
241
|
+
{imageSize.width > 0 && (
|
|
242
|
+
<span className="sak-matting-meta-pill">
|
|
243
|
+
{imageSize.width} × {imageSize.height} px
|
|
244
|
+
</span>
|
|
245
|
+
)}
|
|
246
|
+
</div>
|
|
247
|
+
<p className="sak-matting-subtitle">
|
|
248
|
+
{text(
|
|
249
|
+
'吸取背景主色自动抠除,并支持局部擦除或恢复原图像素细节',
|
|
250
|
+
'Sample background color for automatic removal and refine edges with erase/restore brushes'
|
|
251
|
+
)}
|
|
252
|
+
</p>
|
|
253
|
+
</div>
|
|
254
|
+
</div>
|
|
255
|
+
|
|
256
|
+
<div className="sak-matting-header-actions">
|
|
257
|
+
<button
|
|
258
|
+
type="button"
|
|
259
|
+
className="sak-matting-ghost-btn"
|
|
260
|
+
disabled={!blob || busy}
|
|
261
|
+
onClick={resetAllParameters}
|
|
262
|
+
title={text('重置全部参数与笔迹', 'Reset all parameters and strokes')}
|
|
263
|
+
>
|
|
264
|
+
<RotateCcw size={14} />
|
|
265
|
+
<span>{text('重置全部', 'Reset all')}</span>
|
|
266
|
+
</button>
|
|
267
|
+
<button
|
|
268
|
+
type="button"
|
|
269
|
+
className="sak-matting-close-btn"
|
|
270
|
+
onClick={onClose}
|
|
271
|
+
aria-label={text('关闭', 'Close')}
|
|
272
|
+
title={text('关闭 (Esc)', 'Close (Esc)')}
|
|
273
|
+
>
|
|
274
|
+
<X size={18} />
|
|
275
|
+
</button>
|
|
276
|
+
</div>
|
|
277
|
+
</header>
|
|
278
|
+
|
|
279
|
+
{/* 主体区域:左侧画布工作台 + 右侧参数控制栏 */}
|
|
280
|
+
<div className="sak-matting-body">
|
|
281
|
+
<div className="sak-matting-workspace">
|
|
282
|
+
{/* 画布顶部工具栏 */}
|
|
283
|
+
<div className="sak-matting-toolbar">
|
|
284
|
+
<div className="sak-matting-tools" role="group" aria-label={text('工具', 'Tools')}>
|
|
285
|
+
{toolButton('pick', text('取色', 'Pick color'), text('吸管取色', 'Pick'), Pipette)}
|
|
286
|
+
{toolButton('erase', text('擦除', 'Erase'), text('擦除像素', 'Erase'), Eraser)}
|
|
287
|
+
{toolButton('restore', text('恢复原图', 'Restore original'), text('恢复原图', 'Restore'), Paintbrush)}
|
|
288
|
+
</div>
|
|
289
|
+
|
|
290
|
+
<div className="sak-matting-divider" aria-hidden="true" />
|
|
291
|
+
|
|
292
|
+
<div className="sak-matting-history">
|
|
293
|
+
<button
|
|
294
|
+
type="button"
|
|
295
|
+
className="sak-matting-icon-btn"
|
|
296
|
+
title={text('撤销笔迹', 'Undo stroke')}
|
|
297
|
+
aria-label={text('撤销笔迹', 'Undo stroke')}
|
|
298
|
+
disabled={!strokes.length || busy}
|
|
299
|
+
onClick={() => setStrokes(previous => previous.slice(0, -1))}
|
|
300
|
+
>
|
|
301
|
+
<Undo2 size={15} />
|
|
302
|
+
</button>
|
|
303
|
+
<button
|
|
304
|
+
type="button"
|
|
305
|
+
className="sak-matting-icon-btn"
|
|
306
|
+
title={text('清除笔迹', 'Clear strokes')}
|
|
307
|
+
aria-label={text('清除笔迹', 'Clear strokes')}
|
|
308
|
+
disabled={!strokes.length || busy}
|
|
309
|
+
onClick={() => setStrokes([])}
|
|
310
|
+
>
|
|
311
|
+
<RotateCcw size={15} />
|
|
312
|
+
</button>
|
|
313
|
+
{strokes.length > 0 && (
|
|
314
|
+
<span className="sak-matting-stroke-count" title={text('已绘制笔迹数量', 'Brush stroke count')}>
|
|
315
|
+
{strokes.length} {text('笔', 'strokes')}
|
|
316
|
+
</span>
|
|
317
|
+
)}
|
|
318
|
+
</div>
|
|
319
|
+
|
|
320
|
+
<div className="sak-matting-divider" aria-hidden="true" />
|
|
321
|
+
|
|
322
|
+
{/* 背景底纹切换与原图对比 */}
|
|
323
|
+
<div className="sak-matting-backdrop-group" role="group" aria-label={text('预览底色', 'Preview backdrop')}>
|
|
324
|
+
<button
|
|
325
|
+
type="button"
|
|
326
|
+
className="sak-matting-icon-btn"
|
|
327
|
+
aria-pressed={backdrop === 'checker'}
|
|
328
|
+
title={text('透明棋盘格底纹', 'Checkerboard backdrop')}
|
|
329
|
+
onClick={() => setBackdrop('checker')}
|
|
330
|
+
>
|
|
331
|
+
<Grid size={14} />
|
|
332
|
+
</button>
|
|
333
|
+
<button
|
|
334
|
+
type="button"
|
|
335
|
+
className="sak-matting-icon-btn"
|
|
336
|
+
aria-pressed={backdrop === 'dark'}
|
|
337
|
+
title={text('深色高对比背景', 'Dark backdrop')}
|
|
338
|
+
onClick={() => setBackdrop('dark')}
|
|
339
|
+
>
|
|
340
|
+
<Moon size={14} />
|
|
341
|
+
</button>
|
|
342
|
+
<button
|
|
343
|
+
type="button"
|
|
344
|
+
className="sak-matting-icon-btn"
|
|
345
|
+
aria-pressed={backdrop === 'light'}
|
|
346
|
+
title={text('浅色纯白背景', 'Light backdrop')}
|
|
347
|
+
onClick={() => setBackdrop('light')}
|
|
348
|
+
>
|
|
349
|
+
<Sun size={14} />
|
|
350
|
+
</button>
|
|
351
|
+
<button
|
|
352
|
+
type="button"
|
|
353
|
+
className="sak-matting-compare-btn"
|
|
354
|
+
aria-pressed={compareOriginal}
|
|
355
|
+
disabled={!blob || busy}
|
|
356
|
+
title={text('切换查看原图与抠图效果对比', 'Toggle original vs matted preview')}
|
|
357
|
+
onClick={() => setCompareOriginal(prev => !prev)}
|
|
358
|
+
>
|
|
359
|
+
<Eye size={14} />
|
|
360
|
+
<span>{compareOriginal ? text('正在看原图', 'Original') : text('对比原图', 'Compare')}</span>
|
|
361
|
+
</button>
|
|
362
|
+
</div>
|
|
363
|
+
|
|
364
|
+
{/* 缩放控制 */}
|
|
365
|
+
<div className="sak-matting-zoom">
|
|
366
|
+
<button
|
|
367
|
+
type="button"
|
|
368
|
+
className="sak-matting-icon-btn"
|
|
369
|
+
aria-label={text('缩小', 'Zoom out')}
|
|
370
|
+
title={text('缩小', 'Zoom out')}
|
|
371
|
+
onClick={() => setZoom(value => Math.max(0.5, value - 0.25))}
|
|
372
|
+
>
|
|
373
|
+
<ZoomOut size={15} />
|
|
374
|
+
</button>
|
|
375
|
+
<span
|
|
376
|
+
className="sak-matting-zoom-value"
|
|
377
|
+
onClick={() => setZoom(1)}
|
|
378
|
+
title={text('点击恢复 100% 缩放', 'Click to reset to 100%')}
|
|
379
|
+
>
|
|
380
|
+
{Math.round(zoom * 100)}%
|
|
381
|
+
</span>
|
|
382
|
+
<button
|
|
383
|
+
type="button"
|
|
384
|
+
className="sak-matting-icon-btn"
|
|
385
|
+
aria-label={text('放大', 'Zoom in')}
|
|
386
|
+
title={text('放大', 'Zoom in')}
|
|
387
|
+
onClick={() => setZoom(value => Math.min(4, value + 0.25))}
|
|
388
|
+
>
|
|
389
|
+
<ZoomIn size={15} />
|
|
390
|
+
</button>
|
|
391
|
+
</div>
|
|
392
|
+
</div>
|
|
393
|
+
|
|
394
|
+
{/* 画布预览舞台 */}
|
|
395
|
+
<div className={`sak-matting-stage is-backdrop-${backdrop}`}>
|
|
396
|
+
<div
|
|
397
|
+
className="sak-matting-canvas"
|
|
398
|
+
style={{ width: source.current ? `min(${zoom * 100}%, ${zoom * 60 * source.current.width / source.current.height}vh)` : undefined }}
|
|
399
|
+
>
|
|
400
|
+
<canvas
|
|
401
|
+
ref={canvas}
|
|
402
|
+
aria-label={text('图片预览', 'Image preview')}
|
|
403
|
+
onPointerDown={pointerDown}
|
|
404
|
+
onPointerMove={pointerMove}
|
|
405
|
+
onPointerUp={pointerUp}
|
|
406
|
+
onPointerCancel={pointerUp}
|
|
407
|
+
onPointerLeave={() => setCursor(undefined)}
|
|
408
|
+
/>
|
|
409
|
+
{cursor && tool !== 'pick' && (
|
|
410
|
+
<span
|
|
411
|
+
className={`sak-matting-cursor is-${tool}`}
|
|
412
|
+
style={{
|
|
413
|
+
left: `${cursor.x}%`,
|
|
414
|
+
top: `${cursor.y}%`,
|
|
415
|
+
width: `${brushSize * 2 / dimensions.current.width * 100}%`
|
|
416
|
+
}}
|
|
417
|
+
/>
|
|
418
|
+
)}
|
|
419
|
+
</div>
|
|
420
|
+
</div>
|
|
421
|
+
|
|
422
|
+
{/* 底部状态提示栏 */}
|
|
423
|
+
<div className="sak-matting-stage-footer">
|
|
424
|
+
<div className="sak-matting-hint">
|
|
425
|
+
{tool === 'pick' ? (
|
|
426
|
+
<>
|
|
427
|
+
<Pipette size={13} />
|
|
428
|
+
<span>{text('点击画布任意背景区域即可吸取颜色并设为连通起点', 'Click any background pixel on canvas to pick color and set seed')}</span>
|
|
429
|
+
</>
|
|
430
|
+
) : tool === 'erase' ? (
|
|
431
|
+
<>
|
|
432
|
+
<Eraser size={13} />
|
|
433
|
+
<span>{text(`拖拽涂抹以擦除残留背景像素(当前笔刷半径 ${brushSize} px)`, `Drag on canvas to erase pixels (brush radius ${brushSize} px)`)}</span>
|
|
434
|
+
</>
|
|
435
|
+
) : (
|
|
436
|
+
<>
|
|
437
|
+
<Paintbrush size={13} />
|
|
438
|
+
<span>{text(`拖拽涂抹以恢复被误删的原图主体细节(当前笔刷半径 ${brushSize} px)`, `Drag on canvas to restore original pixels (brush radius ${brushSize} px)`)}</span>
|
|
439
|
+
</>
|
|
440
|
+
)}
|
|
441
|
+
</div>
|
|
442
|
+
|
|
443
|
+
<div className="sak-matting-seed-status">
|
|
444
|
+
<span className="sak-matting-color-chip">
|
|
445
|
+
<i style={{ backgroundColor: color }} />
|
|
446
|
+
<code>{color.toUpperCase()}</code>
|
|
447
|
+
</span>
|
|
448
|
+
<span>
|
|
449
|
+
{!colorMatting
|
|
450
|
+
? text('仅画笔模式', 'Brush only')
|
|
451
|
+
: !contiguous
|
|
452
|
+
? text('全局色彩匹配', 'Global match')
|
|
453
|
+
: seed
|
|
454
|
+
? text(`起点 (${Math.round(seed.x)}, ${Math.round(seed.y)})`, `Seed (${Math.round(seed.x)}, ${Math.round(seed.y)})`)
|
|
455
|
+
: text('四周边缘连通', 'Edge flood-fill')}
|
|
456
|
+
</span>
|
|
457
|
+
</div>
|
|
458
|
+
</div>
|
|
459
|
+
</div>
|
|
460
|
+
|
|
461
|
+
{/* 右侧参数控制栏 */}
|
|
462
|
+
<div className="sak-matting-controls">
|
|
463
|
+
{/* 卡片 1:颜色抠图参数 */}
|
|
464
|
+
<div className={`sak-matting-card ${!colorMatting ? 'is-muted' : ''}`}>
|
|
465
|
+
<div className="sak-matting-card-header">
|
|
466
|
+
<label className="sak-matting-check sak-matting-primary-toggle">
|
|
467
|
+
<input
|
|
468
|
+
type="checkbox"
|
|
469
|
+
checked={colorMatting}
|
|
470
|
+
onChange={event => setColorMatting(event.target.checked)}
|
|
471
|
+
/>
|
|
472
|
+
<Sparkles size={14} className="sak-matting-section-icon" />
|
|
473
|
+
<strong>{text('颜色抠图', 'Color matting')}</strong>
|
|
474
|
+
</label>
|
|
475
|
+
</div>
|
|
476
|
+
|
|
477
|
+
<div className="sak-matting-card-body">
|
|
478
|
+
<label className="sak-matting-color">
|
|
479
|
+
<span className="sak-matting-field-label">{text('背景颜色', 'Background color')}</span>
|
|
480
|
+
<div className="sak-matting-color-picker-wrap">
|
|
481
|
+
<code className="sak-matting-hex">{color.toUpperCase()}</code>
|
|
482
|
+
<input
|
|
483
|
+
type="color"
|
|
484
|
+
value={color}
|
|
485
|
+
disabled={!colorMatting}
|
|
486
|
+
onChange={event => {
|
|
487
|
+
setColor(event.target.value);
|
|
488
|
+
setSeed(undefined);
|
|
489
|
+
}}
|
|
490
|
+
/>
|
|
491
|
+
</div>
|
|
492
|
+
</label>
|
|
493
|
+
|
|
494
|
+
{/* 快捷背景色预设 */}
|
|
495
|
+
<div className="sak-matting-swatches" aria-label={text('常用背景色预设', 'Preset background colors')}>
|
|
496
|
+
{PRESET_COLORS.map(preset => (
|
|
497
|
+
<button
|
|
498
|
+
key={preset.hex}
|
|
499
|
+
type="button"
|
|
500
|
+
disabled={!colorMatting}
|
|
501
|
+
className={`sak-matting-swatch-btn ${color.toLowerCase() === preset.hex ? 'is-active' : ''}`}
|
|
502
|
+
title={text(`预设:${preset.zh} (${preset.hex})`, `Preset: ${preset.en} (${preset.hex})`)}
|
|
503
|
+
onClick={() => {
|
|
504
|
+
setColor(preset.hex);
|
|
505
|
+
setSeed(undefined);
|
|
506
|
+
}}
|
|
507
|
+
>
|
|
508
|
+
<i style={{ backgroundColor: preset.hex }} />
|
|
509
|
+
<span>{text(preset.zh, preset.en)}</span>
|
|
510
|
+
</button>
|
|
511
|
+
))}
|
|
512
|
+
</div>
|
|
513
|
+
|
|
514
|
+
<label className="sak-matting-slider-field">
|
|
515
|
+
<div className="sak-matting-slider-header">
|
|
516
|
+
<span className="sak-matting-field-label">{text('容差', 'Tolerance')}</span>
|
|
517
|
+
<span className="sak-matting-value-badge">{tolerance}</span>
|
|
518
|
+
</div>
|
|
519
|
+
<input
|
|
520
|
+
type="range"
|
|
521
|
+
min="0"
|
|
522
|
+
max="100"
|
|
523
|
+
value={tolerance}
|
|
524
|
+
disabled={!colorMatting}
|
|
525
|
+
onChange={event => setTolerance(Number(event.target.value))}
|
|
526
|
+
/>
|
|
527
|
+
<div className="sak-matting-slider-scale">
|
|
528
|
+
<small>{text('精准 (0)', 'Strict (0)')}</small>
|
|
529
|
+
<small>{text('宽泛 (100)', 'Wide (100)')}</small>
|
|
530
|
+
</div>
|
|
531
|
+
</label>
|
|
532
|
+
|
|
533
|
+
<label className="sak-matting-slider-field">
|
|
534
|
+
<div className="sak-matting-slider-header">
|
|
535
|
+
<span className="sak-matting-field-label">{text('羽化', 'Feather')}</span>
|
|
536
|
+
<span className="sak-matting-value-badge">{feather}</span>
|
|
537
|
+
</div>
|
|
538
|
+
<input
|
|
539
|
+
type="range"
|
|
540
|
+
min="0"
|
|
541
|
+
max="5"
|
|
542
|
+
step="0.5"
|
|
543
|
+
value={feather}
|
|
544
|
+
disabled={!colorMatting}
|
|
545
|
+
onChange={event => setFeather(Number(event.target.value))}
|
|
546
|
+
/>
|
|
547
|
+
<div className="sak-matting-slider-scale">
|
|
548
|
+
<small>{text('硬边缘 (0)', 'Sharp (0)')}</small>
|
|
549
|
+
<small>{text('柔和过渡 (5)', 'Soft (5)')}</small>
|
|
550
|
+
</div>
|
|
551
|
+
</label>
|
|
552
|
+
|
|
553
|
+
<label className="sak-matting-check sak-matting-subcheck">
|
|
554
|
+
<input
|
|
555
|
+
type="checkbox"
|
|
556
|
+
checked={contiguous}
|
|
557
|
+
disabled={!colorMatting}
|
|
558
|
+
onChange={event => setContiguous(event.target.checked)}
|
|
559
|
+
/>
|
|
560
|
+
<span>{text('仅连通区域', 'Contiguous only')}</span>
|
|
561
|
+
</label>
|
|
562
|
+
|
|
563
|
+
{seed && (
|
|
564
|
+
<div className="sak-matting-seed-box">
|
|
565
|
+
<small>
|
|
566
|
+
{text(
|
|
567
|
+
`已锁定取色起点 (${Math.round(seed.x)}, ${Math.round(seed.y)})`,
|
|
568
|
+
`Seed locked at (${Math.round(seed.x)}, ${Math.round(seed.y)})`
|
|
569
|
+
)}
|
|
570
|
+
</small>
|
|
571
|
+
<button type="button" className="sak-matting-link" onClick={() => setSeed(undefined)}>
|
|
572
|
+
{text('使用边缘区域', 'Use edges')}
|
|
573
|
+
</button>
|
|
574
|
+
</div>
|
|
575
|
+
)}
|
|
576
|
+
</div>
|
|
577
|
+
</div>
|
|
578
|
+
|
|
579
|
+
{/* 卡片 2:画笔精修参数 */}
|
|
580
|
+
<div className="sak-matting-card">
|
|
581
|
+
<div className="sak-matting-card-header">
|
|
582
|
+
<div className="sak-matting-card-title">
|
|
583
|
+
<Sliders size={14} className="sak-matting-section-icon" />
|
|
584
|
+
<strong>{text('画笔手工精修', 'Brush Refinement')}</strong>
|
|
585
|
+
</div>
|
|
586
|
+
</div>
|
|
587
|
+
|
|
588
|
+
<div className="sak-matting-card-body">
|
|
589
|
+
<label className="sak-matting-slider-field">
|
|
590
|
+
<div className="sak-matting-slider-header">
|
|
591
|
+
<span className="sak-matting-field-label">{text('笔刷半径', 'Brush radius')}</span>
|
|
592
|
+
<span className="sak-matting-value-badge">{brushSize} px</span>
|
|
593
|
+
</div>
|
|
594
|
+
<input
|
|
595
|
+
type="range"
|
|
596
|
+
min="2"
|
|
597
|
+
max="256"
|
|
598
|
+
value={brushSize}
|
|
599
|
+
disabled={tool === 'pick'}
|
|
600
|
+
onChange={event => setBrushSize(Number(event.target.value))}
|
|
601
|
+
/>
|
|
602
|
+
</label>
|
|
603
|
+
|
|
604
|
+
<div className="sak-matting-brush-presets">
|
|
605
|
+
{BRUSH_PRESETS.map(size => (
|
|
606
|
+
<button
|
|
607
|
+
key={size}
|
|
608
|
+
type="button"
|
|
609
|
+
disabled={!blob || busy}
|
|
610
|
+
className={`sak-matting-preset-pill ${brushSize === size && tool !== 'pick' ? 'is-active' : ''}`}
|
|
611
|
+
onClick={() => {
|
|
612
|
+
setBrushSize(size);
|
|
613
|
+
if (tool === 'pick') setTool('erase');
|
|
614
|
+
}}
|
|
615
|
+
>
|
|
616
|
+
{size}px
|
|
617
|
+
</button>
|
|
618
|
+
))}
|
|
619
|
+
</div>
|
|
620
|
+
</div>
|
|
621
|
+
</div>
|
|
622
|
+
|
|
623
|
+
{error && <p role="alert" className="sak-matting-error">{error}</p>}
|
|
624
|
+
|
|
625
|
+
{/* 底部主操作区 */}
|
|
626
|
+
<div className="sak-matting-footer-actions">
|
|
627
|
+
<button
|
|
628
|
+
type="button"
|
|
629
|
+
className="sak-matting-cancel"
|
|
630
|
+
disabled={busy}
|
|
631
|
+
onClick={onClose}
|
|
632
|
+
>
|
|
633
|
+
{text('取消', 'Cancel')}
|
|
634
|
+
</button>
|
|
635
|
+
<button
|
|
636
|
+
type="button"
|
|
637
|
+
disabled={!blob || busy}
|
|
638
|
+
onClick={() => void apply()}
|
|
639
|
+
className="sak-matting-apply"
|
|
640
|
+
>
|
|
641
|
+
{busy ? text('处理中...', 'Processing...') : text('应用', 'Apply')}
|
|
642
|
+
</button>
|
|
643
|
+
</div>
|
|
644
|
+
</div>
|
|
645
|
+
</div>
|
|
646
|
+
</div>
|
|
647
|
+
</div>
|
|
648
|
+
);
|
|
649
|
+
}
|