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
use crate::filters::Filter;
impl Filter {
/// Creates a grayscale filter effect with the specified intensity.
///
/// # Arguments
/// - `amount`: The grayscale intensity where `0.0` leaves the image
/// unchanged and `1.0` produces full grayscale.
///
/// # Returns
/// - [`Filter`] applying a grayscale conversion effect.
///
/// # Reference
///
/// https://www.w3.org/TR/filter-effects-1/#grayscaleEquivalent
pub fn grayscale(amount: f32) -> Self {
let x = 1.0 - amount.clamp(0.0, 1.0);
Self::new(|ctx| {
ctx.color_matrix()
.matrix([
[
0.2126 + 0.7874 * x,
0.7152 - 0.7152 * x,
0.0722 - 0.0722 * x,
0.0,
0.0,
],
[
0.2126 - 0.2126 * x,
0.7152 + 0.2848 * x,
0.0722 - 0.0722 * x,
0.0,
0.0,
],
[
0.2126 - 0.2126 * x,
0.7152 - 0.7152 * x,
0.0722 + 0.9278 * x,
0.0,
0.0,
],
[0.0, 0.0, 0.0, 1.0, 0.0],
])
.finish();
})
}
}