Skip to main content

leptos_css/
opacity.rs

1use std::fmt;
2
3use crate::checked_value::impl_checked_value;
4use crate::{InvalidCssNumber, UnitInterval, serialization::CssWriteTo};
5
6/// A normalized opacity value in the inclusive range `0..=1`.
7#[derive(Clone, Copy, Debug, PartialEq)]
8pub struct Opacity(UnitInterval);
9
10impl Opacity {
11    /// Construct an opacity value.
12    #[track_caller]
13    pub fn new(value: f64) -> Self {
14        Self(UnitInterval::new(value))
15    }
16
17    /// Attempt to construct an opacity value.
18    pub fn try_new(value: f64) -> Result<Self, InvalidCssNumber> {
19        UnitInterval::try_new(value).map(Self)
20    }
21}
22
23impl CssWriteTo for Opacity {
24    fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
25        write!(w, "{}", self.0)
26    }
27}
28
29impl_checked_value!(Opacity);