Okmain: How to pick an OK main colour of an image
This post introduces "Okmain," a new Rust/Python library designed to extract the most visually "OK" main color from an image, far surpassing the common 1x1 pixel resizing method. It delves into the technical nuances of color science, clustering algorithms, and performance optimizations. The author also shares valuable, if mixed, insights from using large language models in the development process, making it a compelling read for developers interested in practical computer vision and AI-assisted coding.
The Lowdown
The author, frustrated by the "dull and muddy" main colors produced by the common 1x1 pixel image resizing method used by a consulting client, embarked on a weekend project. This effort led to the creation of "Okmain," a new library written in Rust with a Python wrapper, designed to intelligently select a visually pleasant and representative main color from an image. Inspired by the Oklab color space, the library aims to find an "OK" main color through a sophisticated combination of techniques.
- Color Clustering: Instead of simply averaging all pixels, Okmain utilizes K-means clustering (supporting up to four clusters) to identify distinct color groups, which prevents vibrant colors from blending into unattractive muddy browns.
- Oklab Color Space: All internal color calculations are performed within the perceptually uniform Oklab color space. This choice correctly handles non-linear gamma correction and perceived color intensity, leading to smoother mixing and more accurate color averages compared to operations in the sRGB space.
- Cluster Sorting: The visual prominence of color clusters is determined by a combination of pixel count (weighted by their centrality within the image using a distance mask) and the color's inherent visual prominence, approximated by its chroma (saturation). This ensures the selected "main" color is truly dominant.
- Performance Optimizations: Okmain is engineered for speed, employing strategies such as image downsampling, de-interleaving pixel data for efficient auto-vectorization, and using K-means++ for better initial cluster selection. These optimizations allow it to extract dominant colors from multi-megapixel images in approximately 100ms.
- LLM Development Experience: The author experimented with LLM agents (specifically Opus) for this greenfield development. While useful for generating "slam dunk" code like debug binaries, LLMs demonstrated limitations in producing code with "intentful abstraction," clean idiomatic style, and optimal performance, often requiring significant manual rewrites for critical sections.
Okmain thus provides a robust and visually superior method for extracting main colors from images, effectively addressing the common pitfalls of simpler approaches. The project also served as a valuable learning experience for the author in areas spanning color science, performance tuning, library distribution, and the practical applications and current limitations of LLMs in software development. The author concludes by encouraging others to utilize the library and includes a playful job inquiry.