/
color.rs
245 lines (221 loc) · 8.37 KB
/
color.rs
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Animated types for CSS colors.
use values::animated::{Animate, Procedure, ToAnimatedZero};
use values::distance::{ComputeSquaredDistance, SquaredDistance};
use values::computed::ComplexColorRatios;
/// An animated RGBA color.
///
/// Unlike in computed values, each component value may exceed the
/// range `[0.0, 1.0]`.
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToAnimatedZero)]
pub struct RGBA {
/// The red component.
pub red: f32,
/// The green component.
pub green: f32,
/// The blue component.
pub blue: f32,
/// The alpha component.
pub alpha: f32,
}
impl RGBA {
/// Returns a transparent color.
#[inline]
pub fn transparent() -> Self {
Self::new(0., 0., 0., 0.)
}
/// Returns a new color.
#[inline]
pub fn new(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
RGBA {
red: red,
green: green,
blue: blue,
alpha: alpha,
}
}
}
/// Unlike Animate for computed colors, we don't clamp any component values.
///
/// FIXME(nox): Why do computed colors even implement Animate?
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. {
// Ideally we should return color value that only alpha component is
// 0, but this is what current gecko does.
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()
}
}
impl Animate for ComplexColorRatios {
#[inline]
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
let bg = self.bg.animate(&other.bg, procedure)?;
let fg = self.fg.animate(&other.fg, procedure)?;
Ok(ComplexColorRatios { bg, fg })
}
}
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Color {
Numeric(RGBA),
Foreground,
Complex(RGBA, ComplexColorRatios),
}
impl Color {
fn currentcolor() -> Self {
Color::Foreground
}
/// Returns a transparent intermediate color.
pub fn transparent() -> Self {
Color::Numeric(RGBA::transparent())
}
fn effective_intermediate_rgba(&self) -> RGBA {
match *self {
Color::Numeric(color) => color,
Color::Foreground => RGBA::transparent(),
Color::Complex(color, ratios) => RGBA {
alpha: color.alpha * ratios.bg,
..color.clone()
},
}
}
fn effective_ratios(&self) -> ComplexColorRatios {
match *self {
Color::Numeric(..) => ComplexColorRatios::NUMERIC,
Color::Foreground => ComplexColorRatios::FOREGROUND,
Color::Complex(.., ratios) => ratios,
}
}
}
impl Animate for Color {
#[inline]
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
// Common cases are interpolating between two numeric colors,
// two currentcolors, and a numeric color and a currentcolor.
let (this_weight, other_weight) = procedure.weights();
Ok(match (*self, *other, procedure) {
// Any interpolation of currentColor with currentColor returns currentColor.
(Color::Foreground, Color::Foreground, Procedure::Interpolate { .. }) => {
Color::currentcolor()
}
// Animating two numeric colors.
(Color::Numeric(c1), Color::Numeric(c2), _) => {
Color::Numeric(c1.animate(&c2, procedure)?)
}
// Combinations of numeric color and currentColor
(Color::Foreground, Color::Numeric(color), _) => Color::Complex(
color,
ComplexColorRatios {
bg: other_weight as f32,
fg: this_weight as f32,
},
),
(Color::Numeric(color), Color::Foreground, _) => Color::Complex(
color,
ComplexColorRatios {
bg: this_weight as f32,
fg: other_weight as f32,
},
),
// Any other animation of currentColor with currentColor is complex.
(Color::Foreground, Color::Foreground, _) => Color::Complex(
RGBA::transparent(),
ComplexColorRatios {
bg: 0.,
fg: (this_weight + other_weight) as f32,
},
),
// Defer to complex calculations
_ => {
// For interpolating between two complex colors, we need to
// generate colors with effective alpha value.
let self_color = self.effective_intermediate_rgba();
let other_color = other.effective_intermediate_rgba();
let color = self_color.animate(&other_color, procedure)?;
// Then we compute the final background ratio, and derive
// the final alpha value from the effective alpha value.
let self_ratios = self.effective_ratios();
let other_ratios = other.effective_ratios();
let ratios = self_ratios.animate(&other_ratios, procedure)?;
let alpha = color.alpha / ratios.bg;
let color = RGBA { alpha, ..color };
if ratios == ComplexColorRatios::NUMERIC {
Color::Numeric(color)
} else if ratios == ComplexColorRatios::FOREGROUND {
Color::Foreground
} else {
Color::Complex(color, ratios)
}
}
})
}
}
impl ComputeSquaredDistance for Color {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
// All comments from the Animate impl also applies here.
Ok(match (*self, *other) {
(Color::Foreground, Color::Foreground) => SquaredDistance::from_sqrt(0.),
(Color::Numeric(c1), Color::Numeric(c2)) => c1.compute_squared_distance(&c2)?,
(Color::Foreground, Color::Numeric(color))
| (Color::Numeric(color), Color::Foreground) => {
// `computed_squared_distance` is symmetic.
color.compute_squared_distance(&RGBA::transparent())?
+ SquaredDistance::from_sqrt(1.)
}
(_, _) => {
let self_color = self.effective_intermediate_rgba();
let other_color = other.effective_intermediate_rgba();
let self_ratios = self.effective_ratios();
let other_ratios = other.effective_ratios();
self_color.compute_squared_distance(&other_color)?
+ self_ratios.bg.compute_squared_distance(&other_ratios.bg)?
+ self_ratios.fg.compute_squared_distance(&other_ratios.fg)?
}
})
}
}
impl ToAnimatedZero for Color {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> {
// FIXME(nox): This does not look correct to me.
Err(())
}
}