Files
paint-ui/store/color-store.ts

94 lines
2.3 KiB
TypeScript
Raw Normal View History

feat: implement Phase 5 - Advanced Color System 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>
2025-11-21 01:55:28 +01:00
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
interface ColorStore {
/** Primary color (foreground) */
primaryColor: string;
/** Secondary color (background) */
secondaryColor: string;
/** Recent colors history */
recentColors: string[];
/** Custom color swatches */
customSwatches: string[];
/** Set primary color */
setPrimaryColor: (color: string) => void;
/** Set secondary color */
setSecondaryColor: (color: string) => void;
/** Swap primary and secondary colors */
swapColors: () => void;
/** Add color to recent history */
addToRecent: (color: string) => void;
/** Add custom swatch */
addCustomSwatch: (color: string) => void;
/** Remove custom swatch */
removeCustomSwatch: (color: string) => void;
/** Clear recent colors */
clearRecent: () => void;
}
const MAX_RECENT_COLORS = 20;
const MAX_CUSTOM_SWATCHES = 20;
export const useColorStore = create<ColorStore>()(
persist(
(set, get) => ({
primaryColor: '#000000',
secondaryColor: '#ffffff',
recentColors: [],
customSwatches: [],
setPrimaryColor: (color) => {
set({ primaryColor: color });
get().addToRecent(color);
},
setSecondaryColor: (color) => {
set({ secondaryColor: color });
},
swapColors: () => {
const { primaryColor, secondaryColor } = get();
set({
primaryColor: secondaryColor,
secondaryColor: primaryColor,
});
},
addToRecent: (color) => {
set((state) => {
const recent = state.recentColors.filter((c) => c !== color);
recent.unshift(color);
return {
recentColors: recent.slice(0, MAX_RECENT_COLORS),
};
});
},
addCustomSwatch: (color) => {
set((state) => {
if (state.customSwatches.includes(color)) return state;
return {
customSwatches: [...state.customSwatches, color].slice(
-MAX_CUSTOM_SWATCHES
),
};
});
},
removeCustomSwatch: (color) => {
set((state) => ({
customSwatches: state.customSwatches.filter((c) => c !== color),
}));
},
clearRecent: () => {
set({ recentColors: [] });
},
}),
{
name: 'color-storage',
}
)
);