use crate::plots::traits::PlotConfig;
use crate::render::{Color, LineStyle, MarkerStyle};
#[derive(Debug, Clone)]
pub struct LineConfig {
pub line_width: Option<f32>,
pub line_style: LineStyle,
pub color: Option<Color>,
pub marker: Option<MarkerStyle>,
pub marker_size: f32,
pub show_markers: bool,
pub alpha: f32,
pub draw_line: bool,
}
impl Default for LineConfig {
fn default() -> Self {
Self {
line_width: None,
line_style: LineStyle::Solid,
color: None,
marker: None,
marker_size: 6.0,
show_markers: false,
alpha: 1.0,
draw_line: true,
}
}
}
impl PlotConfig for LineConfig {}
impl LineConfig {
pub fn new() -> Self {
Self::default()
}
pub fn line_width(mut self, width: f32) -> Self {
self.line_width = Some(width.max(0.1));
self
}
pub fn line_style(mut self, style: LineStyle) -> Self {
self.line_style = style;
self
}
pub fn color(mut self, color: Color) -> Self {
self.color = Some(color);
self
}
pub fn with_markers(mut self, style: MarkerStyle, size: f32) -> Self {
self.marker = Some(style);
self.marker_size = size.max(0.1);
self.show_markers = true;
self
}
pub fn marker(mut self, style: MarkerStyle) -> Self {
self.marker = Some(style);
self.show_markers = true;
self
}
pub fn marker_size(mut self, size: f32) -> Self {
self.marker_size = size.max(0.1);
self
}
pub fn show_markers(mut self, show: bool) -> Self {
self.show_markers = show;
self
}
pub fn alpha(mut self, alpha: f32) -> Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn draw_line(mut self, draw: bool) -> Self {
self.draw_line = draw;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = LineConfig::default();
assert!(config.line_width.is_none());
assert!(matches!(config.line_style, LineStyle::Solid));
assert!(config.color.is_none());
assert!(!config.show_markers);
assert!(config.draw_line);
assert!((config.alpha - 1.0).abs() < f32::EPSILON);
}
#[test]
fn test_builder_methods() {
let config = LineConfig::new()
.line_width(2.5)
.line_style(LineStyle::Dashed)
.color(Color::RED)
.with_markers(MarkerStyle::Circle, 8.0)
.alpha(0.7);
assert_eq!(config.line_width, Some(2.5));
assert!(matches!(config.line_style, LineStyle::Dashed));
assert_eq!(config.color, Some(Color::RED));
assert!(config.show_markers);
assert!(matches!(config.marker, Some(MarkerStyle::Circle)));
assert!((config.marker_size - 8.0).abs() < f32::EPSILON);
assert!((config.alpha - 0.7).abs() < f32::EPSILON);
}
#[test]
fn test_line_width_clamping() {
let config = LineConfig::new().line_width(-5.0);
assert_eq!(config.line_width, Some(0.1));
}
#[test]
fn test_alpha_clamping() {
let config_low = LineConfig::new().alpha(-0.5);
let config_high = LineConfig::new().alpha(1.5);
assert!((config_low.alpha - 0.0).abs() < f32::EPSILON);
assert!((config_high.alpha - 1.0).abs() < f32::EPSILON);
}
}