Crate okolors

Crate okolors 

Source
Expand description

Create a color palette from an image using k-means clustering in the Oklab color space.

This library is a simple wrapper around the quantette crate but only exposes functionality for generating color palettes. Additionally, this crate adds a few additional options not present in quantette. It is intended that additional stylistic and opinionated options will be added to this crate, whereas quantette is solely focused on accurate color quantization.

§Features

This crate has three features that are enabled by default:

  • threads: adds the parallel option.
  • image: enables integration with the image crate.
  • std: use Rust’s standard library (this crate is no_std compatible if all features are disabled).

§Examples

To start, create an Okolors from a RgbImage (note that the image feature is needed):

use okolors::Okolors;

// let img = image::open("some image")?.into_rgb8();
let img = image::RgbImage::new(256, 256);
let palette_builder = Okolors::try_from(&img)?;

Instead of an RgbImage, a slice of Srgb<u8> colors can be used instead:

let srgb = vec![Srgb::new(0, 0, 0)];
let palette_builder = Okolors::new(&srgb).unwrap();

If the default options aren’t to your liking, you can tweak them:

use okolors::{PaletteSize, KmeansOptions};

let palette_builder = Okolors::try_from(&img)?
    .palette_size(PaletteSize::from_u8_clamped(16))
    .lightness_weight(0.5)
    .kmeans_options(KmeansOptions::new().sampling_factor(1.0))
    .sort_by_frequency(true)
    .parallel(true);

To finally generate the palette, use:

To clarify, the Oklab colorspace is used to quantize the colors in all cases. The methods above just determine what colorspace you want the final colors converted into.

All of the color types present in the public API for this crate (like Srgb or Oklab) are from the palette crate. You can check it out for more information. For example, its documentation should provide you everything you need to know to cast a Vec<Srgb<u8>> into a Vec<[u8; 3]> or vice versa.

Modules§

deps
Re-exports of third party crates whose types are present in okolors’s public API.

Structs§

KmeansOptions
The various options for k-means quantization.
LengthOutOfRange
The error returned when the length of a value or input is not in the supported range.
Okolors
A builder struct to specify options for palette generation.
PaletteSize
This type is used to specify the number of colors in a palette.

Constants§

MAX_PIXELS
The maximum amount of pixels supported by this library (u32::MAX).