use crate::render::{Color, LineStyle};
#[derive(Debug, Clone, PartialEq)]
pub struct GridStyle {
pub visible: bool,
pub color: Color,
pub line_width: f32,
pub alpha: f32,
pub line_style: LineStyle,
pub minor: bool,
pub minor_line_width: f32,
pub minor_alpha: f32,
}
impl Default for GridStyle {
fn default() -> Self {
Self {
visible: true,
color: Color::from_gray(204), line_width: 0.5,
alpha: 0.3,
line_style: LineStyle::Solid,
minor: false,
minor_line_width: 0.25,
minor_alpha: 0.15,
}
}
}
impl GridStyle {
pub fn new() -> Self {
Self::default()
}
pub fn hidden() -> Self {
Self {
visible: false,
..Default::default()
}
}
pub fn prominent() -> Self {
Self {
visible: true,
color: Color::from_gray(176), line_width: 0.8,
alpha: 0.5,
line_style: LineStyle::Solid,
minor: false,
minor_line_width: 0.4,
minor_alpha: 0.25,
}
}
pub fn visible(mut self, enabled: bool) -> Self {
self.visible = enabled;
self
}
pub fn color(mut self, color: Color) -> Self {
self.color = color;
self
}
pub fn line_width(mut self, width: f32) -> Self {
self.line_width = width.max(0.0);
self
}
pub fn alpha(mut self, alpha: f32) -> Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn line_style(mut self, style: LineStyle) -> Self {
self.line_style = style;
self
}
pub fn minor(mut self, enabled: bool) -> Self {
self.minor = enabled;
self
}
pub fn minor_line_width(mut self, width: f32) -> Self {
self.minor_line_width = width.max(0.0);
self
}
pub fn minor_alpha(mut self, alpha: f32) -> Self {
self.minor_alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn effective_color(&self) -> Color {
self.color.with_alpha(self.alpha)
}
pub fn effective_minor_color(&self) -> Color {
self.color.with_alpha(self.minor_alpha)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_matches_seaborn_whitegrid() {
let style = GridStyle::default();
assert!(style.visible);
assert_eq!(style.color.r, 204);
assert_eq!(style.color.g, 204);
assert_eq!(style.color.b, 204);
assert!((style.line_width - 0.5).abs() < 0.001);
assert!((style.alpha - 0.3).abs() < 0.001);
assert!(matches!(style.line_style, LineStyle::Solid));
assert!(!style.minor);
}
#[test]
fn test_hidden() {
let style = GridStyle::hidden();
assert!(!style.visible);
assert!((style.alpha - 0.3).abs() < 0.001);
}
#[test]
fn test_prominent() {
let style = GridStyle::prominent();
assert!(style.visible);
assert!(style.alpha > 0.3); assert!(style.line_width > 0.5); }
#[test]
fn test_builder_methods() {
let style = GridStyle::default()
.visible(false)
.color(Color::BLUE)
.line_width(2.0)
.alpha(0.5)
.line_style(LineStyle::Dashed)
.minor(true);
assert!(!style.visible);
assert_eq!(style.color, Color::BLUE);
assert!((style.line_width - 2.0).abs() < 0.001);
assert!((style.alpha - 0.5).abs() < 0.001);
assert!(matches!(style.line_style, LineStyle::Dashed));
assert!(style.minor);
}
#[test]
fn test_effective_color() {
let style = GridStyle::default();
let effective = style.effective_color();
assert_eq!(effective.r, 204);
assert_eq!(effective.g, 204);
assert_eq!(effective.b, 204);
assert_eq!(effective.a, 76); }
#[test]
fn test_clamping() {
let style = GridStyle::default()
.alpha(2.0) .line_width(-5.0);
assert!((style.alpha - 1.0).abs() < 0.001);
assert!((style.line_width - 0.0).abs() < 0.001);
}
}