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
use std::f32;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut};
use linalg;
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Colorf {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
impl Colorf {
pub fn new(r: f32, g: f32, b: f32) -> Colorf {
Colorf { r: r, g: g, b: b, a: 1.0 }
}
pub fn with_alpha(r: f32, g: f32, b: f32, a: f32) -> Colorf {
Colorf { r: r, g: g, b: b, a: a }
}
pub fn broadcast(r: f32) -> Colorf {
Colorf { r: r, g: r, b: r, a: r }
}
pub fn black() -> Colorf { Colorf::broadcast(0.0) }
pub fn clamp(&self) -> Colorf {
Colorf { r: linalg::clamp(self.r, 0.0, 1.0),
g: linalg::clamp(self.g, 0.0, 1.0),
b: linalg::clamp(self.b, 0.0, 1.0),
a: linalg::clamp(self.a, 0.0, 1.0) }
}
pub fn luminance(&self) -> f32 {
0.2126 * self.r + 0.7152 * self.g + 0.0722 * self.b
}
pub fn is_black(&self) -> bool {
self.r == 0f32 && self.g == 0f32 && self.b == 0f32
}
pub fn has_nans(&self) -> bool {
f32::is_nan(self.r) || f32::is_nan(self.g) || f32::is_nan(self.b) || f32::is_nan(self.a)
}
pub fn has_infs(&self) -> bool {
f32::is_infinite(self.r) || f32::is_infinite(self.g) || f32::is_infinite(self.b) || f32::is_infinite(self.a)
}
pub fn to_srgb(&self) -> Colorf {
let a = 0.055f32;
let b = 1f32 / 2.4;
let mut srgb = Colorf::broadcast(0.0);
for i in 0..3 {
if self[i] <= 0.0031308 {
srgb[i] = 12.92 * self[i];
} else {
srgb[i] = (1.0 + a) * f32::powf(self[i], b) - a;
}
}
srgb
}
pub fn exp(&self) -> Colorf {
Colorf { r: f32::exp(self.r), g: f32::exp(self.g),
b: f32::exp(self.b), a: f32::exp(self.a) }
}
}
impl Add for Colorf {
type Output = Colorf;
fn add(self, rhs: Colorf) -> Colorf {
Colorf { r: self.r + rhs.r, g: self.g + rhs.g, b: self.b + rhs.b, a: self.a + rhs.a }
}
}
impl Sub for Colorf {
type Output = Colorf;
fn sub(self, rhs: Colorf) -> Colorf {
Colorf { r: self.r - rhs.r, g: self.g - rhs.g, b: self.b - rhs.b, a: self.a - rhs.a }
}
}
impl Mul for Colorf {
type Output = Colorf;
fn mul(self, rhs: Colorf) -> Colorf {
Colorf { r: self.r * rhs.r, g: self.g * rhs.g, b: self.b * rhs.b, a: self.a * rhs.a }
}
}
impl Mul<f32> for Colorf {
type Output = Colorf;
fn mul(self, rhs: f32) -> Colorf {
Colorf { r: self.r * rhs, g: self.g * rhs, b: self.b * rhs, a: self.a * rhs }
}
}
impl Mul<Colorf> for f32 {
type Output = Colorf;
fn mul(self, rhs: Colorf) -> Colorf {
Colorf { r: self * rhs.r, g: self * rhs.g, b: self * rhs.b, a: self * rhs.a }
}
}
impl Div for Colorf {
type Output = Colorf;
fn div(self, rhs: Colorf) -> Colorf {
Colorf { r: self.r / rhs.r, g: self.g / rhs.g, b: self.b / rhs.b, a: self.a / rhs.a }
}
}
impl Div<f32> for Colorf {
type Output = Colorf;
fn div(self, rhs: f32) -> Colorf {
Colorf { r: self.r / rhs, g: self.g / rhs, b: self.b / rhs, a: self.a / rhs }
}
}
impl Neg for Colorf {
type Output = Colorf;
fn neg(self) -> Colorf {
Colorf { r: -self.r, g: -self.g, b: -self.b, a: -self.a }
}
}
impl Index<usize> for Colorf {
type Output = f32;
fn index(&self, i: usize) -> &f32 {
match i {
0 => &self.r,
1 => &self.g,
2 => &self.b,
3 => &self.a,
_ => panic!("Invalid index into color"),
}
}
}
impl IndexMut<usize> for Colorf {
fn index_mut(&mut self, i: usize) -> &mut f32 {
match i {
0 => &mut self.r,
1 => &mut self.g,
2 => &mut self.b,
3 => &mut self.a,
_ => panic!("Invalid index into color"),
}
}
}