Skip to content

ceshine/okmain

 
 

Repository files navigation

Okmain: OK main colors

okmain finds the main colors of an image and makes sure they look good.

Sometimes you need to show a "dominant" color (or colors) of an image. It can be a background or a placeholder. There are several ways of doing that; a popular quick-and-dirty method is to resize the image to a handful of pixels, or even just one.

However, this method tends to produce muted, dirty-looking colors. Most images have clusters of colors: the dominant colors of an image of a lush green field with a clear sky above it are not a muddy average of blue and green, it's a bright blue and green. Okmain clusters colors explicitly, recovering and ranking main colors while keeping them sharp and clean.

Here's a comparison:

Comparison of colors extracted via 1x1 resize and Okmain

Technical highlights

  • Color operations in a state-of-the-art perceptually linear color space (Oklab)
  • Rust implementation for speed and safety
  • Finding main colors of a reasonably sized image takes about 100ms
  • Minimal and stable dependencies
  • Fast custom K-means color clustering, optimized for auto-vectorization (confirmed with disassembly)
  • Position- and visual prominence-based color prioritization (more central and higher Oklab chroma pixels tend to be more important)
  • Tunable parameters

Read more about Okmain in the blog post.

Packages

okmain is available in:

  • Rust
  • Python (a wrapper over the Rust crate)
  • CLI (command-line interface)

CLI

The okmain_cli crate provides a command-line tool for extracting dominant colors from images directly from your terminal.

Installation (requires Rust):

cargo install okmain_cli

Usage:

# Single image, hex output (default)
okmain path/to/image.jpg

# RGB format
okmain path/to/image.png --format rgb

# Top 2 colors only with timing info
okmain path/to/image.webp -n 2 --verbose

# Process entire directory
okmain path/to/images/ --format hex

Output formats:

  • hex (default): #RRGGBB
  • rgb: rgb(r, g, b)

LLM disclosure

LLMs are used extensively in the development of Okmain, but all generated code is reviewed and rewritten by a human.

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Rust 87.1%
  • Python 12.0%
  • Just 0.9%