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_kmeans, pixels_from_rgba};
use swatchthis::algorithms::kmeans::{KmeansColorSpace, 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_kmeans(&pixels, 2, KmeansColorSpace::Rgb, InitMethod::KMeansPlusPlus, 42);

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

Modules§

algorithms
color
preprocessors
swatch

Functions§

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