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
use super::{IsColorChannel, IsColor, HasAlpha, HasntAlpha, Rgb, Hsla, Hsva};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Rgba<T> {
    pub red: T,
    pub green: T,
    pub blue: T,
    pub alpha: T,
}

impl<T: IsColorChannel> IsColor for Rgba<T> {
    type Channel = T;
}

impl<T: IsColorChannel> HasAlpha for Rgba<T> {
    type Alphaless = Rgb<T>;

    fn split_alpha(self) -> (Self::Alphaless, Self::Channel) {
        let Rgba { red, green, blue, alpha } = self;
        (Rgb { red, green, blue }, alpha)
    }
}

impl<T> Rgba<T> {
    pub fn new(red: T, green: T, blue: T, alpha: T) -> Self {
        Self { red, green, blue, alpha }
    }
}

impl<T: IsColorChannel> Default for Rgba<T> {
    fn default() -> Self {
        Self::new(T::MIN, T::MIN, T::MIN, T::MAX)
    }
}

impl<T: IsColorChannel> From<Rgb<T>> for Rgba<T> {
    fn from(rgb: Rgb<T>) -> Self {
        rgb.with_alpha(T::MAX)
    }
}

impl From<Rgba<u8>> for Rgba<f32> {
    fn from(Rgba { red, green, blue, alpha }: Rgba<u8>) -> Self {
        Self::new(
            red as f32 / 255.0,
            green as f32 / 255.0,
            blue as f32 / 255.0,
            alpha as f32 / 255.0,
        )
    }
}

impl From<Rgba<f32>> for Rgba<u8> {
    fn from(Rgba { red, green, blue, alpha }: Rgba<f32>) -> Self {
        Self::new(
            (red.clamp_channel() * 255.0).round() as u8,
            (green.clamp_channel() * 255.0).round() as u8,
            (blue.clamp_channel() * 255.0).round() as u8,
            (alpha.clamp_channel() * 255.0).round() as u8,
        )
    }
}

impl From<Hsla<f32>> for Rgba<f32> {
    fn from(hsla: Hsla<f32>) -> Self {
        let (hsl, alpha) = hsla.split_alpha();
        Rgb::from(hsl).with_alpha(alpha)
    }
}

impl From<Hsva<f32>> for Rgba<f32> {
    fn from(hsva: Hsva<f32>) -> Self {
        let (hsv, alpha) = hsva.split_alpha();
        Rgb::from(hsv).with_alpha(alpha)
    }
}