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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use num::Float;
use clamp;
pub use self::srgb::Srgb;
pub use self::gamma_rgb::GammaRgb;
mod srgb;
mod gamma_rgb;
pub trait RgbPixel<T: Float = f32> {
fn from_rgba(red: T, green: T, blue: T, alpha: T) -> Self;
fn to_rgba(&self) -> (T, T, T, T);
}
impl<T: Float> RgbPixel<T> for (f32, f32, f32, f32) {
fn from_rgba(red: T, green: T, blue: T, alpha: T) -> (f32, f32, f32, f32) {
( red.to_f32().unwrap(), green.to_f32().unwrap(), blue.to_f32().unwrap(), alpha.to_f32().unwrap() )
}
fn to_rgba(&self) -> (T, T, T, T) {
let (r, g, b, a) = *self;
( T::from(r).unwrap(), T::from(g).unwrap(), T::from(b).unwrap(), T::from(a).unwrap() )
}
}
impl<T: Float> RgbPixel<T> for (f32, f32, f32) {
fn from_rgba(red: T, green: T, blue: T, _alpha: T) -> (f32, f32, f32) {
( red.to_f32().unwrap(), green.to_f32().unwrap(), blue.to_f32().unwrap() )
}
fn to_rgba(&self) -> (T, T, T, T) {
let (r, g, b) = *self;
( T::from(r).unwrap(), T::from(g).unwrap(), T::from(b).unwrap(), T::one() )
}
}
impl<T: Float> RgbPixel<T> for (f64, f64, f64, f64) {
fn from_rgba(red: T, green: T, blue: T, alpha: T) -> (f64, f64, f64, f64) {
( red.to_f64().unwrap(), green.to_f64().unwrap(), blue.to_f64().unwrap(), alpha.to_f64().unwrap() )
}
fn to_rgba(&self) -> (T, T, T, T) {
let (r, g, b, a) = *self;
( T::from(r).unwrap(), T::from(g).unwrap(), T::from(b).unwrap(), T::from(a).unwrap() )
}
}
impl<T: Float> RgbPixel<T> for (f64, f64, f64) {
fn from_rgba(red: T, green: T, blue: T, _alpha: T) -> (f64, f64, f64) {
(red.to_f64().unwrap(), green.to_f64().unwrap(), blue.to_f64().unwrap())
}
fn to_rgba(&self) -> (T, T, T, T) {
let (r, g, b) = *self;
( T::from(r).unwrap(), T::from(g).unwrap(), T::from(b).unwrap(), T::one() )
}
}
impl<T: Float> RgbPixel<T> for (u8, u8, u8, u8) {
fn from_rgba(red: T, green: T, blue: T, alpha: T) -> (u8, u8, u8, u8) {
let c255 = T::from(255.0).unwrap();
(
clamp(red * c255, T::zero(), c255).to_u8().unwrap(),
clamp(green * c255, T::zero(), c255).to_u8().unwrap(),
clamp(blue * c255, T::zero(), c255).to_u8().unwrap(),
clamp(alpha * c255, T::zero(), c255).to_u8().unwrap(),
)
}
fn to_rgba(&self) -> (T, T, T, T) {
let (r, g, b, a) = *self;
let c255 = T::from(255.0).unwrap();
(
T::from(r).unwrap() / c255,
T::from(g).unwrap() / c255,
T::from(b).unwrap() / c255,
T::from(a).unwrap() / c255,
)
}
}
impl<T: Float> RgbPixel<T> for (u8, u8, u8) {
fn from_rgba(red: T, green: T, blue: T, _alpha: T) -> (u8, u8, u8) {
let c255 = T::from(255.0).unwrap();
(
clamp(red * c255, T::zero(), c255).to_u8().unwrap(),
clamp(green * c255, T::zero(), c255).to_u8().unwrap(),
clamp(blue * c255, T::zero(), c255).to_u8().unwrap(),
)
}
fn to_rgba(&self) -> (T, T, T, T) {
let (r, g, b) = *self;
let c255 = T::from(255.0).unwrap();
(
T::from(r).unwrap() / c255,
T::from(g).unwrap() / c255,
T::from(b).unwrap() / c255,
T::one(),
)
}
}
impl<T: Float> RgbPixel<T> for [f32; 4] {
fn from_rgba(red: T, green: T, blue: T, alpha: T) -> [f32; 4] {
[ red.to_f32().unwrap(), green.to_f32().unwrap(), blue.to_f32().unwrap(), alpha.to_f32().unwrap() ]
}
fn to_rgba(&self) -> (T, T, T, T) {
( T::from(self[0]).unwrap(), T::from(self[1]).unwrap(), T::from(self[2]).unwrap(), T::from(self[3]).unwrap() )
}
}
impl<T: Float> RgbPixel<T> for [f32; 3] {
fn from_rgba(red: T, green: T, blue: T, _alpha: T) -> [f32; 3] {
[red.to_f32().unwrap(), green.to_f32().unwrap(), blue.to_f32().unwrap()]
}
fn to_rgba(&self) -> (T, T, T, T) {
(T::from(self[0]).unwrap(), T::from(self[1]).unwrap(), T::from(self[2]).unwrap(), T::one())
}
}
impl<T: Float> RgbPixel<T> for [f64; 4] {
fn from_rgba(red: T, green: T, blue: T, alpha: T) -> [f64; 4] {
[red.to_f64().unwrap(), green.to_f64().unwrap(), blue.to_f64().unwrap(), alpha.to_f64().unwrap()]
}
fn to_rgba(&self) -> (T, T, T, T) {
(T::from(self[0]).unwrap(), T::from(self[1]).unwrap(), T::from(self[2]).unwrap(), T::from(self[3]).unwrap())
}
}
impl<T: Float> RgbPixel<T> for [f64; 3] {
fn from_rgba(red: T, green: T, blue: T, _alpha: T) -> [f64; 3] {
[red.to_f64().unwrap(), green.to_f64().unwrap(), blue.to_f64().unwrap()]
}
fn to_rgba(&self) -> (T, T, T, T) {
(T::from(self[0]).unwrap(), T::from(self[1]).unwrap(), T::from(self[2]).unwrap(), T::one())
}
}
impl<T: Float> RgbPixel<T> for [u8; 4] {
fn from_rgba(red: T, green: T, blue: T, alpha: T) -> [u8; 4] {
let c255 = T::from(255.0).unwrap();
[
clamp(red * c255, T::zero(), c255).to_u8().unwrap(),
clamp(green * c255, T::zero(), c255).to_u8().unwrap(),
clamp(blue * c255, T::zero(), c255).to_u8().unwrap(),
clamp(alpha * c255, T::zero(), c255).to_u8().unwrap(),
]
}
fn to_rgba(&self) -> (T, T, T, T) {
let c255 = T::from(255.0).unwrap();
(
T::from(self[0]).unwrap() / c255,
T::from(self[1]).unwrap() / c255,
T::from(self[2]).unwrap() / c255,
T::from(self[3]).unwrap() / c255,
)
}
}
impl<T: Float> RgbPixel<T> for [u8; 3] {
fn from_rgba(red: T, green: T, blue: T, _alpha: T) -> [u8; 3] {
let c255 = T::from(255.0).unwrap();
[
clamp(red * c255, T::zero(), c255).to_u8().unwrap(),
clamp(green * c255, T::zero(), c255).to_u8().unwrap(),
clamp(blue * c255, T::zero(), c255).to_u8().unwrap(),
]
}
fn to_rgba(&self) -> (T, T, T, T) {
let c255 = T::from(255.0).unwrap();
(
T::from(self[0]).unwrap() / c255,
T::from(self[1]).unwrap() / c255,
T::from(self[2]).unwrap() / c255,
T::one(),
)
}
}