/
effects.rs
164 lines (148 loc) · 6.06 KB
/
effects.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
/* 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 values related to effects.
use properties::animated_properties::{Animatable, IntermediateColor};
#[cfg(not(feature = "gecko"))]
use values::Impossible;
use values::computed::{Angle, Number};
#[cfg(feature = "gecko")]
use values::computed::effects::Filter as ComputedFilter;
#[cfg(feature = "gecko")]
use values::computed::effects::FilterList as ComputedFilterList;
use values::computed::effects::SimpleShadow as ComputedSimpleShadow;
use values::computed::length::Length;
use values::generics::effects::Filter as GenericFilter;
use values::generics::effects::FilterList as GenericFilterList;
/// An animated value for the `filter` property.
pub type FilterList = GenericFilterList<Filter>;
/// An animated value for a single `filter`.
#[cfg(feature = "gecko")]
pub type Filter = GenericFilter<Angle, Number, Length, SimpleShadow>;
/// An animated value for a single `filter`.
#[cfg(not(feature = "gecko"))]
pub type Filter = GenericFilter<Angle, Number, Length, Impossible>;
/// An animated value for the `drop-shadow()` filter.
///
/// Contrary to the canonical order from the spec, the color is serialised
/// first, like in Gecko and Webkit.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq)]
pub struct SimpleShadow {
/// Color.
pub color: IntermediateColor,
/// Horizontal radius.
pub horizontal: Length,
/// Vertical radius.
pub vertical: Length,
/// Blur radius.
pub blur: Length,
}
#[cfg(feature = "gecko")]
impl From<ComputedFilterList> for FilterList {
#[inline]
fn from(filters: ComputedFilterList) -> Self {
filters.0.into_vec().into_iter().map(|f| f.into()).collect::<Vec<_>>().into()
}
}
#[cfg(feature = "gecko")]
impl From<FilterList> for ComputedFilterList {
#[inline]
fn from(filters: FilterList) -> Self {
filters.0.into_vec().into_iter().map(|f| f.into()).collect::<Vec<_>>().into()
}
}
#[cfg(feature = "gecko")]
impl From<ComputedFilter> for Filter {
#[inline]
fn from(filter: ComputedFilter) -> Self {
match filter {
GenericFilter::Blur(angle) => GenericFilter::Blur(angle),
GenericFilter::Brightness(factor) => GenericFilter::Brightness(factor),
GenericFilter::Contrast(factor) => GenericFilter::Contrast(factor),
GenericFilter::Grayscale(factor) => GenericFilter::Grayscale(factor),
GenericFilter::HueRotate(factor) => GenericFilter::HueRotate(factor),
GenericFilter::Invert(factor) => GenericFilter::Invert(factor),
GenericFilter::Opacity(factor) => GenericFilter::Opacity(factor),
GenericFilter::Saturate(factor) => GenericFilter::Saturate(factor),
GenericFilter::Sepia(factor) => GenericFilter::Sepia(factor),
GenericFilter::DropShadow(shadow) => {
GenericFilter::DropShadow(shadow.into())
},
#[cfg(feature = "gecko")]
GenericFilter::Url(url) => GenericFilter::Url(url),
}
}
}
#[cfg(feature = "gecko")]
impl From<Filter> for ComputedFilter {
#[inline]
fn from(filter: Filter) -> Self {
match filter {
GenericFilter::Blur(angle) => GenericFilter::Blur(angle),
GenericFilter::Brightness(factor) => GenericFilter::Brightness(factor),
GenericFilter::Contrast(factor) => GenericFilter::Contrast(factor),
GenericFilter::Grayscale(factor) => GenericFilter::Grayscale(factor),
GenericFilter::HueRotate(factor) => GenericFilter::HueRotate(factor),
GenericFilter::Invert(factor) => GenericFilter::Invert(factor),
GenericFilter::Opacity(factor) => GenericFilter::Opacity(factor),
GenericFilter::Saturate(factor) => GenericFilter::Saturate(factor),
GenericFilter::Sepia(factor) => GenericFilter::Sepia(factor),
GenericFilter::DropShadow(shadow) => {
GenericFilter::DropShadow(shadow.into())
},
#[cfg(feature = "gecko")]
GenericFilter::Url(url) => GenericFilter::Url(url.clone())
}
}
}
impl From<ComputedSimpleShadow> for SimpleShadow {
#[inline]
fn from(shadow: ComputedSimpleShadow) -> Self {
SimpleShadow {
color: shadow.color.into(),
horizontal: shadow.horizontal,
vertical: shadow.vertical,
blur: shadow.blur,
}
}
}
impl From<SimpleShadow> for ComputedSimpleShadow {
#[inline]
fn from(shadow: SimpleShadow) -> Self {
ComputedSimpleShadow {
color: shadow.color.into(),
horizontal: shadow.horizontal,
vertical: shadow.vertical,
blur: shadow.blur,
}
}
}
impl Animatable for SimpleShadow {
#[inline]
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result<Self, ()> {
let color = self.color.add_weighted(&other.color, self_portion, other_portion)?;
let horizontal = self.horizontal.add_weighted(&other.horizontal, self_portion, other_portion)?;
let vertical = self.vertical.add_weighted(&other.vertical, self_portion, other_portion)?;
let blur = self.blur.add_weighted(&other.blur, self_portion, other_portion)?;
Ok(SimpleShadow {
color: color,
horizontal: horizontal,
vertical: vertical,
blur: blur,
})
}
#[inline]
fn compute_distance(&self, other: &Self) -> Result<f64, ()> {
self.compute_squared_distance(other).map(|sd| sd.sqrt())
}
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<f64, ()> {
Ok(
self.color.compute_squared_distance(&other.color)? +
self.horizontal.compute_squared_distance(&other.horizontal)? +
self.vertical.compute_squared_distance(&other.vertical)? +
self.blur.compute_squared_distance(&other.blur)?
)
}
}