From af75d1bf5c244939a989c6b31b710c7b632f832b Mon Sep 17 00:00:00 2001 From: Dorival Pedroso Date: Sun, 18 Jan 2026 07:16:22 +1000 Subject: [PATCH 1/2] Improve fill_alpha and line_alpha in Contour --- src/contour.rs | 33 +++++++++++++++++++++++---------- tests/test_contour.rs | 2 ++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/contour.rs b/src/contour.rs index f4e7ea2..d803fe6 100644 --- a/src/contour.rs +++ b/src/contour.rs @@ -48,7 +48,6 @@ use std::fmt::Write; /// /// ![integ_contour.svg](https://raw.githubusercontent.com/cpmech/plotpy/main/figures/integ_contour.svg) pub struct Contour { - alpha: f64, // Alpha (transparacy) of the plot colors: Vec, // Colors to be used instead of colormap levels: Vec, // Pre-defined levels colormap_name: String, // Colormap name @@ -62,6 +61,8 @@ pub struct Contour { colorbar_location: String, // Colorbar location colorbar_extra: String, // Extra options for the colorbar number_format_cb: String, // Number format for the labels in lines contour + fill_alpha: Option, // Transparency of the filled contour + line_alpha: Option, // Transparency of the line contour line_color: String, // Line color for the lines contour line_style: String, // Line style for the lines contour line_width: f64, // Line width for the lines contour @@ -84,7 +85,6 @@ impl Contour { /// Creates a new Contour object pub fn new() -> Self { Contour { - alpha: 1.0, colors: Vec::new(), levels: Vec::new(), colormap_name: "bwr".to_string(), @@ -98,6 +98,8 @@ impl Contour { colorbar_location: String::new(), colorbar_extra: String::new(), number_format_cb: String::new(), + fill_alpha: None, + line_alpha: None, line_color: "black".to_string(), line_style: String::new(), line_width: 0.0, @@ -225,12 +227,6 @@ impl Contour { } } - /// Sets the alpha value to alpha instead of the pre-defined alpha - pub fn set_alpha(&mut self, alpha: f64) -> &mut Self { - self.alpha = alpha; - self - } - /// Sets the colors to be used instead of a pre-defined colormap /// /// Will use `colormap_index` instead if its empty. @@ -355,6 +351,18 @@ impl Contour { self } + /// Sets the transparency of the filled contour + pub fn set_fill_alpha(&mut self, alpha: f64) -> &mut Self { + self.fill_alpha = Some(alpha); + self + } + + /// Sets the transparency of the line contour + pub fn set_line_alpha(&mut self, alpha: f64) -> &mut Self { + self.line_alpha = Some(alpha); + self + } + /// Sets the line color for the lines contour (default is black) pub fn set_line_color(&mut self, color: &str) -> &mut Self { self.line_color = String::from(color); @@ -469,6 +477,9 @@ impl Contour { /// Returns options for filled contour fn options_filled(&self) -> String { let mut opt = String::new(); + if let Some(a) = self.fill_alpha { + write!(&mut opt, ",alpha={}", a).unwrap(); + } if self.colors.len() > 0 { write!(&mut opt, ",colors=colors",).unwrap(); } else { @@ -482,13 +493,15 @@ impl Contour { if self.extra_filled != "" { write!(&mut opt, ",{}", self.extra_filled).unwrap(); } - write!(&mut opt, ",alpha={}", self.alpha).unwrap(); opt } /// Returns options for line contour fn options_line(&self) -> String { let mut opt = String::new(); + if let Some(a) = self.line_alpha { + write!(&mut opt, ",alpha={}", a).unwrap(); + } if self.line_color != "" { write!(&mut opt, ",colors=['{}']", self.line_color).unwrap(); } @@ -704,4 +717,4 @@ mod tests { contour.clear_buffer(); assert_eq!(contour.buffer, ""); } -} \ No newline at end of file +} diff --git a/tests/test_contour.rs b/tests/test_contour.rs index 1040ea1..bdb2350 100644 --- a/tests/test_contour.rs +++ b/tests/test_contour.rs @@ -178,6 +178,8 @@ fn test_contour_draw_tri() -> Result<(), StrError> { .set_line_color("black") .set_line_style("-") .set_line_width(1.5) + .set_fill_alpha(1.0) + .set_line_alpha(1.0) .set_tri_show_edges(true) .set_tri_edges_color("green") .set_tri_edges_line_width(1.0) From 43a0ab564dcd9489b76ad043f2da16b96a2a07bf Mon Sep 17 00:00:00 2001 From: Dorival Pedroso Date: Sun, 18 Jan 2026 07:16:39 +1000 Subject: [PATCH 2/2] Bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 66f4e1a..2367c1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "plotpy" -version = "1.19.0" +version = "1.20.0" edition = "2021" license = "MIT" description = "Rust plotting library using Python (Matplotlib)"