fix(annotate): 反白像素公式纠正为 r+(255-2r)*k(旧公式 k=0 变纯白,导致颜色发艳)v1.20.5

This commit is contained in:
qiqi committed 2026-11-17 20:53:11 +08:00
1 parent bab181b12f
commit f8998faef0
3 files changed
+41 -10

No files matched your search

+1 -1
View File
@@ -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",
+21 -4
View File
@@ -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()
}
+19 -5
View File
@@ -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) {