5.3 KiB
5.3 KiB
PRD: Dominant Color Extraction for Uploaded Images
Overview
This PRD outlines the implementation of automatic dominant color extraction for images uploaded to the media library. This feature will analyze uploaded images to extract their primary colors, enabling color-based organization, search, and visual enhancements throughout the application.
Goals
- Automatic Color Analysis: Extract dominant colors from images during the upload process
- Data Storage: Store color information efficiently alongside existing image metadata
- Visual Enhancement: Use extracted colors to enhance UI/UX in galleries and image displays
- Performance: Ensure color extraction doesn't significantly impact upload performance
Technical Approach
Color Extraction Library Options
-
node-vibrant (Recommended)
- Pros: Lightweight, fast, good algorithm, actively maintained
- Cons: Node.js only (server-side processing)
- NPM:
node-vibrant
-
color-thief-node
- Pros: Simple API, battle-tested algorithm
- Cons: Less feature-rich than vibrant
- NPM:
colorthief
-
Cloudinary Color Analysis
- Pros: Integrated with existing upload pipeline, no extra processing
- Cons: Requires paid plan, vendor lock-in
- API:
colorsparameter in upload response
Recommended Approach: node-vibrant
import Vibrant from 'node-vibrant'
// Extract colors from uploaded image
const palette = await Vibrant.from(buffer).getPalette()
const dominantColors = {
vibrant: palette.Vibrant?.hex,
darkVibrant: palette.DarkVibrant?.hex,
lightVibrant: palette.LightVibrant?.hex,
muted: palette.Muted?.hex,
darkMuted: palette.DarkMuted?.hex,
lightMuted: palette.LightMuted?.hex
}
Database Schema Changes
Option 1: Add to Existing exifData JSON (Recommended)
model Media {
// ... existing fields
exifData Json? // Add color data here: { colors: { vibrant, muted, etc }, ...existingExif }
}
Option 2: Separate Colors Field
model Media {
// ... existing fields
dominantColors Json? // { vibrant, darkVibrant, lightVibrant, muted, darkMuted, lightMuted }
}
API Changes
Upload Endpoint (/api/media/upload)
Update the upload handler to extract colors:
// After successful upload to Cloudinary
if (file.type.startsWith('image/') && file.type !== 'image/svg+xml') {
const buffer = await file.arrayBuffer()
// Extract EXIF data (existing)
const exifData = await extractExifData(file)
// Extract dominant colors (new)
const colorData = await extractDominantColors(buffer)
// Combine data
const metadata = {
...exifData,
colors: colorData
}
}
Response Format
{
"id": 123,
"url": "...",
"dominantColors": {
"vibrant": "#4285f4",
"darkVibrant": "#1a73e8",
"lightVibrant": "#8ab4f8",
"muted": "#5f6368",
"darkMuted": "#3c4043",
"lightMuted": "#e8eaed"
}
}
UI/UX Considerations
1. Media Library Display
- Show color swatches on hover/focus
- Optional: Color-based filtering or sorting
2. Gallery Image Modal
- Display color palette in metadata section
- Show hex values for each color
- Copy-to-clipboard functionality for colors
3. Album/Gallery Views
- Use dominant color for background accents
- Create dynamic gradients from extracted colors
- Enhance loading states with color placeholders
4. Potential Future Features
- Color-based search ("find blue images")
- Automatic theme generation for albums
- Color harmony analysis for galleries
Implementation Plan
Phase 1: Backend Implementation (1 day)
- Install and configure node-vibrant
- Create color extraction utility function
- Integrate into upload pipeline
- Update database schema (migration)
- Update API responses
Phase 2: Basic Frontend Display (0.5 day)
- Update Media type definitions
- Display colors in GalleryImageModal
- Add color swatches to media details
Phase 3: Enhanced UI Features (1 day)
- Implement color-based backgrounds
- Add loading placeholders with colors
- Create color palette component
Phase 4: Testing & Optimization (0.5 day)
- Test with various image types
- Optimize for performance
- Handle edge cases (B&W images, etc.)
Success Metrics
- Performance: Color extraction adds < 200ms to upload time
- Accuracy: Colors accurately represent image content
- Coverage: 95%+ of uploaded images have color data
- User Experience: Improved visual coherence in galleries
Edge Cases & Considerations
- Black & White Images: Should return grayscale values
- Transparent PNGs: Handle alpha channel appropriately
- Very Large Images: Consider downsampling for performance
- Failed Extraction: Gracefully handle errors without blocking upload
Future Enhancements
- Color Search: Search images by dominant color
- Auto-Tagging: Suggest tags based on color analysis
- Accessibility: Use colors to improve contrast warnings
- Analytics: Track most common colors in library
- Batch Processing: Extract colors for existing images
Dependencies
node-vibrant: ^3.2.1- No additional infrastructure required
- Compatible with existing Cloudinary workflow
Timeline
- Total effort: 2-3 days
- Can be implemented incrementally
- No breaking changes to existing functionality