When PaletteRx extracts colors from a website, it might find 50, 80, or even 200+ individual color values. But a meaningful palette has 8 to 15 colors, not 200. The challenge is reducing those raw values to a representative set without losing important distinctions.
Why RGB Distance Fails
The naive approach is Euclidean distance in RGB space: treat each color as a point in a 3D cube and group colors that are "close" together. The problem is that human perception does not map linearly to RGB values. We perceive large differences between certain hues as smaller differences in RGB, while small visual differences can be large in RGB.
Perceptual Color Space
PaletteRx converts colors to HSL and uses a weighted distance formula that accounts for perceptual factors. Hue differences are weighted more heavily for saturated colors (because a shift from red to orange is very noticeable), while lightness differences matter more for desaturated colors.
The Clustering Algorithm
The algorithm works in two passes. First, colors are sorted by frequency. Each color is compared to existing clusters. If it falls within the perceptual threshold, it joins that cluster. If it is sufficiently different from all existing clusters, it becomes a new cluster.
The second pass handles edge cases: very similar but not identical hues that might have been separated in the first pass. A second merge at a slightly larger threshold catches these near-duplicates while still preserving genuinely distinct colors.
Special Handling for Achromatic Colors
Grays, whites, and blacks get special treatment. They are clustered by lightness alone (since their hue is irrelevant), with tighter thresholds to preserve the difference between a light background gray and a medium border gray.