Skip to content

Commit 165b3a3

Browse files
authored
Merge pull request #452 from jonaspleyer/master
Introduce Colormaps
2 parents c34fcc0 + 693234b commit 165b3a3

File tree

7 files changed

+393
-5
lines changed

7 files changed

+393
-5
lines changed

plotters/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ default = [
7070
"ttf",
7171
"image",
7272
"deprecated_items", "all_series", "all_elements",
73-
"full_palette"
73+
"full_palette",
74+
"colormaps"
7475
]
7576
all_series = ["area_series", "line_series", "point_series", "surface_series"]
7677
all_elements = ["errorbar", "candlestick", "boxplot", "histogram"]
@@ -83,6 +84,7 @@ svg_backend = ["plotters-svg"]
8384

8485
# Colors
8586
full_palette = []
87+
colormaps = []
8688

8789
# Elements
8890
errorbar = []

plotters/examples/3d-plot2.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3636
(-15..=15).map(|x| x as f64 / 5.0),
3737
pdf,
3838
)
39-
.style_func(&|&v| {
40-
(&HSLColor(240.0 / 360.0 - 240.0 / 360.0 * v / 5.0, 1.0, 0.7)).into()
41-
}),
39+
.style_func(&|&v| (VulcanoHSL::get_color(v / 5.0)).into()),
4240
)?;
4341

4442
root.present()?;

plotters/examples/colormaps.rs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use plotters::prelude::*;
2+
3+
const OUT_FILE_NAME: &'static str = "plotters-doc-data/colormaps.png";
4+
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]
67+
fn entry_point() {
68+
main().unwrap()
69+
}

plotters/examples/mandelbrot.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2828

2929
for (x, y, c) in mandelbrot_set(xr, yr, (pw as usize, ph as usize), 100) {
3030
if c != 100 {
31-
plotting_area.draw_pixel((x, y), &HSLColor(c as f64 / 100.0, 1.0, 0.5))?;
31+
plotting_area.draw_pixel((x, y), &MandelbrotHSL::get_color(c as f64 / 100.0))?;
3232
} else {
3333
plotting_area.draw_pixel((x, y), &BLACK)?;
3434
}

plotters/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,9 @@ pub mod prelude {
852852
#[cfg(feature = "full_palette")]
853853
pub use crate::style::full_palette;
854854

855+
#[cfg(feature = "colormaps")]
856+
pub use crate::style::colors::colormaps::*;
857+
855858
pub use crate::style::{
856859
AsRelative, Color, FontDesc, FontFamily, FontStyle, FontTransform, HSLColor, IntoFont,
857860
IntoTextStyle, Palette, Palette100, Palette99, Palette9999, PaletteColor, RGBAColor,

0 commit comments

Comments
 (0)