use crate::colour::Colour;
use crate::colourmap::ColourmapId;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum ColourSource {
Solid(Colour),
PerSample(Vec<Colour>),
Scalar {
values: Vec<f32>,
range: Option<(f32, f32)>,
colourmap: Option<ColourmapId>,
},
Natural {
range: Option<(f32, f32)>,
colourmap: Option<ColourmapId>,
},
}
impl Default for ColourSource {
fn default() -> Self {
Self::Natural {
range: None,
colourmap: None,
}
}
}
impl ColourSource {
pub fn per_sample_len(&self) -> Option<usize> {
match self {
Self::PerSample(colours) => Some(colours.len()),
Self::Scalar { values, .. } => Some(values.len()),
Self::Solid(_) | Self::Natural { .. } => None,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum SizeSource {
Uniform(f32),
PerSample(Vec<f32>),
Scalar {
values: Vec<f32>,
domain: Option<(f32, f32)>,
output: (f32, f32),
},
Natural {
domain: Option<(f32, f32)>,
output: (f32, f32),
},
}
impl Default for SizeSource {
fn default() -> Self {
Self::Uniform(1.0)
}
}
impl SizeSource {
pub fn per_sample_len(&self) -> Option<usize> {
match self {
Self::PerSample(sizes) => Some(sizes.len()),
Self::Scalar { values, .. } => Some(values.len()),
Self::Uniform(_) | Self::Natural { .. } => None,
}
}
pub fn output_range(&self) -> (f32, f32) {
match self {
Self::Uniform(size) => (*size, *size),
Self::PerSample(sizes) => auto_range(sizes).unwrap_or((0.0, 0.0)),
Self::Scalar { output, .. } | Self::Natural { output, .. } => *output,
}
}
}
pub fn map_range(value: f32, domain: (f32, f32), output: (f32, f32)) -> f32 {
let (lo, hi) = domain;
let span = hi - lo;
if !span.is_finite() || span.abs() < f32::EPSILON {
return output.0;
}
let t = ((value - lo) / span).clamp(0.0, 1.0);
output.0 + t * (output.1 - output.0)
}
pub fn auto_range(values: &[f32]) -> Option<(f32, f32)> {
let mut lo = f32::INFINITY;
let mut hi = f32::NEG_INFINITY;
for &v in values {
if v.is_finite() {
lo = lo.min(v);
hi = hi.max(v);
}
}
(lo <= hi).then_some((lo, hi))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_constant_domain_maps_to_the_bottom_of_the_output() {
assert_eq!(map_range(5.0, (5.0, 5.0), (0.2, 1.0)), 0.2);
}
#[test]
fn values_outside_the_domain_clamp_rather_than_extrapolate() {
assert_eq!(map_range(-10.0, (0.0, 1.0), (0.0, 4.0)), 0.0);
assert_eq!(map_range(10.0, (0.0, 1.0), (0.0, 4.0)), 4.0);
}
#[test]
fn an_inverted_output_range_is_honoured() {
assert_eq!(map_range(1.0, (0.0, 1.0), (4.0, 1.0)), 1.0);
assert_eq!(map_range(0.0, (0.0, 1.0), (4.0, 1.0)), 4.0);
}
#[test]
fn auto_range_skips_non_finite_values() {
let r = auto_range(&[1.0, f32::NAN, 3.0, f32::INFINITY]);
assert_eq!(r, Some((1.0, 3.0)));
}
#[test]
fn auto_range_is_none_when_nothing_is_finite() {
assert_eq!(auto_range(&[]), None);
assert_eq!(auto_range(&[f32::NAN, f32::NEG_INFINITY]), None);
}
#[test]
fn per_sample_len_reports_only_the_per_sample_variants() {
assert_eq!(ColourSource::Solid(Colour::WHITE).per_sample_len(), None);
assert_eq!(ColourSource::default().per_sample_len(), None);
assert_eq!(
ColourSource::PerSample(vec![Colour::WHITE; 3]).per_sample_len(),
Some(3)
);
assert_eq!(
ColourSource::Scalar {
values: vec![0.0; 7],
range: None,
colourmap: None,
}
.per_sample_len(),
Some(7)
);
}
#[test]
fn a_uniform_size_reports_its_own_value_as_the_range() {
assert_eq!(SizeSource::Uniform(2.0).output_range(), (2.0, 2.0));
assert_eq!(
SizeSource::Natural {
domain: None,
output: (0.1, 3.0)
}
.output_range(),
(0.1, 3.0)
);
}
#[test]
fn a_per_sample_size_reports_its_own_span() {
let s = SizeSource::PerSample(vec![3.0, 11.0, 7.0]);
assert_eq!(s.output_range(), (3.0, 11.0));
assert_eq!(s.per_sample_len(), Some(3));
}
#[test]
fn an_empty_per_sample_size_has_no_span() {
assert_eq!(SizeSource::PerSample(Vec::new()).output_range(), (0.0, 0.0));
}
}