1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
use crate::style::css_values::*;
use ordered_float::NotNan;

#[derive(Clone, Debug)]
pub struct ExactLength {
    pub unit: Unit,
    pub value: NotNan<f64>,
}

#[derive(Clone, Debug)]
pub enum Unit {
    Px,
    Rem,
    Em,
    Cm,
    Vw,
    Vh,
}

pub fn px<T: Into<f64>>(val: T) -> ExactLength {
    ExactLength {
        value: NotNan::new(val.into()).unwrap(),
        unit: crate::style::measures::Unit::Px,
    }
}

pub fn vw<T: Into<f64>>(val: T) -> ExactLength {
    ExactLength {
        value: NotNan::new(val.into()).unwrap(),
        unit: Unit::Vw,
    }
}

pub fn vh<T: Into<f64>>(val: T) -> ExactLength {
    ExactLength {
        value: NotNan::new(val.into()).unwrap(),
        unit: Unit::Vh,
    }
}

pub fn cm<T: Into<f64>>(val: T) -> ExactLength {
    ExactLength {
        value: NotNan::new(val.into()).unwrap(),
        unit: Unit::Cm,
    }
}

pub fn rem<T: Into<f64>>(val: T) -> ExactLength {
    ExactLength {
        value: NotNan::new(val.into()).unwrap(),
        unit: Unit::Rem,
    }
}

pub fn em<T: Into<f64>>(val: T) -> ExactLength {
    ExactLength {
        value: NotNan::new(val.into()).unwrap(),
        unit: Unit::Em,
    }
}

pub fn hsl<H: Into<f64>,S: Into<f64>,L: Into<f64>>(h: H, s: S, l: L) -> CssColor {
    let h = h.into();
    let s = s.into();
    let l = l.into();
    CssColor::Hsl(h, s, l)
}

pub fn rgb<R: Into<f64>,G: Into<f64>,B: Into<f64>>(r: R, g: G, b: B) -> CssColor {
    let r = r.into();
    let g = g.into();
    let b = b.into();

    CssColor::Rgba(r, g, b, 1.0)
}

pub fn rgba<R: Into<f64>,G: Into<f64>,B: Into<f64>,A: Into<f64>>(r: R ,g: G, b: B, a: A) -> CssColor {
    let r = r.into();
    let g = g.into();
    let b = b.into();
    let a = a.into();
    CssColor::Rgba(r, g, b, a)
}

impl std::fmt::Display for ExactLength {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.unit {
            Unit::Px => write!(f, "{}px", self.value),
            Unit::Rem => write!(f, "{}rem", self.value),
            Unit::Em => write!(f, "{}em", self.value),
            Unit::Cm => write!(f, "{}cm", self.value),
            Unit::Vw => write!(f, "{}vw", self.value),
            Unit::Vh => write!(f, "{}vh", self.value),
        }
    }
}

#[derive(Clone, Debug)]
pub struct Percent(pub f64);

pub fn pc<T: Into<f64>>(val: T) -> Percent {
    Percent(val.into())
}

impl std::fmt::Display for Percent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}%", self.0)
    }
}