Okmain: How to pick an OK main colour of an image
5 days ago
- #color extraction
- #Oklab
- #k-means clustering
- The article discusses a method to extract a visually pleasant, representative solid color from an image for the back of a card.
- A common approach of resizing the image to 1x1 pixel often results in dull colors, prompting the development of a better solution.
- The author created a library called Okmain, which uses k-means clustering in the Oklab color space to find dominant colors.
- Okmain limits clusters to four for performance and quality, adjusting if clusters are too similar.
- Operating in Oklab avoids issues with sRGB, such as non-linear gamma correction and perceptual color intensity, leading to smoother color mixing.
- Visual prominence of colors is determined by cluster size, centrality in the image, and chroma (saturation).
- Optimizations include downsampling images, using SIMD for performance, and focusing on auto-vectorization-friendly code.
- The author experimented with LLM agents for development but found manual rewriting necessary for clarity and correctness in critical parts.
- Despite challenges, the project resulted in a functional library, with the author learning about k-means, SIMD, and mixed Python/Rust libraries.