use super::colors::Color;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Fill {
pub fill_type: Option<String>,
pub rotation: i64,
pub stops: Vec<GradientStop>,
pub start_color: Color,
pub end_color: Color,
}
#[derive(Debug, Clone)]
pub struct GradientStop {
pub position: f64,
pub color: Color,
}
impl GradientStop {
fn position_bits(&self) -> u64 {
let normalised = if self.position == 0.0 {
0.0
} else {
self.position
};
normalised.to_bits()
}
}
impl PartialEq for GradientStop {
fn eq(&self, other: &Self) -> bool {
self.position_bits() == other.position_bits() && self.color == other.color
}
}
impl Eq for GradientStop {}
impl std::hash::Hash for GradientStop {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.position_bits().hash(state);
self.color.hash(state);
}
}
impl GradientStop {
pub fn new(position: f64, color: Color) -> Self {
let clamped = position.clamp(0.0, 1.0);
GradientStop {
position: if clamped == 0.0 { 0.0 } else { clamped },
color,
}
}
}
pub fn spread_stops(colors: &[Color]) -> Vec<GradientStop> {
if colors.is_empty() {
return Vec::new();
}
let interval = 1.0 / (colors.len() - 1).max(1) as f64;
colors
.iter()
.enumerate()
.map(|(index, color)| GradientStop::new(index as f64 * interval, color.clone()))
.collect()
}
impl Default for Fill {
fn default() -> Self {
Fill {
fill_type: None,
rotation: 0,
stops: Vec::new(),
start_color: Color::new(Color::WHITE),
end_color: Color::new(Color::BLACK),
}
}
}
impl Fill {
pub fn linear_gradient(colors: &[Color]) -> Self {
Fill {
fill_type: Some(Fill::FILL_GRADIENT_LINEAR.to_string()),
stops: spread_stops(colors),
..Fill::default()
}
}
pub fn path_gradient(colors: &[Color]) -> Self {
Fill {
fill_type: Some(Fill::FILL_GRADIENT_PATH.to_string()),
stops: spread_stops(colors),
..Fill::default()
}
}
pub fn with_stops(mut self, colors: &[Color]) -> Self {
self.stops = spread_stops(colors);
self
}
pub fn is_gradient(&self) -> bool {
matches!(
self.fill_type.as_deref(),
Some(Fill::FILL_GRADIENT_LINEAR) | Some(Fill::FILL_GRADIENT_PATH)
)
}
pub const FILL_NONE: Option<&'static str> = None;
pub const FILL_SOLID: &'static str = "solid";
pub const FILL_GRADIENT_LINEAR: &'static str = "linear";
pub const FILL_GRADIENT_PATH: &'static str = "path";
pub const FILL_PATTERN_DARKDOWN: &'static str = "darkDown";
pub const FILL_PATTERN_DARKGRAY: &'static str = "darkGray";
pub const FILL_PATTERN_DARKGRID: &'static str = "darkGrid";
pub const FILL_PATTERN_DARKHORIZONTAL: &'static str = "darkHorizontal";
pub const FILL_PATTERN_DARKTRELLIS: &'static str = "darkTrellis";
pub const FILL_PATTERN_DARKUP: &'static str = "darkUp";
pub const FILL_PATTERN_DARKVERTICAL: &'static str = "darkVertical";
pub const FILL_PATTERN_GRAY0625: &'static str = "gray0625";
pub const FILL_PATTERN_GRAY125: &'static str = "gray125";
pub const FILL_PATTERN_LIGHTDOWN: &'static str = "lightDown";
pub const FILL_PATTERN_LIGHTGRAY: &'static str = "lightGray";
pub const FILL_PATTERN_LIGHTGRID: &'static str = "lightGrid";
pub const FILL_PATTERN_LIGHTHORIZONTAL: &'static str = "lightHorizontal";
pub const FILL_PATTERN_LIGHTTRELLIS: &'static str = "lightTrellis";
pub const FILL_PATTERN_LIGHTUP: &'static str = "lightUp";
pub const FILL_PATTERN_LIGHTVERTICAL: &'static str = "lightVertical";
pub const FILL_PATTERN_MEDIUMGRAY: &'static str = "mediumGray";
pub fn new() -> Self {
Fill::default()
}
pub fn solid(color: Color) -> Self {
Fill {
fill_type: Some(Fill::FILL_SOLID.to_string()),
start_color: color,
..Fill::default()
}
}
pub fn with_fill_type(mut self, pattern: Option<&str>) -> Self {
self.fill_type = pattern.map(|p| p.to_string());
self
}
pub fn with_start_color(mut self, color: Color) -> Self {
self.start_color = color;
self
}
pub fn with_end_color(mut self, color: Color) -> Self {
self.end_color = color;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_match_python() {
let fill = Fill::new();
assert_eq!(fill.fill_type, None);
assert_eq!(fill.rotation, 0);
assert_eq!(fill.start_color.index, Color::WHITE);
assert_eq!(fill.end_color.index, Color::BLACK);
}
#[test]
fn solid_helper() {
let fill = Fill::solid(Color::new("FFFF0000"));
assert_eq!(fill.fill_type.as_deref(), Some("solid"));
assert_eq!(fill.start_color.index, "FFFF0000");
}
#[test]
fn equality_uses_all_fields() {
let a = Fill::new();
let mut b = Fill::new();
assert_eq!(a, b);
b.rotation = 90;
assert_ne!(a, b);
}
#[test]
fn stops_are_spread_evenly_across_the_gradient() {
let stops = spread_stops(&[
Color::new("FF000000"),
Color::new("FFFFFFFF"),
Color::new("FFFF0000"),
]);
assert_eq!(stops.len(), 3);
assert_eq!(stops[0].position, 0.0);
assert_eq!(stops[1].position, 0.5);
assert_eq!(stops[2].position, 1.0);
}
#[test]
fn a_single_colour_gradient_puts_the_stop_in_the_middle() {
let stops = spread_stops(&[Color::new("FF112233")]);
assert_eq!(stops.len(), 1);
assert_eq!(stops[0].position, 0.0);
}
#[test]
fn positions_are_clamped_to_the_unit_range() {
assert_eq!(
GradientStop::new(-1.0, Color::new("FF000000")).position,
0.0
);
assert_eq!(GradientStop::new(4.0, Color::new("FF000000")).position, 1.0);
}
#[test]
fn negative_zero_is_normalised_so_two_stops_compare_equal() {
let a = GradientStop::new(-0.0, Color::new("FF000000"));
let b = GradientStop::new(0.0, Color::new("FF000000"));
assert_eq!(a, b);
assert_eq!(a.position.to_bits(), b.position.to_bits());
}
#[test]
fn a_gradient_is_told_from_a_pattern_by_its_type_and_its_stops() {
let gradient = Fill::linear_gradient(&[Color::new("FF000000"), Color::new("FFFFFFFF")]);
assert!(gradient.is_gradient());
assert_eq!(gradient.stops.len(), 2);
let solid = Fill {
fill_type: Some(Fill::FILL_SOLID.to_string()),
..Fill::default()
};
assert!(!solid.is_gradient());
assert!(solid.stops.is_empty());
}
}