lichtung-plot 0.3.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
use crate::{Error, Result};

/// One physical object-position component, resolved in CSS pixels.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PositionComponent {
    fraction: f64,
    offset: f64,
}

impl PositionComponent {
    /// A percentage of the difference between frame and object size.
    pub fn percent(value: f64) -> Result<Self> {
        Self::new(value / 100., 0.)
    }

    /// An offset from the leading edge, in CSS pixels.
    pub fn px(value: f64) -> Result<Self> {
        Self::new(0., value)
    }

    /// Combine a fractional alignment with a CSS-pixel offset.
    pub fn new(fraction: f64, offset: f64) -> Result<Self> {
        if !fraction.is_finite() || !offset.is_finite() {
            return Err(Error::Data("object position must be finite".into()));
        }
        Ok(Self { fraction, offset })
    }

    pub(crate) const fn aligned(fraction: f64) -> Self {
        Self {
            fraction,
            offset: 0.,
        }
    }

    pub(crate) fn components(self) -> (f64, f64) {
        (self.fraction, self.offset)
    }

    pub(crate) fn resolve(self, spare: f64) -> f64 {
        self.fraction * spare + self.offset
    }
}