• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use plotters::prelude::*;
2 
3 const OUT_FILE_NAME: &str = "plotters-doc-data/colormaps.png";
4 
main() -> Result<(), Box<dyn std::error::Error>>5 fn main() -> Result<(), Box<dyn std::error::Error>> {
6     let colormaps_rgb: [(Box<dyn ColorMap<RGBColor>>, &str); 4] = [
7         (Box::new(ViridisRGB {}), "Viridis"),
8         (Box::new(BlackWhite {}), "BlackWhite"),
9         (Box::new(Bone {}), "Bone"),
10         (Box::new(Copper {}), "Copper"),
11     ];
12 
13     let colormaps_hsl: [(Box<dyn ColorMap<HSLColor>>, &str); 2] = [
14         (Box::new(MandelbrotHSL {}), "MandelbrotHSL"),
15         (Box::new(VulcanoHSL {}), "VulcanoHSL"),
16     ];
17 
18     let size_x: i32 = 800;
19     let n_colormaps = colormaps_rgb.len() + colormaps_hsl.len();
20     let size_y = 200 + n_colormaps as u32 * 100;
21     let root = BitMapBackend::new(OUT_FILE_NAME, (size_x as u32, size_y)).into_drawing_area();
22 
23     root.fill(&WHITE)?;
24 
25     let mut chart = ChartBuilder::on(&root)
26         .caption("Demonstration of predefined colormaps", ("sans-serif", 20))
27         .build_cartesian_2d(
28             -150.0..size_x as f32 + 50.0,
29             0.0..3.0 * (n_colormaps as f32),
30         )?;
31 
32     use plotters::style::text_anchor::*;
33     let centered = Pos::new(HPos::Center, VPos::Center);
34     let label_style = TextStyle::from(("monospace", 14.0).into_font()).pos(centered);
35 
36     let mut colormap_counter = 0;
37     macro_rules! plot_colormaps(
38         ($colormap:expr) => {
39             for (colormap, colormap_name) in $colormap.iter() {
40                 chart.draw_series(
41                     (0..size_x as i32).map(|x| {
42                         Rectangle::new([
43                             (x as f32,     3.0*(n_colormaps - 1 - colormap_counter) as f32 + 0.5),
44                             (x as f32+1.0, 3.0*(n_colormaps - 1 - colormap_counter) as f32 + 2.5)
45                         ],
46                     colormap.get_color_normalized(x as f32, 0.0, size_x as f32).filled())
47                     })
48                 )?;
49                 chart.draw_series(
50                     [Text::new(colormap_name.to_owned(), (-75.0, 3.0*(n_colormaps-1-colormap_counter) as f32 + 1.5), &label_style)]
51                 )?;
52                 colormap_counter+=1;
53             }
54         }
55     );
56 
57     plot_colormaps!(colormaps_rgb);
58     plot_colormaps!(colormaps_hsl);
59 
60     // To avoid the IO failure being ignored silently, we manually call the present function
61     root.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir");
62     println!("Result has been saved to {}", OUT_FILE_NAME);
63 
64     Ok(())
65 }
66 #[test]
entry_point()67 fn entry_point() {
68     main().unwrap()
69 }
70