use crate::colour::Rgb;
use super::options::{Shade, Shading};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Axial,
Radial,
}
impl Kind {
pub fn code(self) -> u8 {
match self {
Kind::Axial => 2,
Kind::Radial => 3,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Ramp {
pub name: String,
pub kind: Kind,
pub coords: Vec<f64>,
pub stops: Vec<(f64, Rgb)>,
pub extend: (bool, bool),
}
impl Ramp {
pub fn of(shade: &Shade, name: &str) -> Ramp {
match shade.kind {
Shading::Axis => Ramp {
name: name.to_string(),
kind: Kind::Axial,
coords: vec![0.0, -50.0, 0.0, 50.0],
stops: vec![
(0.0, shade.bottom),
(0.25, shade.bottom),
(0.5, shade.middle),
(0.75, shade.top),
(1.0, shade.top),
],
extend: (false, false),
},
Shading::Radial => Ramp {
name: name.to_string(),
kind: Kind::Radial,
coords: vec![0.0, 0.0, 0.0, 0.0, 0.0, 50.0],
stops: vec![(0.0, shade.inner), (0.5, shade.outer), (1.0, shade.outer)],
extend: (true, false),
},
Shading::Ball => Ramp {
name: name.to_string(),
kind: Kind::Radial,
coords: vec![-10.0, 10.0, 0.0, -10.0, 10.0, 50.0],
stops: vec![
(0.0, mix(shade.ball, WHITE, 0.15)),
(0.18, mix(shade.ball, WHITE, 0.75)),
(0.36, mix(shade.ball, BLACK, 0.70)),
(0.5, mix(shade.ball, BLACK, 0.50)),
(1.0, BLACK),
],
extend: (true, false),
},
}
}
pub fn dictionary(&self) -> String {
let number = super::render::number;
let mut functions = String::new();
let mut bounds = String::new();
let mut encode = String::new();
for pair in self.stops.windows(2) {
let ((_, from), (at, to)) = (pair[0], pair[1]);
functions.push_str(&format!(
"<< /FunctionType 2 /Domain [0 1] /C0 [{} {} {}] /C1 [{} {} {}] /N 1 >> ",
number(from.0),
number(from.1),
number(from.2),
number(to.0),
number(to.1),
number(to.2),
));
encode.push_str("0 1 ");
if at < 1.0 {
bounds.push_str(&format!("{} ", number(at)));
}
}
let coords: Vec<String> = self.coords.iter().map(|c| number(*c)).collect();
let extend = |on: bool| match on {
true => "true",
false => "false",
};
format!(
"<< /ShadingType {} /ColorSpace /DeviceRGB /Coords [{}] \
/Function << /FunctionType 3 /Domain [0 1] /Functions [ {}] \
/Bounds [ {}] /Encode [ {}] >> /Extend [{} {}] >>",
self.kind.code(),
coords.join(" "),
functions,
bounds.trim_end(),
encode.trim_end(),
extend(self.extend.0),
extend(self.extend.1),
)
}
}
pub fn placement(shade: &Shade, box_: (f64, f64, f64, f64)) -> ((f64, f64), f64, (f64, f64)) {
let (min_x, min_y, max_x, max_y) = box_;
let centre = ((min_x + max_x) / 2.0, (min_y + max_y) / 2.0);
let (width, height) = (max_x - min_x, max_y - min_y);
let (sin, cos) = shade.angle.to_radians().sin_cos();
let (abs_sin, abs_cos) = (sin.abs(), cos.abs());
let total = abs_cos + abs_sin;
let scale = match total == 0.0 {
true => (0.0, 0.0),
false => (
(abs_cos * width + abs_sin * height) / (50.0 * total),
(abs_sin * width + abs_cos * height) / (50.0 * total),
),
};
(centre, shade.angle, scale)
}
const WHITE: Rgb = (1.0, 1.0, 1.0);
const BLACK: Rgb = (0.0, 0.0, 0.0);
fn mix(a: Rgb, b: Rgb, fraction: f64) -> Rgb {
(
a.0 * fraction + b.0 * (1.0 - fraction),
a.1 * fraction + b.1 * (1.0 - fraction),
a.2 * fraction + b.2 * (1.0 - fraction),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_axis_ramp_is_flat_at_both_ends() {
let shade = Shade {
top: (1.0, 0.0, 0.0),
bottom: (0.0, 0.0, 1.0),
middle: (0.5, 0.0, 0.5),
angle: 90.0,
..Shade::default()
};
let ramp = Ramp::of(&shade, "pgfsh0");
assert_eq!(ramp.kind, Kind::Axial);
assert_eq!(
ramp.stops,
vec![
(0.0, (0.0, 0.0, 1.0)),
(0.25, (0.0, 0.0, 1.0)),
(0.5, (0.5, 0.0, 0.5)),
(0.75, (1.0, 0.0, 0.0)),
(1.0, (1.0, 0.0, 0.0)),
]
);
let dictionary = ramp.dictionary();
assert!(dictionary.contains("/ShadingType 2"), "{dictionary}");
assert!(
dictionary.contains("/Bounds [ 0.25 0.5 0.75]"),
"{dictionary}"
);
assert!(
dictionary.contains("/C0 [0 0 1] /C1 [0 0 1]"),
"{dictionary}"
);
assert!(dictionary.contains("/Extend [false false]"), "{dictionary}");
}
#[test]
fn the_radial_ramp_is_two_circles_about_the_middle() {
let shade = Shade {
kind: Shading::Radial,
inner: (1.0, 0.0, 0.0),
outer: (0.0, 0.0, 1.0),
..Shade::default()
};
let ramp = Ramp::of(&shade, "pgfsh0");
assert_eq!(ramp.coords, vec![0.0, 0.0, 0.0, 0.0, 0.0, 50.0]);
assert_eq!(ramp.extend, (true, false));
let dictionary = ramp.dictionary();
assert!(dictionary.contains("/ShadingType 3"), "{dictionary}");
assert!(dictionary.contains("/Bounds [ 0.5]"), "{dictionary}");
}
#[test]
fn the_placement_is_pgfs_own_two_scales() {
let shade = Shade {
angle: 90.0,
..Shade::default()
};
let (centre, angle, (sx, sy)) = placement(&shade, (0.0, 0.0, 56.69362, 28.3468));
assert!((centre.0 - 28.3468).abs() < 1e-4, "{centre:?}");
assert!((centre.1 - 14.17339).abs() < 1e-4, "{centre:?}");
assert_eq!(angle, 90.0);
assert!((sx - 0.56694).abs() < 1e-3, "{sx}");
assert!((sy - 1.13387).abs() < 1e-3, "{sy}");
let (_, _, (sx, sy)) = placement(&Shade::default(), (0.0, 0.0, 56.69362, 28.3468));
assert!((sx - 1.13387).abs() < 1e-3, "{sx}");
assert!((sy - 0.56694).abs() < 1e-3, "{sy}");
}
#[test]
fn a_balls_highlight_is_up_and_to_the_left() {
let shade = Shade {
kind: Shading::Ball,
ball: (0.0, 0.0, 1.0),
..Shade::default()
};
let ramp = Ramp::of(&shade, "pgfsh0");
assert_eq!(ramp.coords[0], -10.0);
assert_eq!(ramp.coords[1], 10.0);
assert_eq!(ramp.stops[0].1, (0.85, 0.85, 1.0));
assert_eq!(ramp.stops.last().unwrap().1, (0.0, 0.0, 0.0));
}
}