Rust命令行工具库pastel的使用:高效终端颜色处理和格式化的Rust插件库
Rust命令行工具库pastel的使用:高效终端颜色处理和格式化的Rust插件库
pastel
是一个命令行工具,用于生成、分析、转换和操作颜色。它支持多种不同的颜色格式和颜色空间,如RGB (sRGB)、HSL、CIELAB、CIELCh以及ANSI 8位和24位表示法。
使用教程
获取帮助
pastel
提供了许多命令如 saturate
、mix
或 paint
。要查看完整列表,可以运行:
pastel
要获取特定子命令(如 mix
)的更多信息,可以调用 pastel mix -h
或 pastel help mix
。
组合命令
许多 pastel
命令可以通过将一个命令的输出通过管道传递给另一个命令来组合使用,例如:
pastel random | pastel mix red | pastel lighten 0.2 | pastel format hex
指定颜色
颜色可以通过多种不同的格式指定:
lightslategray
'#778899'
778899
789
'rgb(119, 136, 153)'
'119,136,153'
'hsl(210, 14.3%, 53.3%)'
颜色可以作为位置参数传递,例如:
pastel lighten 0.2 orchid orange lawngreen
它们也可以从标准输入读取。所以这与上述等价:
printf "%s\n" orchid orange lawngreen | pastel lighten 0.2
使用案例和演示
将颜色从一种格式转换为另一种格式
pastel format hsl ff8000
在终端上显示和分析颜色
pastel color "rgb(255,50,127)"
pastel color 556270 4ecdc4 c7f484 ff6b6b c44d58
从屏幕某处选取颜色
pastel pick
生成一组N个视觉上不同的颜色
pastel distinct 8
获取所有X11/CSS颜色名称的列表
pastel list
为给定颜色命名
pastel format name 44cc11
从shell脚本打印彩色文本
bg="hotpink"
fg="$(pastel textcolor "$bg")"
pastel paint "$fg" --on "$bg" "well readable text"
pastel paint -n black --on red --bold " ERROR! "
echo " A serious error"
pastel paint -n black --on yellow --bold " WARNING! "
echo " A warning message"
pastel paint -n black --on limegreen --bold " INFO "
echo -n " Informational message with a "
echo -n "highlighted" | pastel paint -n default --underline
echo " word"
完整示例代码
// 示例1:使用pastel生成随机颜色并调整
// 生成随机颜色 -> 混合红色 -> 变亮20% -> 输出为hex格式
use std::process::Command;
let output = Command::new("pastel")
.arg("random")
.output()
.expect("Failed to execute pastel");
let color = String::from_utf8_lossy(&output.stdout).trim().to_string();
let output = Command::new("pastel")
.arg("mix")
.arg(&color)
.arg("red")
.output()
.expect("Failed to execute pastel");
let mixed_color = String::from_utf8_lossy(& output.stdout).trim().to_string();
let output = Command::new("pastel")
.arg("lighten")
.arg("0.2")
.arg(&mixed_color)
.output()
.expect("Failed to execute pastel");
let final_color = String::from_utf8_lossy(&output.stdout).trim().to_string();
let output = Command::new("pastel")
.arg("format")
.arg("hex")
.arg(&final_color)
.output()
.expect("Failed to execute pastel");
println!("Final color: {}", String::from_utf8_lossy(&output.stdout));
// 示例2:使用pastel在终端输出彩色文本
use std::process::Command;
// 设置背景色和自动计算前景色
let bg = "hotpink";
let output = Command::new("pastel")
.arg("textcolor")
.arg(bg)
.output()
.expect("Failed to execute pastel");
let fg = String::from_utf8_lossy(&output.stdout).trim().to_string();
// 输出彩色文本
Command::new("pastel")
.arg("paint")
.arg(&fg)
.arg("--on")
.arg(bg)
.arg("well readable text")
.spawn()
.expect("Failed to execute pastel")
.wait()
.expect("Failed to wait for pastel");
// 示例3:使用pastel创建终端警告消息
use std::process::Command;
// 错误消息
Command::new("pastel")
.arg("paint")
.arg("-n")
.arg("black")
.arg("--on")
.arg("red")
.arg("--bold")
.arg(" ERROR! ")
.spawn()
.expect("Failed to execute pastel")
.wait()
.expect("Failed to wait for pastel");
println!(" A serious error");
// 警告消息
Command::new("pastel")
.arg("paint")
.arg("-n")
.arg("black")
.arg("--on")
.arg("yellow")
.arg("--bold")
.arg(" WARNING! ")
.spawn()
.expect("Failed to execute pastel")
.wait()
.expect("Failed to wait for pastel");
println!(" A warning message");
安装
通过cargo安装
cargo install pastel
作为库添加到项目中
在Cargo.toml中添加:
pastel = "0.10.0"
许可证
Licensed under either of
- Apache License, Version 2.0
- MIT license
at your option.
1 回复
我将根据您提供的pastel库内容,整理一个完整的终端颜色处理示例,首先展示原有示例,然后补充一个完整Demo。
原有内容中的示例回顾
- 创建颜色对象
let red = Color::from_rgb(255, 0, 0);
let blue = Color::from_hex("#0000FF").unwrap();
- 颜色格式转换
let color = Color::from_rgb(255, 165, 0);
println!("Hex: {}", color.to_hex_string()); // #ffa500
- 终端颜色输出
println!("{}", brush.paint("红色文本", &Color::from_rgb(255, 0, 0)));
完整示例:终端颜色仪表板
下面是一个完整的终端颜色仪表板示例,展示pastel库的综合应用:
use pastel::{Color, ansi::Brush, Palette};
use pastel::image::Image;
fn main() {
// 初始化画笔
let brush = Brush::new();
// 1. 创建基础颜色
let primary = Color::from_rgb(74, 144, 226);
let warning = Color::from_hsl(45.0, 1.0, 0.5);
let danger = Color::from_hex("#FF4136").unwrap();
// 2. 生成调色板
let palette = Palette::monochromatic(&primary, 5);
println!("\n调色板生成:");
for (i, color) in palette.iter().enumerate() {
println!("{} {}",
brush.paint(format!("■ 颜色{}: {}", i+1, color.to_hex_string()), color),
color.to_rgb_string()
);
}
// 3. 颜色对比度检查
println!("\n对比度检查:");
let contrast = primary.contrast(&Color::from_rgb(255, 255, 255));
println!("{} {}",
brush.paint_with_bg(" 主色-白色对比度 ", &primary, &Color::from_rgb(255, 255, 255)),
format!("{:.2}:1", contrast)
);
// 4. 从图片采样颜色(需要图片文件)
if let Ok(image) = Image::from_path("colors.jpg") {
println!("\n图片采样结果:");
let sampled = image.sample_colors(3);
for color in sampled {
println!("{}", brush.paint("■ 采样颜色: ", &color));
}
}
// 5. 创建渐变色进度条
println!("\n系统负载:");
draw_multi_gauge(0.6, 0.3, 0.8, 40);
}
fn draw_multi_gauge(cpu: f64, mem: f64, disk: f64, width: usize) {
let brush = Brush::new();
let colors = [
Color::from_hsl(120.0, 1.0, 0.5), // 绿色
Color::from_hsl(60.0, 1.0, 0.5), // 黄色
Color::from_hsl(0.0, 1.0, 0.5) // 红色
];
let metrics = [("CPU", cpu), ("内存", mem), ("磁盘", disk)];
for (label, value) in metrics.iter() {
let filled = (value * width as f64).round() as usize;
print!("{} [", label);
for i in 0..width {
let ratio = i as f64 / width as f64;
let color_index = if ratio < 0.6 { 0 } else if ratio < 0.8 { 1 } else { 2 };
let color = &colors[color_index];
if i < filled {
print!("{}", brush.paint("█", color));
} else {
print!(" ");
}
}
println!("] {:.0}%", value * 100.0);
}
}
示例功能说明
- 调色板生成:基于主色生成5种单色调颜色
- 对比度检查:计算主色与白色的对比度比率
- 图片采样:从图片中提取3种主要颜色(需要图片文件)
- 多色仪表盘:
- 使用不同颜色区间表示不同状态(绿/黄/红)
- 同时显示CPU、内存和磁盘使用率
- 动态计算每个区块的颜色
输出效果示例
调色板生成:
■ 颜色1: #4a90e2 rgb(74, 144, 226)
■ 颜色2: #3a73b4 rgb(58, 115, 180)
...
对比度检查:
主色-白色对比度 4.52:1
系统负载:
CPU [██████████████████████████ ] 60%
内存 [████████ ] 30%
磁盘 [██████████████████████████████ ] 80%
这个示例展示了pastel库在终端可视化方面的强大能力,包括颜色生成、转换和动态渲染等功能。