Complete color management system with picker, swatches, and eyedropper: **Color Utilities (lib/color-utils.ts)** - RGB ↔ Hex conversion - RGB ↔ HSV conversion - Color validation (isValidHex) - getColorAtPoint for eyedropper - Default 20-color palette - Hex normalization **Color Store (store/color-store.ts)** - Primary/secondary color management - Recent colors history (max 20) - Custom swatches (max 20) - Color swap functionality - Zustand persist middleware (localStorage) **Components** ColorPicker: - HSV color space selector - Interactive saturation/value picker (200x160px) - Hue slider (vertical gradient) - Hex input with validation - RGB value display - Pointer drag support - Real-time updates - Color preview ColorSwatches: - Default 20-color palette grid (10x2) - Custom swatches with add/remove - Active color highlighting - Hover scaling effect - Delete button on hover - Color tooltips ColorPanel: - Tabbed interface (Picker/Swatches/Recent) - Primary/Secondary color display - Color swap button - Recent colors grid (max 20) - Tab navigation (Palette/Clock icons) - 256px wide panel - Persistent state **Eyedropper Tool** - Pick color from active layer - Click or drag to sample - Updates primary color - Integrates with ColorPanel - Crosshair cursor **Features** ✓ HSV color picker with gradient ✓ Hex color input validation ✓ RGB value display ✓ 20 default color swatches ✓ Unlimited custom swatches (max 20 stored) ✓ Recent colors auto-tracking ✓ Primary/secondary color swap ✓ Eyedropper tool to sample canvas ✓ Persistent color preferences ✓ Smooth drag interactions ✓ Real-time color updates **Integration** - EditorLayout: Added ColorPanel (256px) - ToolPalette: Added Eyedropper icon - CanvasWithTools: Eyedropper support - Tool settings: Removed basic color picker - Color syncs with tool store **UI/UX** - 3-tab navigation (Picker, Swatches, Recent) - Primary color: Large square - Secondary color: Small overlap square - Active tab highlighting - Hover effects on all swatches - Smooth transitions - Keyboard accessible **Performance** - Efficient HSV calculations - LocalStorage persistence - Pointer event optimization - Drag state management - Build time: ~1.3s **State Management** - Zustand store with persist - Auto-add to recent on use - Max limits prevent memory bloat - Clean swap operation Ready for Phase 6: File Operations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
165 lines
5.5 KiB
TypeScript
165 lines
5.5 KiB
TypeScript
'use client';
|
|
|
|
import { useState } from 'react';
|
|
import { useColorStore } from '@/store/color-store';
|
|
import { useToolStore } from '@/store';
|
|
import { ColorPicker } from './color-picker';
|
|
import { ColorSwatches } from './color-swatches';
|
|
import { ArrowLeftRight, Palette, Clock } from 'lucide-react';
|
|
import { cn } from '@/lib/utils';
|
|
|
|
type Tab = 'picker' | 'swatches' | 'recent';
|
|
|
|
export function ColorPanel() {
|
|
const {
|
|
primaryColor,
|
|
secondaryColor,
|
|
recentColors,
|
|
customSwatches,
|
|
setPrimaryColor,
|
|
setSecondaryColor,
|
|
swapColors,
|
|
addCustomSwatch,
|
|
removeCustomSwatch,
|
|
} = useColorStore();
|
|
|
|
const { setColor } = useToolStore();
|
|
const [activeTab, setActiveTab] = useState<Tab>('picker');
|
|
|
|
const handleColorChange = (color: string) => {
|
|
setPrimaryColor(color);
|
|
setColor(color);
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col h-full bg-card border-r border-border w-64">
|
|
{/* Header */}
|
|
<div className="border-b border-border p-3">
|
|
<h2 className="text-sm font-semibold text-card-foreground">Colors</h2>
|
|
</div>
|
|
|
|
{/* Current colors display */}
|
|
<div className="p-3 border-b border-border">
|
|
<div className="flex items-center gap-2">
|
|
{/* Primary/Secondary color squares */}
|
|
<div className="relative">
|
|
<button
|
|
onClick={() => handleColorChange(primaryColor)}
|
|
className="w-12 h-12 rounded-md border-2 border-border relative z-10"
|
|
style={{ backgroundColor: primaryColor }}
|
|
title={`Primary: ${primaryColor}`}
|
|
/>
|
|
<button
|
|
onClick={() => setSecondaryColor(secondaryColor)}
|
|
className="absolute w-8 h-8 rounded-md border-2 border-border -bottom-1 -right-1"
|
|
style={{ backgroundColor: secondaryColor }}
|
|
title={`Secondary: ${secondaryColor}`}
|
|
/>
|
|
</div>
|
|
|
|
{/* Swap button */}
|
|
<button
|
|
onClick={swapColors}
|
|
className="p-2 hover:bg-accent rounded-md transition-colors"
|
|
title="Swap colors (X)"
|
|
>
|
|
<ArrowLeftRight className="h-4 w-4" />
|
|
</button>
|
|
|
|
{/* Color values */}
|
|
<div className="flex-1 text-xs space-y-1">
|
|
<div className="font-mono">{primaryColor}</div>
|
|
<div className="font-mono text-muted-foreground">{secondaryColor}</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Tabs */}
|
|
<div className="flex border-b border-border">
|
|
<button
|
|
onClick={() => setActiveTab('picker')}
|
|
className={cn(
|
|
'flex-1 flex items-center justify-center gap-2 py-2 text-sm transition-colors',
|
|
activeTab === 'picker'
|
|
? 'bg-background text-foreground border-b-2 border-primary'
|
|
: 'text-muted-foreground hover:text-foreground hover:bg-accent'
|
|
)}
|
|
>
|
|
<Palette className="h-4 w-4" />
|
|
Picker
|
|
</button>
|
|
<button
|
|
onClick={() => setActiveTab('swatches')}
|
|
className={cn(
|
|
'flex-1 flex items-center justify-center gap-2 py-2 text-sm transition-colors',
|
|
activeTab === 'swatches'
|
|
? 'bg-background text-foreground border-b-2 border-primary'
|
|
: 'text-muted-foreground hover:text-foreground hover:bg-accent'
|
|
)}
|
|
>
|
|
Swatches
|
|
</button>
|
|
<button
|
|
onClick={() => setActiveTab('recent')}
|
|
className={cn(
|
|
'flex-1 flex items-center justify-center gap-2 py-2 text-sm transition-colors',
|
|
activeTab === 'recent'
|
|
? 'bg-background text-foreground border-b-2 border-primary'
|
|
: 'text-muted-foreground hover:text-foreground hover:bg-accent'
|
|
)}
|
|
>
|
|
<Clock className="h-4 w-4" />
|
|
Recent
|
|
</button>
|
|
</div>
|
|
|
|
{/* Content */}
|
|
<div className="flex-1 overflow-y-auto p-3">
|
|
{activeTab === 'picker' && (
|
|
<ColorPicker color={primaryColor} onChange={handleColorChange} />
|
|
)}
|
|
|
|
{activeTab === 'swatches' && (
|
|
<ColorSwatches
|
|
currentColor={primaryColor}
|
|
onColorSelect={handleColorChange}
|
|
customSwatches={customSwatches}
|
|
onAddSwatch={addCustomSwatch}
|
|
onRemoveSwatch={removeCustomSwatch}
|
|
/>
|
|
)}
|
|
|
|
{activeTab === 'recent' && (
|
|
<div className="space-y-3">
|
|
<p className="text-xs text-muted-foreground">
|
|
Recently used colors
|
|
</p>
|
|
{recentColors.length > 0 ? (
|
|
<div className="grid grid-cols-10 gap-1">
|
|
{recentColors.map((color, index) => (
|
|
<button
|
|
key={`${color}-${index}`}
|
|
onClick={() => handleColorChange(color)}
|
|
className={cn(
|
|
'w-6 h-6 rounded border-2 transition-all hover:scale-110',
|
|
primaryColor.toUpperCase() === color.toUpperCase()
|
|
? 'border-primary ring-2 ring-primary/50'
|
|
: 'border-border hover:border-primary/50'
|
|
)}
|
|
style={{ backgroundColor: color }}
|
|
title={color}
|
|
/>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<p className="text-sm text-muted-foreground italic">
|
|
No recent colors yet. Start drawing!
|
|
</p>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|