HN
Today

Creating a Color Palette from an Image

Crafting an image's perfect color palette is tougher than it looks, as this author's journey through multiple algorithmic iterations shows. By moving from RGB to OKLCH and employing sophisticated clustering and merging techniques, she achieves palettes that feel "chosen, not measured." The Hacker News crowd is impressed by the deep technical dive and the practical results of this challenging problem.

72
Score
11
Comments
#10
Highest Rank
10h
on Front Page
First Seen
Apr 30, 2:00 AM
Last Seen
Apr 30, 11:00 AM
Rank Over Time
20101415191918202426

The Lowdown

Creating a visually pleasing and representative color palette from an image is a surprisingly complex challenge. Amanda Hinton details her iterative process to develop Spectrimage, a tool designed to extract five "human-selected" colors from any photograph. Her goal is to produce consistent, meaningful palettes without muddy averages or redundant swatches, balancing algorithmic precision with aesthetic intuition.

  • Iteration 1: The RGB Struggle: Initially, Hinton used median-cut quantization in RGB, combined with complex partitioning and deduplication rules. While it "mostly worked," the system became a brittle "pile of patches" with thirteen named constants and six rules for gray, lacking reasonability and struggling with new edge cases.
  • Iteration 2: OKLCH and K-means: Abandoning RGB's limitations (like HSL S blowing up near black), Hinton pivoted to the OKLCH color space, which offers a better measure of "colorful-ness" (chroma). She implemented K-means++ clustering (with K=10, then merging down) to group similar pixels, using a deterministic seed for consistency. Initial merging based on distance faced issues with distinct colors collapsing.
  • Iteration 3: Hue-Weighting and Increased K: Recognizing that human perception weighs hue and chroma differently from lightness, Hinton introduced hue-weighting in the merge step. The chromatic plane now counts twice as much as lightness, preventing distinct hues from merging prematurely. She also increased K to 14, improving the detection of small, vibrant accents in largely achromatic images.
  • Iteration 4: Refined Guards and Allocation: To address persistent failures (phantom colors, unwanted browns, redundant grays), Hinton added structural changes:
    • Phantom Guard: Clusters below 2.5% pixel weight and low chroma are dropped.
    • Mass Split: Achromatic and chromatic slots are allocated proportionally based on image content, merging same-bucket achromatic clusters to free up chromatic slots.
    • Centroid-Aware Representatives: Swatch selection now varies; for chromatic clusters, the highest-chroma pixel is chosen, while for essentially gray clusters, the pixel closest to the centroid ensures a true gray swatch.
  • Future Improvements: Hinton notes further areas for refinement, such as thinning same-family ramps and improving sorting.

The result is an algorithm that produces human-perceived palettes more effectively, demonstrating how careful choice of color space, clustering, and nuanced merging rules can transform a difficult computer vision problem into an elegant solution.

The Gossip

Praising Palette Prowess

Commenters are overwhelmingly impressed with the quality and sophistication of the algorithm, often calling it the "best color palette generator" they've seen. Many recognize the inherent difficulty of the problem and commend the author's iterative approach and results.

Diving into Deep Tech Details

The discussion often veers into the technical minutiae, with users sharing their own experiences with color quantization, suggesting further improvements, or seeking clarification on specific design choices. The shift to OKLCH and K-means is a particular point of interest.

Palette Proliferation & Alternatives

Users share various related tools, libraries, and blog posts, highlighting other approaches to color palette generation, from AI-driven solutions to similar iterative processes, showcasing a vibrant ecosystem of color-focused utilities.