use crate::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PositionComponent {
fraction: f64,
offset: f64,
}
impl PositionComponent {
pub fn percent(value: f64) -> Result<Self> {
Self::new(value / 100., 0.)
}
pub fn px(value: f64) -> Result<Self> {
Self::new(0., value)
}
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
}
}