auto-palette
🎨 A Rust library for automatically extracting prominent color palettes from images.
Features
| Theme | Color Palette |
|---|---|
(Default) |
![]() |
Colorful |
![]() |
Vivid |
![]() |
Muted |
![]() |
Light |
![]() |
[!NOTE] Photo by Laura Clugston on Unsplash
- Automatically extracts prominent color palettes from images.
- Provides detailed information on color, position, and population.
- Supports multiple extraction algorithms:
DBSCAN,DBSCAN++, andKMeans. - Supports numerous color spaces:
RGB,HSL,LABand more. - Theme-based swatch selection:
Colorful,Vivid,Muted,Light, andDark.
Installation
Using auto-palette in your Rust project, add it to your Cargo.toml.
[]
= "0.8.1"
[!WARNING] This project is currently in version is pre-1.0.0. While the API is stable for general use, breaking changes may still occur in future updates.
Usage
Here is a basic example that demonstrates how to extract the color palette and find the prominent colors.
use ;
For more advanced examples, see the examples directory.
Documentation
See the full documentation on docs.rs.
ImageData
The ImageData struct represents the image data that is used to extract the color palette.
ImageData::load
Loads the image data from the file.
This method requires the image feature to be enabled. The image feature is enabled by default.
[]
= { = "0.8.0", = ["image"] }
= { = "0.25.6", = ["jpeg"] } # if you want to load jpeg images
// Load the image data from the file
let image_data = load.unwrap;
ImageData::new
Creates 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 = ;
let image_data = new.unwrap;
Palette
The Palette struct represents the color palette extracted from the ImageData.
Palette::extract
Extracts 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 = load.unwrap;
// Extract the color palette from the image data
let palette: = extract.unwrap;
Palette::builder
Creates 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 = load.unwrap;
// Extract the color palette from the image data with the specified algorithm
let palette: = builder
.algorithm // Use DBSCAN++ algorithm for extraction
.filter // Filter out pixels with alpha < 64
.max_swatches // Limit the maximum number of swatches to 128
.build // Build the palette from the image data
.unwrap;
Palette::find_swatches
Finds 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;
Palette::find_swatches_with_theme
Finds 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;
Swatch
The 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;
for swatch in swatches
[!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!; println!; println!; println!; println!;
Development
Follow the instructions below to build and test the project:
- Fork and clone the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and write tests.
- Test your changes with
cargo test --lib. - Format the code with
cargo +nightly fmtandtaplo fmt. - Create a pull request.
License
This project is distributed under the MIT License. See the LICENSE file for details.




