Color Sorter
Sort colors by hue, saturation, luminance, or perceptual distance. Add, reorder, and export palettes as CSS, JSON, SVG, or PNG.
About
Arranging colors in a visually coherent order is a non-trivial computational problem. A naive alphabetical sort of hex codes produces chaotic sequences because hexadecimal notation encodes R, G, B channels sequentially, not perceptually. Human vision perceives color difference non-linearly. Two colors with a Euclidean RGB distance of 50 may look identical, while another pair at the same distance looks jarringly different. This tool converts colors into perceptually uniform spaces (HSL, CIE LAB) before sorting. The LAB-based perceptual sort uses a nearest-neighbor heuristic on ĪE* distances, approximating the traveling salesman problem to produce the smoothest possible gradient from an arbitrary set of colors.
Incorrect color ordering in UI design causes visual noise, reduces scannability, and breaks aesthetic coherence in design systems. This is especially critical when building theme palettes with 20+ tokens. The step-sort algorithm groups colors into 8 hue bands and sorts within each band by luminance, mimicking how physical paint swatches are organized. Note: perceptual sorting assumes a D65 illuminant and a 2° standard observer. Results may not match appearance under non-standard lighting conditions.
Formulas
Relative luminance per WCAG 2.1 (used for luminance sort):
where each linearized channel is computed as:
CIE LAB perceptual distance between two colors:
where L* is perceptual lightness (0 - 100), a* is the green - red axis, and b* is the blue - yellow axis. RGB ā LAB conversion goes through CIE XYZ with D65 reference white Xn = 95.047, Yn = 100.0, Zn = 108.883.
HSL hue extraction from RGB:
where Ī = max(R,G,B) ā min(R,G,B). Step sort quantizes H into 8 bands of 45° each, then sorts by L within each band.
Reference Data
| Sort Method | Color Space | Primary Key | Secondary Key | Best For | Time Complexity |
|---|---|---|---|---|---|
| Hue | HSL | H (0 - 360°) | L | Rainbow gradients | O(n log n) |
| Saturation | HSL | S (0 - 100%) | H | Vivid ā muted ordering | O(n log n) |
| Lightness | HSL | L (0 - 100%) | H | Dark ā light ramps | O(n log n) |
| Luminance | RGB (linear) | Y (0 - 1) | - | Accessibility contrast | O(n log n) |
| Red Channel | RGB | R (0 - 255) | G, B | Warm tone isolation | O(n log n) |
| Green Channel | RGB | G (0 - 255) | R, B | Natural/organic tones | O(n log n) |
| Blue Channel | RGB | B (0 - 255) | R, G | Cool tone isolation | O(n log n) |
| Step Sort | HSL | Hue band (8 groups) | L within band | Paint swatch layout | O(n log n) |
| Perceptual (ĪE*) | CIE LAB | Nearest ĪE* neighbor | - | Smoothest visual gradient | O(n2) |
| Reverse | Any | Current index (desc) | - | Flip current order | O(n) |
| Shuffle | - | Fisher-Yates random | - | Randomization | O(n) |