feat: 彩色 QR 码 — 自定义前景色/背景色

- QrConfig 新增 fg_color/bg_color 字段(CSS 十六进制格式)
- QrCode 存储解析后的 [u8;3] RGB
- PNG 渲染 Luma→Rgba,支持 RGBA 颜色
- SVG 渲染使用 QrCode 颜色字段
- CLI 新增 --fg/--bg 参数
- 新增 parse_hex_color 支持 #RRGGBB 和 #RGB
- 新增 2 个颜色测试(74 tests total)
This commit is contained in:
2026-06-19 21:10:56 +08:00
parent 3d11ba2446
commit 23ccb37b52
6 changed files with 119 additions and 14 deletions
+12 -6
View File
@@ -5,7 +5,15 @@ pub fn render_svg(qr: &QrCode) -> String {
let margin = qr.margin as u32;
let total = matrix_size + 2 * margin;
// 预估 SVG 大小: 固定头部 + 每个暗模块约 48 字节
let fg = format!(
"#{:02X}{:02X}{:02X}",
qr.fg_color[0], qr.fg_color[1], qr.fg_color[2]
);
let bg = format!(
"#{:02X}{:02X}{:02X}",
qr.bg_color[0], qr.bg_color[1], qr.bg_color[2]
);
let dark_count = qr
.modules()
.iter()
@@ -14,19 +22,17 @@ pub fn render_svg(qr: &QrCode) -> String {
.count();
let mut svg = String::with_capacity(200 + dark_count * 50);
svg.push_str(&format!(
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{}" height="{}" viewBox="0 0 {} {}">"#,
total, total, total, total
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{total}" height="{total}" viewBox="0 0 {total} {total}">"#
));
svg.push_str(&format!(
r#"<rect width="{}" height="{}" fill="white"/>"#,
total, total
r#"<rect width="{total}" height="{total}" fill="{bg}"/>"#
));
for y in 0..matrix_size {
for x in 0..matrix_size {
if qr.modules()[y as usize][x as usize] {
svg.push_str(&format!(
r#"<rect x="{}" y="{}" width="1" height="1" fill="black"/>"#,
r#"<rect x="{}" y="{}" width="1" height="1" fill="{fg}"/>"#,
x + margin,
y + margin
));