15 unstable releases (3 breaking)

0.4.2 Aug 8, 2024
0.4.1 Aug 8, 2024
0.4.0 Jul 28, 2024
0.3.3 May 25, 2024
0.1.1 Dec 31, 2023

#25 in No standard library

Download history 212/week @ 2024-07-05 199/week @ 2024-07-12 167/week @ 2024-07-19 319/week @ 2024-07-26 418/week @ 2024-08-02 373/week @ 2024-08-09 188/week @ 2024-08-16 195/week @ 2024-08-23 200/week @ 2024-08-30 155/week @ 2024-09-06 287/week @ 2024-09-13 353/week @ 2024-09-20 193/week @ 2024-09-27 220/week @ 2024-10-04 319/week @ 2024-10-11 229/week @ 2024-10-18

993 downloads per month
Used in 2 crates

MIT/Apache

345KB
7.5K SLoC

Material colors

crates.io: material-colors Documentation Build Status License: MIT or Apache 2.0

An unofficial port of the material-color-utilities library for creating Material You themes and color schemes.

Features

  • std: enabled by default, disabling makes it possible to use the crate in no_std environments, provided there is an allocator available
  • image: adds support for extracting colors from images, requires std feature enabled
  • serde: adds support for JSON serialization of themes and color schemes
  • no-libm: removes the built-in implementation of FloatExt trait, which is based on libm

Examples

From HEX color:

use material_colors::{color::Argb, theme::ThemeBuilder};

let theme = ThemeBuilder::with_source(Argb::from_u32(0xffaae5a4)).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 func:resize from struct:Image provided by struct:ImageReader. 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(())
}

Current status of no-std support

This library requires alloc because Quantizer and Score make heavy use of Vec, and DynamicColor requires Box for function storage.

It also makes heavy use of various floating point functions, which greatly reduces the number of supported platforms. Yes, we have libm as a fallback, but it gives extremely different and inaccurate results, with unexpected consequences, and is also obviously much slower.

In case you have a platform where there are corresponding instructions for operations on floating point numbers, you will have to fork the repository yourself, as I unfortunately don't have any way to create an implementation for every platform that has corresponding instructions. If you have any suggestions, however, I'd be happy to hear them.

MSRV

The Minimum Supported Rust Version is currently 1.63.0.

License

Dual-licensed to be compatible with the Rust project.

Licensed under the Apache License, Version 2.0 or the MIT license, at your option. This project may not be copied, modified, or distributed except according to those terms.

Dependencies

~1.4–2.7MB
~43K SLoC