Line data Source code
1 : import { ref, watch } from 'vue'
2 : import { useMaskEditorStore } from './maskEditorStore'
3 : import { ColorComparisonMethod } from './types'
4 : import type { Point2D as Point } from '@comfyorg/shared-frontend-utils/geometry'
5 : import { rgbToHsl } from '@comfyorg/shared-frontend-utils/colorUtil'
6 :
7 30 : const getPixelAlpha = (
8 : data: Uint8ClampedArray,
9 : x: number,
10 : y: number,
11 : width: number
12 : ): number => {
13 0 : return data[(y * width + x) * 4 + 3]
14 : }
15 :
16 30 : const getPixelColor = (
17 : data: Uint8ClampedArray,
18 : x: number,
19 : y: number,
20 : width: number
21 : ): { r: number; g: number; b: number } => {
22 0 : const index = (y * width + x) * 4
23 0 : return {
24 : r: data[index],
25 : g: data[index + 1],
26 : b: data[index + 2]
27 : }
28 : }
29 :
30 30 : const setPixel = (
31 : data: Uint8ClampedArray,
32 : x: number,
33 : y: number,
34 : width: number,
35 : alpha: number,
36 : color: { r: number; g: number; b: number }
37 : ): void => {
38 0 : const index = (y * width + x) * 4
39 0 : data[index] = color.r
40 0 : data[index + 1] = color.g
41 0 : data[index + 2] = color.b
42 0 : data[index + 3] = alpha
43 : }
44 :
45 : // Color comparison utilities
46 30 : const rgbToHSL = (
47 : r: number,
48 : g: number,
49 : b: number
50 : ): { h: number; s: number; l: number } => {
51 0 : const hsl = rgbToHsl({ r, g, b })
52 0 : return { h: hsl.h * 360, s: hsl.s * 100, l: hsl.l * 100 }
53 : }
54 :
55 30 : const rgbToLab = (rgb: {
56 : r: number
57 : g: number
58 : b: number
59 : }): {
60 : l: number
61 : a: number
62 : b: number
63 : } => {
64 0 : let r = rgb.r / 255
65 0 : let g = rgb.g / 255
66 0 : let b = rgb.b / 255
67 :
68 0 : r = r > 0.04045 ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92
69 0 : g = g > 0.04045 ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92
70 0 : b = b > 0.04045 ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92
71 :
72 0 : r *= 100
73 0 : g *= 100
74 0 : b *= 100
75 :
76 0 : const x = r * 0.4124 + g * 0.3576 + b * 0.1805
77 0 : const y = r * 0.2126 + g * 0.7152 + b * 0.0722
78 0 : const z = r * 0.0193 + g * 0.1192 + b * 0.9505
79 :
80 : const xn = 95.047
81 : const yn = 100.0
82 : const zn = 108.883
83 :
84 0 : const xyz = [x / xn, y / yn, z / zn]
85 0 : for (let i = 0; i < xyz.length; i++) {
86 0 : xyz[i] =
87 : xyz[i] > 0.008856 ? Math.pow(xyz[i], 1 / 3) : 7.787 * xyz[i] + 16 / 116
88 : }
89 :
90 0 : return {
91 : l: 116 * xyz[1] - 16,
92 : a: 500 * (xyz[0] - xyz[1]),
93 : b: 200 * (xyz[1] - xyz[2])
94 : }
95 : }
96 :
97 30 : const isPixelInRangeSimple = (
98 : pixel: { r: number; g: number; b: number },
99 : target: { r: number; g: number; b: number },
100 : tolerance: number
101 : ): boolean => {
102 : const distance = Math.sqrt(
103 : Math.pow(pixel.r - target.r, 2) +
104 : Math.pow(pixel.g - target.g, 2) +
105 : Math.pow(pixel.b - target.b, 2)
106 : )
107 0 : return distance <= tolerance
108 : }
109 :
110 30 : const isPixelInRangeHSL = (
111 : pixel: { r: number; g: number; b: number },
112 : target: { r: number; g: number; b: number },
113 : tolerance: number
114 : ): boolean => {
115 0 : const pixelHSL = rgbToHSL(pixel.r, pixel.g, pixel.b)
116 0 : const targetHSL = rgbToHSL(target.r, target.g, target.b)
117 :
118 0 : const hueDiff = Math.abs(pixelHSL.h - targetHSL.h)
119 0 : const satDiff = Math.abs(pixelHSL.s - targetHSL.s)
120 0 : const lightDiff = Math.abs(pixelHSL.l - targetHSL.l)
121 :
122 : const distance = Math.sqrt(
123 : Math.pow((hueDiff / 360) * 255, 2) +
124 : Math.pow((satDiff / 100) * 255, 2) +
125 : Math.pow((lightDiff / 100) * 255, 2)
126 : )
127 0 : return distance <= tolerance
128 : }
129 :
130 30 : const isPixelInRangeLab = (
131 : pixel: { r: number; g: number; b: number },
132 : target: { r: number; g: number; b: number },
133 : tolerance: number
134 : ): boolean => {
135 0 : const pixelLab = rgbToLab(pixel)
136 0 : const targetLab = rgbToLab(target)
137 :
138 : const deltaE = Math.sqrt(
139 : Math.pow(pixelLab.l - targetLab.l, 2) +
140 : Math.pow(pixelLab.a - targetLab.a, 2) +
141 : Math.pow(pixelLab.b - targetLab.b, 2)
142 : )
143 :
144 : const normalizedDeltaE = (deltaE / 100) * 255
145 0 : return normalizedDeltaE <= tolerance
146 : }
147 :
148 30 : const isPixelInRange = (
149 : pixel: { r: number; g: number; b: number },
150 : target: { r: number; g: number; b: number },
151 : tolerance: number,
152 : method: ColorComparisonMethod
153 : ): boolean => {
154 0 : switch (method) {
155 : case ColorComparisonMethod.Simple:
156 0 : return isPixelInRangeSimple(pixel, target, tolerance)
157 : case ColorComparisonMethod.HSL:
158 0 : return isPixelInRangeHSL(pixel, target, tolerance)
159 : case ColorComparisonMethod.LAB:
160 0 : return isPixelInRangeLab(pixel, target, tolerance)
161 : default:
162 0 : return isPixelInRangeSimple(pixel, target, tolerance)
163 : }
164 : }
165 :
166 60 : export function useCanvasTools() {
167 60 : const store = useMaskEditorStore()
168 60 : const lastColorSelectPoint = ref<Point | null>(null)
169 :
170 60 : const paintBucketFill = (point: Point): void => {
171 0 : const ctx = store.maskCtx
172 0 : const canvas = store.maskCanvas
173 0 : if (!ctx || !canvas) return
174 :
175 0 : const startX = Math.floor(point.x)
176 0 : const startY = Math.floor(point.y)
177 0 : const width = canvas.width
178 0 : const height = canvas.height
179 :
180 0 : if (startX < 0 || startX >= width || startY < 0 || startY >= height) {
181 0 : return
182 : }
183 :
184 0 : const imageData = ctx.getImageData(0, 0, width, height)
185 0 : const data = imageData.data
186 :
187 0 : const targetAlpha = getPixelAlpha(data, startX, startY, width)
188 0 : const isFillMode = targetAlpha !== 255
189 :
190 0 : if (targetAlpha === -1) return
191 :
192 0 : const maskColor = store.maskColor
193 0 : const tolerance = store.paintBucketTolerance
194 0 : const fillOpacity = Math.floor((store.fillOpacity / 100) * 255)
195 :
196 0 : const stack: Array<[number, number]> = []
197 0 : const visited = new Uint8Array(width * height)
198 :
199 0 : const shouldProcessPixel = (
200 : currentAlpha: number,
201 : targetAlpha: number,
202 : tolerance: number,
203 : isFillMode: boolean
204 : ): boolean => {
205 0 : if (currentAlpha === -1) return false
206 :
207 0 : if (isFillMode) {
208 0 : return (
209 : currentAlpha !== 255 &&
210 : Math.abs(currentAlpha - targetAlpha) <= tolerance
211 : )
212 : } else {
213 0 : return (
214 : currentAlpha === 255 ||
215 : Math.abs(currentAlpha - targetAlpha) <= tolerance
216 : )
217 : }
218 : }
219 :
220 0 : if (shouldProcessPixel(targetAlpha, targetAlpha, tolerance, isFillMode)) {
221 0 : stack.push([startX, startY])
222 : }
223 :
224 0 : while (stack.length > 0) {
225 0 : const [x, y] = stack.pop()!
226 0 : const visitedIndex = y * width + x
227 :
228 0 : if (visited[visitedIndex]) continue
229 :
230 : const currentAlpha = getPixelAlpha(data, x, y, width)
231 0 : if (
232 : !shouldProcessPixel(currentAlpha, targetAlpha, tolerance, isFillMode)
233 : ) {
234 0 : continue
235 : }
236 :
237 0 : visited[visitedIndex] = 1
238 0 : setPixel(data, x, y, width, isFillMode ? fillOpacity : 0, maskColor)
239 :
240 0 : const checkNeighbor = (nx: number, ny: number) => {
241 0 : if (nx < 0 || nx >= width || ny < 0 || ny >= height) return
242 0 : if (!visited[ny * width + nx]) {
243 0 : const alpha = getPixelAlpha(data, nx, ny, width)
244 0 : if (shouldProcessPixel(alpha, targetAlpha, tolerance, isFillMode)) {
245 0 : stack.push([nx, ny])
246 : }
247 : }
248 : }
249 :
250 0 : checkNeighbor(x - 1, y)
251 0 : checkNeighbor(x + 1, y)
252 0 : checkNeighbor(x, y - 1)
253 0 : checkNeighbor(x, y + 1)
254 : }
255 :
256 0 : ctx.putImageData(imageData, 0, 0)
257 0 : store.canvasHistory.saveState()
258 : }
259 :
260 60 : const colorSelectFill = async (point: Point): Promise<void> => {
261 0 : const maskCtx = store.maskCtx
262 0 : const imgCtx = store.imgCtx
263 0 : const imgCanvas = store.imgCanvas
264 :
265 0 : if (!maskCtx || !imgCtx || !imgCanvas) return
266 :
267 0 : const width = imgCanvas.width
268 0 : const height = imgCanvas.height
269 0 : lastColorSelectPoint.value = point
270 :
271 0 : const maskData = maskCtx.getImageData(0, 0, width, height)
272 0 : const maskDataArray = maskData.data
273 0 : const imageDataArray = imgCtx.getImageData(0, 0, width, height).data
274 :
275 0 : const tolerance = store.colorSelectTolerance
276 0 : const method = store.colorComparisonMethod
277 0 : const maskColor = store.maskColor
278 0 : const selectOpacity = Math.floor((store.selectionOpacity / 100) * 255)
279 0 : const applyWholeImage = store.applyWholeImage
280 0 : const maskBoundary = store.maskBoundary
281 0 : const maskTolerance = store.maskTolerance
282 :
283 0 : if (applyWholeImage) {
284 0 : const targetPixel = getPixelColor(
285 : imageDataArray,
286 : Math.floor(point.x),
287 : Math.floor(point.y),
288 : width
289 : )
290 :
291 0 : const CHUNK_SIZE = 10000
292 0 : for (let i = 0; i < width * height; i += CHUNK_SIZE) {
293 0 : const endIndex = Math.min(i + CHUNK_SIZE, width * height)
294 0 : for (let pixelIndex = i; pixelIndex < endIndex; pixelIndex++) {
295 0 : const x = pixelIndex % width
296 0 : const y = Math.floor(pixelIndex / width)
297 0 : if (
298 : isPixelInRange(
299 : getPixelColor(imageDataArray, x, y, width),
300 : targetPixel,
301 : tolerance,
302 : method
303 : )
304 : ) {
305 0 : setPixel(maskDataArray, x, y, width, selectOpacity, maskColor)
306 : }
307 : }
308 0 : await new Promise((resolve) => setTimeout(resolve, 0))
309 : }
310 : } else {
311 0 : const startX = Math.floor(point.x)
312 0 : const startY = Math.floor(point.y)
313 :
314 0 : if (startX < 0 || startX >= width || startY < 0 || startY >= height) {
315 0 : return
316 : }
317 :
318 0 : const targetPixel = getPixelColor(imageDataArray, startX, startY, width)
319 0 : const stack: Array<[number, number]> = []
320 0 : const visited = new Uint8Array(width * height)
321 :
322 0 : stack.push([startX, startY])
323 :
324 0 : while (stack.length > 0) {
325 0 : const [x, y] = stack.pop()!
326 0 : const visitedIndex = y * width + x
327 :
328 0 : if (
329 : visited[visitedIndex] ||
330 : !isPixelInRange(
331 : getPixelColor(imageDataArray, x, y, width),
332 : targetPixel,
333 : tolerance,
334 : method
335 : )
336 : ) {
337 0 : continue
338 : }
339 :
340 0 : visited[visitedIndex] = 1
341 0 : setPixel(maskDataArray, x, y, width, selectOpacity, maskColor)
342 :
343 0 : const checkNeighbor = (nx: number, ny: number) => {
344 0 : if (nx < 0 || nx >= width || ny < 0 || ny >= height) return
345 0 : if (visited[ny * width + nx]) return
346 0 : if (
347 : !isPixelInRange(
348 : getPixelColor(imageDataArray, nx, ny, width),
349 : targetPixel,
350 : tolerance,
351 : method
352 : )
353 : )
354 0 : return
355 0 : if (
356 : maskBoundary &&
357 : 255 - getPixelAlpha(maskDataArray, nx, ny, width) <= maskTolerance
358 : )
359 0 : return
360 :
361 0 : stack.push([nx, ny])
362 : }
363 :
364 0 : checkNeighbor(x - 1, y)
365 0 : checkNeighbor(x + 1, y)
366 0 : checkNeighbor(x, y - 1)
367 0 : checkNeighbor(x, y + 1)
368 : }
369 : }
370 :
371 0 : maskCtx.putImageData(maskData, 0, 0)
372 0 : store.canvasHistory.saveState()
373 : }
374 :
375 60 : const invertMask = (): void => {
376 1 : const ctx = store.maskCtx
377 1 : const canvas = store.maskCanvas
378 1 : if (!ctx || !canvas) return
379 :
380 1 : const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
381 1 : const data = imageData.data
382 :
383 1 : let maskR = 0,
384 1 : maskG = 0,
385 1 : maskB = 0
386 1 : for (let i = 0; i < data.length; i += 4) {
387 4096 : if (data[i + 3] > 0) {
388 0 : maskR = data[i]
389 0 : maskG = data[i + 1]
390 0 : maskB = data[i + 2]
391 0 : break
392 : }
393 : }
394 :
395 1 : for (let i = 0; i < data.length; i += 4) {
396 4096 : const alpha = data[i + 3]
397 4096 : data[i + 3] = 255 - alpha
398 :
399 4096 : if (alpha === 0) {
400 4096 : data[i] = maskR
401 4096 : data[i + 1] = maskG
402 4096 : data[i + 2] = maskB
403 : }
404 : }
405 :
406 1 : ctx.putImageData(imageData, 0, 0)
407 1 : store.canvasHistory.saveState()
408 : }
409 :
410 60 : const clearMask = (): void => {
411 1 : const maskCtx = store.maskCtx
412 1 : const maskCanvas = store.maskCanvas
413 1 : const rgbCtx = store.rgbCtx
414 1 : const rgbCanvas = store.rgbCanvas
415 :
416 1 : if (maskCtx && maskCanvas) {
417 1 : maskCtx.clearRect(0, 0, maskCanvas.width, maskCanvas.height)
418 : }
419 1 : if (rgbCtx && rgbCanvas) {
420 1 : rgbCtx.clearRect(0, 0, rgbCanvas.width, rgbCanvas.height)
421 : }
422 1 : store.canvasHistory.saveState()
423 : }
424 :
425 60 : const clearLastColorSelectPoint = () => {
426 5 : lastColorSelectPoint.value = null
427 : }
428 :
429 60 : watch(
430 : [
431 : () => store.colorSelectTolerance,
432 : () => store.colorComparisonMethod,
433 : () => store.selectionOpacity
434 : ],
435 : async () => {
436 0 : if (
437 : lastColorSelectPoint.value &&
438 : store.colorSelectLivePreview &&
439 : store.canUndo
440 : ) {
441 0 : store.canvasHistory.undo()
442 0 : await colorSelectFill(lastColorSelectPoint.value)
443 : }
444 : }
445 : )
446 :
447 60 : return {
448 : paintBucketFill,
449 :
450 : colorSelectFill,
451 : clearLastColorSelectPoint,
452 :
453 : invertMask,
454 : clearMask
455 : }
456 : }
|