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§
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
Rgbpixels. Alpha is discarded.