use crate::components::{Orientation, Rgb};
#[derive(Clone)]
pub struct Legend {
pub background_color: Option<Rgb>,
pub border_color: Option<Rgb>,
pub border_width: Option<usize>,
pub font: Option<String>,
pub orientation: Option<Orientation>,
pub x: Option<f64>,
pub y: Option<f64>,
}
impl Default for Legend {
fn default() -> Self {
Self {
background_color: Some(Rgb(255, 255, 255)),
border_color: None,
border_width: None,
font: None,
orientation: None,
x: None,
y: None,
}
}
}
impl Legend {
pub fn new() -> Self {
Self::default()
}
pub fn background_color(mut self, color: Rgb) -> Self {
self.background_color = Some(color);
self
}
pub fn border_color(mut self, color: Rgb) -> Self {
self.border_color = Some(color);
self
}
pub fn border_width(mut self, width: usize) -> Self {
self.border_width = Some(width);
self
}
pub fn font(mut self, font: impl Into<String>) -> Self {
self.font = Some(font.into());
self
}
pub fn orientation(mut self, orientation: Orientation) -> Self {
self.orientation = Some(orientation);
self
}
pub fn x(mut self, x: f64) -> Self {
self.x = Some(x);
self
}
pub fn y(mut self, y: f64) -> Self {
self.y = Some(y);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default() {
let legend = Legend::new();
let bg = legend.background_color.unwrap();
assert_eq!(bg.0, 255);
assert_eq!(bg.1, 255);
assert_eq!(bg.2, 255);
assert!(legend.border_color.is_none());
assert!(legend.border_width.is_none());
assert!(legend.font.is_none());
assert!(legend.orientation.is_none());
assert!(legend.x.is_none());
assert!(legend.y.is_none());
}
#[test]
fn test_background_color() {
let legend = Legend::new().background_color(Rgb(200, 200, 200));
let bg = legend.background_color.unwrap();
assert_eq!(bg.0, 200);
assert_eq!(bg.1, 200);
assert_eq!(bg.2, 200);
}
#[test]
fn test_border_color() {
let legend = Legend::new().border_color(Rgb(0, 0, 0));
let bc = legend.border_color.unwrap();
assert_eq!(bc.0, 0);
assert_eq!(bc.1, 0);
assert_eq!(bc.2, 0);
}
#[test]
fn test_border_width() {
let legend = Legend::new().border_width(2);
assert_eq!(legend.border_width, Some(2));
}
#[test]
fn test_orientation() {
let legend = Legend::new().orientation(Orientation::Horizontal);
assert!(legend.orientation.is_some());
}
#[test]
fn test_builder_chaining() {
let legend = Legend::new()
.background_color(Rgb(100, 100, 100))
.border_color(Rgb(50, 50, 50))
.border_width(3)
.font("Arial")
.orientation(Orientation::Vertical)
.x(0.5)
.y(0.8);
let bg = legend.background_color.unwrap();
assert_eq!(bg.0, 100);
let bc = legend.border_color.unwrap();
assert_eq!(bc.0, 50);
assert_eq!(legend.border_width, Some(3));
assert_eq!(legend.font, Some("Arial".to_string()));
assert!(legend.orientation.is_some());
assert!((legend.x.unwrap() - 0.5).abs() < 1e-6);
assert!((legend.y.unwrap() - 0.8).abs() < 1e-6);
}
}