use crate::data::{IntoSeries, Series};
use crate::scale::Colormap;
#[derive(Clone)]
pub struct Cells<'a> {
pub(crate) columns: usize,
pub(crate) values: Series<'a>,
pub(crate) extents: Option<((f64, f64), (f64, f64))>,
pub(crate) colormap: Colormap,
}
impl<'a> Cells<'a> {
pub fn matrix(columns: usize, values: impl IntoSeries<'a>) -> Cells<'a> {
let values = values.into_series();
assert!(
columns > 0 && values.len() % columns == 0,
"Cells::matrix requires columns to divide the value count evenly"
);
Cells {
columns,
values,
extents: None,
colormap: Colormap::DEFAULT,
}
}
#[must_use]
pub fn extents(mut self, x: (f64, f64), y: (f64, f64)) -> Cells<'a> {
assert!(
x.0.is_finite() && x.1.is_finite() && y.0.is_finite() && y.1.is_finite(),
"Cells::extents requires finite bounds"
);
self.extents = Some((x, y));
self
}
#[must_use]
pub fn colormap(mut self, colormap: Colormap) -> Cells<'a> {
self.colormap = colormap;
self
}
pub fn into_owned(self) -> Cells<'static> {
Cells {
columns: self.columns,
values: self.values.into_owned(),
extents: self.extents,
colormap: self.colormap,
}
}
}
impl std::fmt::Debug for Cells<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Cells")
.field("columns", &self.columns)
.field("rows", &(self.values.len() / self.columns.max(1)))
.finish_non_exhaustive()
}
}
#[cfg(test)]
#[path = "tests/cells_tests.rs"]
mod tests;