use crate::dml::color::ColorFormat;
use crate::enums::dml::MsoPatternType;
use crate::error::PptxError;
use crate::units::RelationshipId;
#[derive(Debug, Clone, PartialEq)]
pub struct SolidFill {
pub color: ColorFormat,
}
#[derive(Debug, Clone, PartialEq)]
pub struct GradientFill {
pub stops: Vec<GradientStop>,
pub angle: Option<f64>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct GradientStop {
pub position: f64,
pub color: ColorFormat,
}
impl GradientStop {
pub fn new(position: f64, color: ColorFormat) -> Result<Self, PptxError> {
if !(0.0..=1.0).contains(&position) {
return Err(PptxError::InvalidValue {
field: "GradientStop.position",
value: position.to_string(),
expected: "0.0 to 1.0",
});
}
Ok(Self { position, color })
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct PatternFill {
pub preset: Option<MsoPatternType>,
pub fore_color: Option<ColorFormat>,
pub back_color: Option<ColorFormat>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PictureFill {
pub image_r_id: RelationshipId,
pub stretch: bool,
pub tile: bool,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gradient_stops() {
let gf = GradientFill {
stops: vec![
GradientStop {
position: 0.0,
color: ColorFormat::rgb(255, 0, 0),
},
GradientStop {
position: 0.5,
color: ColorFormat::rgb(0, 255, 0),
},
GradientStop {
position: 1.0,
color: ColorFormat::rgb(0, 0, 255),
},
],
angle: Some(45.0),
};
assert_eq!(gf.stops.len(), 3);
assert_eq!(gf.stops[1].position, 0.5);
}
#[test]
fn test_picture_fill_tile_field() {
let pf = PictureFill {
image_r_id: RelationshipId::try_from("rId3").unwrap(),
stretch: false,
tile: true,
};
assert!(pf.tile);
assert!(!pf.stretch);
}
#[test]
fn test_gradient_stop_new_valid() {
let stop = GradientStop::new(0.5, ColorFormat::rgb(255, 0, 0)).unwrap(); assert_eq!(stop.position, 0.5);
}
#[test]
fn test_gradient_stop_new_boundary_values() {
assert!(GradientStop::new(0.0, ColorFormat::rgb(0, 0, 0)).is_ok());
assert!(GradientStop::new(1.0, ColorFormat::rgb(0, 0, 0)).is_ok());
}
#[test]
fn test_gradient_stop_new_out_of_range() {
assert!(GradientStop::new(-0.1, ColorFormat::rgb(0, 0, 0)).is_err());
assert!(GradientStop::new(1.1, ColorFormat::rgb(0, 0, 0)).is_err());
}
}