Rust颜色处理库matugen的使用:Material Design主题颜色生成与动态调色板工具
Rust颜色处理库matugen的使用:Material Design主题颜色生成与动态调色板工具
功能特性
-
生成/导出Material You颜色调色板:
- 从图片或颜色生成Material You颜色调色板
- 将生成的调色板输出为JSON到标准输出,或在模板中使用关键词导出为文件
-
关键词过滤器:
- 使用过滤器改变关键词的值,如改变颜色的亮度、使用
replace
、to_upper
、to_lower
和set_lightness
操作字符串
- 使用过滤器改变关键词的值,如改变颜色的亮度、使用
-
自定义关键词/颜色:
- 在配置文件中定义自己的自定义关键词或想要协调的颜色,然后可以在模板中使用
-
调色板定制:
- 定制调色板的对比度和方案类型
-
重启应用/更换壁纸:
- 重启支持的应用并在Windows、MacOS、Linux和NetBSD上设置壁纸
支持的平台
- Windows
- Linux
- MacOS
- NetBSD
安装
使用Cargo安装
cargo install matugen
作为库使用
在项目目录中运行以下Cargo命令:
cargo add matugen
或者在Cargo.toml中添加以下行:
matugen = "2.4.1"
示例代码
以下是一个使用matugen生成Material You调色板的完整示例:
use matugen::{generate_palette, ColorScheme, PaletteOptions};
use std::path::Path;
fn main() {
// 从图片生成调色板
let image_path = Path::new("path/to/your/image.jpg");
// 设置调色板选项
let options = PaletteOptions {
scheme: ColorScheme::Dark, // 使用暗色主题
contrast: 0.5, // 中等对比度
..Default::default()
};
// 生成调色板
let palette = generate_palette(&image_path, options).unwrap();
// 打印生成的调色板
println!("Primary color: {}", palette.primary);
println!("Secondary color: {}", palette.secondary);
println!("Tertiary color: {}", palette.tertiary);
println!("Error color: {}", palette.error);
// 使用调色板中的颜色
for (name, color) in palette.schemes {
println!("{}: {}", name, color);
}
// 你也可以从颜色直接生成调色板
let color = "#6200EE"; // 紫色
let palette_from_color = generate_palette(color, options).unwrap();
}
完整示例demo
以下是一个更完整的matugen使用示例,包含从图片生成调色板并应用到GUI应用的场景:
use matugen::{generate_palette, ColorScheme, PaletteOptions};
use std::path::Path;
use serde_json::{json, to_string_pretty};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. 从图片生成调色板
let wallpaper = Path::new("wallpaper.jpg");
let options = PaletteOptions {
scheme: ColorScheme::Dark,
contrast: 0.5,
..Default::default()
};
let palette = generate_palette(&wallpaper, options)?;
// 2. 打印基础颜色
println!("生成的调色板:");
println!("- 主色: {}", palette.primary);
println!("- 次要色: {}", palette.secondary);
println!("- 第三色: {}", palette.tertiary);
// 3. 生成JSON格式的主题配置
let theme = json!({
"colors": {
"primary": palette.primary.to_string(),
"on_primary": palette.on_primary.to_string(),
"primary_container": palette.primary_container.to_string(),
"on_primary_container": palette.on_primary_container.to_string(),
"secondary": palette.secondary.to_string(),
"on_secondary": palette.on_secondary.to_string(),
},
"schemes": {
"light": {
"primary": palette.light.primary.to_string(),
"surface": palette.light.surface.to_string()
},
"dark": {
"primary": palette.dark.primary.to_string(),
"surface": palette.dark.surface.to_string()
}
}
});
// 4. 保存主题到文件
let theme_json = to_string_pretty(&theme)?;
std::fs::write("theme.json", theme_json)?;
println!("主题已保存到theme.json");
// 5. 使用模板生成主题
let template = r#"
{
"primary": "{{primary}}",
"secondary": "{{secondary}}",
"light": {
"background": "{{light.background}}"
}
}
"#;
// 这里可以使用matugen的模板功能填充实际值
Ok(())
}
主题模板
matugen支持使用模板来导出主题文件,以下是模板示例:
{
"primary": "{{primary}}",
"secondary": "{{secondary}}",
"tertiary": "{{tertiary}}",
"error": "{{error}}",
"light": {
"primary": "{{light.primary}}",
"secondary": "{{light.secondary}}"
},
"dark": {
"primary": "{{dark.primary}}",
"secondary": "{{dark.secondary}}"
}
}
注意事项
- 该库遵循GPL-2.0许可证
- 最新版本为2.4.1
- 需要Rust 2021版或更高版本
matugen是一个强大的工具,可以帮助你轻松地为你的应用程序生成符合Material Design规范的动态调色板,支持从图像或特定颜色生成协调的颜色方案。
1 回复
Rust颜色处理库matugen的使用指南
介绍
matugen是一个Rust库,用于根据Material Design 3规范生成主题颜色和动态调色板。它可以从图像或颜色中提取主色调,并生成完整的调色板系统,非常适合需要遵循Material Design设计规范的应用程序开发。
主要功能:
- 从图像或颜色生成Material主题
- 创建动态调色板
- 支持浅色和深色主题
- 遵循Material Design 3 (Material You)规范
安装
在Cargo.toml中添加依赖:
[dependencies]
matugen = "0.1"
基本使用方法
从颜色生成主题
use matugen::{theme_from_color, Color};
fn main() {
// 使用十六进制颜色代码
let color = Color::from_hex("#6750A4").unwrap();
// 生成主题
let theme = theme_from_color(&color, false, false).unwrap();
println!("Primary color: {:?}", theme.primary);
println!("Secondary color: {:?}", theme.secondary);
println!("Tertiary color: {:?}", theme.tertiary);
}
从图像生成主题
use matugen::{theme_from_image, Image};
fn main() {
// 加载图像
let image = Image::from_path("path/to/image.jpg").unwrap();
// 生成主题 (参数分别是: 图像, 是否提取深色主题, 是否使用内容策略)
let theme = theme_from_image(&image, false, false).unwrap();
println!("Generated theme colors:");
println!("Primary: {:?}", theme.primary);
println!("On Primary: {:?}", theme.on_primary);
println!("Primary Container: {:?}", theme.primary_container);
}
高级功能
创建动态调色板
use matugen::{palette::Palette, Color};
fn main() {
let seed_color = Color::from_hex("#4285F4").unwrap();
let palette = Palette::new(&seed_color);
println!("Primary tones:");
for (tone, color) in palette.primary.iter() {
println!("Tone {}: {:?}", tone, color);
}
}
生成完整的主题系统
use matugen::{theme_system_from_color, Color};
fn main() {
let color = Color::from_hex("#FF5722").unwrap();
let theme_system = theme_system_from_color(&color).unwrap();
// 浅色主题
println!("Light theme primary: {:?}", theme_system.light.primary);
// 深色主题
println!("Dark theme primary: {:?}", theme_system.dark.primary);
}
实际应用示例
在GUI应用中使用生成的调色板
use iced::{Application, Settings, Color as IcedColor};
use matugen::{theme_from_color, Color};
struct MyApp {
theme: matugen::Theme,
}
impl Application for MyApp {
// ... 其他必要的实现
fn theme(&self) -> iced::Theme {
iced::Theme::custom(iced::theme::Palette {
primary: IcedColor::from_rgba8(
self.theme.primary.red,
self.theme.primary.green,
self.theme.primary.blue,
self.theme.primary.alpha as f32 / 255.0,
),
// 设置其他颜色...
..Default::default()
})
}
}
fn main() {
let seed_color = Color::from_hex("#6200EE").unwrap();
let theme = theme_from_color(&seed_color, false, false).unwrap();
MyApp::run(Settings::with_flags(theme)).unwrap();
}
完整示例代码
下面是一个完整的示例,展示如何使用matugen库创建一个简单的GUI应用,并应用生成的Material主题:
use iced::{Application, Command, Element, Settings, Theme as IcedTheme};
use iced::widget::{Column, Text, Button};
use matugen::{theme_from_color, Color};
// 定义应用结构体
struct MatugenDemo {
theme: matugen::Theme,
color_index: usize,
}
// 预定义的颜色列表
const COLORS: &[&str] = &[
"#6750A4", // 紫色
"#4285F4", // 蓝色
"#FF5722", // 橙色
"#6200EE", // 深紫色
];
#[derive(Debug, Clone)]
enum Message {
ChangeColor,
}
impl Application for MatugenDemo {
type Message = Message;
type Theme = IcedTheme;
type Executor = iced::executor::Default;
type Flags = matugen::Theme;
// 初始化应用
fn new(theme: Self::Flags) -> (Self, Command<Message>) {
(Self {
theme,
color_index: 0,
}, Command::none())
}
// 应用标题
fn title(&self) -> String {
String::from("Matugen Demo")
}
// 处理消息
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::ChangeColor => {
self.color_index = (self.color_index + 1) % COLORS.len();
let new_color = Color::from_hex(COLORS[self.color_index]).unwrap();
self.theme = theme_from_color(&new_color, false, false).unwrap();
Command::none()
}
}
}
// 应用主题
fn theme(&self) -> IcedTheme {
IcedTheme::custom(iced::theme::Palette {
primary: iced::Color::from_rgba8(
self.theme.primary.red,
self.theme.primary.green,
self.theme.primary.blue,
self.theme.primary.alpha as f32 / 255.0,
),
success: iced::Color::from_rgba8(
self.theme.secondary.red,
self.theme.secondary.green,
self.theme.secondary.blue,
self.theme.secondary.alpha as f32 / 255.0,
),
danger: iced::Color::from_rgba8(
self.theme.tertiary.red,
self.theme.tertiary.green,
self.theme.tertiary.blue,
self.theme.tertiary.alpha as f32 / 255.0,
),
..Default::default()
})
}
// 构建UI
fn view(&self) -> Element<Message> {
Column::new()
.push(Text::new("Matugen Demo").size(24))
.push(Text::new(format!("当前主题颜色: {}", COLORS[self.color_index])))
.push(Button::new("切换颜色").on_press(Message::ChangeColor))
.padding(20)
.spacing(10)
.into()
}
}
fn main() {
// 初始颜色
let seed_color = Color::from_hex(COLORS[0]).unwrap();
let theme = theme_from_color(&seed_color, false, false).unwrap();
// 启动应用
MatugenDemo::run(Settings::with_flags(theme)).unwrap();
}
注意事项
-
图像处理功能需要启用
image
特性:matugen = { version = "0.1", features = ["image"] }
-
颜色值范围是0-255的RGBA格式
-
对于性能敏感的应用,考虑在后台线程执行颜色生成操作
matugen库为Rust开发者提供了实现Material Design主题系统的便捷方式,特别适合跨平台应用和需要动态主题支持的场景。