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
//! The alpha and color filter that a layer drawn in place lays over the
//! colors of its content.
use std::hash::{Hash, Hasher};
use cranpose_ui::Brush;
use cranpose_ui_graphics::{Color, ColorFilter, GraphicsLayer, RenderHash};
use crate::style_shared::apply_paint_to_color;
/// The alpha and color filter of a layer drawn in place. Every color of the
/// layer's content takes them, as [`apply_layer_to_color`] applies them. A
/// text resolves the colors of its glyphs from its style and spans first.
/// Each resolved color then takes this paint.
///
/// [`apply_layer_to_color`]: crate::style_shared::apply_layer_to_color
#[derive(Clone, Debug, PartialEq)]
pub struct LayerPaint {
alpha: f32,
color_filter: Option<ColorFilter>,
}
impl LayerPaint {
/// The paint that leaves every color as it is.
pub const NONE: Self = Self {
alpha: 1.0,
color_filter: None,
};
/// The alpha and color filter of `layer`.
pub fn of(layer: &GraphicsLayer) -> Self {
Self {
alpha: layer.alpha,
color_filter: layer.color_filter.clone(),
}
}
// Whether the paint changes a color.
pub(crate) fn paints(&self) -> bool {
self.alpha != 1.0 || self.color_filter.is_some()
}
// `color` under the paint: snapped to eight bits, then multiplied by the
// alpha and filtered. A paint that changes nothing leaves `color` as it
// is.
pub(crate) fn color(&self, color: Color) -> Color {
if self.paints() {
apply_paint_to_color(color, self.alpha, self.color_filter.as_ref())
} else {
color
}
}
/// `brush` with each of its colors under the paint.
pub fn brush(&self, brush: Brush) -> Brush {
if self.paints() {
brush.map_colors(|color| self.color(color))
} else {
brush
}
}
}
impl Hash for LayerPaint {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u32(self.alpha.to_bits());
state.write_u64(
self.color_filter
.as_ref()
.map_or(0, RenderHash::render_hash),
);
}
}