material-color-rs

Crates.iomaterial-color-rs
lib.rsmaterial-color-rs
version0.1.0
created_at2025-12-30 10:28:38.601823+00
updated_at2025-12-30 10:28:38.601823+00
descriptionRust implementation of Material Design 3 color generation algorithms with HCT color space and dynamic theme system
homepagehttps://github.com/KitsuneX07/material-color-utilities
repositoryhttps://github.com/KitsuneX07/material-color-utilities
max_upload_size
id2012438
size246,843
(KitsuneX07)

documentation

https://docs.rs/material-color-rs

README

Material Color Utilities

Crates.io Documentation License: MIT

Rust 实现的 Material Design 3 颜色生成工具库,提供 HCT 颜色空间和动态主题系统。

快速开始

[dependencies]
material-color-rs = "0.1"

# 可选:启用 iced 框架支持
material-color-rs = { version = "0.1", features = ["iced"] }

基本用法

use material_color_rs::generate_theme_from_color;

// 生成主题
let theme = generate_theme_from_color("#39C5BB")?;
let light_scheme = theme.schemes.get("light").unwrap();

// 获取颜色(多种格式)
let primary_argb = light_scheme.get_argb("primary").unwrap();  // u32 ARGB
let (r, g, b) = light_scheme.get_rgb("primary").unwrap();      // RGB 元组
let rgba = light_scheme.get_rgba("primary").unwrap();          // [u8; 4]

// 导出 JSON
let json = serde_json::to_string_pretty(&theme.to_json())?;

iced 框架集成

// 启用 features = ["iced"]
let primary = light_scheme.get_iced("primary").unwrap();

container(content)
    .style(|_theme| container::Style {
        background: Some(primary.into()),
        ..Default::default()
    })

可用颜色角色

主色: primary, onPrimary, primaryContainer, onPrimaryContainer 次要色: secondary, onSecondary, secondaryContainer, onSecondaryContainer 第三色: tertiary, onTertiary, tertiaryContainer, onTertiaryContainer 表面色: surface, onSurface, surfaceVariant, onSurfaceVariant 容器色: surfaceContainer, surfaceContainerLow, surfaceContainerHigh, ... 其他: error, outline, shadow, scrim, inverseSurface, ...

架构设计

项目采用双层数据结构设计:

  • 核心层: MaterialTheme 使用 u32 ARGB 格式存储,提供极致性能
  • 序列化层: MaterialThemeJson 用于 JSON 导入/导出

这种设计确保内部操作零开销,同时保持灵活的数据交换能力。

License

MIT License - 详见 LICENSE 文件

Commit count: 0

cargo fmt