feat: 增强功能批量入库 - 双色彩引擎/抖动/杂色清理/品牌色板/PDF导出/编辑工具/分享/i18n 等验收代码
This commit is contained in:
1 parent
02762400fb
commit
f846fabfc6
77 files changed
+11372
-226
No files matched your search
+76
-7
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import ChibiPage from './ChibiPage';
|
||||
import Nav from './components/Nav';
|
||||
import Hero from './components/Hero';
|
||||
@@ -7,6 +7,7 @@ import Footer from './components/Footer';
|
||||
import ControlsPanel from './components/ControlsPanel';
|
||||
import PreviewPanel from './components/PreviewPanel';
|
||||
import InventoryPanel from './components/InventoryPanel';
|
||||
import EditToolbar from './components/EditToolbar';
|
||||
import RedeemModal from './components/RedeemModal';
|
||||
import AgencyModal from './components/AgencyModal';
|
||||
import OrderModal from './components/OrderModal';
|
||||
@@ -16,6 +17,15 @@ import {
|
||||
type PatternSettings,
|
||||
type SourceImage,
|
||||
} from './lib/usePattern';
|
||||
import {
|
||||
EditHistory,
|
||||
flipGrid,
|
||||
gridStateToProcessResult,
|
||||
mirrorGrid,
|
||||
resultToGridState,
|
||||
rotateGrid,
|
||||
type GridState,
|
||||
} from './lib/grid-edit';
|
||||
import { buildExportCanvas, downloadCanvas } from './lib/drawPattern';
|
||||
import { consumeCode, redeemCode } from './lib/api';
|
||||
|
||||
@@ -24,6 +34,7 @@ const DEFAULT_SETTINGS: PatternSettings = {
|
||||
maxColors: 24,
|
||||
removeBg: false,
|
||||
showGrid: true,
|
||||
brandId: 'MARD',
|
||||
};
|
||||
|
||||
const DEFAULT_PLAN: Plan = {
|
||||
@@ -51,10 +62,32 @@ function HomePage() {
|
||||
const [uploadError, setUploadError] = useState<string | null>(null);
|
||||
const [modal, setModal] = useState<ModalState>(null);
|
||||
|
||||
// 编辑工具(T-069/T-068):当前网格快照 + 撤销/重做历史。
|
||||
const [gridState, setGridState] = useState<GridState | null>(null);
|
||||
const historyRef = useRef<EditHistory | null>(null);
|
||||
if (historyRef.current === null) {
|
||||
historyRef.current = new EditHistory(50);
|
||||
}
|
||||
const history = historyRef.current;
|
||||
|
||||
const redeemed = redeemedCode !== null;
|
||||
|
||||
const { result, processing, error } = usePattern(source, settings, runId);
|
||||
|
||||
// 新的管线结果到达时,重置编辑状态(历史不跨图纸保留)。
|
||||
useEffect(() => {
|
||||
history.clear();
|
||||
setGridState(result ? resultToGridState(result, settings.gridSize) : null);
|
||||
}, [result, settings.gridSize, history]);
|
||||
|
||||
// 预览/清单/下载统一使用「编辑后的网格」重建的 ProcessResult;
|
||||
// 结果尚未同步进 state 的首帧直接由原始 result 派生,避免闪烁。
|
||||
const editedResult = useMemo(() => {
|
||||
if (!result) return null;
|
||||
const base = gridState ?? resultToGridState(result, settings.gridSize);
|
||||
return gridStateToProcessResult(base);
|
||||
}, [gridState, result, settings.gridSize]);
|
||||
|
||||
// Revoke object URLs when the source is replaced.
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
@@ -91,7 +124,7 @@ function HomePage() {
|
||||
}, []);
|
||||
|
||||
const handleDownload = useCallback(async () => {
|
||||
if (!result || !source || !redeemedCode) return;
|
||||
if (!editedResult || !source || !redeemedCode) return;
|
||||
const baseName = source.name.replace(/\.[^.]+$/, '') || '拼豆图纸';
|
||||
try {
|
||||
// 下载消耗 1 次权益(首次免费),成功后再触发本地图纸导出
|
||||
@@ -101,15 +134,36 @@ function HomePage() {
|
||||
downloadGrant: '',
|
||||
});
|
||||
setDownloadError(null);
|
||||
const canvas = buildExportCanvas(result, settings.gridSize, baseName);
|
||||
const canvas = buildExportCanvas(editedResult, settings.gridSize, baseName, settings.brandId);
|
||||
downloadCanvas(
|
||||
canvas,
|
||||
`${baseName}-${settings.gridSize}x${settings.gridSize}-MARD拼豆图纸.png`,
|
||||
`${baseName}-${settings.gridSize}x${settings.gridSize}-${settings.brandId}拼豆图纸.png`,
|
||||
);
|
||||
} catch (err) {
|
||||
setDownloadError(err instanceof Error ? err.message : '下载失败,请稍后重试');
|
||||
}
|
||||
}, [result, source, settings.gridSize, redeemedCode]);
|
||||
}, [editedResult, source, settings.gridSize, settings.brandId, redeemedCode]);
|
||||
|
||||
// 编辑工具:在历史中记录新状态后再应用变换(撤销/重做走历史栈)。
|
||||
const applyEdit = useCallback(
|
||||
(transform: (s: GridState) => GridState) => {
|
||||
if (!gridState) return;
|
||||
const next = transform(gridState);
|
||||
history.push(next);
|
||||
setGridState(next);
|
||||
},
|
||||
[gridState, history],
|
||||
);
|
||||
|
||||
const handleUndo = useCallback(() => {
|
||||
const prev = history.undo();
|
||||
if (prev) setGridState(prev);
|
||||
}, [history]);
|
||||
|
||||
const handleRedo = useCallback(() => {
|
||||
const next = history.redo();
|
||||
if (next) setGridState(next);
|
||||
}, [history]);
|
||||
|
||||
const beadCount = result?.totalBeads ?? 0;
|
||||
const colorCount = result?.palette.length ?? 0;
|
||||
@@ -139,17 +193,32 @@ function HomePage() {
|
||||
<PreviewPanel
|
||||
source={source}
|
||||
settings={settings}
|
||||
result={result}
|
||||
result={editedResult}
|
||||
processing={processing}
|
||||
error={error}
|
||||
redeemed={redeemed}
|
||||
downloadError={downloadError}
|
||||
onDownload={handleDownload}
|
||||
toolbar={
|
||||
result ? (
|
||||
<EditToolbar
|
||||
canUndo={history.canUndo}
|
||||
canRedo={history.canRedo}
|
||||
onUndo={handleUndo}
|
||||
onRedo={handleRedo}
|
||||
onRotate={() => applyEdit((s) => rotateGrid(s, 90))}
|
||||
onMirrorVertical={() => applyEdit((s) => mirrorGrid(s, 'vertical'))}
|
||||
onMirrorHorizontal={() => applyEdit((s) => mirrorGrid(s, 'horizontal'))}
|
||||
onFlip={() => applyEdit((s) => flipGrid(s))}
|
||||
/>
|
||||
) : undefined
|
||||
}
|
||||
/>
|
||||
|
||||
<InventoryPanel
|
||||
result={result}
|
||||
result={editedResult}
|
||||
redeemed={redeemed}
|
||||
brandId={settings.brandId}
|
||||
onRedeemClick={() => setModal({ kind: 'redeem' })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -583,6 +583,24 @@
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
/* ── 05 标签已选计数(DESIGN.md §4.3) ── */
|
||||
|
||||
.chibi-tag-count {
|
||||
margin-left: auto;
|
||||
font-size: var(--text-xs);
|
||||
font-weight: 700;
|
||||
color: var(--text-tertiary);
|
||||
background: var(--bg-subtle);
|
||||
border-radius: var(--radius-full);
|
||||
padding: 2px 10px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.chibi-tag-count.is-full {
|
||||
color: var(--accent-hover);
|
||||
background: var(--accent-soft);
|
||||
}
|
||||
|
||||
/* ── 05 补充描述 + 参考图上传 ── */
|
||||
|
||||
.chibi-detail-row {
|
||||
@@ -626,6 +644,11 @@
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.chibi-char-counter.is-limit {
|
||||
color: var(--danger);
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.chibi-upload {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
||||
@@ -607,6 +607,12 @@ export function ChibiPage() {
|
||||
<span className="chibi-section-num">05</span>
|
||||
<h2 className="chibi-section-title">细节提示词</h2>
|
||||
<span className="chibi-section-note">最多选 {MAX_TAGS} 项</span>
|
||||
<span
|
||||
className={`chibi-tag-count${selectedTags.length >= MAX_TAGS ? " is-full" : ""}`}
|
||||
aria-live="polite"
|
||||
>
|
||||
已选 {selectedTags.length}/{MAX_TAGS}
|
||||
</span>
|
||||
</div>
|
||||
<div className="chibi-tags">
|
||||
{tags.map((tag) => {
|
||||
@@ -641,15 +647,17 @@ export function ChibiPage() {
|
||||
value={promptText}
|
||||
onChange={(e) => setPromptText(e.target.value)}
|
||||
/>
|
||||
<div className="chibi-char-counter">
|
||||
<div
|
||||
className={`chibi-char-counter${promptText.length >= MAX_PROMPT_LENGTH ? " is-limit" : ""}`}
|
||||
>
|
||||
{promptText.length}/{MAX_PROMPT_LENGTH}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<span className="chibi-field-label">参考图上传(选填)</span>
|
||||
<span className="chibi-field-label">上传风格示例图(可选)</span>
|
||||
<UploadBox
|
||||
title="上传参考图"
|
||||
hint="支持 JPG、PNG、WEBP · 不超过 12MB"
|
||||
title="上传风格示例图"
|
||||
hint="不会照搬示例主体"
|
||||
value={referenceImage}
|
||||
onChange={setReferenceImage}
|
||||
/>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useRef, type CSSProperties } from 'react';
|
||||
import type { PatternSettings, SourceImage } from '../lib/usePattern';
|
||||
import { BRANDS } from '../lib/brand-palettes';
|
||||
|
||||
const GRID_MIN = 16;
|
||||
const GRID_MAX = 128;
|
||||
@@ -106,6 +107,31 @@ export default function ControlsPanel({
|
||||
}}
|
||||
/>
|
||||
|
||||
{/* 品牌色板 */}
|
||||
<div className="control-group">
|
||||
<div className="control-label-row">
|
||||
<label className="control-label" id="brand-select-label">
|
||||
品牌色板
|
||||
</label>
|
||||
<strong className="control-value">{settings.brandId}</strong>
|
||||
</div>
|
||||
<div className="preset-row" role="group" aria-labelledby="brand-select-label">
|
||||
{BRANDS.map((brand) => (
|
||||
<button
|
||||
key={brand.id}
|
||||
type="button"
|
||||
className={`preset-btn${settings.brandId === brand.id ? ' preset-btn--active' : ''}`}
|
||||
title={brand.name}
|
||||
aria-pressed={settings.brandId === brand.id}
|
||||
onClick={() => onChangeSettings({ brandId: brand.id })}
|
||||
>
|
||||
{brand.id}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<p className="control-hint">图纸色号与备料清单将按所选品牌编码</p>
|
||||
</div>
|
||||
|
||||
{/* 图纸尺寸滑杆 */}
|
||||
<div className="control-group">
|
||||
<div className="control-label-row">
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
import type { ReactNode } from 'react';
|
||||
|
||||
interface EditToolbarProps {
|
||||
/** 是否还有可撤销的历史步骤(无图纸或初始状态时禁用)。 */
|
||||
canUndo: boolean;
|
||||
/** 是否还有可重做的历史步骤。 */
|
||||
canRedo: boolean;
|
||||
onUndo: () => void;
|
||||
onRedo: () => void;
|
||||
/** 顺时针旋转 90°。 */
|
||||
onRotate: () => void;
|
||||
/** 左右镜像(跨垂直轴,左右对调)。 */
|
||||
onMirrorVertical: () => void;
|
||||
/** 上下镜像(跨水平轴,上下对调)。 */
|
||||
onMirrorHorizontal: () => void;
|
||||
/** 对角翻转(主对角线对称)。 */
|
||||
onFlip: () => void;
|
||||
}
|
||||
|
||||
/** 描边式 SVG 图标(无 emoji),stroke 使用 currentColor 随按钮文字变色。 */
|
||||
function Icon(props: { children: ReactNode }): ReactNode {
|
||||
return (
|
||||
<svg
|
||||
width="14"
|
||||
height="14"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2.2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
aria-hidden="true"
|
||||
className="edit-tool-icon"
|
||||
>
|
||||
{props.children}
|
||||
</svg>
|
||||
);
|
||||
}
|
||||
|
||||
const IconUndo = (
|
||||
<Icon>
|
||||
<path d="M9 14 4 9l5-5" />
|
||||
<path d="M4 9h10.5a5.5 5.5 0 0 1 0 11H11" />
|
||||
</Icon>
|
||||
);
|
||||
|
||||
const IconRedo = (
|
||||
<Icon>
|
||||
<path d="m15 14 5-5-5-5" />
|
||||
<path d="M20 9H9.5a5.5 5.5 0 0 0 0 11H13" />
|
||||
</Icon>
|
||||
);
|
||||
|
||||
const IconRotate = (
|
||||
<Icon>
|
||||
<path d="M21 12a9 9 0 1 1-2.64-6.36" />
|
||||
<path d="M21 3v6h-6" />
|
||||
</Icon>
|
||||
);
|
||||
|
||||
const IconMirrorVertical = (
|
||||
<Icon>
|
||||
<path d="M12 3v18" />
|
||||
<path d="m8 7-4 5 4 5" />
|
||||
<path d="m16 7 4 5-4 5" />
|
||||
</Icon>
|
||||
);
|
||||
|
||||
const IconMirrorHorizontal = (
|
||||
<Icon>
|
||||
<path d="M3 12h18" />
|
||||
<path d="m7 8-5 4 5 4" />
|
||||
<path d="m17 8 5 4-5 4" />
|
||||
</Icon>
|
||||
);
|
||||
|
||||
const IconFlip = (
|
||||
<Icon>
|
||||
<path d="M21 3 3 21" />
|
||||
<path d="M21 3h-6" />
|
||||
<path d="M3 21v-6" />
|
||||
</Icon>
|
||||
);
|
||||
|
||||
/** 拼豆图纸编辑工具栏:撤销 / 重做 / 旋转 / 镜像 / 翻转。 */
|
||||
export default function EditToolbar({
|
||||
canUndo,
|
||||
canRedo,
|
||||
onUndo,
|
||||
onRedo,
|
||||
onRotate,
|
||||
onMirrorVertical,
|
||||
onMirrorHorizontal,
|
||||
onFlip,
|
||||
}: EditToolbarProps) {
|
||||
return (
|
||||
<div className="edit-toolbar" role="toolbar" aria-label="编辑工具">
|
||||
<button
|
||||
type="button"
|
||||
className="edit-tool-btn"
|
||||
disabled={!canUndo}
|
||||
onClick={onUndo}
|
||||
title="撤销上一步"
|
||||
aria-label="撤销上一步"
|
||||
>
|
||||
{IconUndo}
|
||||
<span>撤销</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className="edit-tool-btn"
|
||||
disabled={!canRedo}
|
||||
onClick={onRedo}
|
||||
title="重做下一步"
|
||||
aria-label="重做下一步"
|
||||
>
|
||||
{IconRedo}
|
||||
<span>重做</span>
|
||||
</button>
|
||||
|
||||
<span className="edit-tool-sep" aria-hidden="true" />
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className="edit-tool-btn"
|
||||
onClick={onRotate}
|
||||
title="顺时针旋转 90°"
|
||||
aria-label="顺时针旋转 90°"
|
||||
>
|
||||
{IconRotate}
|
||||
<span>旋转90°</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className="edit-tool-btn"
|
||||
onClick={onMirrorVertical}
|
||||
title="左右镜像(左右对调)"
|
||||
aria-label="左右镜像"
|
||||
>
|
||||
{IconMirrorVertical}
|
||||
<span>左右镜像</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className="edit-tool-btn"
|
||||
onClick={onMirrorHorizontal}
|
||||
title="上下镜像(上下对调)"
|
||||
aria-label="上下镜像"
|
||||
>
|
||||
{IconMirrorHorizontal}
|
||||
<span>上下镜像</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className="edit-tool-btn"
|
||||
onClick={onFlip}
|
||||
title="对角翻转(主对角线对称,可做对称图纸)"
|
||||
aria-label="对角翻转"
|
||||
>
|
||||
{IconFlip}
|
||||
<span>对角翻转</span>
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -3,21 +3,28 @@ import type { ProcessResult } from '../lib/image-processor';
|
||||
interface InventoryPanelProps {
|
||||
result: ProcessResult | null;
|
||||
redeemed: boolean;
|
||||
/** 色号品牌 ID(MARD / COCO / 漫漫 / 盼盼 / 咪小窝),默认 'MARD'。 */
|
||||
brandId?: string;
|
||||
onRedeemClick: () => void;
|
||||
}
|
||||
|
||||
const GHOST_ROWS = 4;
|
||||
|
||||
/** 03 MARD 备料清单 panel (§2.6, §3.4): locked masked state / unlocked list. */
|
||||
export default function InventoryPanel({ result, redeemed, onRedeemClick }: InventoryPanelProps) {
|
||||
/** 03 备料清单 panel (§2.6, §3.4): locked masked state / unlocked list. */
|
||||
export default function InventoryPanel({
|
||||
result,
|
||||
redeemed,
|
||||
brandId = 'MARD',
|
||||
onRedeemClick,
|
||||
}: InventoryPanelProps) {
|
||||
const totalBeads = result?.totalBeads ?? 0;
|
||||
const colorCount = result?.palette.length ?? 0;
|
||||
|
||||
return (
|
||||
<section className="panel panel--inventory" aria-label="03 MARD 备料清单">
|
||||
<section className="panel panel--inventory" aria-label={`03 ${brandId} 备料清单`}>
|
||||
<div className="panel-header">
|
||||
<span className="panel-index">03</span>
|
||||
<h2 className="panel-title">MARD 备料清单</h2>
|
||||
<h2 className="panel-title">{brandId} 备料清单</h2>
|
||||
{redeemed ? (
|
||||
<span className="inv-unlocked-badge">已解锁</span>
|
||||
) : (
|
||||
@@ -54,7 +61,7 @@ export default function InventoryPanel({ result, redeemed, onRedeemClick }: Inve
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<p className="inv-empty">上传图片并生成图纸后,这里会显示 MARD 色号清单。</p>
|
||||
<p className="inv-empty">上传图片并生成图纸后,这里会显示 {brandId} 色号清单。</p>
|
||||
)}
|
||||
|
||||
<p className="inv-extra">
|
||||
@@ -94,7 +101,7 @@ export default function InventoryPanel({ result, redeemed, onRedeemClick }: Inve
|
||||
</div>
|
||||
<p className="inv-lock-title">完整备料清单已锁定</p>
|
||||
<p className="inv-lock-desc">
|
||||
兑换后可查看全部 MARD 色号、每色颗数、总颗数及 10% 备豆建议
|
||||
兑换后可查看全部 {brandId} 色号、每色颗数、总颗数及 10% 备豆建议
|
||||
</p>
|
||||
<button type="button" className="inv-unlock-btn" onClick={onRedeemClick}>
|
||||
输入兑换码解锁
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useEffect, useRef, type ReactNode } from 'react';
|
||||
import type { PatternSettings, SourceImage } from '../lib/usePattern';
|
||||
import type { ProcessResult } from '../lib/image-processor';
|
||||
import { drawPreview } from '../lib/drawPattern';
|
||||
@@ -12,6 +12,8 @@ interface PreviewPanelProps {
|
||||
redeemed: boolean;
|
||||
downloadError: string | null;
|
||||
onDownload: () => void;
|
||||
/** 可选的编辑工具栏(撤销/重做/旋转/镜像等),渲染在画布上方。 */
|
||||
toolbar?: ReactNode;
|
||||
}
|
||||
|
||||
/** 02 实时预览 panel (§2.6): canvas + ruler + download button. */
|
||||
@@ -24,6 +26,7 @@ export default function PreviewPanel({
|
||||
redeemed,
|
||||
downloadError,
|
||||
onDownload,
|
||||
toolbar,
|
||||
}: PreviewPanelProps) {
|
||||
const canvasRef = useRef<HTMLCanvasElement | null>(null);
|
||||
|
||||
@@ -51,6 +54,8 @@ export default function PreviewPanel({
|
||||
<span className="panel-hint">每5格加粗 · 每10格标号</span>
|
||||
</div>
|
||||
|
||||
{toolbar}
|
||||
|
||||
<div className="preview-canvas-wrap">
|
||||
{processing ? (
|
||||
<div className="preview-empty">
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
/**
|
||||
* 多品牌色板(brand-palettes.ts)测试。
|
||||
*
|
||||
* 覆盖:291 标准色中间层的完整性、各品牌实测色数(与参考 JSON 核对)、
|
||||
* 品牌色板推导,以及按品牌在 LAB 空间查找最近色号。
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { BRANDS, STANDARD_COLORS, getBrandPalette, findNearestByBrand } from '../brand-palettes.js';
|
||||
import { srgbToLab } from '../lab.js';
|
||||
import { MARD_PALETTE } from '../mard-palette.js';
|
||||
|
||||
/** 任务给定的 Zippland 站方宣称色数(168/144/144/96/96),仅作记录与对照。 */
|
||||
const DECLARED_COUNTS: Readonly<Record<string, number>> = {
|
||||
MARD: 168,
|
||||
COCO: 144,
|
||||
漫漫: 144,
|
||||
盼盼: 96,
|
||||
咪小窝: 96,
|
||||
};
|
||||
|
||||
/**
|
||||
* 从参考 JSON(zippland-291标准色映射.json)实测的派生色板长度:
|
||||
* 291 个标准色中,仅漫漫在 #55514C 记为 "-"(无对应色号),其余品牌全部有映射。
|
||||
*/
|
||||
const ACTUAL_PALETTE_COUNTS: Readonly<Record<string, number>> = {
|
||||
MARD: 291,
|
||||
COCO: 291,
|
||||
漫漫: 290,
|
||||
盼盼: 291,
|
||||
咪小窝: 291,
|
||||
};
|
||||
|
||||
/** 各品牌在映射表中出现的唯一色号数(漫漫的 S4 被两个标准色共用,故唯一色号比条目少 1)。 */
|
||||
const ACTUAL_UNIQUE_CODE_COUNTS: Readonly<Record<string, number>> = {
|
||||
MARD: 291,
|
||||
COCO: 291,
|
||||
漫漫: 289,
|
||||
盼盼: 291,
|
||||
咪小窝: 291,
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// STANDARD_COLORS 291 标准色中间层
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('STANDARD_COLORS 291 标准色中间层', () => {
|
||||
it('嵌入全部 291 个标准色,hex 唯一且格式合法', () => {
|
||||
expect(STANDARD_COLORS).toHaveLength(291);
|
||||
const hexes = STANDARD_COLORS.map((s) => s.hex);
|
||||
expect(new Set(hexes).size).toBe(291);
|
||||
for (const s of STANDARD_COLORS) {
|
||||
expect(s.hex).toMatch(/^#[0-9A-F]{6}$/);
|
||||
expect(s.rgb).toHaveLength(3);
|
||||
expect(s.lab).toHaveLength(3);
|
||||
for (const v of [...s.rgb, ...s.lab]) expect(Number.isFinite(v)).toBe(true);
|
||||
expect(Object.keys(s.brands).length).toBeGreaterThan(0);
|
||||
}
|
||||
});
|
||||
|
||||
it('每个标准色的品牌键都落在 5 个品牌集合内', () => {
|
||||
const brandIds = new Set(BRANDS.map((b) => b.id));
|
||||
for (const s of STANDARD_COLORS) {
|
||||
for (const key of Object.keys(s.brands)) {
|
||||
expect(brandIds.has(key)).toBe(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('标准色 LAB 与现有 MARD 管线交叉验证一致(#FAF4C8)', () => {
|
||||
const standard = STANDARD_COLORS.find((s) => s.hex === '#FAF4C8')!;
|
||||
const mard = MARD_PALETTE.find((m) => m.hex === '#FAF4C8')!;
|
||||
expect(standard.lab[0]).toBeCloseTo(mard.lab[0], 2);
|
||||
expect(standard.lab[1]).toBeCloseTo(mard.lab[1], 2);
|
||||
expect(standard.lab[2]).toBeCloseTo(mard.lab[2], 2);
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// BRANDS 品牌定义
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('BRANDS 品牌定义', () => {
|
||||
it('按固定顺序定义 5 个品牌', () => {
|
||||
expect(BRANDS.map((b) => b.id)).toEqual(['MARD', 'COCO', '漫漫', '盼盼', '咪小窝']);
|
||||
expect(new Set(BRANDS.map((b) => b.id)).size).toBe(5);
|
||||
});
|
||||
|
||||
it('每个品牌的 codes 非空、无重复、不含 "-"', () => {
|
||||
for (const brand of BRANDS) {
|
||||
expect(brand.codes.length).toBeGreaterThan(0);
|
||||
expect(new Set(brand.codes).size).toBe(brand.codes.length);
|
||||
expect(brand.codes.every((c) => c.length > 0 && c !== '-')).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('唯一色号数与参考 JSON 实测一致(站方宣称 168/144/144/96/96 与实测不符,以实测为准)', () => {
|
||||
for (const brand of BRANDS) {
|
||||
const declared = DECLARED_COUNTS[brand.id] ?? -1;
|
||||
const expected = ACTUAL_UNIQUE_CODE_COUNTS[brand.id] ?? -1;
|
||||
console.log(
|
||||
`[brand-palettes] ${brand.name}(${brand.id}): 站方宣称 ${declared} 色,映射表实测唯一色号 ${brand.codes.length} 个`,
|
||||
);
|
||||
expect(brand.codes.length).toBe(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// getBrandPalette 按品牌推导色板
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('getBrandPalette 按品牌推导色板', () => {
|
||||
it('各品牌色板长度与参考 JSON 实测一致', () => {
|
||||
for (const brand of BRANDS) {
|
||||
const actual = getBrandPalette(brand.id).length;
|
||||
const expected = ACTUAL_PALETTE_COUNTS[brand.id] ?? -1;
|
||||
const declared = DECLARED_COUNTS[brand.id] ?? -1;
|
||||
console.log(
|
||||
`[brand-palettes] ${brand.id} 色板:实测 ${actual} 条(站方宣称 ${declared} 色)`,
|
||||
);
|
||||
expect(actual).toBe(expected);
|
||||
}
|
||||
});
|
||||
|
||||
it('MARD 色板覆盖全部 291 个标准色', () => {
|
||||
expect(getBrandPalette('MARD')).toHaveLength(STANDARD_COLORS.length);
|
||||
});
|
||||
|
||||
it('漫漫色板跳过 "-" 无映射项,条目不含 "-" 色号', () => {
|
||||
const palette = getBrandPalette('漫漫');
|
||||
expect(palette).toHaveLength(290);
|
||||
expect(palette.every((e) => e.code !== '-')).toBe(true);
|
||||
});
|
||||
|
||||
it('条目结构完整:code/hex/rgb/lab 四要素齐备', () => {
|
||||
for (const entry of getBrandPalette('MARD')) {
|
||||
expect(entry.code).toBeTruthy();
|
||||
expect(entry.hex).toMatch(/^#[0-9A-F]{6}$/);
|
||||
expect(entry.rgb).toHaveLength(3);
|
||||
expect(entry.lab).toHaveLength(3);
|
||||
}
|
||||
});
|
||||
|
||||
it('MARD 色号为 A01 风格(字母前缀 + 数字)', () => {
|
||||
for (const entry of getBrandPalette('MARD')) {
|
||||
expect(entry.code).toMatch(/^[A-Z]{1,2}\d{1,2}$/);
|
||||
}
|
||||
});
|
||||
|
||||
it('盼盼色号全为数字', () => {
|
||||
for (const entry of getBrandPalette('盼盼')) {
|
||||
expect(entry.code).toMatch(/^\d+$/);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// findNearestByBrand 按品牌 LAB 查找
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('findNearestByBrand 按品牌 LAB 查找', () => {
|
||||
it('标准色自身 LAB 精确命中对应色号(距离 0)', () => {
|
||||
for (const hex of ['#FAF4C8', '#FFFFFF', '#000000', '#63F347']) {
|
||||
const s = STANDARD_COLORS.find((x) => x.hex === hex)!;
|
||||
expect(findNearestByBrand(s.lab, 'MARD').code).toBe(s.brands.MARD);
|
||||
}
|
||||
});
|
||||
|
||||
it('纯白 → T01(#FFFFFF),纯黑 → H07(#000000)', () => {
|
||||
expect(findNearestByBrand(srgbToLab([255, 255, 255]), 'MARD').code).toBe('T01');
|
||||
expect(findNearestByBrand(srgbToLab([0, 0, 0]), 'MARD').code).toBe('H07');
|
||||
});
|
||||
|
||||
it('纯红返回红色系 MARD 色号', () => {
|
||||
const match = findNearestByBrand(srgbToLab([255, 0, 0]), 'MARD');
|
||||
expect(match.code).toMatch(/^[A-Z]{1,2}\d{1,2}$/);
|
||||
expect(match.rgb[0]).toBeGreaterThan(match.rgb[2]);
|
||||
});
|
||||
|
||||
it('漫漫在 #55514C 无映射("-")时顺延到最近的有色号标准色', () => {
|
||||
const match = findNearestByBrand(srgbToLab([0x55, 0x51, 0x4c]), '漫漫');
|
||||
expect(match.code).toBeTruthy();
|
||||
expect(match.code).not.toBe('-');
|
||||
});
|
||||
|
||||
it('未知品牌抛出明确错误', () => {
|
||||
expect(() => findNearestByBrand([50, 0, 0], 'NO_SUCH_BRAND')).toThrow(/色号/);
|
||||
});
|
||||
|
||||
it('5 个品牌对任意输入都能返回有效色号', () => {
|
||||
const lab = srgbToLab([100, 150, 200]);
|
||||
for (const brand of BRANDS) {
|
||||
const match = findNearestByBrand(lab, brand.id);
|
||||
expect(match.code).toBeTruthy();
|
||||
expect(match.code).not.toBe('-');
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* 品牌管线集成测试(brand-palettes × image-processor)。
|
||||
*
|
||||
* 覆盖:brandId 缺省时行为与默认 MARD 完全一致(向后兼容);brandId='COCO'
|
||||
* 等非 MARD 品牌时,格子与清单色号切换为该品牌编码;5 个品牌均可正常生成。
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { processImage } from '../image-processor.js';
|
||||
import { BRANDS } from '../brand-palettes.js';
|
||||
import { MARD_PALETTE } from '../mard-palette.js';
|
||||
|
||||
/** 构造 size×size 的 ImageData,每像素由 paint(x, y) 上色。 */
|
||||
function buildTestImageData(
|
||||
size: number,
|
||||
paint: (x: number, y: number) => [number, number, number],
|
||||
): ImageData {
|
||||
const data = new Uint8ClampedArray(size * size * 4);
|
||||
for (let y = 0; y < size; y++) {
|
||||
for (let x = 0; x < size; x++) {
|
||||
const [r, g, b] = paint(x, y);
|
||||
const i = (y * size + x) * 4;
|
||||
data[i] = r;
|
||||
data[i + 1] = g;
|
||||
data[i + 2] = b;
|
||||
data[i + 3] = 255;
|
||||
}
|
||||
}
|
||||
return { data, width: size, height: size } as ImageData;
|
||||
}
|
||||
|
||||
/** 16×16 image: white everywhere except a 4×4 pure-red block at top-left. */
|
||||
function redOnWhiteImage(): ImageData {
|
||||
return buildTestImageData(16, (x, y) => (x < 4 && y < 4 ? [255, 0, 0] : [255, 255, 255]));
|
||||
}
|
||||
|
||||
const baseOptions = {
|
||||
gridSize: 4,
|
||||
maxColors: 8,
|
||||
removeBg: false,
|
||||
bgThreshold: 20,
|
||||
};
|
||||
|
||||
const brandCodeSets = new Map(BRANDS.map((b) => [b.id, new Set(b.codes)]));
|
||||
|
||||
describe('processImage × 品牌管线', () => {
|
||||
it("brandId 缺省与显式 'MARD' 结果完全一致(默认行为不变)", () => {
|
||||
const omitted = processImage(redOnWhiteImage(), baseOptions);
|
||||
const explicit = processImage(redOnWhiteImage(), { ...baseOptions, brandId: 'MARD' });
|
||||
expect(omitted).toEqual(explicit);
|
||||
});
|
||||
|
||||
it("brandId='COCO' 时所有格子色号均为 COCO 品牌色号", () => {
|
||||
const result = processImage(redOnWhiteImage(), { ...baseOptions, brandId: 'COCO' });
|
||||
const cocoCodes = brandCodeSets.get('COCO')!;
|
||||
expect(result.grid).toHaveLength(16);
|
||||
for (const cell of result.grid) {
|
||||
if (cell.empty) continue;
|
||||
expect(cocoCodes.has(cell.code)).toBe(true);
|
||||
}
|
||||
expect(result.palette.length).toBeGreaterThan(0);
|
||||
for (const entry of result.palette) {
|
||||
expect(cocoCodes.has(entry.code)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("brandId='COCO' 的红色块格子色号符合 COCO 编码风格(字母+数字)", () => {
|
||||
const result = processImage(redOnWhiteImage(), { ...baseOptions, brandId: 'COCO' });
|
||||
const redCell = result.grid.find((c) => c.x === 0 && c.y === 0)!;
|
||||
expect(redCell.empty).toBe(false);
|
||||
expect(redCell.code).toMatch(/^[A-Z]{1,2}\d{1,2}$/);
|
||||
});
|
||||
|
||||
it('COCO 与 MARD 对同一输入产出不同的品牌色号(映射已切换)', () => {
|
||||
const coco = processImage(redOnWhiteImage(), { ...baseOptions, brandId: 'COCO' });
|
||||
const mard = processImage(redOnWhiteImage(), baseOptions);
|
||||
const cocoCodes = new Set(coco.palette.map((e) => e.code));
|
||||
const mardCodes = new Set(mard.palette.map((e) => e.code));
|
||||
// 白色格子 → COCO L14(#FFFFFF),MARD 色板无 L 系列,必不相同。
|
||||
expect([...cocoCodes].some((c) => !mardCodes.has(c))).toBe(true);
|
||||
});
|
||||
|
||||
it('5 个品牌均可成功生成,且全部格子色号属于对应品牌的有效色号集', () => {
|
||||
for (const brand of BRANDS) {
|
||||
// 默认 MARD 走历史 MARD_PALETTE(A1 风格),其余品牌走 291 标准色映射色号。
|
||||
const codes =
|
||||
brand.id === 'MARD'
|
||||
? new Set(MARD_PALETTE.map((m) => m.code))
|
||||
: brandCodeSets.get(brand.id)!;
|
||||
const result = processImage(redOnWhiteImage(), {
|
||||
...baseOptions,
|
||||
brandId: brand.id,
|
||||
});
|
||||
expect(result.totalBeads).toBe(16);
|
||||
for (const cell of result.grid) {
|
||||
if (!cell.empty) {
|
||||
expect(codes.has(cell.code)).toBe(true);
|
||||
}
|
||||
}
|
||||
for (const entry of result.palette) {
|
||||
expect(codes.has(entry.code)).toBe(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* CIEDE2000 模块测试 — 使用 Sharma 2005 论文的 34 组补充测试数据
|
||||
* ("The CIEDE2000 Color-Difference Formula: Implementation Notes,
|
||||
* Supplementary Test Data, and Mathematical Observations", Table 1)。
|
||||
*
|
||||
* 该测试表覆盖了公式的关键路径:色相角跨 0°/360° 边界、中性色
|
||||
* (G 补偿与色相未定义情形)、蓝色区域旋转项 RT、大色差等。
|
||||
* 容差 ±5e-4(期望值保留 4 位小数,任何公式实现错误都会远超此量级)。
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { ciede2000 } from '../ciede2000.js';
|
||||
import { srgbToLab } from '../lab.js';
|
||||
import type { Lab } from '../lab.js';
|
||||
|
||||
/** Sharma 测试对:[lab1, lab2, 期望 ΔE00]。 */
|
||||
const SHARMA_PAIRS: readonly (readonly [Lab, Lab, number])[] = [
|
||||
[[50.0, 2.6772, -79.7751], [50.0, 0.0, -82.7485], 2.0425],
|
||||
[[50.0, 3.1571, -77.2803], [50.0, 0.0, -82.7485], 2.8615],
|
||||
[[50.0, 2.8361, -74.02], [50.0, 0.0, -82.7485], 3.4412],
|
||||
[[50.0, -1.3802, -84.2814], [50.0, 0.0, -82.7485], 1.0],
|
||||
[[50.0, -1.1848, -84.8006], [50.0, 0.0, -82.7485], 1.0],
|
||||
[[50.0, -0.9009, -85.5211], [50.0, 0.0, -82.7485], 1.0],
|
||||
[[50.0, 0.0, 0.0], [50.0, -1.0, 2.0], 2.3669],
|
||||
[[50.0, -1.0, 2.0], [50.0, 0.0, 0.0], 2.3669],
|
||||
[[50.0, 2.49, -0.001], [50.0, -2.49, 0.0009], 7.1792],
|
||||
[[50.0, 2.49, -0.001], [50.0, -2.49, 0.001], 7.1792],
|
||||
[[50.0, 2.49, -0.001], [50.0, -2.49, 0.0011], 7.2195],
|
||||
[[50.0, 2.49, -0.001], [50.0, -2.49, 0.0012], 7.2195],
|
||||
[[50.0, -0.001, 2.49], [50.0, 0.0009, -2.49], 4.8045],
|
||||
[[50.0, -0.001, 2.49], [50.0, 0.001, -2.49], 4.8045],
|
||||
[[50.0, -0.001, 2.49], [50.0, 0.0011, -2.49], 4.7461],
|
||||
[[50.0, 2.5, 0.0], [50.0, 0.0, -2.5], 4.3065],
|
||||
[[50.0, 2.5, 0.0], [73.0, 25.0, -18.0], 27.1492],
|
||||
[[50.0, 2.5, 0.0], [61.0, -5.0, 29.0], 22.8977],
|
||||
[[50.0, 2.5, 0.0], [56.0, -27.0, -3.0], 31.903],
|
||||
[[50.0, 2.5, 0.0], [58.0, 24.0, 15.0], 19.4535],
|
||||
[[50.0, 2.5, 0.0], [50.0, 3.1736, 0.5854], 1.0],
|
||||
[[50.0, 2.5, 0.0], [50.0, 3.2972, 0.0], 1.0],
|
||||
[[50.0, 2.5, 0.0], [50.0, 1.8634, 0.5757], 1.0],
|
||||
[[50.0, 2.5, 0.0], [50.0, 3.2592, 0.335], 1.0],
|
||||
[[60.2574, -34.0099, 36.2677], [60.4626, -34.1751, 39.4387], 1.2644],
|
||||
[[63.0109, -31.0961, -5.8663], [62.8187, -29.7946, -4.0864], 1.263],
|
||||
[[61.2901, 3.7196, -5.3901], [61.4292, 2.248, -4.962], 1.8731],
|
||||
[[35.0831, -44.1164, 3.7933], [35.0232, -40.0716, 1.5901], 1.8645],
|
||||
[[22.7233, 20.0904, -46.694], [23.0331, 14.973, -42.5619], 2.0373],
|
||||
[[36.4612, 47.858, 18.3852], [36.2715, 50.5065, 21.2231], 1.4146],
|
||||
[[90.8027, -2.0831, 1.441], [91.1528, -1.6435, 0.0447], 1.4441],
|
||||
[[90.9257, -0.5406, -0.9208], [88.6381, -0.8985, -0.7239], 1.5381],
|
||||
[[6.7747, -0.2908, -2.4247], [5.8714, -0.0985, -2.2286], 0.6377],
|
||||
[[2.0776, 0.0795, -1.135], [0.9033, -0.0636, -0.5514], 0.9082],
|
||||
];
|
||||
|
||||
describe('ciede2000(Sharma 2005 参考测试数据)', () => {
|
||||
it('34 组测试对全部与论文期望值一致(±5e-4)', () => {
|
||||
for (const [i, [lab1, lab2, expected]] of SHARMA_PAIRS.entries()) {
|
||||
expect(ciede2000(lab1, lab2), `第 ${i + 1} 组测试对`).toBeCloseTo(expected, 3);
|
||||
}
|
||||
});
|
||||
|
||||
it('对称:ΔE(a,b) === ΔE(b,a)', () => {
|
||||
const a: Lab = [50.0, 2.49, -0.001];
|
||||
const b: Lab = [50.0, -2.49, 0.0011];
|
||||
expect(ciede2000(a, b)).toBeCloseTo(ciede2000(b, a), 10);
|
||||
});
|
||||
|
||||
it('相同颜色 ΔE = 0', () => {
|
||||
const lab: Lab = [53.24, 80.09, 67.2];
|
||||
expect(ciede2000(lab, lab)).toBe(0);
|
||||
});
|
||||
|
||||
it('默认权重因子(kL=kC=kH=1)与显式传入 1 一致', () => {
|
||||
const a: Lab = [50.0, 2.6772, -79.7751];
|
||||
const b: Lab = [50.0, 0.0, -82.7485];
|
||||
expect(ciede2000(a, b)).toBeCloseTo(ciede2000(a, b, 1, 1, 1), 10);
|
||||
});
|
||||
|
||||
it('感知排序:ΔE 更小者视觉更接近(红色系 vs 蓝色系)', () => {
|
||||
const red: Lab = srgbToLab([255, 0, 0]);
|
||||
const darkRed: Lab = srgbToLab([200, 0, 0]);
|
||||
const blue: Lab = srgbToLab([0, 0, 255]);
|
||||
expect(ciede2000(red, blue)).toBeGreaterThan(ciede2000(red, darkRed));
|
||||
expect(ciede2000(red, blue)).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* cleanup.ts 测试 — 连通区域合并(BFS + 多数投票)与噪声标记。
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { mergeSimilarRegions, removeNoise } from '../cleanup.js';
|
||||
import { labDistance, srgbToLab, type Rgb3 } from '../lab.js';
|
||||
|
||||
// 相近的红色对(LAB 距离远小于默认阈值 24)
|
||||
const a1: Rgb3 = [190, 25, 25];
|
||||
const a2: Rgb3 = [210, 35, 35];
|
||||
// 相近的蓝色对
|
||||
const b1: Rgb3 = [25, 25, 190];
|
||||
const b2: Rgb3 = [35, 35, 210];
|
||||
const white: Rgb3 = [255, 255, 255];
|
||||
|
||||
describe('mergeSimilarRegions', () => {
|
||||
it('把相邻的相近颜色 BFS 合并为同一区域(小合成网格,两个色块)', () => {
|
||||
// 5×5:左上 2×2 红色块(3×a1 + 1×a2),右下 2×2 蓝色块(3×b1 + 1×b2),
|
||||
// 其余白色(与红/蓝的 LAB 距离远超阈值,不会串区域)。
|
||||
expect(labDistance(srgbToLab(a1), srgbToLab(a2))).toBeLessThan(24);
|
||||
expect(labDistance(srgbToLab(b1), srgbToLab(b2))).toBeLessThan(24);
|
||||
|
||||
const grid: Rgb3[] = Array.from({ length: 25 }, () => white);
|
||||
// 左上块(下标 0,1,5,6)
|
||||
grid[0] = a1;
|
||||
grid[1] = a1;
|
||||
grid[5] = a1;
|
||||
grid[6] = a2;
|
||||
// 右下块(下标 18,19,23,24)
|
||||
grid[18] = b1;
|
||||
grid[19] = b1;
|
||||
grid[23] = b1;
|
||||
grid[24] = b2;
|
||||
|
||||
const changed = mergeSimilarRegions(grid, 5, { mergeThreshold: 24, maxMergeDistance: 40 });
|
||||
|
||||
// 两个色块的少数派颜色被统一为多数派颜色
|
||||
expect(grid[6]).toEqual(a1);
|
||||
expect(grid[24]).toEqual(b1);
|
||||
// 只改写了这两个格子
|
||||
expect(changed).toEqual([6, 24]);
|
||||
// 白色区域不受影响
|
||||
expect(grid[12]).toEqual(white);
|
||||
expect(grid[0]).toEqual(a1);
|
||||
expect(grid[18]).toEqual(b1);
|
||||
});
|
||||
|
||||
it('多数投票:区域为 5 个相近色 + 1 个孤立色时全部统一为多数色', () => {
|
||||
// 3×3:前两行共 6 格为相近红色(5×c1 + 1×c2),第三行白色
|
||||
const c1: Rgb3 = [190, 25, 25];
|
||||
const c2: Rgb3 = [210, 35, 35];
|
||||
const grid: Rgb3[] = [
|
||||
c1, c1, c1,
|
||||
c1, c2, c1,
|
||||
white, white, white,
|
||||
];
|
||||
|
||||
const changed = mergeSimilarRegions(grid, 3); // 使用默认阈值
|
||||
|
||||
// 唯一少数派格子被改写为多数派颜色
|
||||
expect(grid[4]).toEqual(c1);
|
||||
expect(changed).toEqual([4]);
|
||||
// 其余格子保持不变
|
||||
for (let i = 0; i < grid.length; i++) {
|
||||
if (i === 4) continue;
|
||||
expect(grid[i]).toEqual(i < 6 ? c1 : white);
|
||||
}
|
||||
});
|
||||
|
||||
it('不同颜色相邻但 LAB 距离超阈值时不合并', () => {
|
||||
// 红格紧邻白格:红区不吞并白区
|
||||
const grid: Rgb3[] = [
|
||||
a1, white, white,
|
||||
white, white, white,
|
||||
white, white, white,
|
||||
];
|
||||
const changed = mergeSimilarRegions(grid, 3, { mergeThreshold: 10, maxMergeDistance: 20 });
|
||||
expect(changed).toEqual([]);
|
||||
expect(grid[0]).toEqual(a1);
|
||||
});
|
||||
|
||||
it('空网格返回空数组', () => {
|
||||
expect(mergeSimilarRegions([], 0)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('removeNoise', () => {
|
||||
it('把面积小于 minRegionSize 的孤立区域标记为噪声', () => {
|
||||
// 4×4:孤立 1 格红色(下标 0)、2×2 红色块(下标 5,6,9,10),其余白色
|
||||
const grid: Rgb3[] = Array.from({ length: 16 }, () => white);
|
||||
grid[0] = a1;
|
||||
for (const idx of [5, 6, 9, 10]) grid[idx] = a1;
|
||||
|
||||
const noise = removeNoise(grid, 4); // 默认 minRegionSize = 4
|
||||
|
||||
// 孤立 1 格 → 噪声
|
||||
expect(noise[0]).toBe(true);
|
||||
// 2×2 区域面积恰为 4 → 非噪声
|
||||
for (const idx of [5, 6, 9, 10]) expect(noise[idx]).toBe(false);
|
||||
// 白色大区域(11 格)→ 非噪声
|
||||
expect(noise[12]).toBe(false);
|
||||
});
|
||||
|
||||
it('支持自定义 minRegionSize', () => {
|
||||
const grid: Rgb3[] = Array.from({ length: 16 }, () => white);
|
||||
for (const idx of [5, 6, 9, 10]) grid[idx] = a1; // 2×2 = 4 格
|
||||
|
||||
// minRegionSize = 5 时,4 格区域也算噪声
|
||||
const noise = removeNoise(grid, 4, 5);
|
||||
expect(noise[5]).toBe(true);
|
||||
expect(noise[9]).toBe(true);
|
||||
// 白色区域 12 格仍非噪声
|
||||
expect(noise[0]).toBe(false);
|
||||
});
|
||||
|
||||
it('空网格返回空数组', () => {
|
||||
expect(removeNoise([], 0)).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* color-matching.ts(双色彩引擎选择器)测试 —
|
||||
* 验证引擎分发、通用最近邻搜索与 MARD 色板便捷函数。
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { colorDistance, findNearest, findNearestColorWithEngine } from '../color-matching.js';
|
||||
import { labDistance, srgbToLab } from '../lab.js';
|
||||
import { oklabDistance, srgbToOklab } from '../oklab.js';
|
||||
import { ciede2000 } from '../ciede2000.js';
|
||||
import { MARD_PALETTE, findNearestColor } from '../mard-palette.js';
|
||||
import type { Lab } from '../lab.js';
|
||||
|
||||
describe('colorDistance(引擎分发)', () => {
|
||||
const labA: Lab = [50.0, 2.49, -0.001];
|
||||
const labB: Lab = [50.0, -2.49, 0.0011];
|
||||
const oklabA = srgbToOklab([255, 0, 0]);
|
||||
const oklabB = srgbToOklab([0, 0, 255]);
|
||||
|
||||
it("'lab' 引擎转发到 labDistance", () => {
|
||||
expect(colorDistance('lab', labA, labB)).toBeCloseTo(labDistance(labA, labB), 10);
|
||||
});
|
||||
|
||||
it("'oklab' 引擎转发到 oklabDistance", () => {
|
||||
expect(colorDistance('oklab', oklabA, oklabB)).toBeCloseTo(oklabDistance(oklabA, oklabB), 10);
|
||||
});
|
||||
|
||||
it("'ciede2000' 引擎转发到 ciede2000", () => {
|
||||
expect(colorDistance('ciede2000', labA, labB)).toBeCloseTo(ciede2000(labA, labB), 10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findNearest(通用最近邻)', () => {
|
||||
it("'lab' 引擎在 MARD 色板中命中自身(距离 0)", () => {
|
||||
const target = MARD_PALETTE[0]!.lab;
|
||||
const { entry, distance } = findNearest('lab', target, MARD_PALETTE);
|
||||
expect(entry.code).toBe(MARD_PALETTE[0]!.code);
|
||||
expect(distance).toBe(0);
|
||||
});
|
||||
|
||||
it("'ciede2000' 引擎在 MARD 色板中命中自身(距离 0)", () => {
|
||||
const target = MARD_PALETTE[13]!.lab;
|
||||
const { entry, distance } = findNearest('ciede2000', target, MARD_PALETTE);
|
||||
expect(entry.code).toBe(MARD_PALETTE[13]!.code);
|
||||
expect(distance).toBeCloseTo(0, 6);
|
||||
});
|
||||
|
||||
it("'oklab' 引擎接受 OKLab 空间输入(同空间原语)", () => {
|
||||
const pick = MARD_PALETTE[40]!;
|
||||
const target = srgbToOklab(pick.rgb);
|
||||
// 先把色板条目转换到 OKLab,再搜索。
|
||||
const oklabPalette = MARD_PALETTE.map((e) => ({ code: e.code, lab: srgbToOklab(e.rgb) }));
|
||||
const { entry, distance } = findNearest('oklab', target, oklabPalette);
|
||||
expect(entry.code).toBe(pick.code);
|
||||
expect(distance).toBeLessThan(1e-3);
|
||||
});
|
||||
|
||||
it('支持仅含 lab 字段的任意色板', () => {
|
||||
const custom = [
|
||||
{ name: 'a', lab: [50, 0, 0] as Lab },
|
||||
{ name: 'b', lab: [10, 0, 0] as Lab },
|
||||
];
|
||||
const { entry } = findNearest('lab', [12, 0, 0], custom);
|
||||
expect(entry.name).toBe('b');
|
||||
});
|
||||
|
||||
it('空色板抛错', () => {
|
||||
expect(() => findNearest('lab', [50, 0, 0], [])).toThrow();
|
||||
});
|
||||
|
||||
it('返回的 distance 与 colorDistance 一致', () => {
|
||||
const target = MARD_PALETTE[7]!.lab;
|
||||
const { entry, distance } = findNearest('ciede2000', target, MARD_PALETTE);
|
||||
expect(distance).toBeCloseTo(colorDistance('ciede2000', target, entry.lab), 10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findNearestColorWithEngine(MARD 色板,引擎可选)', () => {
|
||||
it("默认 'lab' 引擎等价于 mard-palette 的 findNearestColor", () => {
|
||||
const target = MARD_PALETTE[5]!.lab;
|
||||
expect(findNearestColorWithEngine(target).code).toBe(findNearestColor(target).code);
|
||||
});
|
||||
|
||||
it("'ciede2000' 引擎对自身条目命中(距离近似 0)", () => {
|
||||
const pick = MARD_PALETTE[13]!;
|
||||
const match = findNearestColorWithEngine(pick.lab, MARD_PALETTE, 'ciede2000');
|
||||
expect(match.code).toBe(pick.code);
|
||||
});
|
||||
|
||||
it("'oklab' 引擎对自身条目命中(经 lab→sRGB→OKLab 转换)", () => {
|
||||
// 取若干彼此分离的色板条目:转换误差远小于条目间距,应命中自身。
|
||||
for (const idx of [0, 13, 40, 60]) {
|
||||
const pick = MARD_PALETTE[idx]!;
|
||||
const match = findNearestColorWithEngine(pick.lab, MARD_PALETTE, 'oklab');
|
||||
expect(match.code, `索引 ${idx} (${pick.code})`).toBe(pick.code);
|
||||
}
|
||||
});
|
||||
|
||||
it("'oklab' 引擎对纯红目标返回红色系条目", () => {
|
||||
// 目标 = 纯红的 CIE LAB,OKLab 引擎应命中红色主导的条目。
|
||||
const redLab = srgbToLab([255, 0, 0]);
|
||||
const match = findNearestColorWithEngine(redLab, MARD_PALETTE, 'oklab');
|
||||
expect(match.rgb[0]).toBeGreaterThan(match.rgb[2]); // 红色主导
|
||||
expect(match.rgb[0]).toBeGreaterThan(150);
|
||||
});
|
||||
|
||||
it('空色板抛错', () => {
|
||||
expect(() => findNearestColorWithEngine([50, 0, 0], [], 'lab')).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,182 @@
|
||||
/**
|
||||
* Dithering module tests — Floyd–Steinberg error diffusion & Bayer ordered
|
||||
* dithering: palette-index validity, palette-only outputs, and gradient
|
||||
* mixing, plus processImage pipeline wiring.
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { floydSteinberg, orderedDither } from '../dither.js';
|
||||
import type { DitherPaletteEntry } from '../dither.js';
|
||||
import type { RGB } from '../lab.js';
|
||||
import { processImage } from '../image-processor.js';
|
||||
import type { ProcessOptions } from '../image-processor.js';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 测试工具
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** 2 色色板:纯黑 + 纯白。 */
|
||||
const blackWhitePalette: DitherPaletteEntry[] = [
|
||||
{ rgb: [0, 0, 0] },
|
||||
{ rgb: [255, 255, 255] },
|
||||
];
|
||||
|
||||
/** 生成 row-major 的灰度网格。 */
|
||||
function grayGrid(width: number, height: number): RGB[] {
|
||||
const grid: RGB[] = [];
|
||||
for (let i = 0; i < width * height; i++) {
|
||||
const v = Math.round((i / (width * height - 1)) * 255);
|
||||
grid.push([v, v, v]);
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
/** 断言索引数组全部落在色板范围内。 */
|
||||
function expectValidIndices(indices: number[], paletteLength: number): void {
|
||||
expect(indices).toHaveLength(indices.length);
|
||||
for (const idx of indices) {
|
||||
expect(idx).toBeGreaterThanOrEqual(0);
|
||||
expect(idx).toBeLessThan(paletteLength);
|
||||
}
|
||||
}
|
||||
|
||||
/** 断言结果只用色板颜色:每个索引都能在色板中取到颜色。 */
|
||||
function expectOnlyPaletteColors(indices: number[], palette: readonly DitherPaletteEntry[]): void {
|
||||
for (const idx of indices) {
|
||||
expect(palette[idx]).toBeDefined();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// floydSteinberg
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('floydSteinberg', () => {
|
||||
it('2×2 渐变网格产生的索引全部在色板范围内', () => {
|
||||
const grid: RGB[] = [
|
||||
[0, 0, 0],
|
||||
[85, 85, 85],
|
||||
[170, 170, 170],
|
||||
[255, 255, 255],
|
||||
];
|
||||
const indices = floydSteinberg(grid, 2, 2, blackWhitePalette);
|
||||
expect(indices).toHaveLength(4);
|
||||
expectValidIndices(indices, blackWhitePalette.length);
|
||||
});
|
||||
|
||||
it('结果只用色板颜色(每个输出索引都有效)', () => {
|
||||
const grid = grayGrid(8, 8);
|
||||
const indices = floydSteinberg(grid, 8, 8, blackWhitePalette);
|
||||
expectOnlyPaletteColors(indices, blackWhitePalette);
|
||||
});
|
||||
|
||||
it('8×8 黑→白渐变 + 2 色色板:产生混合索引(非全同)', () => {
|
||||
const grid = grayGrid(8, 8);
|
||||
const indices = floydSteinberg(grid, 8, 8, blackWhitePalette);
|
||||
const distinct = new Set(indices);
|
||||
expect(distinct.size).toBeGreaterThan(1);
|
||||
// 误差扩散应同时用上黑与白两个索引。
|
||||
expect(distinct.has(0)).toBe(true);
|
||||
expect(distinct.has(1)).toBe(true);
|
||||
});
|
||||
|
||||
it('空色板抛错', () => {
|
||||
expect(() => floydSteinberg(grayGrid(2, 2), 2, 2, [])).toThrow();
|
||||
});
|
||||
|
||||
it('像素数量与宽高不一致时抛错', () => {
|
||||
expect(() => floydSteinberg(grayGrid(4, 4), 2, 2, blackWhitePalette)).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// orderedDither
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('orderedDither', () => {
|
||||
it('Bayer 4×4 有序抖动的索引全部有效(只落在色板内)', () => {
|
||||
const grid = grayGrid(8, 8);
|
||||
const indices = orderedDither(grid, 8, 8, blackWhitePalette);
|
||||
expect(indices).toHaveLength(64);
|
||||
expectValidIndices(indices, blackWhitePalette.length);
|
||||
expectOnlyPaletteColors(indices, blackWhitePalette);
|
||||
});
|
||||
|
||||
it('8×8 黑→白渐变 + 2 色色板:产生混合索引(非全同)', () => {
|
||||
const grid = grayGrid(8, 8);
|
||||
const indices = orderedDither(grid, 8, 8, blackWhitePalette);
|
||||
const distinct = new Set(indices);
|
||||
expect(distinct.size).toBeGreaterThan(1);
|
||||
expect(distinct.has(0)).toBe(true);
|
||||
expect(distinct.has(1)).toBe(true);
|
||||
});
|
||||
|
||||
it('空色板抛错', () => {
|
||||
expect(() => orderedDither(grayGrid(2, 2), 2, 2, [])).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// image-processor.ts 集成(dither 接线)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function buildTestImageData(size: number, paint: (x: number, y: number) => [number, number, number]): ImageData {
|
||||
const data = new Uint8ClampedArray(size * size * 4);
|
||||
for (let y = 0; y < size; y++) {
|
||||
for (let x = 0; x < size; x++) {
|
||||
const [r, g, b] = paint(x, y);
|
||||
const i = (y * size + x) * 4;
|
||||
data[i] = r;
|
||||
data[i + 1] = g;
|
||||
data[i + 2] = b;
|
||||
data[i + 3] = 255;
|
||||
}
|
||||
}
|
||||
return { data, width: size, height: size } as ImageData;
|
||||
}
|
||||
|
||||
describe('processImage with dither option', () => {
|
||||
const baseOptions: ProcessOptions = {
|
||||
gridSize: 8,
|
||||
maxColors: 2,
|
||||
removeBg: false,
|
||||
bgThreshold: 20,
|
||||
};
|
||||
|
||||
// 16×16 水平渐变图像:左上角为白,向右下渐变为黑。
|
||||
const gradientImage = buildTestImageData(16, (x, y) => {
|
||||
const v = Math.round(((x + y) / 30) * 255);
|
||||
return [v, v, v];
|
||||
});
|
||||
|
||||
it('dither 缺省时行为不变(默认 none,全部非空、索引仍有效)', () => {
|
||||
const result = processImage(gradientImage, baseOptions);
|
||||
expect(result.grid).toHaveLength(64);
|
||||
expect(result.totalBeads).toBe(64);
|
||||
for (const cell of result.grid) {
|
||||
expect(cell.empty).toBe(false);
|
||||
expect(cell.code).toMatch(/^[A-Z]\d{1,2}$/);
|
||||
}
|
||||
});
|
||||
|
||||
it("dither: 'floyd-steinberg' 产生完整有效的色板网格", () => {
|
||||
const result = processImage(gradientImage, { ...baseOptions, dither: 'floyd-steinberg' });
|
||||
expect(result.grid).toHaveLength(64);
|
||||
expect(result.totalBeads).toBe(64);
|
||||
for (const cell of result.grid) {
|
||||
expect(cell.empty).toBe(false);
|
||||
expect(cell.code).toMatch(/^[A-Z]\d{1,2}$/);
|
||||
}
|
||||
});
|
||||
|
||||
it("dither: 'ordered' 产生完整有效的色板网格", () => {
|
||||
const result = processImage(gradientImage, { ...baseOptions, dither: 'ordered' });
|
||||
expect(result.grid).toHaveLength(64);
|
||||
expect(result.totalBeads).toBe(64);
|
||||
for (const cell of result.grid) {
|
||||
expect(cell.empty).toBe(false);
|
||||
expect(cell.code).toMatch(/^[A-Z]\d{1,2}$/);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* exclusion.ts 测试 — 颜色排除与重映射(含 processImage 集成)。
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { excludeAndRemap, restoreExcluded } from '../exclusion.js';
|
||||
import { MARD_PALETTE, nearestColorFromRgb, type MardColor } from '../mard-palette.js';
|
||||
import { processImage } from '../image-processor.js';
|
||||
import type { Lab3, Rgb3 } from '../lab.js';
|
||||
|
||||
// 迷你色板:红色与蓝色相距极远
|
||||
const redEntry: MardColor = {
|
||||
code: 'R1',
|
||||
hex: '#E00000',
|
||||
rgb: [224, 0, 0],
|
||||
lab: [53.24, 80.09, 67.2],
|
||||
};
|
||||
const blueEntry: MardColor = {
|
||||
code: 'B1',
|
||||
hex: '#0000E0',
|
||||
rgb: [0, 0, 224],
|
||||
lab: [30, 68, -112],
|
||||
};
|
||||
const miniPalette: MardColor[] = [redEntry, blueEntry];
|
||||
const redLab: Lab3 = [53.24, 80.09, 67.2]; // 与 redEntry 完全一致
|
||||
|
||||
describe('excludeAndRemap', () => {
|
||||
it('最近色号未被排除时直接返回', () => {
|
||||
const result = excludeAndRemap(redLab, miniPalette, new Set(['X9']));
|
||||
expect(result.code).toBe('R1');
|
||||
expect(result.lab).toEqual(redEntry.lab);
|
||||
});
|
||||
|
||||
it('最近色号被排除时返回最近的未排除色号', () => {
|
||||
const result = excludeAndRemap(redLab, miniPalette, new Set(['R1']));
|
||||
expect(result.code).toBe('B1');
|
||||
});
|
||||
|
||||
it('全部色号被排除时兜底返回最近的色号', () => {
|
||||
const result = excludeAndRemap(redLab, miniPalette, new Set(['R1', 'B1']));
|
||||
expect(result.code).toBe('R1');
|
||||
});
|
||||
|
||||
it('空排除集合等价于普通最近邻', () => {
|
||||
const result = excludeAndRemap(redLab, miniPalette, new Set());
|
||||
expect(result.code).toBe('R1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('restoreExcluded', () => {
|
||||
it('把使用被排除色号的格子恢复为最近的未排除色号,其余格子不动', () => {
|
||||
const excluded = new Set([MARD_PALETTE[0]!.code, MARD_PALETTE[1]!.code]);
|
||||
const cells: { code: string; lab: Lab3; rgb?: Rgb3 }[] = [
|
||||
{
|
||||
code: MARD_PALETTE[0]!.code,
|
||||
lab: MARD_PALETTE[0]!.lab,
|
||||
rgb: MARD_PALETTE[0]!.rgb,
|
||||
},
|
||||
{ code: 'X9', lab: [90, 0, 0] }, // 未排除色号 → 不应被改动
|
||||
];
|
||||
|
||||
const restored = restoreExcluded(cells, excluded);
|
||||
|
||||
expect(restored).toEqual([0]);
|
||||
// 被恢复的格子不再使用被排除的色号,且结果与 excludeAndRemap 一致
|
||||
expect(excluded.has(cells[0]!.code)).toBe(false);
|
||||
expect(cells[0]!.code).not.toBe(MARD_PALETTE[0]!.code);
|
||||
expect(cells[0]!.code).toBe(
|
||||
excludeAndRemap(MARD_PALETTE[0]!.lab, MARD_PALETTE, excluded).code,
|
||||
);
|
||||
// 未排除的格子完全不受影响
|
||||
expect(cells[1]!.code).toBe('X9');
|
||||
});
|
||||
|
||||
it('空排除集合不修改任何格子', () => {
|
||||
const cells: { code: string; lab: Lab3 }[] = [{ code: 'A1', lab: [90, 0, 0] }];
|
||||
expect(restoreExcluded(cells, new Set())).toEqual([]);
|
||||
expect(cells[0]!.code).toBe('A1');
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// processImage 集成
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function buildTestImageData(
|
||||
size: number,
|
||||
paint: (x: number, y: number) => [number, number, number],
|
||||
): ImageData {
|
||||
const data = new Uint8ClampedArray(size * size * 4);
|
||||
for (let y = 0; y < size; y++) {
|
||||
for (let x = 0; x < size; x++) {
|
||||
const [r, g, b] = paint(x, y);
|
||||
const i = (y * size + x) * 4;
|
||||
data[i] = r;
|
||||
data[i + 1] = g;
|
||||
data[i + 2] = b;
|
||||
data[i + 3] = 255;
|
||||
}
|
||||
}
|
||||
return { data, width: size, height: size } as ImageData;
|
||||
}
|
||||
|
||||
/** 16×16 图像:白底 + 左上角 4×4 纯红块。 */
|
||||
function redOnWhiteImage(): ImageData {
|
||||
return buildTestImageData(16, (x, y) => (x < 4 && y < 4 ? [255, 0, 0] : [255, 255, 255]));
|
||||
}
|
||||
|
||||
describe('processImage 集成', () => {
|
||||
const baseOptions = {
|
||||
gridSize: 4,
|
||||
maxColors: 8,
|
||||
removeBg: false,
|
||||
bgThreshold: 20,
|
||||
};
|
||||
|
||||
it('excludedCodes 排除目标色号后,色板与格子中不再出现该色号', () => {
|
||||
const baseline = processImage(redOnWhiteImage(), baseOptions);
|
||||
const redTarget = nearestColorFromRgb([255, 0, 0]).code;
|
||||
// 基线确实使用了该色号
|
||||
expect(baseline.palette.some((e) => e.code === redTarget)).toBe(true);
|
||||
|
||||
const result = processImage(redOnWhiteImage(), {
|
||||
...baseOptions,
|
||||
excludedCodes: [redTarget],
|
||||
});
|
||||
|
||||
// 排除生效:色板与所有非空格子均无该色号
|
||||
expect(result.palette.some((e) => e.code === redTarget)).toBe(false);
|
||||
expect(result.grid.every((c) => c.empty || c.code !== redTarget)).toBe(true);
|
||||
// 输出色板不增长,珠子总数不变
|
||||
expect(result.palette.length).toBeLessThanOrEqual(baseline.palette.length);
|
||||
expect(result.totalBeads).toBe(baseline.totalBeads);
|
||||
});
|
||||
|
||||
it('空排除列表与默认行为完全一致(向后兼容)', () => {
|
||||
const plain = processImage(redOnWhiteImage(), baseOptions);
|
||||
const empty = processImage(redOnWhiteImage(), { ...baseOptions, excludedCodes: [] });
|
||||
expect(empty.palette).toEqual(plain.palette);
|
||||
expect(empty.grid).toEqual(plain.grid);
|
||||
});
|
||||
|
||||
it('cleanup 参数不破坏管线(合并正常执行且网格大小不变)', () => {
|
||||
const result = processImage(redOnWhiteImage(), {
|
||||
...baseOptions,
|
||||
cleanup: { mergeThreshold: 24, maxMergeDistance: 24 },
|
||||
});
|
||||
expect(result.grid).toHaveLength(16);
|
||||
expect(result.totalBeads).toBe(16);
|
||||
expect(result.grid.every((c) => !c.empty)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,262 @@
|
||||
/**
|
||||
* 网格编辑工具(grid-edit.ts)测试。
|
||||
*
|
||||
* 覆盖:旋转 90° 在 2×2 上的位置交换、4×90° 恒等、180° 与 90°×2 等价;
|
||||
* 水平/垂直镜像位置正确、两次镜像复原;对角翻转两次复原;
|
||||
* EditHistory 的 push/undo/redo 序列、limit 截断、undo 越界返回 null,
|
||||
* 以及快照不可变性(push 后修改原状态不污染历史)。
|
||||
*/
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import {
|
||||
EditHistory,
|
||||
flipGrid,
|
||||
gridStateToProcessResult,
|
||||
mirrorGrid,
|
||||
resultToGridState,
|
||||
rotateGrid,
|
||||
type GridState,
|
||||
} from '../grid-edit.js';
|
||||
import type { ProcessResult } from '../image-processor.js';
|
||||
|
||||
/** 构造 size×size 网格;`code` 为 undefined 的格子视为空单元格。 */
|
||||
function makeGrid(
|
||||
size: number,
|
||||
colorAt: (x: number, y: number) => [number, number, number],
|
||||
codeAt?: (x: number, y: number) => string | undefined,
|
||||
): GridState {
|
||||
const grid = [];
|
||||
for (let y = 0; y < size; y++) {
|
||||
for (let x = 0; x < size; x++) {
|
||||
const rgb = colorAt(x, y);
|
||||
const code = codeAt ? codeAt(x, y) : undefined;
|
||||
grid.push(code === undefined ? { rgb } : { rgb, code });
|
||||
}
|
||||
}
|
||||
return { grid, size };
|
||||
}
|
||||
|
||||
/** 2×2 基准网格:a b / c d。 */
|
||||
function twoByTwo(): GridState {
|
||||
return makeGrid(
|
||||
2,
|
||||
(x, y) => (y === 0 ? (x === 0 ? [255, 0, 0] : [0, 255, 0]) : x === 0 ? [0, 0, 255] : [255, 255, 0]),
|
||||
(x, y) => ['a', 'b', 'c', 'd'][y * 2 + x],
|
||||
);
|
||||
}
|
||||
|
||||
/** 读取 (x, y) 处的格子。 */
|
||||
function cellAt(state: GridState, x: number, y: number) {
|
||||
return state.grid[y * state.size + x]!;
|
||||
}
|
||||
|
||||
describe('rotateGrid 旋转', () => {
|
||||
it('2×2 顺时针旋转 90°:a b / c d → c a / d b', () => {
|
||||
const rotated = rotateGrid(twoByTwo(), 90);
|
||||
expect(cellAt(rotated, 0, 0).code).toBe('c');
|
||||
expect(cellAt(rotated, 1, 0).code).toBe('a');
|
||||
expect(cellAt(rotated, 0, 1).code).toBe('d');
|
||||
expect(cellAt(rotated, 1, 1).code).toBe('b');
|
||||
});
|
||||
|
||||
it('4 次 90° 旋转等于恒等(rgb 与 code 均复原)', () => {
|
||||
const original = twoByTwo();
|
||||
let rotated = original;
|
||||
for (let i = 0; i < 4; i++) rotated = rotateGrid(rotated, 90);
|
||||
expect(rotated).toEqual(original);
|
||||
});
|
||||
|
||||
it('180° 旋转与连续两次 90° 等价', () => {
|
||||
const original = twoByTwo();
|
||||
expect(rotateGrid(original, 180)).toEqual(rotateGrid(rotateGrid(original, 90), 90));
|
||||
});
|
||||
|
||||
it('90° 与 270° 互为逆操作', () => {
|
||||
const original = twoByTwo();
|
||||
expect(rotateGrid(rotateGrid(original, 90), 270)).toEqual(original);
|
||||
});
|
||||
|
||||
it('空单元格(无 code)在旋转后仍为空单元格', () => {
|
||||
const grid = makeGrid(2, (x, y) => [x * 10, y * 10, 5], (x, y) => (x === 0 && y === 0 ? undefined : 'X'));
|
||||
const rotated = rotateGrid(grid, 90);
|
||||
// (0,0) 旋转后落在 (1,0);它应保留 code undefined
|
||||
expect(cellAt(rotated, 1, 0).code).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('mirrorGrid 镜像', () => {
|
||||
it('水平镜像(上下交换):a b / c d → c d / a b', () => {
|
||||
const mirrored = mirrorGrid(twoByTwo(), 'horizontal');
|
||||
expect(cellAt(mirrored, 0, 0).code).toBe('c');
|
||||
expect(cellAt(mirrored, 1, 0).code).toBe('d');
|
||||
expect(cellAt(mirrored, 0, 1).code).toBe('a');
|
||||
expect(cellAt(mirrored, 1, 1).code).toBe('b');
|
||||
});
|
||||
|
||||
it('垂直镜像(左右交换):a b / c d → b a / d c', () => {
|
||||
const mirrored = mirrorGrid(twoByTwo(), 'vertical');
|
||||
expect(cellAt(mirrored, 0, 0).code).toBe('b');
|
||||
expect(cellAt(mirrored, 1, 0).code).toBe('a');
|
||||
expect(cellAt(mirrored, 0, 1).code).toBe('d');
|
||||
expect(cellAt(mirrored, 1, 1).code).toBe('c');
|
||||
});
|
||||
|
||||
it('两次同轴镜像复原为原网格', () => {
|
||||
const original = makeGrid(3, (x, y) => [x * 40, y * 40, 20], (x, y) => `C${x}${y}`);
|
||||
expect(mirrorGrid(mirrorGrid(original, 'horizontal'), 'horizontal')).toEqual(original);
|
||||
expect(mirrorGrid(mirrorGrid(original, 'vertical'), 'vertical')).toEqual(original);
|
||||
});
|
||||
});
|
||||
|
||||
describe('flipGrid 对角翻转', () => {
|
||||
it('主对角线翻转即转置:2×2 上 a b / c d → a c / b d', () => {
|
||||
const flipped = flipGrid(twoByTwo());
|
||||
expect(cellAt(flipped, 0, 1).code).toBe('b');
|
||||
expect(cellAt(flipped, 1, 0).code).toBe('c');
|
||||
});
|
||||
|
||||
it('两次对角翻转复原为原网格', () => {
|
||||
const original = makeGrid(3, (x, y) => [x * 30, y * 30, 10], (x, y) => `C${x}${y}`);
|
||||
expect(flipGrid(flipGrid(original))).toEqual(original);
|
||||
});
|
||||
});
|
||||
|
||||
describe('EditHistory 撤销/重做', () => {
|
||||
it('push/undo/redo 基本序列', () => {
|
||||
const history = new EditHistory();
|
||||
const s0 = twoByTwo();
|
||||
const s1 = rotateGrid(s0, 90);
|
||||
const s2 = rotateGrid(s1, 90);
|
||||
|
||||
expect(history.canUndo).toBe(false);
|
||||
history.push(s0);
|
||||
history.push(s1);
|
||||
history.push(s2);
|
||||
expect(history.canUndo).toBe(true);
|
||||
|
||||
expect(history.undo()).toEqual(s1);
|
||||
expect(history.canRedo).toBe(true);
|
||||
expect(history.redo()).toEqual(s2);
|
||||
expect(history.canRedo).toBe(false);
|
||||
});
|
||||
|
||||
it('undo 越过起点返回 null,且不改变 canUndo/canRedo 状态', () => {
|
||||
const history = new EditHistory();
|
||||
const s0 = twoByTwo();
|
||||
const s1 = rotateGrid(s0, 90);
|
||||
history.push(s0); // Edit1 完成后的状态
|
||||
history.push(s1); // Edit2 完成后的状态 → 可撤回到 s0
|
||||
expect(history.undo()).toEqual(s0);
|
||||
expect(history.undo()).toBeNull();
|
||||
expect(history.canUndo).toBe(false);
|
||||
// redo 仍可用(刚撤销过的状态还在 future 栈中)
|
||||
expect(history.canRedo).toBe(true);
|
||||
});
|
||||
|
||||
it('limit 截断最旧的快照', () => {
|
||||
const history = new EditHistory(2);
|
||||
const s0 = twoByTwo();
|
||||
const s1 = rotateGrid(s0, 90);
|
||||
const s2 = rotateGrid(s1, 90);
|
||||
const s3 = rotateGrid(s2, 90);
|
||||
|
||||
history.push(s0);
|
||||
history.push(s1);
|
||||
history.push(s2);
|
||||
history.push(s3);
|
||||
|
||||
// 最旧的 s0 已被丢弃:只能撤回到 s1
|
||||
expect(history.undo()).toEqual(s2);
|
||||
expect(history.undo()).toEqual(s1);
|
||||
expect(history.undo()).toBeNull();
|
||||
});
|
||||
|
||||
it('push 后修改原状态不污染历史快照(不可变性)', () => {
|
||||
const history = new EditHistory();
|
||||
const s = twoByTwo();
|
||||
const s2 = rotateGrid(s, 90);
|
||||
history.push(s); // Edit1 完成后的状态
|
||||
history.push(s2); // Edit2 完成后的状态 → 可撤回到 s
|
||||
|
||||
// 修改 push 时传入的状态
|
||||
s.grid[0]!.rgb[0] = 99;
|
||||
s.grid[1] = { rgb: [1, 2, 3], code: 'T01' };
|
||||
// 修改最近一次 push 的状态(undo 后可通过 redo 恢复)
|
||||
s2.grid[0]!.rgb[0] = 77;
|
||||
|
||||
const restored = history.undo();
|
||||
expect(restored).not.toBeNull();
|
||||
expect(restored!.grid[0]!.rgb[0]).not.toBe(99);
|
||||
expect(restored!.grid[0]).toEqual({ rgb: [255, 0, 0], code: 'a' });
|
||||
expect(restored!.grid[1]).toEqual({ rgb: [0, 255, 0], code: 'b' });
|
||||
|
||||
const redone = history.redo();
|
||||
expect(redone).not.toBeNull();
|
||||
expect(redone!.grid[0]!.rgb[0]).not.toBe(77);
|
||||
});
|
||||
|
||||
it('push 后 future 栈被清空(新编辑使重做失效)', () => {
|
||||
const history = new EditHistory();
|
||||
const s0 = twoByTwo();
|
||||
const s1 = rotateGrid(s0, 90);
|
||||
const s2 = rotateGrid(s1, 90);
|
||||
|
||||
history.push(s0);
|
||||
history.push(s1);
|
||||
history.undo();
|
||||
expect(history.canRedo).toBe(true);
|
||||
|
||||
history.push(s2); // 新编辑
|
||||
expect(history.canRedo).toBe(false);
|
||||
});
|
||||
|
||||
it('clear 重置全部状态', () => {
|
||||
const history = new EditHistory();
|
||||
history.push(twoByTwo());
|
||||
history.clear();
|
||||
expect(history.canUndo).toBe(false);
|
||||
expect(history.canRedo).toBe(false);
|
||||
expect(history.undo()).toBeNull();
|
||||
expect(history.redo()).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('ProcessResult ↔ GridState 互转', () => {
|
||||
it('gridStateToProcessResult 按编辑后的网格重算每色颗数与总数', () => {
|
||||
// 2×2:a=2 颗、b=1 颗、1 个空单元格
|
||||
const state = makeGrid(2, (x, y) => [x * 50, y * 50, 25], (x, y) => (x === 1 && y === 1 ? undefined : x === 0 ? 'A01' : 'B02'));
|
||||
|
||||
const result = gridStateToProcessResult(state);
|
||||
|
||||
expect(result.totalBeads).toBe(3);
|
||||
expect(result.palette).toHaveLength(2);
|
||||
expect(result.palette[0]).toMatchObject({ code: 'A01', count: 2 });
|
||||
expect(result.palette[1]).toMatchObject({ code: 'B02', count: 1 });
|
||||
expect(result.grid.filter((c) => c.empty)).toHaveLength(1);
|
||||
// 空单元格的 code 还原为 '',非空格保留 code
|
||||
expect(result.grid.find((c) => c.empty)!.code).toBe('');
|
||||
expect(result.grid.find((c) => !c.empty)!.code).toBeTruthy();
|
||||
});
|
||||
|
||||
it('resultToGridState 与 gridStateToProcessResult 往返保持 rgb/code/empty', () => {
|
||||
const result: ProcessResult = {
|
||||
grid: [
|
||||
{ x: 0, y: 0, rgb: [255, 0, 0], lab: [50, 70, 60], code: 'R01', empty: false },
|
||||
{ x: 1, y: 0, rgb: [0, 255, 0], lab: [80, -60, 70], code: 'G02', empty: false },
|
||||
{ x: 0, y: 1, rgb: [244, 241, 232], lab: [95, 0, 5], code: '', empty: true },
|
||||
{ x: 1, y: 1, rgb: [0, 0, 255], lab: [30, 60, -90], code: 'B03', empty: false },
|
||||
],
|
||||
palette: [],
|
||||
totalBeads: 3,
|
||||
};
|
||||
|
||||
const state = resultToGridState(result, 2);
|
||||
const rebuilt = gridStateToProcessResult(state);
|
||||
|
||||
expect(rebuilt.grid.map((c) => ({ rgb: c.rgb, code: c.code, empty: c.empty }))).toEqual(
|
||||
result.grid.map((c) => ({ rgb: c.rgb, code: c.code, empty: c.empty })),
|
||||
);
|
||||
expect(rebuilt.totalBeads).toBe(3);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* OKLab 模块测试 — 参考值来自 Björn Ottosson 的 OKLab 规范
|
||||
* (https://bottosson.github.io/posts/oklab/,sRGB 三原色与
|
||||
* 中性色的公开参考值),往返测试验证 srgbToOklab → oklabToSrgb ≈ 恒等。
|
||||
*
|
||||
* 标度约定:OKLab L/a/b 均为 0..1 量级(sRGB 白 = [1, 0, 0]),
|
||||
* 与 CIE LAB(L∈[0,100])不同;sRGB 通道恒为 0..255。
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { srgbToOklab, oklabToSrgb, oklabDistance } from '../oklab.js';
|
||||
import type { RGB } from '../lab.js';
|
||||
|
||||
describe('srgbToOklab', () => {
|
||||
it('sRGB 白 → [1, 0, 0](0..1 标度)', () => {
|
||||
const lab = srgbToOklab([255, 255, 255]);
|
||||
expect(lab[0]).toBeCloseTo(1, 5);
|
||||
expect(lab[1]).toBeCloseTo(0, 5);
|
||||
expect(lab[2]).toBeCloseTo(0, 5);
|
||||
});
|
||||
|
||||
it('sRGB 黑 → [0, 0, 0]', () => {
|
||||
const lab = srgbToOklab([0, 0, 0]);
|
||||
expect(lab[0]).toBeCloseTo(0, 5);
|
||||
expect(lab[1]).toBeCloseTo(0, 5);
|
||||
expect(lab[2]).toBeCloseTo(0, 5);
|
||||
});
|
||||
|
||||
it('sRGB 红 → Ottosson 参考值', () => {
|
||||
const lab = srgbToOklab([255, 0, 0]);
|
||||
expect(lab[0]).toBeCloseTo(0.627955, 5);
|
||||
expect(lab[1]).toBeCloseTo(0.224863, 5);
|
||||
expect(lab[2]).toBeCloseTo(0.125846, 5);
|
||||
});
|
||||
|
||||
it('sRGB 绿 → Ottosson 参考值', () => {
|
||||
const lab = srgbToOklab([0, 255, 0]);
|
||||
expect(lab[0]).toBeCloseTo(0.86644, 5);
|
||||
expect(lab[1]).toBeCloseTo(-0.233888, 5);
|
||||
expect(lab[2]).toBeCloseTo(0.179498, 5);
|
||||
});
|
||||
|
||||
it('sRGB 蓝 → Ottosson 参考值', () => {
|
||||
const lab = srgbToOklab([0, 0, 255]);
|
||||
expect(lab[0]).toBeCloseTo(0.452013, 5);
|
||||
expect(lab[1]).toBeCloseTo(-0.032457, 5);
|
||||
expect(lab[2]).toBeCloseTo(-0.311528, 5);
|
||||
});
|
||||
|
||||
it('中灰 (128,128,128) 保持中性(a≈0, b≈0)且亮度 ≈0.6', () => {
|
||||
// 注意:OKLab 亮度非线性,L=0.5 对应 sRGB ≈99,故 128 灰的 L≈0.6
|
||||
//(与 CIE LAB 中 128 灰 L≈53.6 同理,均非 50)。
|
||||
const lab = srgbToOklab([128, 128, 128]);
|
||||
expect(Math.abs(lab[1])).toBeLessThan(1e-3);
|
||||
expect(Math.abs(lab[2])).toBeLessThan(1e-3);
|
||||
expect(lab[0]).toBeGreaterThan(0.59);
|
||||
expect(lab[0]).toBeLessThan(0.61);
|
||||
});
|
||||
});
|
||||
|
||||
describe('oklabToSrgb(往返)', () => {
|
||||
/** 断言 srgb → oklab → srgb 每通道误差 < 0.5(即 0.5/255)。 */
|
||||
function expectRoundTrip(rgb: RGB): void {
|
||||
const back = oklabToSrgb(srgbToOklab(rgb));
|
||||
const [r, g, b] = back;
|
||||
expect(Math.abs(r - rgb[0]!)).toBeLessThan(0.5);
|
||||
expect(Math.abs(g - rgb[1]!)).toBeLessThan(0.5);
|
||||
expect(Math.abs(b - rgb[2]!)).toBeLessThan(0.5);
|
||||
}
|
||||
|
||||
it('sRGB → OKLab → sRGB ≈ 恒等(白/黑/三原色/中间调)', () => {
|
||||
const samples: RGB[] = [
|
||||
[255, 255, 255],
|
||||
[0, 0, 0],
|
||||
[255, 0, 0],
|
||||
[0, 255, 0],
|
||||
[0, 0, 255],
|
||||
[128, 128, 128],
|
||||
[255, 128, 0],
|
||||
[75, 0, 130],
|
||||
[64, 224, 208],
|
||||
];
|
||||
for (const sample of samples) {
|
||||
expectRoundTrip(sample);
|
||||
}
|
||||
});
|
||||
|
||||
it('白与黑在 OKLab 中距离恰为 1([1,0,0] vs [0,0,0])', () => {
|
||||
expect(oklabDistance(srgbToOklab([255, 255, 255]), srgbToOklab([0, 0, 0]))).toBeCloseTo(1, 5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('oklabDistance', () => {
|
||||
it('相同颜色距离为 0', () => {
|
||||
const lab = srgbToOklab([10, 200, 30]);
|
||||
expect(oklabDistance(lab, lab)).toBe(0);
|
||||
});
|
||||
|
||||
it('对称', () => {
|
||||
const a = srgbToOklab([255, 0, 0]);
|
||||
const b = srgbToOklab([0, 0, 255]);
|
||||
expect(oklabDistance(a, b)).toBeCloseTo(oklabDistance(b, a), 10);
|
||||
});
|
||||
|
||||
it('不同颜色距离为正,且相似色更近', () => {
|
||||
const red = srgbToOklab([255, 0, 0]);
|
||||
const darkRed = srgbToOklab([200, 0, 0]);
|
||||
const blue = srgbToOklab([0, 0, 255]);
|
||||
expect(oklabDistance(red, blue)).toBeGreaterThan(0);
|
||||
expect(oklabDistance(red, blue)).toBeGreaterThan(oklabDistance(red, darkRed));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,407 @@
|
||||
/**
|
||||
* 多品牌拼豆色板 —— 基于「291 标准色中间层」的多品牌色号映射。
|
||||
*
|
||||
* 设计来源:Zippland/perler-beads 开源项目(参考文件 zippland-291标准色映射.json)。
|
||||
*
|
||||
* 核心思想:
|
||||
* 1. 291 个标准色作为「中间层」—— 每个标准色都带 5 个品牌的色号
|
||||
* (MARD / COCO / 漫漫 / 盼盼 / 咪小窝)。
|
||||
* 2. 任意输入颜色先在 CIE LAB 空间找到最近的标准色,再查表得到目标品牌的色号。
|
||||
* 3. 与现有 MARD 管线(mard-palette.ts)兼容:标准色的 rgb/lab 均由 hex 现算
|
||||
* (srgbToLab),例如 #FAF4C8 的 LAB 值与 MARD_PALETTE 首项完全一致,可交叉验证。
|
||||
*
|
||||
* 色数说明:Zippland 站方宣称各品牌色数为 MARD 168 / COCO 144 / 漫漫 144 /
|
||||
* 盼盼 96 / 咪小窝 96,但映射表实测各品牌覆盖 291 个标准色(漫漫 290 条,
|
||||
* 其中 #55514C 记 "-" 表示无对应色号)。本模块以实测数据为准。
|
||||
*/
|
||||
|
||||
import { srgbToLab, labDistance, type Lab3 } from './lab.js';
|
||||
|
||||
/** 品牌元信息。codes 为该品牌在 291 标准色映射中出现的全部色号(实测去重、排序)。 */
|
||||
export interface BrandInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
codes: string[];
|
||||
}
|
||||
|
||||
/** 291 标准色中间层条目:hex 与 brands 逐字来自参考 JSON,rgb/lab 由 hex 计算。 */
|
||||
export interface StandardColor {
|
||||
hex: string;
|
||||
rgb: [number, number, number];
|
||||
lab: [number, number, number];
|
||||
brands: Record<string, string>;
|
||||
}
|
||||
|
||||
/** 品牌色板条目:品牌色号 + 其标准色的 hex/rgb/lab。 */
|
||||
export interface BrandColor {
|
||||
code: string;
|
||||
hex: string;
|
||||
rgb: [number, number, number];
|
||||
lab: [number, number, number];
|
||||
}
|
||||
|
||||
/** 原始映射数据(逐字保留参考 JSON;"-" 表示该品牌无此色)。 */
|
||||
const SOURCE: readonly { hex: string; brands: Record<string, string> }[] = [
|
||||
{ hex: "#FAF4C8", brands: {"MARD":"A01","COCO":"E02","漫漫":"E2","盼盼":"65","咪小窝":"77"} },
|
||||
{ hex: "#FFFFD5", brands: {"MARD":"A02","COCO":"E01","漫漫":"B1","盼盼":"2","咪小窝":"2"} },
|
||||
{ hex: "#FEFF8B", brands: {"MARD":"A03","COCO":"E05","漫漫":"B2","盼盼":"28","咪小窝":"28"} },
|
||||
{ hex: "#FBED56", brands: {"MARD":"A04","COCO":"E07","漫漫":"B3","盼盼":"3","咪小窝":"3"} },
|
||||
{ hex: "#F4D738", brands: {"MARD":"A05","COCO":"D03","漫漫":"B4","盼盼":"74","咪小窝":"79"} },
|
||||
{ hex: "#FEAC4C", brands: {"MARD":"A06","COCO":"D05","漫漫":"B5","盼盼":"29","咪小窝":"29"} },
|
||||
{ hex: "#FE8B4C", brands: {"MARD":"A07","COCO":"D08","漫漫":"B6","盼盼":"4","咪小窝":"4"} },
|
||||
{ hex: "#FFDA45", brands: {"MARD":"A08","COCO":"E08","漫漫":"B10","盼盼":"88","咪小窝":"98"} },
|
||||
{ hex: "#FF995B", brands: {"MARD":"A09","COCO":"D06","漫漫":"B11","盼盼":"90","咪小窝":"97"} },
|
||||
{ hex: "#F77C31", brands: {"MARD":"A10","COCO":"D07","漫漫":"B12","盼盼":"89","咪小窝":"96"} },
|
||||
{ hex: "#FFDD99", brands: {"MARD":"A11","COCO":"D01","漫漫":"E11","盼盼":"100","咪小窝":"109"} },
|
||||
{ hex: "#FE9F72", brands: {"MARD":"A12","COCO":"K09","漫漫":"A18","盼盼":"99","咪小窝":"110"} },
|
||||
{ hex: "#FFC365", brands: {"MARD":"A13","COCO":"D04","漫漫":"B13","盼盼":"131","咪小窝":"116"} },
|
||||
{ hex: "#FD543D", brands: {"MARD":"A14","COCO":"C05","漫漫":"B14","盼盼":"138","咪小窝":"135"} },
|
||||
{ hex: "#FFF365", brands: {"MARD":"A15","COCO":"E04","漫漫":"B15","盼盼":"150","咪小窝":"150"} },
|
||||
{ hex: "#FFFF9F", brands: {"MARD":"A16","COCO":"E03","漫漫":"IC04","盼盼":"216","咪小窝":"216"} },
|
||||
{ hex: "#FFE36E", brands: {"MARD":"A17","COCO":"E06","漫漫":"IC9","盼盼":"213","咪小窝":"213"} },
|
||||
{ hex: "#FEBE7D", brands: {"MARD":"A18","COCO":"D02","漫漫":"IC14","盼盼":"223","咪小窝":"208"} },
|
||||
{ hex: "#FD7C72", brands: {"MARD":"A19","COCO":"K10","漫漫":"IC15","盼盼":"218","咪小窝":"218"} },
|
||||
{ hex: "#FFD568", brands: {"MARD":"A20","COCO":"E09","漫漫":"Q6","盼盼":"242","咪小窝":"242"} },
|
||||
{ hex: "#FFE395", brands: {"MARD":"A21","COCO":"E10","漫漫":"R07","盼盼":"276","咪小窝":"261"} },
|
||||
{ hex: "#F4F57D", brands: {"MARD":"A22","COCO":"E11","漫漫":"R06","盼盼":"270","咪小窝":"255"} },
|
||||
{ hex: "#E6C9B7", brands: {"MARD":"A23","COCO":"E12","漫漫":"R08","盼盼":"274","咪小窝":"259"} },
|
||||
{ hex: "#F7F8A2", brands: {"MARD":"A24","COCO":"E13","漫漫":"G3","盼盼":"288","咪小窝":"273"} },
|
||||
{ hex: "#FFD67D", brands: {"MARD":"A25","COCO":"E14","漫漫":"G4","盼盼":"289","咪小窝":"274"} },
|
||||
{ hex: "#FFC830", brands: {"MARD":"A26","COCO":"E15","漫漫":"G5","盼盼":"290","咪小窝":"275"} },
|
||||
{ hex: "#E6EE31", brands: {"MARD":"B01","COCO":"F05","漫漫":"C1","盼盼":"48","咪小窝":"48"} },
|
||||
{ hex: "#63F347", brands: {"MARD":"B02","COCO":"F08","漫漫":"C2","盼盼":"33","咪小窝":"33"} },
|
||||
{ hex: "#9EF780", brands: {"MARD":"B03","COCO":"F04","漫漫":"C7","盼盼":"26","咪小窝":"26"} },
|
||||
{ hex: "#5DE035", brands: {"MARD":"B04","COCO":"F09","漫漫":"C3","盼盼":"66","咪小窝":"78"} },
|
||||
{ hex: "#35E352", brands: {"MARD":"B05","COCO":"F10","漫漫":"C4","盼盼":"39","咪小窝":"39"} },
|
||||
{ hex: "#65E2A6", brands: {"MARD":"B06","COCO":"G04","漫漫":"C9","盼盼":"11","咪小窝":"11"} },
|
||||
{ hex: "#3DAF80", brands: {"MARD":"B07","COCO":"G05","漫漫":"C10","盼盼":"44","咪小窝":"44"} },
|
||||
{ hex: "#1C9C4F", brands: {"MARD":"B08","COCO":"F11","漫漫":"C5","盼盼":"10","咪小窝":"10"} },
|
||||
{ hex: "#27523A", brands: {"MARD":"B09","COCO":"F16","漫漫":"C6","盼盼":"79","咪小窝":"84"} },
|
||||
{ hex: "#95D3C2", brands: {"MARD":"B10","COCO":"G03","漫漫":"C11","盼盼":"96","咪小窝":"100"} },
|
||||
{ hex: "#5D722A", brands: {"MARD":"B11","COCO":"F14","漫漫":"C12","盼盼":"97","咪小窝":"99"} },
|
||||
{ hex: "#166F41", brands: {"MARD":"B12","COCO":"F12","漫漫":"C13","盼盼":"106","咪小窝":"111"} },
|
||||
{ hex: "#CAEB7B", brands: {"MARD":"B13","COCO":"F02","漫漫":"C14","盼盼":"128","咪小窝":"119"} },
|
||||
{ hex: "#ADE946", brands: {"MARD":"B14","COCO":"F06","漫漫":"C15","盼盼":"129","咪小窝":"117"} },
|
||||
{ hex: "#2E5132", brands: {"MARD":"B15","COCO":"F15","漫漫":"C16","盼盼":"130","咪小窝":"122"} },
|
||||
{ hex: "#C5ED9C", brands: {"MARD":"B16","COCO":"F03","漫漫":"C17","盼盼":"141","咪小窝":"133"} },
|
||||
{ hex: "#9BB13A", brands: {"MARD":"B17","COCO":"F13","漫漫":"C18","盼盼":"142","咪小窝":"141"} },
|
||||
{ hex: "#E6EE49", brands: {"MARD":"B18","COCO":"F07","漫漫":"C19","盼盼":"147","咪小窝":"147"} },
|
||||
{ hex: "#24B88C", brands: {"MARD":"B19","COCO":"G06","漫漫":"DH15","盼盼":"191","咪小窝":"174"} },
|
||||
{ hex: "#C2F0CC", brands: {"MARD":"B20","COCO":"G02","漫漫":"DH10","盼盼":"192","咪小窝":"175"} },
|
||||
{ hex: "#156A6B", brands: {"MARD":"B21","COCO":"G07","漫漫":"DH2","盼盼":"207","咪小窝":"194"} },
|
||||
{ hex: "#0B3C43", brands: {"MARD":"B22","COCO":"G08","漫漫":"DH7","盼盼":"206","咪小窝":"193"} },
|
||||
{ hex: "#303A21", brands: {"MARD":"B23","COCO":"F17","漫漫":"DH12","盼盼":"205","咪小窝":"192"} },
|
||||
{ hex: "#EEFCA5", brands: {"MARD":"B24","COCO":"F01","漫漫":"IC5","盼盼":"222","咪小窝":"207"} },
|
||||
{ hex: "#4E846D", brands: {"MARD":"B25","COCO":"F18","漫漫":"Q13","盼盼":"240","咪小窝":"240"} },
|
||||
{ hex: "#8D7A35", brands: {"MARD":"B26","COCO":"F19","漫漫":"Q7","盼盼":"248","咪小窝":"248"} },
|
||||
{ hex: "#CCE1AF", brands: {"MARD":"B27","COCO":"F20","漫漫":"R10","盼盼":"262","咪小窝":"262"} },
|
||||
{ hex: "#9EE5B9", brands: {"MARD":"B28","COCO":"F21","漫漫":"R11","盼盼":"269","咪小窝":"254"} },
|
||||
{ hex: "#C5E254", brands: {"MARD":"B29","COCO":"F22","漫漫":"R09","盼盼":"268","咪小窝":"253"} },
|
||||
{ hex: "#E2FCB1", brands: {"MARD":"B30","COCO":"F23","漫漫":"G6","盼盼":"285","咪小窝":"270"} },
|
||||
{ hex: "#B0E792", brands: {"MARD":"B31","COCO":"F24","漫漫":"G7","盼盼":"286","咪小窝":"271"} },
|
||||
{ hex: "#9CAB5A", brands: {"MARD":"B32","COCO":"F25","漫漫":"G12","盼盼":"287","咪小窝":"272"} },
|
||||
{ hex: "#E8FFE7", brands: {"MARD":"C01","COCO":"G01","漫漫":"C8","盼盼":"64","咪小窝":"76"} },
|
||||
{ hex: "#A9F9FC", brands: {"MARD":"C02","COCO":"H03","漫漫":"D1","盼盼":"30","咪小窝":"30"} },
|
||||
{ hex: "#A0E2FB", brands: {"MARD":"C03","COCO":"H04","漫漫":"D2","盼盼":"63","咪小窝":"75"} },
|
||||
{ hex: "#41CCFF", brands: {"MARD":"C04","COCO":"H05","漫漫":"D3","盼盼":"77","咪小窝":"82"} },
|
||||
{ hex: "#01ACEB", brands: {"MARD":"C05","COCO":"H07","漫漫":"D7","盼盼":"34","咪小窝":"34"} },
|
||||
{ hex: "#50AAF0", brands: {"MARD":"C06","COCO":"H08","漫漫":"D4","盼盼":"25","咪小窝":"25"} },
|
||||
{ hex: "#3677D2", brands: {"MARD":"C07","COCO":"H13","漫漫":"D8","盼盼":"9","咪小窝":"9"} },
|
||||
{ hex: "#0F54C0", brands: {"MARD":"C08","COCO":"H14","漫漫":"D9","盼盼":"52","咪小窝":"71"} },
|
||||
{ hex: "#324BCA", brands: {"MARD":"C09","COCO":"H16","漫漫":"N5","盼盼":"42","咪小窝":"42"} },
|
||||
{ hex: "#3EBCE2", brands: {"MARD":"C10","COCO":"H09","漫漫":"D25","盼盼":"121","咪小窝":"130"} },
|
||||
{ hex: "#28DDDE", brands: {"MARD":"C11","COCO":"H10","漫漫":"D28","盼盼":"122","咪小窝":"113"} },
|
||||
{ hex: "#1C334D", brands: {"MARD":"C12","COCO":"H23","漫漫":"D26","盼盼":"120","咪小窝":"120"} },
|
||||
{ hex: "#CDE8FF", brands: {"MARD":"C13","COCO":"H01","漫漫":"D30","盼盼":"140","咪小窝":"142"} },
|
||||
{ hex: "#D5FDFF", brands: {"MARD":"C14","COCO":"H02","漫漫":"D29","盼盼":"139","咪小窝":"136"} },
|
||||
{ hex: "#22C4C6", brands: {"MARD":"C15","COCO":"H11","漫漫":"D31","盼盼":"143","咪小窝":"132"} },
|
||||
{ hex: "#1557A8", brands: {"MARD":"C16","COCO":"H18","漫漫":"D32","盼盼":"149","咪小窝":"149"} },
|
||||
{ hex: "#04D1F6", brands: {"MARD":"C17","COCO":"H19","漫漫":"D36","盼盼":"163","咪小窝":"156"} },
|
||||
{ hex: "#1D3344", brands: {"MARD":"C18","COCO":"H24","漫漫":"DH6","盼盼":"196","咪小窝":"196"} },
|
||||
{ hex: "#1887A2", brands: {"MARD":"C19","COCO":"H12","漫漫":"DH9","盼盼":"202","咪小窝":"202"} },
|
||||
{ hex: "#176DAF", brands: {"MARD":"C20","COCO":"H17","漫漫":"DH14","盼盼":"197","咪小窝":"197"} },
|
||||
{ hex: "#BEDDFF", brands: {"MARD":"C21","COCO":"H06","漫漫":"IC3","盼盼":"212","咪小窝":"212"} },
|
||||
{ hex: "#67B4BE", brands: {"MARD":"C22","COCO":"H25","漫漫":"Q11","盼盼":"239","咪小窝":"239"} },
|
||||
{ hex: "#C8E2FF", brands: {"MARD":"C23","COCO":"H26","漫漫":"R13","盼盼":"263","咪小窝":"263"} },
|
||||
{ hex: "#7CC4FF", brands: {"MARD":"C24","COCO":"H27","漫漫":"R14","盼盼":"267","咪小窝":"252"} },
|
||||
{ hex: "#A9E5E5", brands: {"MARD":"C25","COCO":"H28","漫漫":"R12","盼盼":"271","咪小窝":"256"} },
|
||||
{ hex: "#3CAED8", brands: {"MARD":"C26","COCO":"H29","漫漫":"R15","盼盼":"265","咪小窝":"250"} },
|
||||
{ hex: "#D3DFFA", brands: {"MARD":"C27","COCO":"H30","漫漫":"G13","盼盼":"279","咪小窝":"264"} },
|
||||
{ hex: "#BBCFED", brands: {"MARD":"C28","COCO":"H31","漫漫":"G14","盼盼":"280","咪小窝":"265"} },
|
||||
{ hex: "#34488E", brands: {"MARD":"C29","COCO":"H32","漫漫":"G15","盼盼":"281","咪小窝":"266"} },
|
||||
{ hex: "#AEB4F2", brands: {"MARD":"D01","COCO":"J07","漫漫":"D5","盼盼":"46","咪小窝":"46"} },
|
||||
{ hex: "#858EDD", brands: {"MARD":"D02","COCO":"J08","漫漫":"D6","盼盼":"36","咪小窝":"36"} },
|
||||
{ hex: "#2F54AF", brands: {"MARD":"D03","COCO":"H15","漫漫":"D10","盼盼":"8","咪小窝":"8"} },
|
||||
{ hex: "#182A84", brands: {"MARD":"D04","COCO":"H20","漫漫":"D11","盼盼":"75","咪小窝":"80"} },
|
||||
{ hex: "#B843C5", brands: {"MARD":"D05","COCO":"J12","漫漫":"D13","盼盼":"32","咪小窝":"32"} },
|
||||
{ hex: "#AC7BDE", brands: {"MARD":"D06","COCO":"J11","漫漫":"D14","盼盼":"27","咪小窝":"27"} },
|
||||
{ hex: "#8854B3", brands: {"MARD":"D07","COCO":"J15","漫漫":"D12","盼盼":"7","咪小窝":"7"} },
|
||||
{ hex: "#E2D3FF", brands: {"MARD":"D08","COCO":"J03","漫漫":"D16","盼盼":"94","咪小窝":"89"} },
|
||||
{ hex: "#D5B9F8", brands: {"MARD":"D09","COCO":"J04","漫漫":"D17","盼盼":"93","咪小窝":"90"} },
|
||||
{ hex: "#361851", brands: {"MARD":"D10","COCO":"J19","漫漫":"D15","盼盼":"92","咪小窝":"91"} },
|
||||
{ hex: "#B9BAE1", brands: {"MARD":"D11","COCO":"J06","漫漫":"D19","盼盼":"105","咪小窝":"104"} },
|
||||
{ hex: "#DE9AD4", brands: {"MARD":"D12","COCO":"J10","漫漫":"D20","盼盼":"104","咪小窝":"105"} },
|
||||
{ hex: "#B90095", brands: {"MARD":"D13","COCO":"J14","漫漫":"D21","盼盼":"103","咪小窝":"106"} },
|
||||
{ hex: "#8B279B", brands: {"MARD":"D14","COCO":"J16","漫漫":"D22","盼盼":"102","咪小窝":"107"} },
|
||||
{ hex: "#2F1F90", brands: {"MARD":"D15","COCO":"H22","漫漫":"D18","盼盼":"101","咪小窝":"108"} },
|
||||
{ hex: "#E3E1EE", brands: {"MARD":"D16","COCO":"J01","漫漫":"D23","盼盼":"118","咪小窝":"126"} },
|
||||
{ hex: "#C4D4F6", brands: {"MARD":"D17","COCO":"J05","漫漫":"D24","盼盼":"119","咪小窝":"128"} },
|
||||
{ hex: "#A45EC7", brands: {"MARD":"D18","COCO":"J13","漫漫":"D27","盼盼":"124","咪小窝":"125"} },
|
||||
{ hex: "#D8C3D7", brands: {"MARD":"D19","COCO":"J09","漫漫":"D33","盼盼":"153","咪小窝":"153"} },
|
||||
{ hex: "#9C32B2", brands: {"MARD":"D20","COCO":"J17","漫漫":"D34","盼盼":"161","咪小窝":"155"} },
|
||||
{ hex: "#9A009B", brands: {"MARD":"D21","COCO":"J18","漫漫":"D35","盼盼":"162","咪小窝":"158"} },
|
||||
{ hex: "#333A95", brands: {"MARD":"D22","COCO":"H21","漫漫":"DH1","盼盼":"198","咪小窝":"198"} },
|
||||
{ hex: "#EBDAFC", brands: {"MARD":"D23","COCO":"J02","漫漫":"IC8","盼盼":"217","咪小窝":"217"} },
|
||||
{ hex: "#7786E5", brands: {"MARD":"D24","COCO":"J20","漫漫":"Q14","盼盼":"244","咪小窝":"244"} },
|
||||
{ hex: "#494FC7", brands: {"MARD":"D25","COCO":"J21","漫漫":"Q15","盼盼":"249","咪小窝":"234"} },
|
||||
{ hex: "#DFC2F8", brands: {"MARD":"D26","COCO":"J22","漫漫":"R01","盼盼":"273","咪小窝":"258"} },
|
||||
{ hex: "#FDD3CC", brands: {"MARD":"E01","COCO":"K03","漫漫":"E1","盼盼":"18","咪小窝":"18"} },
|
||||
{ hex: "#FEC0DF", brands: {"MARD":"E02","COCO":"K15","漫漫":"A7","盼盼":"38","咪小窝":"38"} },
|
||||
{ hex: "#FFB7E7", brands: {"MARD":"E03","COCO":"K17","漫漫":"A8","盼盼":"62","咪小窝":"74"} },
|
||||
{ hex: "#E8649E", brands: {"MARD":"E04","COCO":"K21","漫漫":"A9","盼盼":"6","咪小窝":"6"} },
|
||||
{ hex: "#F551A2", brands: {"MARD":"E05","COCO":"K19","漫漫":"A10","盼盼":"40","咪小窝":"40"} },
|
||||
{ hex: "#F13D74", brands: {"MARD":"E06","COCO":"K22","漫漫":"A11","盼盼":"20","咪小窝":"20"} },
|
||||
{ hex: "#C63478", brands: {"MARD":"E07","COCO":"K25","漫漫":"A12","盼盼":"41","咪小窝":"41"} },
|
||||
{ hex: "#FFDBE9", brands: {"MARD":"E08","COCO":"K12","漫漫":"A13","盼盼":"84","咪小窝":"103"} },
|
||||
{ hex: "#E970CC", brands: {"MARD":"E09","COCO":"K18","漫漫":"A14","盼盼":"98","咪小窝":"95"} },
|
||||
{ hex: "#D33793", brands: {"MARD":"E10","COCO":"K23","漫漫":"A16","盼盼":"83","咪小窝":"94"} },
|
||||
{ hex: "#FCDDD2", brands: {"MARD":"E11","COCO":"K02","漫漫":"A19","盼盼":"125","咪小窝":"131"} },
|
||||
{ hex: "#F78FC3", brands: {"MARD":"E12","COCO":"K16","漫漫":"A20","盼盼":"126","咪小窝":"112"} },
|
||||
{ hex: "#B5006D", brands: {"MARD":"E13","COCO":"K24","漫漫":"A21","盼盼":"127","咪小窝":"124"} },
|
||||
{ hex: "#FFD1BA", brands: {"MARD":"E14","COCO":"K05","漫漫":"E21","盼盼":"137","咪小窝":"140"} },
|
||||
{ hex: "#F8C7C9", brands: {"MARD":"E15","COCO":"K04","漫漫":"A23","盼盼":"135","咪小窝":"139"} },
|
||||
{ hex: "#FFF3EB", brands: {"MARD":"E16","COCO":"K01","漫漫":"IC2","盼盼":"221","咪小窝":"206"} },
|
||||
{ hex: "#FFE2EA", brands: {"MARD":"E17","COCO":"K11","漫漫":"IC7","盼盼":"220","咪小窝":"205"} },
|
||||
{ hex: "#FFC7DB", brands: {"MARD":"E18","COCO":"K13","漫漫":"IC13","盼盼":"210","咪小窝":"210"} },
|
||||
{ hex: "#FEBAD5", brands: {"MARD":"E19","COCO":"K14","漫漫":"IC12","盼盼":"215","咪小窝":"215"} },
|
||||
{ hex: "#D8C7D1", brands: {"MARD":"E20","COCO":"K26","漫漫":"Q1","盼盼":"241","咪小窝":"241"} },
|
||||
{ hex: "#BD9DA1", brands: {"MARD":"E21","COCO":"K27","漫漫":"Q2","盼盼":"253","咪小窝":"238"} },
|
||||
{ hex: "#B785A1", brands: {"MARD":"E22","COCO":"K28","漫漫":"Q4","盼盼":"252","咪小窝":"237"} },
|
||||
{ hex: "#937A8D", brands: {"MARD":"E23","COCO":"K29","漫漫":"Q3","盼盼":"250","咪小窝":"235"} },
|
||||
{ hex: "#E1BCE8", brands: {"MARD":"E24","COCO":"K30","漫漫":"G8","盼盼":"282","咪小窝":"267"} },
|
||||
{ hex: "#FD957B", brands: {"MARD":"F01","COCO":"K08","漫漫":"A1","盼盼":"35","咪小窝":"35"} },
|
||||
{ hex: "#FC3D46", brands: {"MARD":"F02","COCO":"C02","漫漫":"A2","盼盼":"31","咪小窝":"31"} },
|
||||
{ hex: "#F74941", brands: {"MARD":"F03","COCO":"C03","漫漫":"A3","盼盼":"53","咪小窝":"72"} },
|
||||
{ hex: "#FC283C", brands: {"MARD":"F04","COCO":"C06","漫漫":"A4","盼盼":"54","咪小窝":"73"} },
|
||||
{ hex: "#E7002F", brands: {"MARD":"F05","COCO":"C07","漫漫":"A5","盼盼":"5","咪小窝":"5"} },
|
||||
{ hex: "#943630", brands: {"MARD":"F06","COCO":"Z21","漫漫":"E9","盼盼":"16","咪小窝":"16"} },
|
||||
{ hex: "#971937", brands: {"MARD":"F07","COCO":"C10","漫漫":"A6","盼盼":"47","咪小窝":"47"} },
|
||||
{ hex: "#BC0028", brands: {"MARD":"F08","COCO":"C09","漫漫":"A17","盼盼":"81","咪小窝":"92"} },
|
||||
{ hex: "#E2677A", brands: {"MARD":"F09","COCO":"K20","漫漫":"A15","盼盼":"82","咪小窝":"93"} },
|
||||
{ hex: "#8A4526", brands: {"MARD":"F10","COCO":"Z20","漫漫":"E15","盼盼":"116","咪小窝":"115"} },
|
||||
{ hex: "#5A2121", brands: {"MARD":"F11","COCO":"Z23","漫漫":"E16","盼盼":"117","咪小窝":"129"} },
|
||||
{ hex: "#FD4E6A", brands: {"MARD":"F12","COCO":"C01","漫漫":"A22","盼盼":"136","咪小窝":"134"} },
|
||||
{ hex: "#F35744", brands: {"MARD":"F13","COCO":"C04","漫漫":"A24","盼盼":"148","咪小窝":"148"} },
|
||||
{ hex: "#FFA9AD", brands: {"MARD":"F14","COCO":"K07","漫漫":"A25","盼盼":"154","咪小窝":"154"} },
|
||||
{ hex: "#D30022", brands: {"MARD":"F15","COCO":"C08","漫漫":"DH8","盼盼":"204","咪小窝":"191"} },
|
||||
{ hex: "#FEC2A6", brands: {"MARD":"F16","COCO":"K06","漫漫":"IC10","盼盼":"211","咪小窝":"211"} },
|
||||
{ hex: "#E69C79", brands: {"MARD":"F17","COCO":"K31","漫漫":"Q9","盼盼":"245","咪小窝":"245"} },
|
||||
{ hex: "#D37C46", brands: {"MARD":"F18","COCO":"K32","漫漫":"Q10","盼盼":"246","咪小窝":"246"} },
|
||||
{ hex: "#C1444A", brands: {"MARD":"F19","COCO":"K33","漫漫":"Q05","盼盼":"243","咪小窝":"243"} },
|
||||
{ hex: "#CD9391", brands: {"MARD":"F20","COCO":"K34","漫漫":"R04","盼盼":"275","咪小窝":"260"} },
|
||||
{ hex: "#F7B4C6", brands: {"MARD":"F21","COCO":"K35","漫漫":"R03","盼盼":"266","咪小窝":"251"} },
|
||||
{ hex: "#FDC0D0", brands: {"MARD":"F22","COCO":"K36","漫漫":"R02","盼盼":"272","咪小窝":"257"} },
|
||||
{ hex: "#F67E66", brands: {"MARD":"F23","COCO":"K37","漫漫":"R05","盼盼":"264","咪小窝":"249"} },
|
||||
{ hex: "#E698AA", brands: {"MARD":"F24","COCO":"K38","漫漫":"G9","盼盼":"283","咪小窝":"268"} },
|
||||
{ hex: "#E54B4F", brands: {"MARD":"F25","COCO":"K39","漫漫":"G10","盼盼":"284","咪小窝":"269"} },
|
||||
{ hex: "#FFE2CE", brands: {"MARD":"G01","COCO":"Z02","漫漫":"E3","盼盼":"76","咪小窝":"81"} },
|
||||
{ hex: "#FFC4AA", brands: {"MARD":"G02","COCO":"Z05","漫漫":"E4","盼盼":"49","咪小窝":"49"} },
|
||||
{ hex: "#F4C3A5", brands: {"MARD":"G03","COCO":"Z06","漫漫":"E5","盼盼":"80","咪小窝":"85"} },
|
||||
{ hex: "#E1B383", brands: {"MARD":"G04","COCO":"Z08","漫漫":"E6","盼盼":"19","咪小窝":"19"} },
|
||||
{ hex: "#EDB045", brands: {"MARD":"G05","COCO":"Z10","漫漫":"B7","盼盼":"43","咪小窝":"43"} },
|
||||
{ hex: "#E99C17", brands: {"MARD":"G06","COCO":"Z11","漫漫":"B8","盼盼":"50","咪小窝":"50"} },
|
||||
{ hex: "#9D5B3E", brands: {"MARD":"G07","COCO":"Z18","漫漫":"E7","盼盼":"17","咪小窝":"17"} },
|
||||
{ hex: "#753832", brands: {"MARD":"G08","COCO":"Z22","漫漫":"E8","盼盼":"12","咪小窝":"12"} },
|
||||
{ hex: "#E6B483", brands: {"MARD":"G09","COCO":"Z09","漫漫":"E10","盼盼":"91","咪小窝":"102"} },
|
||||
{ hex: "#D98C39", brands: {"MARD":"G10","COCO":"Z15","漫漫":"B9","盼盼":"87","咪小窝":"101"} },
|
||||
{ hex: "#E0C593", brands: {"MARD":"G11","COCO":"Z07","漫漫":"E12","盼盼":"112","咪小窝":"118"} },
|
||||
{ hex: "#FFC890", brands: {"MARD":"G12","COCO":"Z13","漫漫":"E13","盼盼":"113","咪小窝":"127"} },
|
||||
{ hex: "#B7714A", brands: {"MARD":"G13","COCO":"Z14","漫漫":"E17","盼盼":"115","咪小窝":"114"} },
|
||||
{ hex: "#8D614C", brands: {"MARD":"G14","COCO":"Z17","漫漫":"E14","盼盼":"114","咪小窝":"123"} },
|
||||
{ hex: "#FCF9E0", brands: {"MARD":"G15","COCO":"Z03","漫漫":"E19","盼盼":"133","咪小窝":"143"} },
|
||||
{ hex: "#F2D9BA", brands: {"MARD":"G16","COCO":"Z04","漫漫":"E20","盼盼":"134","咪小窝":"138"} },
|
||||
{ hex: "#78524B", brands: {"MARD":"G17","COCO":"Z16","漫漫":"E22","盼盼":"144","咪小窝":"137"} },
|
||||
{ hex: "#FFE4CC", brands: {"MARD":"G18","COCO":"Z01","漫漫":"DH5","盼盼":"203","咪小窝":"203"} },
|
||||
{ hex: "#E07935", brands: {"MARD":"G19","COCO":"Z12","漫漫":"DH3","盼盼":"208","咪小窝":"195"} },
|
||||
{ hex: "#A94023", brands: {"MARD":"G20","COCO":"Z19","漫漫":"DH13","盼盼":"199","咪小窝":"199"} },
|
||||
{ hex: "#B88558", brands: {"MARD":"G21","COCO":"Z24","漫漫":"Q8","盼盼":"247","咪小窝":"247"} },
|
||||
{ hex: "#FDFBFF", brands: {"MARD":"H01","COCO":"A02","漫漫":"F1","盼盼":"15","咪小窝":"15"} },
|
||||
{ hex: "#FEFFFF", brands: {"MARD":"H02","COCO":"A01","漫漫":"F2","盼盼":"1","咪小窝":"1"} },
|
||||
{ hex: "#B6B1BA", brands: {"MARD":"H03","COCO":"B03","漫漫":"F3","盼盼":"13","咪小窝":"13"} },
|
||||
{ hex: "#89858C", brands: {"MARD":"H04","COCO":"B05","漫漫":"F4","盼盼":"78","咪小窝":"83"} },
|
||||
{ hex: "#48464E", brands: {"MARD":"H05","COCO":"B06","漫漫":"F5","盼盼":"45","咪小窝":"45"} },
|
||||
{ hex: "#2F2B2F", brands: {"MARD":"H06","COCO":"B07","漫漫":"F6","盼盼":"51","咪小窝":"70"} },
|
||||
{ hex: "#000000", brands: {"MARD":"H07","COCO":"B09","漫漫":"F7","盼盼":"14","咪小窝":"14"} },
|
||||
{ hex: "#E7D6DB", brands: {"MARD":"H08","COCO":"A09","漫漫":"F8","盼盼":"85","咪小窝":"86"} },
|
||||
{ hex: "#EDEDED", brands: {"MARD":"H09","COCO":"A08","漫漫":"F10","盼盼":"95","咪小窝":"87"} },
|
||||
{ hex: "#EEE9EA", brands: {"MARD":"H10","COCO":"A10","漫漫":"F9","盼盼":"86","咪小窝":"88"} },
|
||||
{ hex: "#CECDD5", brands: {"MARD":"H11","COCO":"B01","漫漫":"F11","盼盼":"123","咪小窝":"121"} },
|
||||
{ hex: "#FFF5ED", brands: {"MARD":"H12","COCO":"A04","漫漫":"E18","盼盼":"132","咪小窝":"144"} },
|
||||
{ hex: "#F5ECD2", brands: {"MARD":"H13","COCO":"A06","漫漫":"E23","盼盼":"145","咪小窝":"146"} },
|
||||
{ hex: "#CFD7D3", brands: {"MARD":"H14","COCO":"B02","漫漫":"F12","盼盼":"146","咪小窝":"145"} },
|
||||
{ hex: "#98A6A8", brands: {"MARD":"H15","COCO":"B04","漫漫":"DH4","盼盼":"201","咪小窝":"201"} },
|
||||
{ hex: "#1D1414", brands: {"MARD":"H16","COCO":"B08","漫漫":"DH11","盼盼":"200","咪小窝":"200"} },
|
||||
{ hex: "#F1EDED", brands: {"MARD":"H17","COCO":"A07","漫漫":"IC6","盼盼":"214","咪小窝":"214"} },
|
||||
{ hex: "#FFFDF0", brands: {"MARD":"H18","COCO":"A03","漫漫":"IC1","盼盼":"219","咪小窝":"204"} },
|
||||
{ hex: "#F6EFE2", brands: {"MARD":"H19","COCO":"A05","漫漫":"IC11","盼盼":"209","咪小窝":"209"} },
|
||||
{ hex: "#949FA3", brands: {"MARD":"H20","COCO":"B10","漫漫":"Q12","盼盼":"251","咪小窝":"236"} },
|
||||
{ hex: "#FFFBE1", brands: {"MARD":"H21","COCO":"A11","漫漫":"G1","盼盼":"291","咪小窝":"276"} },
|
||||
{ hex: "#CACAD4", brands: {"MARD":"H22","COCO":"A12","漫漫":"G2","盼盼":"277","咪小窝":"277"} },
|
||||
{ hex: "#9A9D94", brands: {"MARD":"H23","COCO":"B11","漫漫":"G11","盼盼":"278","咪小窝":"278"} },
|
||||
{ hex: "#BCC6B8", brands: {"MARD":"M01","COCO":"Y01","漫漫":"YX11","盼盼":"168","咪小窝":"168"} },
|
||||
{ hex: "#8AA386", brands: {"MARD":"M02","COCO":"Y02","漫漫":"YX12","盼盼":"172","咪小窝":"172"} },
|
||||
{ hex: "#697D80", brands: {"MARD":"M03","COCO":"Y03","漫漫":"YX2","盼盼":"166","咪小窝":"166"} },
|
||||
{ hex: "#E3D2BC", brands: {"MARD":"M04","COCO":"Y04","漫漫":"YX15","盼盼":"167","咪小窝":"167"} },
|
||||
{ hex: "#D0CCAA", brands: {"MARD":"M05","COCO":"Y05","漫漫":"YX6","盼盼":"174","咪小窝":"159"} },
|
||||
{ hex: "#B0A782", brands: {"MARD":"M06","COCO":"Y06","漫漫":"YX1","盼盼":"169","咪小窝":"169"} },
|
||||
{ hex: "#B4A497", brands: {"MARD":"M07","COCO":"Y07","漫漫":"YX13","盼盼":"171","咪小窝":"171"} },
|
||||
{ hex: "#B38281", brands: {"MARD":"M08","COCO":"Y08","漫漫":"YX14","盼盼":"177","咪小窝":"162"} },
|
||||
{ hex: "#A58767", brands: {"MARD":"M09","COCO":"Y09","漫漫":"YX10","盼盼":"170","咪小窝":"170"} },
|
||||
{ hex: "#C5B2BC", brands: {"MARD":"M10","COCO":"Y10","漫漫":"YX9","盼盼":"164","咪小窝":"164"} },
|
||||
{ hex: "#9F7594", brands: {"MARD":"M11","COCO":"Y11","漫漫":"YX4","盼盼":"176","咪小窝":"161"} },
|
||||
{ hex: "#644749", brands: {"MARD":"M12","COCO":"Y12","漫漫":"YX5","盼盼":"173","咪小窝":"173"} },
|
||||
{ hex: "#D19066", brands: {"MARD":"M13","COCO":"Y13","漫漫":"YX8","盼盼":"175","咪小窝":"160"} },
|
||||
{ hex: "#C77362", brands: {"MARD":"M14","COCO":"Y14","漫漫":"YX3","盼盼":"165","咪小窝":"165"} },
|
||||
{ hex: "#757D78", brands: {"MARD":"M15","COCO":"Y15","漫漫":"YX7","盼盼":"178","咪小窝":"163"} },
|
||||
{ hex: "#FCF7F8", brands: {"MARD":"P01","COCO":"M01","漫漫":"P1","盼盼":"71","咪小窝":"62"} },
|
||||
{ hex: "#B0A9AC", brands: {"MARD":"P02","COCO":"M02","漫漫":"P2","盼盼":"55","咪小窝":"69"} },
|
||||
{ hex: "#AFDCAB", brands: {"MARD":"P03","COCO":"M03","漫漫":"P4","盼盼":"73","咪小窝":"66"} },
|
||||
{ hex: "#FEA49F", brands: {"MARD":"P04","COCO":"M04","漫漫":"P5","盼盼":"72","咪小窝":"64"} },
|
||||
{ hex: "#EE8C3E", brands: {"MARD":"P05","COCO":"M05","漫漫":"P3","盼盼":"56","咪小窝":"63"} },
|
||||
{ hex: "#5FD0A7", brands: {"MARD":"P06","COCO":"M06","漫漫":"P8","盼盼":"157","咪小窝":"65"} },
|
||||
{ hex: "#EB9270", brands: {"MARD":"P07","COCO":"M07","漫漫":"P6","盼盼":"159","咪小窝":"68"} },
|
||||
{ hex: "#F0D958", brands: {"MARD":"P08","COCO":"M08","漫漫":"P7","盼盼":"158","咪小窝":"67"} },
|
||||
{ hex: "#D9D9D9", brands: {"MARD":"P09","COCO":"M09","漫漫":"P13","盼盼":"195","咪小窝":"178"} },
|
||||
{ hex: "#D9C7EA", brands: {"MARD":"P10","COCO":"M10","漫漫":"P18","盼盼":"187","咪小窝":"187"} },
|
||||
{ hex: "#F3ECC9", brands: {"MARD":"P11","COCO":"M11","漫漫":"P9","盼盼":"185","咪小窝":"185"} },
|
||||
{ hex: "#E6EEF2", brands: {"MARD":"P12","COCO":"M12","漫漫":"P12","盼盼":"190","咪小窝":"190"} },
|
||||
{ hex: "#AACBEF", brands: {"MARD":"P13","COCO":"M13","漫漫":"P17","盼盼":"193","咪小窝":"176"} },
|
||||
{ hex: "#337680", brands: {"MARD":"P14","COCO":"M14","漫漫":"P22","盼盼":"183","咪小窝":"183"} },
|
||||
{ hex: "#668575", brands: {"MARD":"P15","COCO":"M15","漫漫":"P23","盼盼":"184","咪小窝":"184"} },
|
||||
{ hex: "#FEBF45", brands: {"MARD":"P16","COCO":"M16","漫漫":"P14","盼盼":"182","咪小窝":"182"} },
|
||||
{ hex: "#FEA324", brands: {"MARD":"P17","COCO":"M17","漫漫":"P19","盼盼":"179","咪小窝":"179"} },
|
||||
{ hex: "#FEB89F", brands: {"MARD":"P18","COCO":"M18","漫漫":"P11","盼盼":"194","咪小窝":"177"} },
|
||||
{ hex: "#FFFEEC", brands: {"MARD":"P19","COCO":"M19","漫漫":"P10","盼盼":"186","咪小窝":"186"} },
|
||||
{ hex: "#FEBECF", brands: {"MARD":"P20","COCO":"M21","漫漫":"P15","盼盼":"188","咪小窝":"180"} },
|
||||
{ hex: "#ECBEBF", brands: {"MARD":"P21","COCO":"M20","漫漫":"P20","盼盼":"180","咪小窝":"188"} },
|
||||
{ hex: "#E4A89F", brands: {"MARD":"P22","COCO":"M22","漫漫":"P16","盼盼":"189","咪小窝":"189"} },
|
||||
{ hex: "#A56268", brands: {"MARD":"P23","COCO":"M23","漫漫":"P21","盼盼":"181","咪小窝":"181"} },
|
||||
{ hex: "#F2A5E8", brands: {"MARD":"Q01","COCO":"W3","漫漫":"W3","盼盼":"109","咪小窝":"W3"} },
|
||||
{ hex: "#E9EC91", brands: {"MARD":"Q02","COCO":"W4","漫漫":"W4","盼盼":"111","咪小窝":"W4"} },
|
||||
{ hex: "#FFFF00", brands: {"MARD":"Q03","COCO":"W1","漫漫":"W1","盼盼":"107","咪小窝":"W1"} },
|
||||
{ hex: "#FFEBFA", brands: {"MARD":"Q04","COCO":"W2","漫漫":"W2","盼盼":"110","咪小窝":"W2"} },
|
||||
{ hex: "#76CEDE", brands: {"MARD":"Q05","COCO":"W5","漫漫":"W5","盼盼":"108","咪小窝":"W5"} },
|
||||
{ hex: "#D50D21", brands: {"MARD":"R01","COCO":"L01","漫漫":"T1","盼盼":"67","咪小窝":"52"} },
|
||||
{ hex: "#F92F83", brands: {"MARD":"R02","COCO":"L02","漫漫":"N1","盼盼":"24","咪小窝":"24"} },
|
||||
{ hex: "#FD8324", brands: {"MARD":"R03","COCO":"L03","漫漫":"N2","盼盼":"22","咪小窝":"22"} },
|
||||
{ hex: "#F8EC31", brands: {"MARD":"R04","COCO":"L04","漫漫":"N3","盼盼":"21","咪小窝":"21"} },
|
||||
{ hex: "#35C75B", brands: {"MARD":"R05","COCO":"L05","漫漫":"N4","盼盼":"23","咪小窝":"23"} },
|
||||
{ hex: "#238891", brands: {"MARD":"R06","COCO":"L06","漫漫":"T4","盼盼":"69","咪小窝":"55"} },
|
||||
{ hex: "#19779D", brands: {"MARD":"R07","COCO":"L07","漫漫":"T5","盼盼":"37","咪小窝":"37"} },
|
||||
{ hex: "#1A60C3", brands: {"MARD":"R08","COCO":"L08","漫漫":"T3","盼盼":"68","咪小窝":"54"} },
|
||||
{ hex: "#9A56B4", brands: {"MARD":"R09","COCO":"L09","漫漫":"T2","盼盼":"70","咪小窝":"56"} },
|
||||
{ hex: "#FFDB4C", brands: {"MARD":"R10","COCO":"L10","漫漫":"L2","盼盼":"156","咪小窝":"53"} },
|
||||
{ hex: "#FFEBFB", brands: {"MARD":"R11","COCO":"L11","漫漫":"T6","盼盼":"151","咪小窝":"151"} },
|
||||
{ hex: "#D8D5CE", brands: {"MARD":"R12","COCO":"L12","漫漫":"T7","盼盼":"160","咪小窝":"157"} },
|
||||
{ hex: "#55514C", brands: {"MARD":"R13","COCO":"L13","漫漫":"-","盼盼":"152","咪小窝":"152"} },
|
||||
{ hex: "#9FE4DF", brands: {"MARD":"R14","COCO":"S1","漫漫":"S1","盼盼":"231","咪小窝":"231"} },
|
||||
{ hex: "#77CEE9", brands: {"MARD":"R15","COCO":"S2","漫漫":"S2","盼盼":"237","咪小窝":"224"} },
|
||||
{ hex: "#3ECFCA", brands: {"MARD":"R16","COCO":"S3","漫漫":"S3","盼盼":"238","咪小窝":"225"} },
|
||||
{ hex: "#4A867A", brands: {"MARD":"R17","COCO":"S4","漫漫":"S5","盼盼":"233","咪小窝":"233"} },
|
||||
{ hex: "#7FCD9D", brands: {"MARD":"R18","COCO":"S5","漫漫":"S4","盼盼":"235","咪小窝":"222"} },
|
||||
{ hex: "#CDE55D", brands: {"MARD":"R19","COCO":"S6","漫漫":"S11","盼盼":"227","咪小窝":"227"} },
|
||||
{ hex: "#E8C7B4", brands: {"MARD":"R20","COCO":"S7","漫漫":"S6","盼盼":"230","咪小窝":"230"} },
|
||||
{ hex: "#AD6F3C", brands: {"MARD":"R21","COCO":"S8","漫漫":"S13","盼盼":"234","咪小窝":"221"} },
|
||||
{ hex: "#6C372F", brands: {"MARD":"R22","COCO":"S9","漫漫":"S15","盼盼":"226","咪小窝":"226"} },
|
||||
{ hex: "#FEB872", brands: {"MARD":"R23","COCO":"S10","漫漫":"S12","盼盼":"224","咪小窝":"219"} },
|
||||
{ hex: "#F3C1C0", brands: {"MARD":"R24","COCO":"S11","漫漫":"S4","盼盼":"228","咪小窝":"228"} },
|
||||
{ hex: "#C9675E", brands: {"MARD":"R25","COCO":"S12","漫漫":"S14","盼盼":"225","咪小窝":"220"} },
|
||||
{ hex: "#D293BE", brands: {"MARD":"R26","COCO":"S13","漫漫":"S9","盼盼":"229","咪小窝":"229"} },
|
||||
{ hex: "#EA8CB1", brands: {"MARD":"R27","COCO":"S14","漫漫":"S8","盼盼":"232","咪小窝":"232"} },
|
||||
{ hex: "#9C87D6", brands: {"MARD":"R28","COCO":"S15","漫漫":"S10","盼盼":"236","咪小窝":"223"} },
|
||||
{ hex: "#FFFFFF", brands: {"MARD":"T01","COCO":"L14","漫漫":"L6","盼盼":"155","咪小窝":"51"} },
|
||||
{ hex: "#FD6FB4", brands: {"MARD":"Y01","COCO":"N01","漫漫":"Y1","盼盼":"59","咪小窝":"59"} },
|
||||
{ hex: "#FEB481", brands: {"MARD":"Y02","COCO":"N02","漫漫":"Y2","盼盼":"60","咪小窝":"60"} },
|
||||
{ hex: "#D7FAA0", brands: {"MARD":"Y03","COCO":"N03","漫漫":"Y3","盼盼":"57","咪小窝":"57"} },
|
||||
{ hex: "#8BDBFA", brands: {"MARD":"Y04","COCO":"N04","漫漫":"Y4","盼盼":"58","咪小窝":"58"} },
|
||||
{ hex: "#E987EA", brands: {"MARD":"Y05","COCO":"N05","漫漫":"Y5","盼盼":"61","咪小窝":"61"} },
|
||||
{ hex: "#DAABB3", brands: {"MARD":"ZG1","COCO":"GB1","漫漫":"ZG1","盼盼":"254","咪小窝":"ZG1"} },
|
||||
{ hex: "#D6AA87", brands: {"MARD":"ZG2","COCO":"GB2","漫漫":"ZG2","盼盼":"255","咪小窝":"ZG2"} },
|
||||
{ hex: "#C1BD8D", brands: {"MARD":"ZG3","COCO":"GB3","漫漫":"ZG3","盼盼":"256","咪小窝":"ZG3"} },
|
||||
{ hex: "#96869F", brands: {"MARD":"ZG4","COCO":"GB4","漫漫":"ZG4","盼盼":"257","咪小窝":"ZG4"} },
|
||||
{ hex: "#8490A6", brands: {"MARD":"ZG5","COCO":"GB5","漫漫":"ZG5","盼盼":"258","咪小窝":"ZG5"} },
|
||||
{ hex: "#94BFE2", brands: {"MARD":"ZG6","COCO":"GB6","漫漫":"ZG6","盼盼":"259","咪小窝":"ZG6"} },
|
||||
{ hex: "#E2A9D2", brands: {"MARD":"ZG7","COCO":"GB7","漫漫":"ZG7","盼盼":"260","咪小窝":"ZG7"} },
|
||||
{ hex: "#AB91C0", brands: {"MARD":"ZG8","COCO":"GB8","漫漫":"ZG8","盼盼":"261","咪小窝":"ZG8"} },
|
||||
];
|
||||
|
||||
/** 把 #RRGGBB 解析为 sRGB 三元组。 */
|
||||
function hexToRgb(hex: string): [number, number, number] {
|
||||
const value = Number.parseInt(hex.slice(1), 16);
|
||||
return [(value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff];
|
||||
}
|
||||
|
||||
/** 收集某品牌在映射表中出现的全部色号(去重、排序;"-" 视为无色号,跳过)。 */
|
||||
function collectBrandCodes(brandId: string): string[] {
|
||||
const codes = new Set<string>();
|
||||
for (const entry of SOURCE) {
|
||||
const code = entry.brands[brandId];
|
||||
if (code && code !== '-') codes.add(code);
|
||||
}
|
||||
return [...codes].sort();
|
||||
}
|
||||
|
||||
/** 品牌定义(固定顺序:MARD / COCO / 漫漫 / 盼盼 / 咪小窝)。 */
|
||||
export const BRANDS: readonly BrandInfo[] = [
|
||||
{ id: 'MARD', name: 'MARD 玛德', codes: collectBrandCodes('MARD') },
|
||||
{ id: 'COCO', name: 'COCO', codes: collectBrandCodes('COCO') },
|
||||
{ id: '漫漫', name: '漫漫', codes: collectBrandCodes('漫漫') },
|
||||
{ id: '盼盼', name: '盼盼', codes: collectBrandCodes('盼盼') },
|
||||
{ id: '咪小窝', name: '咪小窝', codes: collectBrandCodes('咪小窝') },
|
||||
];
|
||||
|
||||
/** 291 标准色中间层:rgb/lab 由 hex 现算,保证与现有 srgbToLab 管线一致。 */
|
||||
export const STANDARD_COLORS: StandardColor[] = SOURCE.map(({ hex, brands }) => {
|
||||
const rgb = hexToRgb(hex);
|
||||
// srgbToLab 返回 readonly 元组,展开成可变 [number, number, number] 以符合 StandardColor。
|
||||
const lab: [number, number, number] = [...srgbToLab(rgb)];
|
||||
return { hex, rgb, lab, brands: { ...brands } };
|
||||
});
|
||||
|
||||
/**
|
||||
* 按品牌推导色板:每个「有该品牌色号」的标准色产生一条 BrandColor。
|
||||
* "-" 视为无色号,跳过(如漫漫 的 #55514C)。
|
||||
*/
|
||||
export function getBrandPalette(brandId: string): BrandColor[] {
|
||||
const palette: BrandColor[] = [];
|
||||
for (const entry of STANDARD_COLORS) {
|
||||
const code = entry.brands[brandId];
|
||||
if (code && code !== '-') {
|
||||
palette.push({ code, hex: entry.hex, rgb: entry.rgb, lab: entry.lab });
|
||||
}
|
||||
}
|
||||
return palette;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在 LAB 空间找到最近的标准色,返回该品牌对应的色号。
|
||||
* 若最近的标准色恰好没有该品牌色号(如漫漫 的 "-" 项),则顺延到最近的有色号标准色。
|
||||
* 品牌在映射表中没有任何色号时抛出明确错误。
|
||||
*/
|
||||
export function findNearestByBrand(lab: Lab3, brandId: string): BrandColor {
|
||||
let best: BrandColor | null = null;
|
||||
let bestDistance = Number.POSITIVE_INFINITY;
|
||||
for (const entry of STANDARD_COLORS) {
|
||||
const code = entry.brands[brandId];
|
||||
if (!code || code === '-') continue;
|
||||
const distance = labDistance(lab, entry.lab);
|
||||
if (distance < bestDistance) {
|
||||
bestDistance = distance;
|
||||
best = { code, hex: entry.hex, rgb: entry.rgb, lab: entry.lab };
|
||||
}
|
||||
}
|
||||
if (!best) {
|
||||
throw new Error(`品牌 "${brandId}" 在 291 标准色映射中没有任何色号`);
|
||||
}
|
||||
return best;
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* CIEDE2000 色差公式(Sharma, Wu & Dalal, 2005)。
|
||||
*
|
||||
* CIEDE2000(ΔE00)是国际照明委员会推荐的感知色差标准公式,
|
||||
* 相比 CIE LAB 欧氏距离增加了五项修正:
|
||||
* 1. 中性色 G 补偿(蓝-黄轴重标定,a' = (1+G)a)
|
||||
* 2. 色相角旋转项(RT,蓝色区域色相曲率)
|
||||
* 3. 亮度权重 SL(L* 非均匀性)
|
||||
* 4. 色度权重 SC(C* 非均匀性)
|
||||
* 5. 色相权重 SH(h* 非均匀性)
|
||||
*
|
||||
* 输入输出约定:输入为 CIE LAB(D65,L∈[0,100]),输出 ΔE00 ≥ 0。
|
||||
* 权重因子默认 kL = kC = kH = 1(“参考条件”),与 Sharma 的
|
||||
* 参考实现一致。实现细节严格对照 Sharma 2005 附录中的 MATLAB
|
||||
* 参考代码,测试用例采用其发表的 34 组补充测试数据。
|
||||
*
|
||||
* 参考: https://hajim.rochester.edu/rs/sites/GSharma/ciede2000/
|
||||
*/
|
||||
|
||||
import type { Lab } from './lab.js';
|
||||
|
||||
const DEG2RAD = Math.PI / 180;
|
||||
const RAD2DEG = 180 / Math.PI;
|
||||
/** 25⁷(色度 G 补偿与旋转项共用的常量)。 */
|
||||
const POW25_7 = Math.pow(25, 7);
|
||||
|
||||
/** 归一化到 [0, 360) 度。 */
|
||||
function mod360(deg: number): number {
|
||||
return ((deg % 360) + 360) % 360;
|
||||
}
|
||||
|
||||
/**
|
||||
* CIEDE2000 色差 ΔE00。
|
||||
*
|
||||
* @param lab1 参考颜色(CIE LAB)
|
||||
* @param lab2 对比颜色(CIE LAB)
|
||||
* @param kL 亮度权重因子(默认 1,参考条件)
|
||||
* @param kC 色度权重因子(默认 1,参考条件)
|
||||
* @param kH 色相权重因子(默认 1,参考条件)
|
||||
*/
|
||||
export function ciede2000(lab1: Lab, lab2: Lab, kL = 1, kC = 1, kH = 1): number {
|
||||
const [L1, a1, b1] = lab1;
|
||||
const [L2, a2, b2] = lab2;
|
||||
|
||||
// ---- 第 1 步:中性色 G 补偿(重标定 a 轴)----
|
||||
const C1 = Math.sqrt(a1 * a1 + b1 * b1);
|
||||
const C2 = Math.sqrt(a2 * a2 + b2 * b2);
|
||||
const Cbar = (C1 + C2) / 2;
|
||||
const G = 0.5 * (1 - Math.sqrt(Math.pow(Cbar, 7) / (Math.pow(Cbar, 7) + POW25_7)));
|
||||
|
||||
const a1p = (1 + G) * a1;
|
||||
const a2p = (1 + G) * a2;
|
||||
|
||||
const C1p = Math.sqrt(a1p * a1p + b1 * b1);
|
||||
const C2p = Math.sqrt(a2p * a2p + b2 * b2);
|
||||
|
||||
// ---- 第 2 步:色相角(度);中性色(a'=b=0)时定义为 0 ----
|
||||
const h1p = a1p === 0 && b1 === 0 ? 0 : mod360(RAD2DEG * Math.atan2(b1, a1p));
|
||||
const h2p = a2p === 0 && b2 === 0 ? 0 : mod360(RAD2DEG * Math.atan2(b2, a2p));
|
||||
|
||||
// ---- 第 3 步:ΔL'、ΔC'、ΔH' ----
|
||||
const dLp = L2 - L1;
|
||||
const dCp = C2p - C1p;
|
||||
|
||||
// Δh':跨 0°/360° 边界时取最短弧;任一颜色为中性色时为 0。
|
||||
let dhp = 0;
|
||||
if (C1p * C2p !== 0) {
|
||||
dhp = h2p - h1p;
|
||||
if (dhp > 180) {
|
||||
dhp -= 360;
|
||||
} else if (dhp < -180) {
|
||||
dhp += 360;
|
||||
}
|
||||
}
|
||||
const dHp = 2 * Math.sqrt(C1p * C2p) * Math.sin((dhp / 2) * DEG2RAD);
|
||||
|
||||
// ---- 第 4 步:平均值(含色相角边界处理)----
|
||||
const Lbp = (L1 + L2) / 2;
|
||||
const Cbp = (C1p + C2p) / 2;
|
||||
|
||||
let hbp: number;
|
||||
if (C1p * C2p !== 0) {
|
||||
// 平均色相角:|h1'-h2'| > 180 时,先给“和”加 360 再取半(等效于
|
||||
// 在平均上 +180),最后归一化到 [0,360)。此顺序不可颠倒——若先取
|
||||
// 半再加 360 再 mod,会得到错误角度(如 315° 被算成 135°)。
|
||||
let hSum = h1p + h2p;
|
||||
if (Math.abs(h1p - h2p) > 180) {
|
||||
hSum += 360;
|
||||
}
|
||||
hbp = mod360(hSum / 2);
|
||||
} else {
|
||||
// 任一颜色为中性色:平均色相角 = 两角之和(非 /2)。
|
||||
hbp = h1p + h2p;
|
||||
}
|
||||
|
||||
// ---- 第 5 步:T(色相权重系数的角度依赖)----
|
||||
const T =
|
||||
1 -
|
||||
0.17 * Math.cos((hbp - 30) * DEG2RAD) +
|
||||
0.24 * Math.cos(2 * hbp * DEG2RAD) +
|
||||
0.32 * Math.cos((3 * hbp + 6) * DEG2RAD) -
|
||||
0.2 * Math.cos((4 * hbp - 63) * DEG2RAD);
|
||||
|
||||
// ---- 第 6 步:SL、SC、SH 与旋转项 RT ----
|
||||
const dTheta = 30 * Math.exp(-Math.pow((hbp - 275) / 25, 2));
|
||||
const Rc = 2 * Math.sqrt(Math.pow(Cbp, 7) / (Math.pow(Cbp, 7) + POW25_7));
|
||||
const SL = 1 + (0.015 * Math.pow(Lbp - 50, 2)) / Math.sqrt(20 + Math.pow(Lbp - 50, 2));
|
||||
const SC = 1 + 0.045 * Cbp;
|
||||
const SH = 1 + 0.015 * Cbp * T;
|
||||
const RT = -Math.sin(2 * dTheta * DEG2RAD) * Rc;
|
||||
|
||||
// ---- 第 7 步:加权合成 ----
|
||||
const dL = dLp / (kL * SL);
|
||||
const dC = dCp / (kC * SC);
|
||||
const dH = dHp / (kH * SH);
|
||||
|
||||
return Math.sqrt(dL * dL + dC * dC + dH * dH + RT * dC * dH);
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
/**
|
||||
* 连通区域合并与噪声清除(Cleanup)。
|
||||
*
|
||||
* 参考 Zippland perler-beads 的做法:先按 LAB 距离做 4 连通 BFS 区域分割,
|
||||
* 再对每个区域做多数投票(majority vote)统一颜色;面积过小的孤立区域
|
||||
* 标记为噪声,供上层决定是否剔除。
|
||||
*
|
||||
* 两个阈值:
|
||||
* - mergeThreshold(默认 24):相邻两格之间 LAB 距离 < 该值才允许并入同一区域。
|
||||
* - maxMergeDistance(默认 24):候选格与区域种子色(区域起点)的 LAB
|
||||
* 距离上限,防止“逐格小步漂移”把差异很大的颜色串成同一个区域。
|
||||
*/
|
||||
|
||||
import { labDistance, srgbToLab, type Lab3, type Rgb3 } from './lab.js';
|
||||
|
||||
/** 合并参数(两个字段均可选,缺省用默认值)。 */
|
||||
export interface CleanupOptions {
|
||||
/** 相邻两格 LAB 距离阈值(默认 24),小于该值视为同一区域。 */
|
||||
mergeThreshold: number;
|
||||
/** 候选格与区域种子色的最大 LAB 距离(默认 24),限制区域颜色漂移。 */
|
||||
maxMergeDistance: number;
|
||||
}
|
||||
|
||||
const DEFAULT_MERGE_THRESHOLD = 24;
|
||||
const DEFAULT_MAX_MERGE_DISTANCE = 24;
|
||||
|
||||
/**
|
||||
* 4 连通 BFS 区域分割:返回每个格子的区域 id(行主序)。
|
||||
* 从每个未访问格出发做洪水填充:相邻格同时满足
|
||||
* ① 与当前格 LAB 距离 < mergeThreshold
|
||||
* ② 与区域种子色 LAB 距离 < maxMergeDistance
|
||||
* 即并入同一区域。
|
||||
*/
|
||||
function segmentRegions(
|
||||
grid: readonly Rgb3[],
|
||||
size: number,
|
||||
mergeThreshold: number,
|
||||
maxMergeDistance: number,
|
||||
): number[] {
|
||||
const n = grid.length;
|
||||
if (n === 0 || size <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const labCache: Lab3[] = grid.map(srgbToLab);
|
||||
const regionId = new Array<number>(n).fill(-1);
|
||||
let nextId = 0;
|
||||
|
||||
for (let start = 0; start < n; start++) {
|
||||
if (regionId[start] !== -1) continue;
|
||||
|
||||
const seedLab = labCache[start]!;
|
||||
const queue: number[] = [start];
|
||||
regionId[start] = nextId;
|
||||
|
||||
while (queue.length > 0) {
|
||||
const idx = queue.shift()!;
|
||||
const x = idx % size;
|
||||
const y = Math.floor(idx / size);
|
||||
|
||||
// 4 连通邻居
|
||||
const neighbors: number[] = [];
|
||||
if (x > 0) neighbors.push(idx - 1);
|
||||
if (x < size - 1) neighbors.push(idx + 1);
|
||||
if (y > 0) neighbors.push(idx - size);
|
||||
if (y < size - 1) neighbors.push(idx + size);
|
||||
|
||||
for (const nb of neighbors) {
|
||||
if (regionId[nb] !== -1) continue;
|
||||
const nbLab = labCache[nb]!;
|
||||
if (
|
||||
labDistance(nbLab, labCache[idx]!) < mergeThreshold &&
|
||||
labDistance(nbLab, seedLab) < maxMergeDistance
|
||||
) {
|
||||
regionId[nb] = nextId;
|
||||
queue.push(nb);
|
||||
}
|
||||
}
|
||||
}
|
||||
nextId++;
|
||||
}
|
||||
|
||||
return regionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* BFS 连通区域合并 + 多数投票。
|
||||
*
|
||||
* 对 grid(Rgb3[],行主序)就地做区域分割,然后把每个区域的所有格子统一
|
||||
* 为该区域内出现次数最多的颜色(多数投票;平局取扫描顺序先出现的颜色)。
|
||||
*
|
||||
* @param grid - 待合并的颜色网格(会被就地修改)。
|
||||
* @param size - 网格边长(size × size)。
|
||||
* @param options - 合并参数(均可选,默认 mergeThreshold 24 / maxMergeDistance 24)。
|
||||
* @returns 颜色被改写(与合并前不同)的格子下标数组。
|
||||
*/
|
||||
export function mergeSimilarRegions(
|
||||
grid: Rgb3[],
|
||||
size: number,
|
||||
options: Partial<CleanupOptions> = {},
|
||||
): number[] {
|
||||
const mergeThreshold = options.mergeThreshold ?? DEFAULT_MERGE_THRESHOLD;
|
||||
const maxMergeDistance = options.maxMergeDistance ?? DEFAULT_MAX_MERGE_DISTANCE;
|
||||
|
||||
const n = grid.length;
|
||||
if (n === 0 || size <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const regionId = segmentRegions(grid, size, mergeThreshold, maxMergeDistance);
|
||||
|
||||
// ---- 统计每个区域的颜色票数(rgbKey -> 票数) ----
|
||||
const votes = new Map<number, Map<string, number>>();
|
||||
const keyToColor = new Map<string, Rgb3>();
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
const rid = regionId[i]!;
|
||||
const color = grid[i]!;
|
||||
const key = `${color[0]}-${color[1]}-${color[2]}`;
|
||||
keyToColor.set(key, color);
|
||||
|
||||
const regionVotes = votes.get(rid) ?? new Map<string, number>();
|
||||
regionVotes.set(key, (regionVotes.get(key) ?? 0) + 1);
|
||||
votes.set(rid, regionVotes);
|
||||
}
|
||||
|
||||
// ---- 每个区域取票数最多的颜色(严格大于:平局取先出现的颜色) ----
|
||||
const majorityKey = new Map<number, string>();
|
||||
for (const [rid, regionVotes] of votes) {
|
||||
let bestKey = '';
|
||||
let bestCount = -1;
|
||||
for (const [key, count] of regionVotes) {
|
||||
if (count > bestCount) {
|
||||
bestCount = count;
|
||||
bestKey = key;
|
||||
}
|
||||
}
|
||||
majorityKey.set(rid, bestKey);
|
||||
}
|
||||
|
||||
// ---- 应用:改写区域颜色,记录被改写的下标 ----
|
||||
const changed: number[] = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
const target = keyToColor.get(majorityKey.get(regionId[i]!)!)!;
|
||||
const current = grid[i]!;
|
||||
if (target[0] !== current[0] || target[1] !== current[1] || target[2] !== current[2]) {
|
||||
grid[i] = target;
|
||||
changed.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
/**
|
||||
* 噪声标记:把面积小于 minRegionSize 的孤立区域标记为噪声。
|
||||
*
|
||||
* 区域分割使用默认阈值(mergeThreshold 24 / maxMergeDistance 24)。
|
||||
*
|
||||
* @param grid - 颜色网格(只读)。
|
||||
* @param size - 网格边长(size × size)。
|
||||
* @param minRegionSize - 最小区域面积(默认 4),区域格子数 < 该值视为噪声。
|
||||
* @returns 每格一个布尔值:true 表示该格属于噪声区域。
|
||||
*/
|
||||
export function removeNoise(
|
||||
grid: readonly Rgb3[],
|
||||
size: number,
|
||||
minRegionSize: number = 4,
|
||||
): boolean[] {
|
||||
const n = grid.length;
|
||||
if (n === 0 || size <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const regionId = segmentRegions(grid, size, DEFAULT_MERGE_THRESHOLD, DEFAULT_MAX_MERGE_DISTANCE);
|
||||
|
||||
// 统计每个区域的面积
|
||||
const regionSize = new Map<number, number>();
|
||||
for (const rid of regionId) {
|
||||
regionSize.set(rid, (regionSize.get(rid) ?? 0) + 1);
|
||||
}
|
||||
|
||||
return regionId.map((rid) => (regionSize.get(rid) ?? 0) < minRegionSize);
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* 色彩匹配引擎选择器 — 与 pixel-beads.com 对齐的“双色彩引擎”。
|
||||
*
|
||||
* 三种距离引擎:
|
||||
* - 'lab' : CIE LAB 欧氏距离(默认,lab.ts 原有行为)
|
||||
* - 'oklab' : OKLab 欧氏距离(感知均匀,0..1 量级)
|
||||
* - 'ciede2000' : CIEDE2000 ΔE00(行业标准感知色差公式)
|
||||
*
|
||||
* 空间约定:
|
||||
* - `colorDistance` / `findNearest` 是“同空间”原语 —— 调用方需保证
|
||||
* target 与 entry.lab 处于同一空间:'oklab' 时传 OKLab 值,
|
||||
* 'lab' / 'ciede2000' 时传 CIE LAB 值。
|
||||
* - `findNearestColorWithEngine` 是 MARD 色板专用便捷函数:入参恒为
|
||||
* CIE LAB(与 mard-palette.ts 的 findNearestColor 约定一致),
|
||||
* 'oklab' 引擎内部自动把色板条目(其 rgb)与目标(经 labToSrgb)
|
||||
* 转换到 OKLab 后再比较。
|
||||
*/
|
||||
|
||||
import type { Lab, RGB } from './lab.js';
|
||||
import { labDistance } from './lab.js';
|
||||
import { srgbToOklab, oklabDistance } from './oklab.js';
|
||||
import { ciede2000 } from './ciede2000.js';
|
||||
import { MARD_PALETTE, type MardColor } from './mard-palette-data.js';
|
||||
|
||||
/** 可用颜色距离引擎。 */
|
||||
export type ColorEngine = 'lab' | 'oklab' | 'ciede2000';
|
||||
|
||||
/** 可参与 `findNearest` 的任意色板条目的最小形状。 */
|
||||
export interface LabPaletteEntry {
|
||||
lab: Lab;
|
||||
}
|
||||
|
||||
/** D65 参考白(与 lab.ts 一致)。 */
|
||||
const XN = 0.95047;
|
||||
const ZN = 1.08883;
|
||||
|
||||
/** 归一化到 [0, 1]。 */
|
||||
function clamp01(v: number): number {
|
||||
return v < 0 ? 0 : v > 1 ? 1 : v;
|
||||
}
|
||||
|
||||
/**
|
||||
* CIE LAB (D65) → sRGB(0..255),超出色域时裁剪。
|
||||
* 仅供 OKLab 匹配路径使用:MARD 色板保存的是 CIE LAB,而 OKLab
|
||||
* 转换需从 sRGB 出发,故先把 CIE LAB 还原为 sRGB 再转换。
|
||||
*/
|
||||
function labToSrgb(lab: Lab): RGB {
|
||||
const [L, a, b] = lab;
|
||||
const fy = (L + 16) / 116;
|
||||
const fx = fy + a / 500;
|
||||
const fz = fy - b / 200;
|
||||
const fx3 = fx * fx * fx;
|
||||
const fy3 = fy * fy * fy;
|
||||
const fz3 = fz * fz * fz;
|
||||
// 逆 pivot 函数:t³ 或 (t − 16/116) / 7.787。
|
||||
const xr = fx3 > 0.008856 ? fx3 : (fx - 16 / 116) / 7.787;
|
||||
const yr = fy3 > 0.008856 ? fy3 : (fy - 16 / 116) / 7.787;
|
||||
const zr = fz3 > 0.008856 ? fz3 : (fz - 16 / 116) / 7.787;
|
||||
|
||||
const x = XN * xr;
|
||||
const y = yr;
|
||||
const z = ZN * zr;
|
||||
|
||||
// XYZ → 线性 sRGB(lab.ts 正向矩阵的逆,D65)。
|
||||
let r = 3.2406 * x - 1.5372 * y - 0.4986 * z;
|
||||
let g = -0.9689 * x + 1.8758 * y + 0.0415 * z;
|
||||
let bl = 0.0557 * x - 0.204 * y + 1.057 * z;
|
||||
|
||||
r = clamp01(r);
|
||||
g = clamp01(g);
|
||||
bl = clamp01(bl);
|
||||
|
||||
// 逆传输函数(与 oklab.ts 相同)。
|
||||
const gamma = (v: number): number => (v <= 0.0031308 ? 12.92 * v : 1.055 * Math.pow(v, 1 / 2.4) - 0.055);
|
||||
return [gamma(r) * 255, gamma(g) * 255, gamma(bl) * 255];
|
||||
}
|
||||
|
||||
/**
|
||||
* 按引擎计算两个颜色(同一空间)的距离。
|
||||
*
|
||||
* @param engine 距离引擎
|
||||
* @param a 颜色 A('oklab' 时须为 OKLab 值,否则为 CIE LAB)
|
||||
* @param b 颜色 B(与 a 同空间)
|
||||
*/
|
||||
export function colorDistance(engine: ColorEngine, a: Lab, b: Lab): number {
|
||||
switch (engine) {
|
||||
case 'lab':
|
||||
return labDistance(a, b);
|
||||
case 'oklab':
|
||||
return oklabDistance(a, b);
|
||||
case 'ciede2000':
|
||||
return ciede2000(a, b);
|
||||
default:
|
||||
// 穷举检查:新增引擎类型时若忘记分发会在这里抛错。
|
||||
throw new Error(`未知的颜色引擎: ${String(engine)}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用最近邻搜索:在任意色板中查找距 target 最近的条目。
|
||||
* target 与条目均须处于引擎对应的颜色空间。空色板抛错。
|
||||
*
|
||||
* @param engine 距离引擎
|
||||
* @param target 目标颜色('oklab' 时须为 OKLab 值,否则为 CIE LAB)
|
||||
* @param palette 色板(条目只需带 `lab` 字段)
|
||||
* @returns 最近条目及其距离
|
||||
*/
|
||||
export function findNearest<T extends LabPaletteEntry>(
|
||||
engine: ColorEngine,
|
||||
target: Lab,
|
||||
palette: readonly T[],
|
||||
): { entry: T; distance: number } {
|
||||
if (palette.length === 0) {
|
||||
throw new Error('findNearest: 色板不能为空');
|
||||
}
|
||||
let best = palette[0]!;
|
||||
let bestDistance = Number.POSITIVE_INFINITY;
|
||||
for (const entry of palette) {
|
||||
const distance = colorDistance(engine, target, entry.lab);
|
||||
if (distance < bestDistance) {
|
||||
bestDistance = distance;
|
||||
best = entry;
|
||||
}
|
||||
}
|
||||
return { entry: best, distance: bestDistance };
|
||||
}
|
||||
|
||||
/**
|
||||
* MARD 色板最近邻(引擎可选版)。
|
||||
*
|
||||
* 与 mard-palette.ts 的 findNearestColor 调用约定一致:入参为 CIE LAB,
|
||||
* 默认返回 MARD 色板条目。'oklab' 引擎时内部将色板条目按 rgb 转换到
|
||||
* OKLab、目标按 labToSrgb → OKLab 转换后再比较。
|
||||
*
|
||||
* @param lab 目标颜色(CIE LAB)
|
||||
* @param palette 色板(默认 MARD_PALETTE)
|
||||
* @param engine 距离引擎(默认 'lab',等价于 findNearestColor)
|
||||
*/
|
||||
export function findNearestColorWithEngine(
|
||||
lab: Lab,
|
||||
palette: readonly MardColor[] = MARD_PALETTE,
|
||||
engine: ColorEngine = 'lab',
|
||||
): MardColor {
|
||||
if (palette.length === 0) {
|
||||
throw new Error('findNearestColorWithEngine: 色板不能为空');
|
||||
}
|
||||
if (engine === 'oklab') {
|
||||
// OKLab 引擎:需把 CIE LAB 目标与色板条目统一转换到 OKLab。
|
||||
const targetOklab = srgbToOklab(labToSrgb(lab));
|
||||
let best = palette[0]!;
|
||||
let bestDistance = Number.POSITIVE_INFINITY;
|
||||
for (const entry of palette) {
|
||||
const distance = oklabDistance(targetOklab, srgbToOklab(entry.rgb));
|
||||
if (distance < bestDistance) {
|
||||
bestDistance = distance;
|
||||
best = entry;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
return findNearest(engine, lab, palette).entry;
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
/**
|
||||
* 抖动(dithering)模块 — 有限色板下平滑渐变的误差扩散/有序抖动。
|
||||
*
|
||||
* 问题背景:当图像被量化到少量 MARD 色板颜色时,连续渐变会产生明显的
|
||||
* 色带(banding)。抖动通过在量化决策前注入受控噪声/误差扩散,让相邻
|
||||
* 单元格在相近的色板颜色之间交替,视觉上恢复平滑的渐变过渡。
|
||||
*
|
||||
* 两种模式:
|
||||
* - 'floyd-steinberg':经典 Floyd–Steinberg 误差扩散(顺序扫描,
|
||||
* 误差按 7/16 右、3/16 左下、5/16 下、1/16 右下 分配)。
|
||||
* - 'ordered' :Bayer 4×4 有序抖动(确定性阈值矩阵,无累积误差,
|
||||
* 速度更快,但纹理呈规则图案)。
|
||||
*
|
||||
* 色板匹配统一走 CIE LAB 最近邻(srgbToLab + labDistance),与
|
||||
* image-processor.ts 的既有匹配质量保持一致。
|
||||
*/
|
||||
|
||||
import { srgbToLab, labDistance, type RGB, type Lab } from './lab.js';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 类型
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** 参与抖动的色板条目最小形状(只需提供 sRGB 颜色)。 */
|
||||
export interface DitherPaletteEntry {
|
||||
rgb: RGB;
|
||||
}
|
||||
|
||||
/** 抖动模式:'none' 为默认(关闭),与旧行为完全一致。 */
|
||||
export type DitherMode = 'none' | 'floyd-steinberg' | 'ordered';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 内部工具
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** 将颜色裁剪到 [0, 255],防止误差累积产生越界值。 */
|
||||
function clamp255(v: number): number {
|
||||
return v < 0 ? 0 : v > 255 ? 255 : v;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在色板中查找距目标最近的条目索引(CIE LAB 欧氏距离)。
|
||||
* 色板 LAB 值预先缓存,避免每个像素重复转换。
|
||||
*/
|
||||
function nearestIndex(
|
||||
rgb: RGB,
|
||||
palette: readonly DitherPaletteEntry[],
|
||||
paletteLab: readonly Lab[],
|
||||
): number {
|
||||
const targetLab = srgbToLab(rgb);
|
||||
let best = 0;
|
||||
let bestDistance = Number.POSITIVE_INFINITY;
|
||||
for (let j = 0; j < palette.length; j++) {
|
||||
const distance = labDistance(targetLab, paletteLab[j]!);
|
||||
if (distance < bestDistance) {
|
||||
bestDistance = distance;
|
||||
best = j;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/** 校验输入规模,避免索引越界。 */
|
||||
function validate(rgbGrid: readonly RGB[], width: number, height: number, palette: readonly unknown[]): void {
|
||||
if (palette.length === 0) {
|
||||
throw new Error('dither: 色板不能为空');
|
||||
}
|
||||
if (width <= 0 || height <= 0) {
|
||||
throw new Error('dither: 宽高必须为正数');
|
||||
}
|
||||
if (rgbGrid.length !== width * height) {
|
||||
throw new Error(
|
||||
`dither: 像素数量(${rgbGrid.length})与宽高乘积(${width}×${height}=${width * height})不一致`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** 预计算色板 LAB 值。 */
|
||||
function buildPaletteLab(palette: readonly DitherPaletteEntry[]): Lab[] {
|
||||
return palette.map((entry) => srgbToLab(entry.rgb));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Floyd–Steinberg 误差扩散
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Floyd–Steinberg 误差扩散抖动。
|
||||
*
|
||||
* 对每个像素(行优先顺序):
|
||||
* 1. 在 LAB 空间中查找最近的色板颜色;
|
||||
* 2. 计算量化误差(RGB 空间:原色 − 色板色);
|
||||
* 3. 按经典权重把误差扩散到尚未处理的相邻像素:
|
||||
* 7/16 → 右侧 3/16 → 左下 5/16 → 正下 1/16 → 右下
|
||||
*
|
||||
* 误差在浮点缓冲区中累积;返回的索引数组与 rgbGrid 一一对应(行优先)。
|
||||
*
|
||||
* @param rgbGrid 行优先的 RGB 像素数组
|
||||
* @param width 网格宽度
|
||||
* @param height 网格高度
|
||||
* @param palette 色板(只需含 rgb 字段)
|
||||
* @returns 每个像素对应的色板索引数组
|
||||
*/
|
||||
export function floydSteinberg(
|
||||
rgbGrid: readonly RGB[],
|
||||
width: number,
|
||||
height: number,
|
||||
palette: readonly DitherPaletteEntry[],
|
||||
): number[] {
|
||||
validate(rgbGrid, width, height, palette);
|
||||
const paletteLab = buildPaletteLab(palette);
|
||||
|
||||
// 浮点误差缓冲区(RGB 三通道)。
|
||||
const buffer = new Float64Array(rgbGrid.length * 3);
|
||||
for (let i = 0; i < rgbGrid.length; i++) {
|
||||
buffer[i * 3] = rgbGrid[i]![0];
|
||||
buffer[i * 3 + 1] = rgbGrid[i]![1];
|
||||
buffer[i * 3 + 2] = rgbGrid[i]![2];
|
||||
}
|
||||
|
||||
const result: number[] = new Array(rgbGrid.length);
|
||||
|
||||
for (let y = 0; y < height; y++) {
|
||||
for (let x = 0; x < width; x++) {
|
||||
const i = y * width + x;
|
||||
const r = clamp255(buffer[i * 3]!);
|
||||
const g = clamp255(buffer[i * 3 + 1]!);
|
||||
const b = clamp255(buffer[i * 3 + 2]!);
|
||||
|
||||
// 1. LAB 最近邻色板匹配。
|
||||
const idx = nearestIndex([r, g, b], palette, paletteLab);
|
||||
result[i] = idx;
|
||||
|
||||
// 2. 量化误差(RGB 空间)。
|
||||
const pr = palette[idx]!.rgb;
|
||||
const er = r - pr[0]!;
|
||||
const eg = g - pr[1]!;
|
||||
const eb = b - pr[2]!;
|
||||
|
||||
// 3. 误差扩散(仅分发给尚未处理的像素)。
|
||||
const add = (target: number, w: number): void => {
|
||||
buffer[target * 3] = (buffer[target * 3] ?? 0) + er * w;
|
||||
buffer[target * 3 + 1] = (buffer[target * 3 + 1] ?? 0) + eg * w;
|
||||
buffer[target * 3 + 2] = (buffer[target * 3 + 2] ?? 0) + eb * w;
|
||||
};
|
||||
|
||||
if (x + 1 < width) {
|
||||
add(i + 1, 7 / 16); // 右
|
||||
}
|
||||
if (y + 1 < height) {
|
||||
if (x > 0) {
|
||||
add(i + width - 1, 3 / 16); // 左下
|
||||
}
|
||||
add(i + width, 5 / 16); // 正下
|
||||
if (x + 1 < width) {
|
||||
add(i + width + 1, 1 / 16); // 右下
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Bayer 4×4 有序抖动
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Bayer 4×4 阈值矩阵(行优先,值域 0..15)。 */
|
||||
const BAYER_4: readonly number[] = [
|
||||
0, 8, 2, 10,
|
||||
12, 4, 14, 6,
|
||||
3, 11, 1, 9,
|
||||
15, 7, 13, 5,
|
||||
];
|
||||
|
||||
/**
|
||||
* Bayer 4×4 有序抖动。
|
||||
*
|
||||
* 对每个像素根据 (x % 4, y % 4) 查表得到确定性噪声,统一叠加到 RGB 三个
|
||||
* 通道后再做 LAB 最近邻匹配。所有通道使用同一偏移(亮度型噪声),避免
|
||||
* 引入色偏,且与二进制 Bayer 抖动的阈值语义一致:
|
||||
* 噪声 n = (7.5 − m) / 16 × 255,m ∈ 0..15 → n ∈ [−119.5, +119.5]。
|
||||
*
|
||||
* @param rgbGrid 行优先的 RGB 像素数组
|
||||
* @param width 网格宽度
|
||||
* @param height 网格高度
|
||||
* @param palette 色板(只需含 rgb 字段)
|
||||
* @returns 每个像素对应的色板索引数组
|
||||
*/
|
||||
export function orderedDither(
|
||||
rgbGrid: readonly RGB[],
|
||||
width: number,
|
||||
height: number,
|
||||
palette: readonly DitherPaletteEntry[],
|
||||
): number[] {
|
||||
validate(rgbGrid, width, height, palette);
|
||||
const paletteLab = buildPaletteLab(palette);
|
||||
|
||||
const result: number[] = new Array(rgbGrid.length);
|
||||
|
||||
for (let y = 0; y < height; y++) {
|
||||
for (let x = 0; x < width; x++) {
|
||||
const i = y * width + x;
|
||||
const m = BAYER_4[(y % 4) * 4 + (x % 4)]!;
|
||||
// 亮度型噪声:三通道同偏移,等价于按阈值矩阵做分级量化。
|
||||
const noise = ((7.5 - m) / 16) * 255;
|
||||
|
||||
const rgb = rgbGrid[i]!;
|
||||
const r = clamp255(rgb[0] + noise);
|
||||
const g = clamp255(rgb[1] + noise);
|
||||
const b = clamp255(rgb[2] + noise);
|
||||
|
||||
result[i] = nearestIndex([r, g, b], palette, paletteLab);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -102,11 +102,15 @@ function drawGrid(
|
||||
const EXPORT_MARGIN = 80;
|
||||
const EXPORT_TITLE_H = 80;
|
||||
|
||||
/** Render a full-size, printable 图纸 canvas (title + grid + 备豆清单). */
|
||||
/**
|
||||
* Render a full-size, printable 图纸 canvas (title + grid + 备豆清单).
|
||||
* @param brandName 品牌显示名(如 'MARD' / 'COCO'),用于图纸标题与备豆清单。
|
||||
*/
|
||||
export function buildExportCanvas(
|
||||
result: ProcessResult,
|
||||
gridSize: number,
|
||||
title: string,
|
||||
brandName = 'MARD',
|
||||
): HTMLCanvasElement {
|
||||
const cell = gridSize >= 96 ? 22 : 28;
|
||||
const gridPx = gridSize * cell;
|
||||
@@ -135,7 +139,7 @@ export function buildExportCanvas(
|
||||
ctx.font = '700 24px "Geist", system-ui, sans-serif';
|
||||
ctx.textAlign = 'left';
|
||||
ctx.textBaseline = 'middle';
|
||||
ctx.fillText(`${title} · ${gridSize}×${gridSize} · MARD 拼豆图纸`, EXPORT_MARGIN, 40);
|
||||
ctx.fillText(`${title} · ${gridSize}×${gridSize} · ${brandName} 拼豆图纸`, EXPORT_MARGIN, 40);
|
||||
|
||||
// ---- Grid ----
|
||||
const radius = cell * 0.42;
|
||||
@@ -174,12 +178,12 @@ export function buildExportCanvas(
|
||||
ctx.restore();
|
||||
}
|
||||
|
||||
// ---- MARD 备豆清单 ----
|
||||
// ---- 备豆清单 ----
|
||||
const paletteTop = EXPORT_TITLE_H + gridPx + 28;
|
||||
ctx.textAlign = 'left';
|
||||
ctx.font = '700 28px "Geist", system-ui, sans-serif';
|
||||
ctx.fillStyle = '#171713';
|
||||
ctx.fillText('MARD 备豆清单', EXPORT_MARGIN, paletteTop);
|
||||
ctx.fillText(`${brandName} 备豆清单`, EXPORT_MARGIN, paletteTop);
|
||||
|
||||
ctx.font = '15px "Geist", system-ui, sans-serif';
|
||||
ctx.fillStyle = '#66645d';
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* 颜色排除与重映射(Color Exclusion & Remap)。
|
||||
*
|
||||
* 参考 Zippland perler-beads 的色板排除功能:用户禁用某个色号后,原本会落
|
||||
* 到该色号的颜色全部重映射到最近的可用(未排除)色号;若整个色板都被排除,
|
||||
* 则退化为返回最近色号本身(兜底,保证总有输出)。
|
||||
*
|
||||
* 返回值为 MardColor(含 code / lab / rgb / hex),即 { code, lab } 的超集。
|
||||
*/
|
||||
|
||||
import { findNearestColor, MARD_PALETTE, type MardColor } from './mard-palette.js';
|
||||
import type { Lab3, Rgb3 } from './lab.js';
|
||||
|
||||
/**
|
||||
* 排除重映射:先找 lab 在 palette 中的最近色号;若该色号被排除,则改找
|
||||
* 最近的未排除色号。全部被排除时兜底返回最近色号本身。
|
||||
*
|
||||
* @param lab - 目标颜色(CIE LAB)。
|
||||
* @param palette - 色板(可为全部或已裁剪的子集)。
|
||||
* @param excludedCodes - 被排除的色号集合。
|
||||
* @returns 映射结果(MardColor,含 code / lab)。
|
||||
*/
|
||||
export function excludeAndRemap(
|
||||
lab: Lab3,
|
||||
palette: readonly MardColor[],
|
||||
excludedCodes: Set<string>,
|
||||
): MardColor {
|
||||
if (excludedCodes.size === 0) {
|
||||
return findNearestColor(lab, palette);
|
||||
}
|
||||
|
||||
// 过滤出未排除的色板
|
||||
const available = palette.filter((entry) => !excludedCodes.has(entry.code));
|
||||
|
||||
if (available.length === 0) {
|
||||
// 全部被排除:兜底返回最近色号
|
||||
return findNearestColor(lab, palette);
|
||||
}
|
||||
|
||||
return findNearestColor(lab, available);
|
||||
}
|
||||
|
||||
/** 可被恢复的格子的最小形状(与 image-processor 的 ProcessedCell 结构兼容)。 */
|
||||
interface MappableCell {
|
||||
code: string;
|
||||
lab: Lab3;
|
||||
rgb?: Rgb3;
|
||||
}
|
||||
|
||||
/**
|
||||
* 撤销排除映射:把 grid 中仍使用被排除色号的格子重新映射回最近的未排除
|
||||
* 色号(即“恢复”到排除前应有的状态),就地修改并返回被恢复的格子下标。
|
||||
*
|
||||
* @param grid - 格子列表(需含 code / lab,rgb 可选),会被就地修改。
|
||||
* @param excludedCodes - 被排除的色号集合。
|
||||
* @returns 被恢复(色号发生变化)的格子下标数组。
|
||||
*/
|
||||
export function restoreExcluded(grid: MappableCell[], excludedCodes: Set<string>): number[] {
|
||||
if (excludedCodes.size === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const restored: number[] = [];
|
||||
for (let i = 0; i < grid.length; i++) {
|
||||
const cell = grid[i]!;
|
||||
if (!excludedCodes.has(cell.code)) continue;
|
||||
|
||||
const remapped = excludeAndRemap(cell.lab, MARD_PALETTE, excludedCodes);
|
||||
cell.code = remapped.code;
|
||||
cell.lab = remapped.lab;
|
||||
if (cell.rgb !== undefined) {
|
||||
cell.rgb = remapped.rgb;
|
||||
}
|
||||
restored.push(i);
|
||||
}
|
||||
|
||||
return restored;
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
/**
|
||||
* Grid editing tools — pure, testable post-processing for the bead grid.
|
||||
*
|
||||
* These functions operate on a flat `GridState` (row-major `size × size`
|
||||
* cells) and never mutate their input: every transform returns a brand-new
|
||||
* grid with cloned cells, so snapshots pushed into {@link EditHistory} stay
|
||||
* immutable and safe to undo/redo.
|
||||
*
|
||||
* Cell convention: a cell whose `code` is omitted (`undefined`) represents an
|
||||
* empty / background-removed cell; a cell with a `code` is a real bead of that
|
||||
* MARD color. `gridStateToProcessResult` reconstructs a full `ProcessResult`
|
||||
* (with `empty`, `lab`, and recomputed palette counts) from a `GridState`.
|
||||
*/
|
||||
|
||||
import { srgbToLab } from './lab.js';
|
||||
import type { ProcessedCell, ProcessedPaletteEntry, ProcessResult } from './image-processor.js';
|
||||
|
||||
/** Mutable RGB tuple, matching the Rgb3 shape used by the pipeline. */
|
||||
export type Rgb = [number, number, number];
|
||||
|
||||
export interface GridCell {
|
||||
rgb: Rgb;
|
||||
/** MARD color code; omitted for empty (background) cells. */
|
||||
code?: string;
|
||||
}
|
||||
|
||||
export interface GridState {
|
||||
/** Flattened row-major cells, length `size * size`. */
|
||||
grid: GridCell[];
|
||||
size: number;
|
||||
}
|
||||
|
||||
export type Rotation = 90 | 180 | 270;
|
||||
|
||||
export type MirrorAxis = 'horizontal' | 'vertical';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Geometry helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Clone a single cell so transforms never share objects with their input. */
|
||||
function cloneCell(cell: GridCell): GridCell {
|
||||
const rgb: Rgb = [cell.rgb[0], cell.rgb[1], cell.rgb[2]];
|
||||
return cell.code === undefined ? { rgb } : { rgb, code: cell.code };
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a new grid by computing the target position `(nx, ny)` of the source
|
||||
* cell at `(x, y)`. Source cells are cloned, so the input is never mutated.
|
||||
*/
|
||||
function transformGrid(
|
||||
state: GridState,
|
||||
compute: (x: number, y: number) => { nx: number; ny: number },
|
||||
): GridState {
|
||||
const { grid, size } = state;
|
||||
const next: GridCell[] = new Array<GridCell>(grid.length);
|
||||
for (let y = 0; y < size; y++) {
|
||||
for (let x = 0; x < size; x++) {
|
||||
const { nx, ny } = compute(x, y);
|
||||
next[ny * size + nx] = cloneCell(grid[y * size + x]!);
|
||||
}
|
||||
}
|
||||
return { grid: next, size };
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate the grid clockwise by `direction` degrees. 90° and 270° are exact
|
||||
* inverse rotations: `rotateGrid(rotateGrid(s, 90), 270)` equals `s`, and
|
||||
* four 90° rotations equal the identity.
|
||||
*/
|
||||
export function rotateGrid(state: GridState, direction: Rotation): GridState {
|
||||
const { size } = state;
|
||||
return transformGrid(state, (x, y) => {
|
||||
if (direction === 90) {
|
||||
return { nx: size - 1 - y, ny: x };
|
||||
}
|
||||
if (direction === 180) {
|
||||
return { nx: size - 1 - x, ny: size - 1 - y };
|
||||
}
|
||||
return { nx: y, ny: size - 1 - x }; // 270°
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Mirror the grid across an axis.
|
||||
* - `'horizontal'`: across the horizontal axis — top ↔ bottom swap.
|
||||
* - `'vertical'`: across the vertical axis — left ↔ right swap.
|
||||
*/
|
||||
export function mirrorGrid(state: GridState, axis: MirrorAxis): GridState {
|
||||
const { size } = state;
|
||||
return transformGrid(state, (x, y) =>
|
||||
axis === 'vertical'
|
||||
? { nx: size - 1 - x, ny: y }
|
||||
: { nx: x, ny: size - 1 - y },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Diagonal flip across the main diagonal (top-left → bottom-right), i.e. a
|
||||
* transpose. Applying it twice returns the identity; combined with mirroring
|
||||
* it generates the full symmetry group from a quarter pattern.
|
||||
*/
|
||||
export function flipGrid(state: GridState): GridState {
|
||||
return transformGrid(state, (x, y) => ({ nx: y, ny: x }));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// EditHistory — undo/redo stack with immutable snapshots
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Undo/redo history for grid edits.
|
||||
*
|
||||
* Usage contract: after computing the result of an edit, call
|
||||
* `push(newState)` — the state *after* the edit. `undo()` walks back to the
|
||||
* previous state; `redo()` replays edits and recovers the exact post-edit
|
||||
* state. Every snapshot is deep-cloned on entry, so mutating the caller's
|
||||
* state afterwards never corrupts the history; pushing a new snapshot clears
|
||||
* the redo stack.
|
||||
*/
|
||||
export class EditHistory {
|
||||
private past: GridState[] = [];
|
||||
private future: GridState[] = [];
|
||||
private current: GridState | null = null;
|
||||
|
||||
constructor(private limit = 50) {
|
||||
this.limit = Math.max(1, Math.floor(limit));
|
||||
}
|
||||
|
||||
get canUndo(): boolean {
|
||||
return this.past.length > 0;
|
||||
}
|
||||
|
||||
get canRedo(): boolean {
|
||||
return this.future.length > 0;
|
||||
}
|
||||
|
||||
/** Record the state that exists *after* an edit was applied. */
|
||||
push(state: GridState): void {
|
||||
if (this.current !== null) {
|
||||
this.past.push(this.current);
|
||||
while (this.past.length > this.limit) {
|
||||
this.past.shift(); // drop the oldest snapshot
|
||||
}
|
||||
}
|
||||
this.current = cloneGrid(state);
|
||||
this.future = [];
|
||||
}
|
||||
|
||||
/** Revert to the state before the most recent edit, or `null` at the start. */
|
||||
undo(): GridState | null {
|
||||
const prev = this.past.pop();
|
||||
if (prev === undefined) return null;
|
||||
if (this.current !== null) this.future.push(this.current);
|
||||
this.current = prev;
|
||||
return cloneGrid(prev);
|
||||
}
|
||||
|
||||
/** Re-apply the last undone edit, or `null` at the end. */
|
||||
redo(): GridState | null {
|
||||
const next = this.future.pop();
|
||||
if (next === undefined) return null;
|
||||
if (this.current !== null) this.past.push(this.current);
|
||||
this.current = next;
|
||||
return cloneGrid(next);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.past = [];
|
||||
this.future = [];
|
||||
this.current = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Deep-clone a GridState (fresh array + fresh cells/tuples). */
|
||||
function cloneGrid(state: GridState): GridState {
|
||||
return { grid: state.grid.map(cloneCell), size: state.size };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ProcessResult ↔ GridState conversion
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Convert a pipeline result (flat `ProcessedCell[]`) into a GridState. */
|
||||
export function resultToGridState(result: ProcessResult, size: number): GridState {
|
||||
const grid: GridCell[] = result.grid.map((cell) =>
|
||||
cell.empty
|
||||
? { rgb: [cell.rgb[0], cell.rgb[1], cell.rgb[2]] }
|
||||
: { rgb: [cell.rgb[0], cell.rgb[1], cell.rgb[2]], code: cell.code },
|
||||
);
|
||||
return { grid, size };
|
||||
}
|
||||
|
||||
/** Format an RGB tuple as a CSS hex string (e.g. `#FAF4C8`). */
|
||||
function rgbToHex(rgb: Rgb): string {
|
||||
const channel = (v: number) => Math.round(v).toString(16).padStart(2, '0');
|
||||
return `#${channel(rgb[0])}${channel(rgb[1])}${channel(rgb[2])}`.toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuild a full `ProcessResult` from a GridState, recomputing per-color bead
|
||||
* counts from the edited grid. Empty cells (no `code`) are excluded from the
|
||||
* counts and marked `empty` for the renderer.
|
||||
*/
|
||||
export function gridStateToProcessResult(state: GridState): ProcessResult {
|
||||
const { grid, size } = state;
|
||||
const codeInfo = new Map<string, { count: number; rgb: Rgb }>();
|
||||
|
||||
const cells: ProcessedCell[] = grid.map((cell, i) => {
|
||||
const x = i % size;
|
||||
const y = Math.floor(i / size);
|
||||
if (cell.code === undefined) {
|
||||
return { x, y, rgb: cell.rgb, lab: srgbToLab(cell.rgb), code: '', empty: true };
|
||||
}
|
||||
const info = codeInfo.get(cell.code) ?? { count: 0, rgb: cell.rgb };
|
||||
info.count += 1;
|
||||
info.rgb = cell.rgb;
|
||||
codeInfo.set(cell.code, info);
|
||||
return { x, y, rgb: cell.rgb, lab: srgbToLab(cell.rgb), code: cell.code, empty: false };
|
||||
});
|
||||
|
||||
const palette: ProcessedPaletteEntry[] = [...codeInfo.entries()]
|
||||
.map(([code, info]) => ({
|
||||
code,
|
||||
hex: rgbToHex(info.rgb),
|
||||
rgb: info.rgb,
|
||||
count: info.count,
|
||||
}))
|
||||
.sort((a, b) => b.count - a.count);
|
||||
|
||||
return {
|
||||
grid: cells,
|
||||
palette,
|
||||
totalBeads: cells.filter((c) => !c.empty).length,
|
||||
};
|
||||
}
|
||||
@@ -7,14 +7,21 @@
|
||||
* majority bucket average as the cell color.
|
||||
* 3. Optional background removal via edge-BFS (see `background.ts`).
|
||||
* 4. k-means++ quantization (5 iterations) to reduce to `maxColors`.
|
||||
* 5. Map each cluster centroid to the nearest MARD palette color.
|
||||
* 6. Count beads per color, build the result grid.
|
||||
* 5. Optional connected-region cleanup (BFS merge + majority vote).
|
||||
* 6. Map each cluster centroid to the nearest MARD palette color, honoring
|
||||
* optional color exclusions (excluded codes remap to the nearest allowed).
|
||||
* 7. Optional dithering (Floyd–Steinberg / Bayer) on cell colors.
|
||||
* 8. Count beads per color, build the result grid.
|
||||
*/
|
||||
|
||||
import { srgbToLab, type Lab3, type Rgb3 } from './lab.js';
|
||||
import { labDistance, srgbToLab, type Lab3, type Rgb3 } from './lab.js';
|
||||
import { kMeansQuantize } from './quantize.js';
|
||||
import { removeBackground } from './background.js';
|
||||
import { findNearestColor, MARD_PALETTE, type MardColor } from './mard-palette.js';
|
||||
import { MARD_PALETTE, type MardColor } from './mard-palette.js';
|
||||
import { findNearestByBrand, getBrandPalette } from './brand-palettes.js';
|
||||
import { mergeSimilarRegions, type CleanupOptions } from './cleanup.js';
|
||||
import { excludeAndRemap } from './exclusion.js';
|
||||
import { floydSteinberg, orderedDither, type DitherMode } from './dither.js';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
@@ -45,6 +52,14 @@ export interface ProcessOptions {
|
||||
removeBg: boolean;
|
||||
/** LAB distance threshold for background removal (default 20). */
|
||||
bgThreshold: number;
|
||||
/** Dithering mode applied during palette matching (default 'none'). */
|
||||
dither?: DitherMode;
|
||||
/** 可选:k-means 后的连通区域合并参数(缺省不合并)。 */
|
||||
cleanup?: CleanupOptions;
|
||||
/** 可选:要排除的 MARD 色号列表(映射阶段重映射到最近的未排除色)。 */
|
||||
excludedCodes?: string[];
|
||||
/** 色号品牌(MARD / COCO / 漫漫 / 盼盼 / 咪小窝),缺省 'MARD';非 MARD 时格子色号切换为该品牌编码。 */
|
||||
brandId?: string;
|
||||
}
|
||||
|
||||
export interface ProcessResult {
|
||||
@@ -74,7 +89,9 @@ export function processImage(
|
||||
imageData: ImageData,
|
||||
options: ProcessOptions,
|
||||
): ProcessResult {
|
||||
const { gridSize, maxColors, removeBg, bgThreshold } = options;
|
||||
const { gridSize, maxColors, removeBg, bgThreshold, cleanup, excludedCodes } = options;
|
||||
const brandId = options.brandId ?? 'MARD';
|
||||
const isDefaultBrand = brandId === 'MARD';
|
||||
const data = imageData.data;
|
||||
const supersampledSize = gridSize * CELL_BLOCK;
|
||||
|
||||
@@ -145,19 +162,72 @@ export function processImage(
|
||||
Math.min(maxColors, foregroundLab.length),
|
||||
);
|
||||
|
||||
// ---- 6. Map each centroid to nearest MARD color (deduplicated) ----
|
||||
const mardColors: MardColor[] = [];
|
||||
// ---- 5.5 可选:k-means 后连通区域合并(BFS + 多数投票) ----
|
||||
// 先把每格映射到最近的量化质心,得到“量化网格”,再对相近颜色的连通
|
||||
// 区域做合并;合并结果回写 cellColors / cellLab,后续色板映射基于合并
|
||||
// 后的颜色,保证前后一致。
|
||||
const excludedSet = new Set(excludedCodes ?? []);
|
||||
// 品牌映射模式下排除列表针对 MARD 色号,不适用于其他品牌色号。
|
||||
const remapExclusions = isDefaultBrand ? excludedSet : new Set<string>();
|
||||
if (cleanup && centroids.length > 0) {
|
||||
const quantizedGrid: Rgb3[] = cellColors.map((_, i) => {
|
||||
let bestIndex = 0;
|
||||
let bestDistance = Number.POSITIVE_INFINITY;
|
||||
for (let j = 0; j < centroids.length; j++) {
|
||||
const d = labDistance(cellLab[i]!, centroids[j]!);
|
||||
if (d < bestDistance) {
|
||||
bestDistance = d;
|
||||
bestIndex = j;
|
||||
}
|
||||
}
|
||||
return labToRgb(centroids[bestIndex]!);
|
||||
});
|
||||
|
||||
mergeSimilarRegions(quantizedGrid, gridSize, cleanup);
|
||||
|
||||
for (let i = 0; i < cellColors.length; i++) {
|
||||
cellColors[i] = quantizedGrid[i]!;
|
||||
cellLab[i] = srgbToLab(quantizedGrid[i]!);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- 6. Map each centroid to nearest brand color (deduplicated) ----
|
||||
const activeColors: MardColor[] = [];
|
||||
const seenCodes = new Set<string>();
|
||||
for (const centroid of centroids) {
|
||||
const mard = findNearestColor(centroid, MARD_PALETTE);
|
||||
if (!seenCodes.has(mard.code)) {
|
||||
seenCodes.add(mard.code);
|
||||
mardColors.push(mard);
|
||||
const color = isDefaultBrand
|
||||
? excludeAndRemap(centroid, MARD_PALETTE, excludedSet)
|
||||
: findNearestByBrand(centroid, brandId);
|
||||
if (!seenCodes.has(color.code)) {
|
||||
seenCodes.add(color.code);
|
||||
activeColors.push(color);
|
||||
}
|
||||
}
|
||||
|
||||
// If quantization produced no colors, fall back to the full palette.
|
||||
const activePalette = mardColors.length > 0 ? mardColors : MARD_PALETTE;
|
||||
const activePalette =
|
||||
activeColors.length > 0
|
||||
? activeColors
|
||||
: isDefaultBrand
|
||||
? MARD_PALETTE
|
||||
: getBrandPalette(brandId);
|
||||
|
||||
// ---- 6.5 Optional dithering: pre-compute palette indices for each cell ----
|
||||
// Applied on the per-cell averaged colors before palette matching, using the
|
||||
// reduced active palette (fewer colors → dithering matters most). Background
|
||||
// cells are snap-fitted to a palette color so they emit zero quantization
|
||||
// error, preventing error from bleeding across the background boundary.
|
||||
let ditherIndices: number[] | undefined;
|
||||
const dither = options.dither ?? 'none';
|
||||
if (dither !== 'none' && activePalette.length > 0) {
|
||||
const ditherGrid: Rgb3[] = cellColors.map((c, i) =>
|
||||
backgroundIndices.has(i) ? activePalette[0]!.rgb : c,
|
||||
);
|
||||
ditherIndices =
|
||||
dither === 'floyd-steinberg'
|
||||
? floydSteinberg(ditherGrid, gridSize, gridSize, activePalette)
|
||||
: orderedDither(ditherGrid, gridSize, gridSize, activePalette);
|
||||
}
|
||||
|
||||
// ---- 7. Build result grid with bead counts ----
|
||||
const grid: ProcessedCell[] = [];
|
||||
@@ -179,8 +249,12 @@ export function processImage(
|
||||
continue;
|
||||
}
|
||||
|
||||
// Map pixel to nearest MARD color from the active palette.
|
||||
const mard = findNearestColor(cellLab[i]!, activePalette);
|
||||
// Map pixel to nearest active-palette color (dithering index when enabled).
|
||||
// 排除映射:被排除的色号重映射到最近的未排除色(仅 MARD 模式生效)。
|
||||
const mard =
|
||||
ditherIndices !== undefined
|
||||
? activePalette[ditherIndices[i]!]!
|
||||
: excludeAndRemap(cellLab[i]!, activePalette, remapExclusions);
|
||||
const count = colorCount.get(mard.code) ?? 0;
|
||||
colorCount.set(mard.code, count + 1);
|
||||
|
||||
@@ -194,10 +268,11 @@ export function processImage(
|
||||
});
|
||||
}
|
||||
|
||||
// Build palette sorted by count descending
|
||||
// Build palette sorted by count descending (lookup from the active palette,
|
||||
// so non-MARD brand codes resolve to their own hex/rgb).
|
||||
const palette: ProcessedPaletteEntry[] = [];
|
||||
for (const [code, count] of colorCount) {
|
||||
const mard = MARD_PALETTE.find((m) => m.code === code);
|
||||
const mard = activePalette.find((m) => m.code === code);
|
||||
if (mard) {
|
||||
palette.push({
|
||||
code: mard.code,
|
||||
@@ -214,4 +289,38 @@ export function processImage(
|
||||
palette,
|
||||
totalBeads: grid.filter((c) => !c.empty).length,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* CIE LAB → sRGB(srgbToLab 的逆变换,色域外裁剪到 [0,1])。
|
||||
* 仅供量化合并阶段把质心 LAB 还原为 RGB 使用。
|
||||
*/
|
||||
function labToRgb(lab: Lab3): Rgb3 {
|
||||
const [L, a, b] = lab;
|
||||
const fy = (L + 16) / 116;
|
||||
const fx = fy + a / 500;
|
||||
const fz = fy - b / 200;
|
||||
const fx3 = fx * fx * fx;
|
||||
const fy3 = fy * fy * fy;
|
||||
const fz3 = fz * fz * fz;
|
||||
const xr = fx3 > 0.008856 ? fx3 : (fx - 16 / 116) / 7.787;
|
||||
const yr = fy3 > 0.008856 ? fy3 : (fy - 16 / 116) / 7.787;
|
||||
const zr = fz3 > 0.008856 ? fz3 : (fz - 16 / 116) / 7.787;
|
||||
const x = 0.95047 * xr;
|
||||
const y = yr;
|
||||
const z = 1.08883 * zr;
|
||||
let r = 3.2406 * x - 1.5372 * y - 0.4986 * z;
|
||||
let g = -0.9689 * x + 1.8758 * y + 0.0415 * z;
|
||||
let bl = 0.0557 * x - 0.204 * y + 1.057 * z;
|
||||
const clamp01 = (v: number): number => (v < 0 ? 0 : v > 1 ? 1 : v);
|
||||
r = clamp01(r);
|
||||
g = clamp01(g);
|
||||
bl = clamp01(bl);
|
||||
const gamma = (v: number): number =>
|
||||
v <= 0.0031308 ? 12.92 * v : 1.055 * Math.pow(v, 1 / 2.4) - 0.055;
|
||||
return [
|
||||
Math.round(gamma(r) * 255),
|
||||
Math.round(gamma(g) * 255),
|
||||
Math.round(gamma(bl) * 255),
|
||||
];
|
||||
}
|
||||
@@ -13,6 +13,11 @@ export { kMeansQuantize } from './quantize.js';
|
||||
|
||||
export { removeBackground } from './background.js';
|
||||
|
||||
export { mergeSimilarRegions, removeNoise } from './cleanup.js';
|
||||
export type { CleanupOptions } from './cleanup.js';
|
||||
|
||||
export { excludeAndRemap, restoreExcluded } from './exclusion.js';
|
||||
|
||||
export { MARD_PALETTE, findNearestColor, nearestColorFromRgb } from './mard-palette.js';
|
||||
export type { MardColor } from './mard-palette-data.js';
|
||||
|
||||
@@ -24,5 +29,8 @@ export type {
|
||||
ProcessResult,
|
||||
} from './image-processor.js';
|
||||
|
||||
export { floydSteinberg, orderedDither } from './dither.js';
|
||||
export type { DitherMode, DitherPaletteEntry } from './dither.js';
|
||||
|
||||
export { renderPattern, drawPaletteLegend, rgbToCss } from './pattern-renderer.js';
|
||||
export type { PatternRenderOptions, LegendRenderOptions } from './pattern-renderer.js';
|
||||
@@ -0,0 +1,120 @@
|
||||
/**
|
||||
* OKLab 色彩空间工具(Björn Ottosson, 2020)。
|
||||
*
|
||||
* OKLab 是一个感知均匀的色彩空间:欧几里得距离 ≈ 人眼感知色差,
|
||||
* 相比 CIE LAB(lab.ts 的欧氏距离)对亮度-色度联合误差更鲁棒,
|
||||
* 是 pixel-beads.com 双色彩引擎(OKLab + CIEDE2000)的默认模式。
|
||||
*
|
||||
* 标度约定(与 CIE LAB 不同,务必注意):
|
||||
* - sRGB 通道: 0..255(与 lab.ts / RGB 类型一致)
|
||||
* - OKLab 的 L / a / b: 0..1 量级(sRGB 白 = [1, 0, 0])
|
||||
*
|
||||
* 实现采用 Ottosson 论文中发布的常量:
|
||||
* sRGB 线性 → LMS(特殊矩阵)→ 立方根 → OKLab(融合矩阵)。
|
||||
* 参考: https://bottosson.github.io/posts/oklab/
|
||||
*/
|
||||
|
||||
import type { RGB, Lab } from './lab.js';
|
||||
|
||||
/** 线性化阈值(与 lab.ts 相同的 sRGB 传输函数)。 */
|
||||
const EPSILON = 0.04045;
|
||||
|
||||
/** sRGB(0..255) → 线性 sRGB 传输函数(与 lab.ts 完全一致)。 */
|
||||
function linearizeChannel(v: number): number {
|
||||
const c = v / 255;
|
||||
return c > EPSILON ? Math.pow((c + 0.055) / 1.055, 2.4) : c / 12.92;
|
||||
}
|
||||
|
||||
/** 线性 sRGB(0..1) → sRGB(0..255) 逆传输函数。 */
|
||||
function delinearizeChannel(v: number): number {
|
||||
return v <= 0.0031308 ? 12.92 * v : 1.055 * Math.pow(v, 1 / 2.4) - 0.055;
|
||||
}
|
||||
|
||||
/** 线性 sRGB → LMS(Ottosson 的感知基矩阵,行主序 3×3)。 */
|
||||
const SRGB_TO_LMS = [
|
||||
0.4122214708, 0.5363325363, 0.0514459929,
|
||||
0.2119034982, 0.6806995451, 0.1073969566,
|
||||
0.0883024619, 0.2817188376, 0.6299787005,
|
||||
] as const;
|
||||
|
||||
/** LMS → OKLab(融合矩阵,行主序 3×3)。 */
|
||||
const LMS_TO_OKLAB = [
|
||||
0.2104542553, 0.793617785, -0.0040720468,
|
||||
1.9779984951, -2.428592205, 0.4505937099,
|
||||
0.0259040371, 0.7827717662, -0.808675766,
|
||||
] as const;
|
||||
|
||||
/** 线性 sRGB → LMS。 */
|
||||
function srgbToLms(r: number, g: number, b: number): [number, number, number] {
|
||||
const m = SRGB_TO_LMS;
|
||||
return [
|
||||
m[0]! * r + m[1]! * g + m[2]! * b,
|
||||
m[3]! * r + m[4]! * g + m[5]! * b,
|
||||
m[6]! * r + m[7]! * g + m[8]! * b,
|
||||
];
|
||||
}
|
||||
|
||||
/** LMS → OKLab(对 LMS 逐通道取立方根后线性融合)。 */
|
||||
function lmsToOklab(l: number, m: number, s: number): Lab {
|
||||
const lc = Math.cbrt(l);
|
||||
const mc = Math.cbrt(m);
|
||||
const sc = Math.cbrt(s);
|
||||
const k = LMS_TO_OKLAB;
|
||||
return [
|
||||
k[0]! * lc + k[1]! * mc + k[2]! * sc,
|
||||
k[3]! * lc + k[4]! * mc + k[5]! * sc,
|
||||
k[6]! * lc + k[7]! * mc + k[8]! * sc,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* sRGB(0..255) → OKLab(L / a / b 均为 0..1 量级,白色 = [1, 0, 0])。
|
||||
*/
|
||||
export function srgbToOklab(rgb: RGB): Lab {
|
||||
const [r, g, b] = rgb;
|
||||
const lr = linearizeChannel(r);
|
||||
const lg = linearizeChannel(g);
|
||||
const lb = linearizeChannel(b);
|
||||
const [l, m, s] = srgbToLms(lr, lg, lb);
|
||||
return lmsToOklab(l, m, s);
|
||||
}
|
||||
|
||||
/**
|
||||
* OKLab(0..1 量级)→ sRGB(0..255)。
|
||||
*
|
||||
* 逆运算:OKLab → LMS(融合矩阵的逆)→ 线性 sRGB(LMS 矩阵的逆)→
|
||||
* 逆传输函数。理论上 OKLab 空间覆盖超出 sRGB 色域的颜色,
|
||||
* 因此逆变换后按 sRGB 色域裁剪(用于调色板预览等场景)。
|
||||
*/
|
||||
export function oklabToSrgb(lab: Lab): RGB {
|
||||
const [L, a, b] = lab;
|
||||
// OKLab → LMS(对融合矩阵求逆)。
|
||||
const lms = [
|
||||
L + 0.3963377774 * a + 0.2158037573 * b,
|
||||
L - 0.1055613458 * a - 0.0638541728 * b,
|
||||
L - 0.0894841775 * a - 1.291485548 * b,
|
||||
];
|
||||
// LMS → 线性 sRGB(对感知基矩阵求逆)。
|
||||
const l = lms[0]! ** 3;
|
||||
const m = lms[1]! ** 3;
|
||||
const s = lms[2]! ** 3;
|
||||
const rLinear = 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s;
|
||||
const gLinear = -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s;
|
||||
const bLinear = -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s;
|
||||
const clamp = (v: number): number => (v < 0 ? 0 : v > 1 ? 1 : v);
|
||||
return [
|
||||
delinearizeChannel(clamp(rLinear)) * 255,
|
||||
delinearizeChannel(clamp(gLinear)) * 255,
|
||||
delinearizeChannel(clamp(bLinear)) * 255,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* OKLab 空间中的欧几里得距离(感知均匀,0..1 量级)。
|
||||
*/
|
||||
export function oklabDistance(a: Lab, b: Lab): number {
|
||||
const dl = a[0] - b[0];
|
||||
const da = a[1] - b[1];
|
||||
const db = a[2] - b[2];
|
||||
return Math.sqrt(dl * dl + da * da + db * db);
|
||||
}
|
||||
@@ -7,6 +7,8 @@ export interface PatternSettings {
|
||||
maxColors: number;
|
||||
removeBg: boolean;
|
||||
showGrid: boolean;
|
||||
/** 色号品牌 ID(MARD / COCO / 漫漫 / 盼盼 / 咪小窝),默认 'MARD'。 */
|
||||
brandId: string;
|
||||
}
|
||||
|
||||
/** A successfully decoded source image. */
|
||||
@@ -54,13 +56,14 @@ function sampleImageData(element: HTMLImageElement, gridSize: number): ImageData
|
||||
|
||||
/** Run the full pipeline on a decoded image. */
|
||||
function runPipeline(source: SourceImage, settings: PatternSettings): ProcessResult {
|
||||
const { gridSize, maxColors, removeBg } = settings;
|
||||
const { gridSize, maxColors, removeBg, brandId } = settings;
|
||||
const imageData = sampleImageData(source.element, gridSize);
|
||||
return processImage(imageData, {
|
||||
gridSize,
|
||||
maxColors,
|
||||
removeBg,
|
||||
bgThreshold: BG_THRESHOLD,
|
||||
brandId,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -849,6 +849,67 @@ a {
|
||||
padding: 0 var(--space-4) var(--space-3);
|
||||
}
|
||||
|
||||
/* ---- 02 编辑工具栏(撤销/重做/旋转/镜像/翻转) ---- */
|
||||
.edit-toolbar {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: var(--space-2);
|
||||
padding: var(--space-2) var(--space-3);
|
||||
border-bottom: 1px solid var(--border);
|
||||
background: var(--bg-hover);
|
||||
}
|
||||
|
||||
.edit-tool-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: var(--space-1);
|
||||
min-height: 30px;
|
||||
padding: 0 var(--space-3);
|
||||
border-radius: var(--radius-md);
|
||||
background: var(--bg-card);
|
||||
border: 1px solid var(--border);
|
||||
color: var(--text-secondary);
|
||||
font-size: var(--text-sm);
|
||||
font-weight: var(--weight-medium);
|
||||
cursor: pointer;
|
||||
white-space: nowrap;
|
||||
transition:
|
||||
background 0.15s ease,
|
||||
border-color 0.15s ease,
|
||||
color 0.15s ease;
|
||||
}
|
||||
|
||||
.edit-tool-btn:hover:not(:disabled) {
|
||||
background: var(--bg-subtle);
|
||||
border-color: var(--border-strong);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.edit-tool-btn:active:not(:disabled) {
|
||||
background: var(--bg-subtle);
|
||||
transform: translateY(1px);
|
||||
}
|
||||
|
||||
.edit-tool-btn:disabled {
|
||||
color: var(--text-disabled);
|
||||
background: var(--bg-card);
|
||||
border-color: var(--border);
|
||||
cursor: not-allowed;
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
.edit-tool-icon {
|
||||
flex: none;
|
||||
}
|
||||
|
||||
.edit-tool-sep {
|
||||
width: 1px;
|
||||
align-self: stretch;
|
||||
margin: 0 var(--space-1);
|
||||
background: var(--border);
|
||||
}
|
||||
|
||||
/* ---- 03 MARD 备料清单 ---- */
|
||||
.inv-lock-tag {
|
||||
display: inline-flex;
|
||||
@@ -1207,6 +1268,17 @@ a {
|
||||
grid-column: auto;
|
||||
}
|
||||
|
||||
/* 编辑工具栏:窄屏下紧凑排布,按钮文字保留、图标缩小 */
|
||||
.edit-toolbar {
|
||||
gap: var(--space-1);
|
||||
padding: var(--space-2);
|
||||
}
|
||||
|
||||
.edit-tool-btn {
|
||||
min-height: 28px;
|
||||
padding: 0 var(--space-2);
|
||||
}
|
||||
|
||||
/* 底部固定 CTA */
|
||||
.mobile-cta {
|
||||
display: flex;
|
||||
|
||||
Reference in new issue
Block a user