Introduction
Designers and developers often need to quickly derive a consistent color palette from an image for UI design, branding, or theming. Traditional tools require switching applications or uploading assets to external services, which can break flow and raise privacy concerns.
This post covers building a fully client-side “Color Palette Extractor” using HTML, CSS, and vanilla JavaScript, where images never leave the browser and the dominant colors are extracted in real time using a simple k-means clustering approach on image pixels.
Solution overview
The solution is a single-page web app that:
- Provides a drag-and-drop upload area for images up to 10 MB.
- Uses HTML5 canvas to downscale the image and access pixel data efficiently.
- Runs a basic k-means clustering algorithm in JavaScript to find dominant colors.
- Renders a responsive grid of color cards with hex and RGB values, and supports one-click copy to clipboard.
Frontend layout and UX
The layout consists of a centered container card with a gradient body background, a title, subtitle, and a stylized upload area. The upload area supports both click-to-upload and drag-and-drop, with subtle hover and drag states using CSS transitions.
Once the user selects a file, the preview section becomes visible, displaying the image, a loading indicator while colors are being extracted, and the resulting palette in a responsive grid. A “Upload New Image” button lets users quickly reset and try another image.
Color extraction with canvas and k-means
To extract colors, the app:
- Creates an off-screen canvas and scales the image down to a maximum size (for example, 200 px on the longest side) for performance.
- Reads pixel data via
getImageData , iterating through the RGBA values and collecting RGB triples.
- Applies a simple k-means clustering function that randomly initializes centroids, assigns pixels to clusters based on Euclidean distance in RGB space, and iteratively updates centroids.
The final palette is computed by averaging the RGB values of each cluster and converting them to hex codes for display. This approach balances simplicity and quality, and runs entirely in the browser without dependencies or backend services.
Rendering the palette and interactions
Each color in the computed palette becomes a “color card,” consisting of a swatch, the hex representation, and the RGB triplet. The grid uses CSS grid-template-columns: repeat(auto-fit, minmax(...)) to adapt to different screen sizes, making the tool usable on both desktop and mobile.
Clicking any color card copies the hex code to the clipboard using the modern navigator.clipboard API, and a small toast-style notification briefly appears in the top-right corner, confirming that the color was copied.
Potential improvements and next steps
This basic implementation can be extended with:
- Customization controls for choosing the number of colors to extract.
- Support for saving palettes as JSON or exporting them to design tools.
- Optional integration with backend services on AWS, such as storing uploaded images or palettes in Amazon S3, or capturing usage metrics via Amazon CloudWatch or AWS Lambda-backed APIs.
Loading article