8 releases

0.2.1 Mar 14, 2024
0.1.6 Feb 2, 2024
0.1.4 Jan 30, 2024
0.1.1 Dec 31, 2023

#123 in Images

Download history 5/week @ 2024-01-19 19/week @ 2024-01-26 31/week @ 2024-02-02 18/week @ 2024-02-09 137/week @ 2024-02-16 289/week @ 2024-02-23 272/week @ 2024-03-01 331/week @ 2024-03-08 321/week @ 2024-03-15 289/week @ 2024-03-22 207/week @ 2024-03-29 168/week @ 2024-04-05 153/week @ 2024-04-12 121/week @ 2024-04-19

687 downloads per month
Used in 2 crates

Apache-2.0

225KB
4.5K SLoC

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.

There are also a relatively large number of unused variables and functions (45 warnings!). I do not know for what reason they are not used in the original code (apart from a few tests), but I decided to leave them for the future.

Examples

From HEX color:

use std::str::FromStr;
use material_colors::theme_from_source_color;
use material_colors::Argb;

fn main() {
    let theme = theme_from_source_color(Argb::from_str("#AAE5A4"), Default::default());

    // 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::theme_from_source_color;
use material_colors::FilterType;
use material_colors::ImageReader;

#[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 = theme_from_source_color(ImageReader::extract_color(&data), Default::default());

    // Do whatever you want...

    Ok(())
}

Dependencies

~1.4–4.5MB
~38K SLoC