1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! ColorMunki Spectrometer CLI Application.
//!
//! This is the interactive command-line interface for the spectro-rs library.
use dialoguer::{Select, theme::ColorfulTheme};
use spectro_rs::{
MeasurementMode, Result, colorimetry::XYZ, device::DevicePosition, discover, i18n, t,
};
fn main() -> Result<()> {
i18n::init_i18n();
// --- Original CLI Logic ---
println!("{}", t!("welcome"));
println!("{}", t!("scanning"));
// Use the simplified discovery API
let mut device = match discover() {
Ok(dev) => dev,
Err(e) => {
println!("{}", t!("no-device"));
return Err(e);
}
};
// Print device info
let info = device.info()?;
println!("\n\x1b[32m{}\x1b[0m", t!("target-found"));
println!(" Model: {}", info.model);
println!(" Serial: {}", info.serial);
println!(" Firmware: {}", info.firmware);
loop {
let selections = &[
t!("menu-measure").to_string(),
t!("menu-measure-emissive").to_string(),
t!("menu-measure-ambient").to_string(),
t!("menu-calibrate").to_string(),
t!("menu-exit").to_string(),
];
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt(t!("menu-title").to_string())
.default(0)
.items(&selections[..])
.interact()
.unwrap();
match selection {
0..=2 => {
let mode = match selection {
0 => MeasurementMode::Reflective,
1 => MeasurementMode::Emissive,
_ => MeasurementMode::Ambient,
};
// Check dial position for ambient mode
if mode == MeasurementMode::Ambient {
let status = device.status()?;
if status.position != DevicePosition::Ambient
&& status.position != DevicePosition::Surface
{
println!(
"\n\x1b[33m[Notice]\x1b[0m Please turn the dial to the \x1b[1mAmbient/Diffuser\x1b[0m position."
);
}
}
// Check calibration for reflective mode
if mode == MeasurementMode::Reflective && !device.is_calibrated(mode) {
println!("\n\x1b[31m[Warning]\x1b[0m Reflective mode needs calibration first.");
continue;
}
match device.measure(mode) {
Ok(spec) => {
println!("\n\x1b[32m{}\x1b[0m", t!("spectral-success"));
// Colorimetry
let norm_xyz = spec.to_xyz();
// Reference White (D50)
let wp = XYZ {
x: 96.42,
y: 100.0,
z: 82.49,
};
let lab = norm_xyz.to_lab(wp);
if mode == MeasurementMode::Emissive {
println!(
"\x1b[36mMonitor Mode:\x1b[0m Screen brightness (nits): {:.2} cd/m²",
norm_xyz.y
);
} else if mode == MeasurementMode::Ambient {
println!(
"\x1b[36mAmbient Mode:\x1b[0m Lighting intensity (relative): {:.2}",
norm_xyz.y
);
}
println!(
"\x1b[33mCIE XYZ:\x1b[0m X:{:.2}, Y:{:.2}, Z:{:.2}",
norm_xyz.x, norm_xyz.y, norm_xyz.z
);
let (x_coord, y_coord) = norm_xyz.to_chromaticity();
println!(
"\x1b[33mChromaticity:\x1b[0m x:{:.4}, y:{:.4}",
x_coord, y_coord
);
println!(
"\x1b[35mCIE L*a*b*:\x1b[0m L:{:.2}, a:{:.2}, b:{:.2}\n",
lab.l, lab.a, lab.b
);
// Advanced spectral analysis for light sources
if mode != MeasurementMode::Reflective {
let cct = norm_xyz.to_cct();
println!("\x1b[36mEstimated CCT:\x1b[0m {:.0} K", cct);
// Spectral Centroid (weighted average wavelength)
let total_power: f32 = spec.values.iter().skip(4).sum();
let centroid: f32 = spec
.values
.iter()
.enumerate()
.skip(4) // Start from 420nm
.map(|(i, v)| (380 + i * 10) as f32 * v)
.sum::<f32>()
/ total_power.max(1e-6);
println!("\x1b[36mSpectral Centroid:\x1b[0m {:.1} nm", centroid);
// Peak detection (skip noise below 420nm)
let peak_idx = spec
.values
.iter()
.enumerate()
.skip(4)
.max_by(|a, b| {
a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal)
})
.map(|(i, _)| i)
.unwrap_or(0);
println!("\x1b[36mPeak Wavelength:\x1b[0m {} nm", 380 + peak_idx * 10);
// Simple ASCII spectrum visualization
println!("\n\x1b[90mSpectrum (420-730nm):\x1b[0m");
let max_val =
spec.values.iter().skip(4).cloned().fold(0.0f32, f32::max);
for (i, v) in spec.values.iter().enumerate().skip(4) {
let bar_len = ((v / max_val.max(1e-6)) * 30.0) as usize;
let wl = 380 + i * 10;
let color = match wl {
420..=450 => "\x1b[34m", // Blue
451..=500 => "\x1b[36m", // Cyan
501..=560 => "\x1b[32m", // Green
561..=590 => "\x1b[33m", // Yellow
591..=620 => "\x1b[38;5;208m", // Orange
_ => "\x1b[31m", // Red
};
println!(
"{:3}nm \x1b[90m{}█{}\x1b[0m",
wl,
color,
"█".repeat(bar_len.min(30))
);
}
println!();
}
}
Err(e) => println!("Error: {}", e),
}
}
3 => {
// Calibrate
println!("\n{}", t!("calibration-required"));
println!("{}", t!("dial-white-dot"));
println!(
"(This position is light-tight, perfect for both Dark and White calibration)"
);
println!("{}", t!("press-enter"));
let mut input = String::new();
let _ = std::io::stdin().read_line(&mut input);
println!("{}", t!("step-dark"));
println!("{}", t!("step-white"));
match device.calibrate() {
Ok(_) => println!("\x1b[32m{}\x1b[0m\n", t!("cal-success")),
Err(e) => println!("\x1b[31mError: {}\x1b[0m\n", e),
}
}
4 => break,
_ => unreachable!(),
}
}
Ok(())
}