xy_gamut_plots/
xy_gamut_plots.rs

1use colorimetry::{observer::Observer, rgb::RgbSpace};
2use colorimetry_plot::{chart::XYChromaticity, style_attr::class, svgdoc::SvgDocument};
3use strum::IntoEnumIterator;
4
5/// Includes the style for the SVG document from an external SCSS file.
6/// This is a SCSS stylesheet that styles the sRGB gamut plot and is embedded into the SVG output.
7const STYLE: &str = include_str!("xy_gamut_plots.scss");
8const PLANCKIAN_LABELS_AT: &[u32] = &[
9    2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6500, 7500, 9300,
10];
11
12pub fn main() -> Result<(), Box<dyn std::error::Error>> {
13    for space in RgbSpace::iter() {
14        println!("Making plot for {space:?}");
15        make_plot(space)?;
16    }
17    Ok(())
18}
19
20pub fn make_plot(space: RgbSpace) -> Result<(), Box<dyn std::error::Error>> {
21    let observer = Observer::Cie1931; // Use the CIE 1931 observer
22                                      // let space = RgbSpace::DisplayP3; // Use the sRGB color space
23
24    // Create an XYChromaticity chart with the specified observer and ranges
25    let xy_chromaticity = XYChromaticity::new((775, 875), (-0.025..=0.75, 0.0..=0.875))
26        .ticks(0.01, 0.01, 5, class("fine-grid"))
27        .ticks(0.1, 0.1, 10, class("grid"))
28        .x_labels(0.1, 10, None)
29        .x_axis_description(&format!("{} x", observer.name()), None)
30        .y_labels(0.1, 10, class("y-labels"))
31        .y_axis_description(&format!("{} y", observer.name()), None)
32        .plot_spectral_locus(class("spectral-locus"))
33        .plot_spectral_locus_ticks(440..=650, 10, 15, class("spectral-locus-ticks"))
34        .plot_spectral_locus_ticks(460..=630, 1, 7, class("spectral-locus-ticks"))
35        .plot_spectral_locus_labels(460..=620, 10, 3, class("spectral-locus-labels"))
36        .plot_rgb_gamut(space, None)
37        .plot_planckian_locus(class("planckian"))
38        .plot_planckian_locus_ticks((1500..=7500).step_by(100), 7, class("planckian-ticks-fine"))
39        .plot_planckian_locus_ticks(PLANCKIAN_LABELS_AT.to_vec(), 15, class("planckian-ticks"))
40        .plot_planckian_locus_labels(PLANCKIAN_LABELS_AT.to_vec(), 18, class("planckian-labels"))
41        .plot_grid(0.01, 0.01, class("fine-grid"))
42        .plot_grid(0.1, 0.1, class("grid"));
43
44    // create the plot
45    SvgDocument::new()
46        .append_scss(STYLE)
47        .add_plot(Box::new(xy_chromaticity))
48        .save(format!("docs/img/{}_gamut.svg", space.as_ref().to_lowercase()).as_str())?;
49    Ok(())
50}