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
198
199
200
use values::animated::{Animate, Procedure, ToAnimatedZero};
use values::distance::{ComputeSquaredDistance, SquaredDistance};
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToAnimatedZero)]
pub struct RGBA {
pub red: f32,
pub green: f32,
pub blue: f32,
pub alpha: f32,
}
impl RGBA {
#[inline]
pub fn transparent() -> Self {
Self::new(0., 0., 0., 0.)
}
#[inline]
pub fn new(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
RGBA { red: red, green: green, blue: blue, alpha: alpha }
}
}
impl Animate for RGBA {
#[inline]
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
let mut alpha = self.alpha.animate(&other.alpha, procedure)?;
if alpha <= 0. {
return Ok(RGBA::transparent());
}
alpha = alpha.min(1.);
let red = (self.red * self.alpha).animate(&(other.red * other.alpha), procedure)? * 1. / alpha;
let green = (self.green * self.alpha).animate(&(other.green * other.alpha), procedure)? * 1. / alpha;
let blue = (self.blue * self.alpha).animate(&(other.blue * other.alpha), procedure)? * 1. / alpha;
Ok(RGBA::new(red, green, blue, alpha))
}
}
impl ComputeSquaredDistance for RGBA {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
let start = [ self.alpha, self.red * self.alpha, self.green * self.alpha, self.blue * self.alpha ];
let end = [ other.alpha, other.red * other.alpha, other.green * other.alpha, other.blue * other.alpha ];
start.iter().zip(&end).map(|(this, other)| this.compute_squared_distance(other)).sum()
}
}
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Color {
pub color: RGBA,
pub foreground_ratio: f32,
}
impl Color {
fn currentcolor() -> Self {
Color {
color: RGBA::transparent(),
foreground_ratio: 1.,
}
}
pub fn transparent() -> Self {
Color {
color: RGBA::transparent(),
foreground_ratio: 0.,
}
}
fn is_currentcolor(&self) -> bool {
self.foreground_ratio >= 1.
}
fn is_numeric(&self) -> bool {
self.foreground_ratio <= 0.
}
fn effective_intermediate_rgba(&self) -> RGBA {
RGBA {
alpha: self.color.alpha * (1. - self.foreground_ratio),
.. self.color
}
}
}
impl Animate for Color {
#[inline]
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
let (this_weight, other_weight) = procedure.weights();
if self.foreground_ratio == other.foreground_ratio {
if self.is_currentcolor() {
Ok(Color::currentcolor())
} else {
Ok(Color {
color: self.color.animate(&other.color, procedure)?,
foreground_ratio: self.foreground_ratio,
})
}
} else if self.is_currentcolor() && other.is_numeric() {
Ok(Color {
color: other.color,
foreground_ratio: this_weight as f32,
})
} else if self.is_numeric() && other.is_currentcolor() {
Ok(Color {
color: self.color,
foreground_ratio: other_weight as f32,
})
} else {
let self_color = self.effective_intermediate_rgba();
let other_color = other.effective_intermediate_rgba();
let color = self_color.animate(&other_color, procedure)?;
let foreground_ratio = self.foreground_ratio.animate(&other.foreground_ratio, procedure)?;
let alpha = color.alpha / (1. - foreground_ratio);
Ok(Color {
color: RGBA {
alpha: alpha,
.. color
},
foreground_ratio: foreground_ratio,
})
}
}
}
impl ComputeSquaredDistance for Color {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
if self.foreground_ratio == other.foreground_ratio {
if self.is_currentcolor() {
Ok(SquaredDistance::Value(0.))
} else {
self.color.compute_squared_distance(&other.color)
}
} else if self.is_currentcolor() && other.is_numeric() {
Ok(
RGBA::transparent().compute_squared_distance(&other.color)? +
SquaredDistance::Value(1.),
)
} else if self.is_numeric() && other.is_currentcolor() {
Ok(
self.color.compute_squared_distance(&RGBA::transparent())? +
SquaredDistance::Value(1.),
)
} else {
let self_color = self.effective_intermediate_rgba();
let other_color = other.effective_intermediate_rgba();
Ok(
self_color.compute_squared_distance(&other_color)? +
self.foreground_ratio.compute_squared_distance(&other.foreground_ratio)?,
)
}
}
}
impl ToAnimatedZero for Color {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> {
Err(())
}
}