Extracting Modern UI Color Palettes from Images using HTML5 Canvas & CIEDE2000

Learn how client-side image palette extraction works in modern web applications. Explore HTML5 Canvas API, color quantization, and Tailwind class mapping.

Abhay VachhaniFull Stack Software Engineer
2 min read
Updated: Jul 26, 2026

Why Client-Side Image Extraction Matters

Extracting color palettes from photographs, brand logos, or design mockups is a popular feature for UI tools.

  • By performing image color extraction entirely client-side using HTML5 Canvas and JavaScript:
  • 1. 100% Data Privacy: User images remain completely private on their machine.
  • 2. Zero Server Overhead: Eliminates expensive server-side image processing pipelines.
  • 3. Instant Realtime Response: Results are returned in milliseconds.

Step 1: Pixel Sampling with Canvas 2D API

When a user drops an image onto the canvas, we render it to an offscreen `HTMLCanvasElement` and sample raw RGBA pixel data:

javascript
const canvas = document.createElement('canvas');

// Downsample image to 150x150 offscreen canvas for instant extraction speed canvas.width = 150; canvas.height = 150; ctx.drawImage(imgElement, 0, 0, 150, 150);

// Read raw RGBA pixel array const imageData = ctx.getImageData(0, 0, 150, 150); const pixels = imageData.data; // Uint8ClampedArray [r, g, b, a, r, g, b, a...] ```


Step 2: Dominant Color Quantization (K-Means)

An image contains hundreds of thousands of individual pixels. To extract 5 to 6 distinct dominant colors, we run a Quantization Algorithm (such as Median Cut or K-Means Clustering):

  1. Filtering: Ignore near-transparent pixels (`alpha < 128`) and extreme pure whites/blacks.
  2. Bucket Grouping: Cluster similar RGB pixel vectors together.
  3. Centroid Sorting: Calculate the average RGB value for the top 6 largest clusters.

Step 3: Mapping Dominant Hexes to Tailwind Classes

Once the dominant HEX colors are calculated, each color is passed through hex2tailwind's CIEDE2000 algorithm to locate the closest corresponding Tailwind CSS v4 class name.

html
<!-- Final Output rendered to developer -->
<div className="flex gap-2">
  <span className="bg-sky-500 text-white px-3 py-1 rounded font-mono">bg-sky-500</span>
  <span className="bg-amber-400 text-slate-900 px-3 py-1 rounded font-mono">bg-amber-400</span>
  <span className="bg-slate-900 text-white px-3 py-1 rounded font-mono">bg-slate-900</span>
</div>

Author Insight: Client-Side Canvas Performance

Author Insight by Abhay Vachhani (Full Stack Engineer): > In high-resolution mobile camera uploads (4K+), reading raw pixel arrays directly from original dimensions will cause browser main thread jank. Downsampling the image to a 150px offscreen canvas preserves original color ratio distribution while reducing pixel iteration from 12 million elements down to just 22,500 elements.

Frequently Asked Questions

Are uploaded images sent to any server when using hex2tailwind's Image Palette tool?

No. The image is processed entirely inside your local browser memory using the HTML5 Canvas API. Zero pixel data ever leaves your device.

How fast is client-side quantization on large 4K images?

By downsampling the image onto a 150x150 offscreen canvas before pixel iteration, quantization completes in under 15 milliseconds without freezing UI rendering threads.

Try Our Developer Tools

Put Modern Color Science into Practice

Convert HEX codes to Tailwind v4 classes, test WCAG contrast ratios, and generate uniform OKLCH palettes in seconds.