15 个不稳定版本 (3 个破坏性更新)
0.4.2 | 2024 年 8 月 8 日 |
---|---|
0.4.1 | 2024 年 8 月 8 日 |
0.4.0 | 2024 年 7 月 28 日 |
0.3.3 | 2024 年 5 月 25 日 |
0.1.1 | 2023 年 12 月 31 日 |
#24 在 无标准库
每月 1,258 次下载
用于 2 crates
345KB
7.5K SLoC
材质颜色
创建 Material You 主题和颜色方案的 material-color-utilities
库的非官方端口。
功能
std
: 默认启用,禁用后可以在有分配器的no_std
环境中使用此 crateimage
: 添加从图像中提取颜色的支持,需要启用std
功能serde
: 添加主题和颜色方案的 JSON 序列化支持no-libm
: 删除基于libm
的内置FloatExt
特质实现
示例
从 HEX 颜色
use material_colors::{color::Argb, theme::ThemeBuilder};
let theme = ThemeBuilder::with_source(Argb::from_u32(0xffaae5a4)).build();
// Do whatever you want...
从图像
⚠️ 在获取图像的 ARGB 像素数组之前,强烈建议(但如果您的图像已经很小或者您不介意执行时间,则不是必需的)通过
func:resize
从struct:Image
调整其尺寸到 128x128,由struct:ImageReader
提供。原因请见 这里。
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(())
}
no-std
支持的当前状态
此库 需要 alloc
,因为 Quantizer
和 Score
对 Vec
的使用非常频繁,而 DynamicColor
需要 Box
来存储函数。
它还大量使用各种浮点函数,这大大减少了支持的平台数量。是的,我们有 libm
作为后备,但它会产生极端不同和不准确的结果,带来意外的后果,而且显然要慢得多。
如果您有一个具有浮点数操作指令的平台,您将需要自行分叉仓库,因为我很遗憾没有为每个具有相应指令的平台创建实现的方法。不过,如果您有任何建议,我会很乐意听取。
MSRV
当前最低支持的Rust版本为1.63.0。
许可证
双授权以兼容Rust项目。
根据您的选择,许可协议为Apache许可证,版本2.0或MIT许可证。此项目只能根据这些条款进行复制、修改或分发。
依赖项
~1.4–2.6MB
~42K SLoC