From f8998faef0f672738c13d1bbe848144270747505 Mon Sep 17 00:00:00 2001 From: qiqi Date: Tue, 17 Nov 2026 20:53:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(annotate):=20=E5=8F=8D=E7=99=BD=E5=83=8F?= =?UTF-8?q?=E7=B4=A0=E5=85=AC=E5=BC=8F=E7=BA=A0=E6=AD=A3=E4=B8=BA=20r+(255?= =?UTF-8?q?-2r)*k=EF=BC=88=E6=97=A7=E5=85=AC=E5=BC=8F=20k=3D0=20=E5=8F=98?= =?UTF-8?q?=E7=BA=AF=E7=99=BD=EF=BC=8C=E5=AF=BC=E8=87=B4=E9=A2=9C=E8=89=B2?= =?UTF-8?q?=E5=8F=91=E8=89=B3=EF=BC=89v1.20.5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- image-annotate/package.json | 2 +- image-annotate/plugin/editor/effects.js | 25 +++++++++++++++++++++---- image-annotate/src/index.js | 24 +++++++++++++++++++----- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/image-annotate/package.json b/image-annotate/package.json index c0b8764..72c3bc4 100644 --- a/image-annotate/package.json +++ b/image-annotate/package.json @@ -1,7 +1,7 @@ { "name": "@iwb-plugin/image-annotate", "private": true, - "version": "1.20.4", + "version": "1.20.5", "description": "IWB 插件:图片标注 / 修图(编辑器移植自 iwb-plaus 扩展,纯 Canvas 2D)", "ibwPlugin": { "id": "image-annotate", diff --git a/image-annotate/plugin/editor/effects.js b/image-annotate/plugin/editor/effects.js index 09fe3f7..9c75f12 100644 --- a/image-annotate/plugin/editor/effects.js +++ b/image-annotate/plugin/editor/effects.js @@ -63,8 +63,14 @@ import { toast } from './commands.js' return } if (FILTER_SUPPORTED) { filterCanvas(canvas, 'invert(' + k + ')'); return } - const okInv = mapPixels(canvas, (r, g, b) => [clamp255(255 - (255 - r) * k), clamp255(255 - (255 - g) * k), clamp255(255 - (255 - b) * k)]) - if (!okInv) blendFallback(canvas, 'invert') + // 线性插值到反相值:r + ((255 - r) - r) * k = r + (255 - 2r) * k + // (旧写法 255-(255-r)*k 在 k=0 时直接变成纯白,k=1 也是白,颜色会整体发灰发艳) + const okInv = mapPixels(canvas, (r, g, b) => [ + clamp255(r + (255 - 2 * r) * k), + clamp255(g + (255 - 2 * g) * k), + clamp255(b + (255 - 2 * b) * k) + ]) + if (!okInv) blendFallback(canvas, 'invert', k) } }, gray: { @@ -78,7 +84,7 @@ import { toast } from './commands.js' const y = luma(r, g, b) return [clamp255(r + (y - r) * k), clamp255(g + (y - g) * k), clamp255(b + (y - b) * k)] }) - if (!okGray) blendFallback(canvas, 'gray') + if (!okGray) blendFallback(canvas, 'gray', k) } }, boost: { @@ -194,14 +200,25 @@ import { toast } from './commands.js' } /** 混合模式兜底(读不到像素时):反相用 difference+白,去色用 saturation+灰 */ - function blendFallback(canvas, mode) { + function blendFallback(canvas, mode, strength) { + const k = typeof strength === 'number' ? Math.max(0, Math.min(1, strength)) : 1 const ctx = canvas.getContext('2d') + const copy = document.createElement('canvas') + copy.width = canvas.width + copy.height = canvas.height + copy.getContext('2d').drawImage(canvas, 0, 0) ctx.save() ctx.setTransform(1, 0, 0, 1, 0, 0) ctx.globalAlpha = 1 if (mode === 'invert') { ctx.globalCompositeOperation = 'difference'; ctx.fillStyle = '#ffffff' } else { ctx.globalCompositeOperation = 'saturation'; ctx.fillStyle = '#808080' } ctx.fillRect(0, 0, canvas.width, canvas.height) + // 按强度混回原图(k=1 全反相,k=0 原样) + if (k < 1) { + ctx.globalCompositeOperation = 'source-over' + ctx.globalAlpha = 1 - k + ctx.drawImage(copy, 0, 0) + } ctx.restore() } diff --git a/image-annotate/src/index.js b/image-annotate/src/index.js index c7ed0a9..8bc1a13 100644 --- a/image-annotate/src/index.js +++ b/image-annotate/src/index.js @@ -9,7 +9,7 @@ var __IWB_ANNOTATE_MANIFEST__ = { id: 'image-annotate', name: '标注 / 修图', - version: '1.20.4', + version: '1.20.5', apiVersion: 1, capabilities: ['commands', 'nodeActions', 'canvas', 'canvasWrite'], settings: { @@ -3918,8 +3918,12 @@ var __IWB_ANNOTATE_MANIFEST__ = { filterCanvas(canvas, "invert(" + k + ")"); return; } - const okInv = mapPixels(canvas, (r, g, b) => [clamp255(255 - (255 - r) * k), clamp255(255 - (255 - g) * k), clamp255(255 - (255 - b) * k)]); - if (!okInv) blendFallback(canvas, "invert"); + const okInv = mapPixels(canvas, (r, g, b) => [ + clamp255(r + (255 - 2 * r) * k), + clamp255(g + (255 - 2 * g) * k), + clamp255(b + (255 - 2 * b) * k) + ]); + if (!okInv) blendFallback(canvas, "invert", k); } }, gray: { @@ -3942,7 +3946,7 @@ var __IWB_ANNOTATE_MANIFEST__ = { const y = luma(r, g, b); return [clamp255(r + (y - r) * k), clamp255(g + (y - g) * k), clamp255(b + (y - b) * k)]; }); - if (!okGray) blendFallback(canvas, "gray"); + if (!okGray) blendFallback(canvas, "gray", k); } }, boost: { @@ -4079,8 +4083,13 @@ var __IWB_ANNOTATE_MANIFEST__ = { ctx.putImageData(img, 0, 0); return true; } - function blendFallback(canvas, mode) { + function blendFallback(canvas, mode, strength) { + const k = typeof strength === "number" ? Math.max(0, Math.min(1, strength)) : 1; const ctx = canvas.getContext("2d"); + const copy = document.createElement("canvas"); + copy.width = canvas.width; + copy.height = canvas.height; + copy.getContext("2d").drawImage(canvas, 0, 0); ctx.save(); ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.globalAlpha = 1; @@ -4092,6 +4101,11 @@ var __IWB_ANNOTATE_MANIFEST__ = { ctx.fillStyle = "#808080"; } ctx.fillRect(0, 0, canvas.width, canvas.height); + if (k < 1) { + ctx.globalCompositeOperation = "source-over"; + ctx.globalAlpha = 1 - k; + ctx.drawImage(copy, 0, 0); + } ctx.restore(); } function luma(r, g, b) {