material-colors

Crates.iomaterial-colors
lib.rsmaterial-colors
version0.3.2
sourcesrc
created_at2023-12-04 18:56:56.118713
updated_at2024-05-08 00:53:46.668322
descriptionUp-to-date material-color-utilities port
homepagehttps://github.com/Aiving/material-colors
repositoryhttps://github.com/Aiving/material-colors
max_upload_size
id1057941
size281,812
Savchenko Ivan (Aiving)

documentation

https://docs.rs/material-colors

README

Material colors

A Rust library for generating Material You themes (as well as color schemes)

Most of the code was taken from the Swift version of material-color-utilities, as its code is the easiest to rewrite.

Examples

From HEX color:

use std::str::FromStr;
use material_colors::{color::Argb, theme::ThemeBuilder};

fn main() {
    let theme = ThemeBuilder::with_source(Argb::from_str("aae5a4").unwrap()).build();

    // Do whatever you want...
}

From image:

⚠️ Before obtaining an array of ARGB pixels for the image, it is recommended (but not necessary if your image is already small in size or you just don't mind about execution time) to adjust its dimensions to 128x128 (by resize function from image crate, for example). The reason is described here.

use material_colors::{
    image::{FilterType, ImageReader},
    theme::ThemeBuilder,
};

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
    let image = reqwest::get("https://picsum.photos/id/866/1920/1080")
        .await?
        .bytes()
        .await?
        .to_vec();

    let mut data = ImageReader::read(image).expect("failed to read image");

    // Lancsoz3 takes a little longer, but provides the best pixels for color extraction.
    // However, if you don't like the results, you can always try other FilterType values.
    data.resize(128, 128, FilterType::Lanczos3);

    let theme = ThemeBuilder::with_source(ImageReader::extract_color(&data)).build();

    // Do whatever you want...

    Ok(())
}
Commit count: 92

cargo fmt