feat: add alpha and morphology core tools

This commit is contained in:
2026-08-24 12:02:42 +05:00
parent b434bf3ef3
commit cb5ccf6da8
4 changed files with 389 additions and 0 deletions
+12
View File
@@ -73,6 +73,18 @@ export function invertAlpha(img: PixelImage): PixelImage {
return out;
}
export function hardenAlpha(img: PixelImage, thresholdPercent: number): PixelImage {
const threshold = (clamp(thresholdPercent, 0, 100) / 100) * 255;
const out = createPixelImage(img.width, img.height);
for (let i = 0; i < out.data.length; i += 4) {
out.data[i] = img.data[i];
out.data[i + 1] = img.data[i + 1];
out.data[i + 2] = img.data[i + 2];
out.data[i + 3] = img.data[i + 3] >= threshold ? 255 : 0;
}
return out;
}
export function colorMask(img: PixelImage, hex: string, tolerancePercent = 0): PixelImage {
const [targetR, targetG, targetB] = parseHex(hex);
const tolerance = (clamp(tolerancePercent, 0, 100) / 100) * MAX_COLOR_DISTANCE;