cranpose_render_common/
layer_paint.rs1use std::hash::{Hash, Hasher};
5
6use cranpose_ui::Brush;
7use cranpose_ui_graphics::{Color, ColorFilter, GraphicsLayer, RenderHash};
8
9use crate::style_shared::apply_paint_to_color;
10
11#[derive(Clone, Debug, PartialEq)]
18pub struct LayerPaint {
19 alpha: f32,
20 color_filter: Option<ColorFilter>,
21}
22
23impl LayerPaint {
24 pub const NONE: Self = Self {
26 alpha: 1.0,
27 color_filter: None,
28 };
29
30 pub fn of(layer: &GraphicsLayer) -> Self {
32 Self {
33 alpha: layer.alpha,
34 color_filter: layer.color_filter.clone(),
35 }
36 }
37
38 pub(crate) fn paints(&self) -> bool {
40 self.alpha != 1.0 || self.color_filter.is_some()
41 }
42
43 pub(crate) fn color(&self, color: Color) -> Color {
47 if self.paints() {
48 apply_paint_to_color(color, self.alpha, self.color_filter.as_ref())
49 } else {
50 color
51 }
52 }
53
54 pub fn brush(&self, brush: Brush) -> Brush {
56 if self.paints() {
57 brush.map_colors(|color| self.color(color))
58 } else {
59 brush
60 }
61 }
62}
63
64impl Hash for LayerPaint {
65 fn hash<H: Hasher>(&self, state: &mut H) {
66 state.write_u32(self.alpha.to_bits());
67 state.write_u64(
68 self.color_filter
69 .as_ref()
70 .map_or(0, RenderHash::render_hash),
71 );
72 }
73}