From 8d0564a1108d4fd555422dcd7c7b014da7c2ffd5 Mon Sep 17 00:00:00 2001 From: zihanjian Date: Fri, 26 Dec 2025 17:49:36 +0800 Subject: [PATCH] fix(pixelation): exclude external and transparent cells from color counting --- src/app/page.tsx | 4 ++-- src/utils/pixelation.ts | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/app/page.tsx b/src/app/page.tsx index ea50c2b..e099185 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -835,7 +835,7 @@ export default function Home() { // 2. 统计初始颜色数量 const initialColorCounts: { [key: string]: number } = {}; initialMappedData.flat().forEach(cell => { - if (cell && cell.key) { + if (cell && cell.key && !cell.isExternal && cell.key !== TRANSPARENT_KEY) { initialColorCounts[cell.key] = (initialColorCounts[cell.key] || 0) + 1; } }); @@ -854,7 +854,7 @@ export default function Home() { // 4. 复制初始数据,准备合并 const mergedData: MappedPixel[][] = initialMappedData.map(row => - row.map(cell => ({...cell, isExternal: false})) + row.map(cell => ({ ...cell, isExternal: cell.isExternal ?? false })) ); // 5. 处理相似颜色合并 diff --git a/src/utils/pixelation.ts b/src/utils/pixelation.ts index f10e7a6..3fdcb46 100644 --- a/src/utils/pixelation.ts +++ b/src/utils/pixelation.ts @@ -1,3 +1,5 @@ +import { transparentColorData } from './pixelEditingUtils'; + // 定义像素化模式 export enum PixelationMode { Dominant = 'dominant', // 卡通模式(主色) @@ -207,12 +209,12 @@ export function calculatePixelGrid( const closestBead = findClosestPaletteColor(representativeRgb, palette); finalCellColorData = { key: closestBead.key, color: closestBead.hex }; } else { - // 如果单元格为空或全透明,使用备用色 - finalCellColorData = { key: t1FallbackColor.key, color: t1FallbackColor.hex }; + // 如果单元格为空或全透明,标记为透明/外部 + finalCellColorData = { ...transparentColorData }; } mappedData[j][i] = finalCellColorData; } } console.log(`Pixel grid calculation complete for mode: ${mode}`); return mappedData; -} \ No newline at end of file +}