Skip to main content

Crate swatchthis

Crate swatchthis 

Source
Expand description

Colour swatch extraction from images using k-means clustering.

Supports clustering in both RGB and CIELAB colour spaces, with random or k-means++ initialisation. Designed to work in native Rust and WebAssembly.

§Example

use swatchthis::{generate_swatches, pixels_from_rgba};
use swatchthis::kmeans::{ColorSpace, InitMethod};

// Simulate a small image: 4 red pixels and 4 blue pixels (RGBA)
let rgba: Vec<u8> = [255, 0, 0, 255].repeat(4)
    .into_iter()
    .chain([0, 0, 255, 255].repeat(4))
    .collect();

let pixels = pixels_from_rgba(&rgba);
let swatches = generate_swatches(&pixels, 2, ColorSpace::Rgb, InitMethod::KMeansPlusPlus, 42);

assert_eq!(swatches.len(), 2);
for s in &swatches {
    println!("{} ({}px)", s.hex(), s.population);
}

Modules§

color
kmeans
swatch

Functions§

generate_swatches
Extracts dominant colour swatches from a slice of pixels.
pixels_from_rgba
Parses raw RGBA bytes into Rgb pixels. Alpha is discarded.