| Crates.io | auto-palette |
| lib.rs | auto-palette |
| version | 0.9.2 |
| created_at | 2024-04-29 13:32:37.992557+00 |
| updated_at | 2025-09-08 13:07:50.70554+00 |
| description | 🎨 A Rust library that extracts prominent color palettes from images automatically. |
| homepage | https://github.com/t28hub/auto-palette |
| repository | https://github.com/t28hub/auto-palette |
| max_upload_size | |
| id | 1224179 |
| size | 520,562 |
🎨 A Rust library for automatically extracting prominent color palettes from images.
| Theme | Color Palette |
|---|---|
(Default) |
![]() |
Colorful |
![]() |
Vivid |
![]() |
Muted |
![]() |
Light |
![]() |
Dark |
![]() |
[!NOTE] Photo by Laura Clugston on Unsplash
DBSCAN, DBSCAN++, KMeans, SLIC, and SNIC.RGB, HSL, LAB and more.Colorful, Vivid, Muted, Light, and Dark.Using auto-palette in your Rust project, add it to your Cargo.toml.
[dependencies]
auto-palette = "0.9.2"
[!NOTE] This project is pre-1.0.0. While the API is generally stable, breaking changes may still occur.
Here is a basic example that demonstrates how to extract the color palette and find the prominent colors.
use auto_palette::{ImageData, Palette};
fn main() {
// Load the image data from the file
let image_data = ImageData::load("../../gfx/holly-booth-hLZWGXy5akM-unsplash.jpg").unwrap();
// Extract the color palette from the image data
let palette: Palette<f64> = Palette::extract(&image_data).unwrap();
println!("Extracted {} swatches", palette.len());
// Find the 5 prominent colors in the palette and print their information
let swatches = palette.find_swatches(5).unwrap();
for swatch in swatches {
println!("Color: {}", swatch.color().to_hex_string());
println!("Position: {:?}", swatch.position());
println!("Population: {}", swatch.population());
}
}
For more advanced examples, see the examples directory.
See the full documentation on docs.rs.
ImageDataThe ImageData struct represents the image data that is used to extract the color palette.
ImageData::loadLoads the image data from the file.
This method requires the image feature to be enabled. The image feature is enabled by default.
[dependencies]
auto-palette = { version = "0.9.2", features = ["image"] }
image = { version = "0.25.6", features = ["jpeg"] } # if you want to load jpeg images
// Load the image data from the file
let image_data = ImageData::load("path/to/image.jpg").unwrap();
ImageData::newCreates a new instance from the raw image data.
Each pixel is represented by four consecutive bytes in the order of R, G, B, and A.
// Create a new instance from the raw image data
let pixels = [
255, 0, 0, 255, // Red
0, 255, 0, 255, // Green
0, 0, 255, 255, // Blue
255, 255, 0, 255, // Yellow
];
let image_data = ImageData::new(2, 2, &pixels).unwrap();
PaletteThe Palette struct represents the color palette extracted from the ImageData.
Palette::extractExtracts the color palette from the given ImageData.
This method is used to extract the color palette with the default Algorithm(DBSCAN).
// Load the image data from the file
let image_data = ImageData::load("path/to/image.jpg").unwrap();
// Extract the color palette from the image data
let palette: Palette<f64> = Palette::extract(&image_data).unwrap();
Palette::builderCreates a new PaletteBuilder instance to customize the palette extraction process.
This method allows you to specify the algorithm, color filter, and other options.
// Load the image data from the file
let image_data = ImageData::load("path/to/image.jpg").unwrap();
// Extract the color palette from the image data with the specified algorithm
let palette: Palette<f64> = Palette::builder()
.algorithm(Algorithm::DBSCANpp) // Use DBSCAN++ algorithm for extraction
.filter(|pixel| pixel[3] < 64) // Filter out pixels with alpha < 64
.max_swatches(128) // Limit the maximum number of swatches to 128
.build(&image_data) // Build the palette from the image data
.unwrap();
Palette::find_swatchesFinds the prominent colors in the palette based on the number of swatches.
Returned swatches are sorted by their population in descending order.
// Find the 5 prominent colors in the palette
let swatches = palette.find_swatches(5);
Palette::find_swatches_with_themeFinds the prominent colors in the palette based on the specified Theme and the number of swatches.
The supported themes are Colorful, Vivid, Muted, Light, and Dark.
// Find the 5 prominent colors in the palette with the specified theme
let swatches = palette.find_swatches_with_theme(5, Theme::Light);
SwatchThe Swatch struct represents the color swatch in the Palette.
It contains detailed information about the color, position, population, and ratio.
// Find the 5 prominent colors in the palette
let swatches = palette.find_swatches(5);
for swatch in swatches {
// Get the color, position, and population of the swatch
println!("Color: {:?}", swatch.color());
println!("Position: {:?}", swatch.position());
println!("Population: {}", swatch.population());
println!("Ratio: {}", swatch.ratio());
}
[!TIP] The
Colorstruct provides various methods to convert the color to different formats, such asRGB,HSL, andCIE L*a*b*.let color = swatch.color(); println!("Hex: {}", color.to_hex_string()); println!("RGB: {:?}", color.to_rgb()); println!("HSL: {:?}", color.to_hsl()); println!("CIE L*a*b*: {:?}", color.to_lab()); println!("Oklch: {:?}", color.to_oklch());
Follow the instructions below to build and test the project:
cargo test --lib.cargo +nightly fmt and taplo fmt.This project is distributed under the MIT License. See the LICENSE file for details.