基本完成自动色板生成功能。

This commit is contained in:
徐涛
2025-01-16 15:47:35 +08:00
parent b3cdb517a5
commit c9626f3b8e
3 changed files with 77 additions and 2 deletions

View File

@@ -0,0 +1,55 @@
import { useMemo } from 'react';
import { useColorFunction } from '../../ColorFunctionContext';
import { FlexColorStand } from '../../components/FlexColorStand';
type PaletteColorsProps = {
color: string;
swatchAmount: number;
referenceBias: number;
useReference: boolean;
min: number;
max: number;
copyMode: 'hex' | 'rgb' | 'hsl' | 'lab' | 'oklch';
};
export function PaletteColors({
color = '000000',
swatchAmount,
referenceBias,
useReference,
min,
max,
copyMode,
}: PaletteColorsProps) {
const { colorFn } = useColorFunction();
const colors = useMemo(() => {
if (!colorFn) {
return Array.from({ length: swatchAmount }, () => color);
}
try {
if (!useReference) {
return colorFn.generate_palette_from_color(color, swatchAmount, min, max);
} else {
return colorFn.generate_palette_from_color(
color,
swatchAmount,
min,
max,
useReference,
referenceBias,
);
}
} catch (e) {
console.error('[Generate Auto Palette]', e);
}
return Array.from({ length: swatchAmount }, () => color);
}, [color, swatchAmount, referenceBias, useReference, min, max]);
return (
<>
{colors.map((c, index) => (
<FlexColorStand key={`${c}-${index}`} color={c} valueMode={copyMode} />
))}
</>
);
}