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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use super::*;
use cint::{Alpha, ColorInterop, EncodedSrgb, Hsv, LinearSrgb, PremultipliedAlpha};

// ---- Color32 ----

impl From<Alpha<EncodedSrgb<u8>>> for Color32 {
    fn from(srgba: Alpha<EncodedSrgb<u8>>) -> Self {
        let Alpha {
            color: EncodedSrgb { r, g, b },
            alpha: a,
        } = srgba;

        Self::from_rgba_unmultiplied(r, g, b, a)
    }
}

// No From<Color32> for Alpha<_> because Color32 is premultiplied

impl From<PremultipliedAlpha<EncodedSrgb<u8>>> for Color32 {
    fn from(srgba: PremultipliedAlpha<EncodedSrgb<u8>>) -> Self {
        let PremultipliedAlpha {
            color: EncodedSrgb { r, g, b },
            alpha: a,
        } = srgba;

        Self::from_rgba_premultiplied(r, g, b, a)
    }
}

impl From<Color32> for PremultipliedAlpha<EncodedSrgb<u8>> {
    fn from(col: Color32) -> Self {
        let (r, g, b, a) = col.to_tuple();

        Self {
            color: EncodedSrgb { r, g, b },
            alpha: a,
        }
    }
}

impl From<PremultipliedAlpha<EncodedSrgb<f32>>> for Color32 {
    fn from(srgba: PremultipliedAlpha<EncodedSrgb<f32>>) -> Self {
        let PremultipliedAlpha {
            color: EncodedSrgb { r, g, b },
            alpha: a,
        } = srgba;

        // This is a bit of an abuse of the function name but it does what we want.
        let r = linear_u8_from_linear_f32(r);
        let g = linear_u8_from_linear_f32(g);
        let b = linear_u8_from_linear_f32(b);
        let a = linear_u8_from_linear_f32(a);

        Self::from_rgba_premultiplied(r, g, b, a)
    }
}

impl From<Color32> for PremultipliedAlpha<EncodedSrgb<f32>> {
    fn from(col: Color32) -> Self {
        let (r, g, b, a) = col.to_tuple();

        // This is a bit of an abuse of the function name but it does what we want.
        let r = linear_f32_from_linear_u8(r);
        let g = linear_f32_from_linear_u8(g);
        let b = linear_f32_from_linear_u8(b);
        let a = linear_f32_from_linear_u8(a);

        Self {
            color: EncodedSrgb { r, g, b },
            alpha: a,
        }
    }
}

impl ColorInterop for Color32 {
    type CintTy = PremultipliedAlpha<EncodedSrgb<u8>>;
}

// ---- Rgba ----

impl From<PremultipliedAlpha<LinearSrgb<f32>>> for Rgba {
    fn from(srgba: PremultipliedAlpha<LinearSrgb<f32>>) -> Self {
        let PremultipliedAlpha {
            color: LinearSrgb { r, g, b },
            alpha: a,
        } = srgba;

        Self([r, g, b, a])
    }
}

impl From<Rgba> for PremultipliedAlpha<LinearSrgb<f32>> {
    fn from(col: Rgba) -> Self {
        let (r, g, b, a) = col.to_tuple();

        Self {
            color: LinearSrgb { r, g, b },
            alpha: a,
        }
    }
}

impl ColorInterop for Rgba {
    type CintTy = PremultipliedAlpha<LinearSrgb<f32>>;
}

// ---- Hsva ----

impl From<Alpha<Hsv<f32>>> for Hsva {
    fn from(srgba: Alpha<Hsv<f32>>) -> Self {
        let Alpha {
            color: Hsv { h, s, v },
            alpha: a,
        } = srgba;

        Self::new(h, s, v, a)
    }
}

impl From<Hsva> for Alpha<Hsv<f32>> {
    fn from(col: Hsva) -> Self {
        let Hsva { h, s, v, a } = col;

        Self {
            color: Hsv { h, s, v },
            alpha: a,
        }
    }
}

impl ColorInterop for Hsva {
    type CintTy = Alpha<Hsv<f32>>;
}

// ---- HsvaGamma ----

impl ColorInterop for HsvaGamma {
    type CintTy = Alpha<Hsv<f32>>;
}

impl From<Alpha<Hsv<f32>>> for HsvaGamma {
    fn from(srgba: Alpha<Hsv<f32>>) -> Self {
        let Alpha {
            color: Hsv { h, s, v },
            alpha: a,
        } = srgba;

        Hsva::new(h, s, v, a).into()
    }
}

impl From<HsvaGamma> for Alpha<Hsv<f32>> {
    fn from(col: HsvaGamma) -> Self {
        let Hsva { h, s, v, a } = col.into();

        Self {
            color: Hsv { h, s, v },
            alpha: a,
        }
    }
}