File size: 4,888 Bytes
2409829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use crate::raster::Image;
use crate::raster_types::{CPU, RasterDataTable};
use crate::registry::types::Percentage;
use crate::vector::VectorDataTable;
use crate::{BlendMode, Color, Ctx, GraphicElement, GraphicGroupTable};

pub(super) trait MultiplyAlpha {
	fn multiply_alpha(&mut self, factor: f64);
}

impl MultiplyAlpha for Color {
	fn multiply_alpha(&mut self, factor: f64) {
		*self = Color::from_rgbaf32_unchecked(self.r(), self.g(), self.b(), (self.a() * factor as f32).clamp(0., 1.))
	}
}
impl MultiplyAlpha for VectorDataTable {
	fn multiply_alpha(&mut self, factor: f64) {
		for instance in self.instance_mut_iter() {
			instance.alpha_blending.opacity *= factor as f32;
		}
	}
}
impl MultiplyAlpha for GraphicGroupTable {
	fn multiply_alpha(&mut self, factor: f64) {
		for instance in self.instance_mut_iter() {
			instance.alpha_blending.opacity *= factor as f32;
		}
	}
}
impl MultiplyAlpha for RasterDataTable<CPU>
where
	GraphicElement: From<Image<Color>>,
{
	fn multiply_alpha(&mut self, factor: f64) {
		for instance in self.instance_mut_iter() {
			instance.alpha_blending.opacity *= factor as f32;
		}
	}
}

pub(super) trait MultiplyFill {
	fn multiply_fill(&mut self, factor: f64);
}
impl MultiplyFill for Color {
	fn multiply_fill(&mut self, factor: f64) {
		*self = Color::from_rgbaf32_unchecked(self.r(), self.g(), self.b(), (self.a() * factor as f32).clamp(0., 1.))
	}
}
impl MultiplyFill for VectorDataTable {
	fn multiply_fill(&mut self, factor: f64) {
		for instance in self.instance_mut_iter() {
			instance.alpha_blending.fill *= factor as f32;
		}
	}
}
impl MultiplyFill for GraphicGroupTable {
	fn multiply_fill(&mut self, factor: f64) {
		for instance in self.instance_mut_iter() {
			instance.alpha_blending.fill *= factor as f32;
		}
	}
}
impl MultiplyFill for RasterDataTable<CPU> {
	fn multiply_fill(&mut self, factor: f64) {
		for instance in self.instance_mut_iter() {
			instance.alpha_blending.fill *= factor as f32;
		}
	}
}

trait SetBlendMode {
	fn set_blend_mode(&mut self, blend_mode: BlendMode);
}

impl SetBlendMode for VectorDataTable {
	fn set_blend_mode(&mut self, blend_mode: BlendMode) {
		for instance in self.instance_mut_iter() {
			instance.alpha_blending.blend_mode = blend_mode;
		}
	}
}
impl SetBlendMode for GraphicGroupTable {
	fn set_blend_mode(&mut self, blend_mode: BlendMode) {
		for instance in self.instance_mut_iter() {
			instance.alpha_blending.blend_mode = blend_mode;
		}
	}
}
impl SetBlendMode for RasterDataTable<CPU> {
	fn set_blend_mode(&mut self, blend_mode: BlendMode) {
		for instance in self.instance_mut_iter() {
			instance.alpha_blending.blend_mode = blend_mode;
		}
	}
}

trait SetClip {
	fn set_clip(&mut self, clip: bool);
}

impl SetClip for VectorDataTable {
	fn set_clip(&mut self, clip: bool) {
		for instance in self.instance_mut_iter() {
			instance.alpha_blending.clip = clip;
		}
	}
}
impl SetClip for GraphicGroupTable {
	fn set_clip(&mut self, clip: bool) {
		for instance in self.instance_mut_iter() {
			instance.alpha_blending.clip = clip;
		}
	}
}
impl SetClip for RasterDataTable<CPU> {
	fn set_clip(&mut self, clip: bool) {
		for instance in self.instance_mut_iter() {
			instance.alpha_blending.clip = clip;
		}
	}
}

#[node_macro::node(category("Style"))]
fn blend_mode<T: SetBlendMode>(
	_: impl Ctx,
	#[implementations(
		GraphicGroupTable,
		VectorDataTable,
		RasterDataTable<CPU>,
	)]
	mut value: T,
	blend_mode: BlendMode,
) -> T {
	// TODO: Find a way to make this apply once to the table's parent (i.e. its row in its parent table or Instance<T>) rather than applying to each row in its own table, which produces the undesired result
	value.set_blend_mode(blend_mode);
	value
}

#[node_macro::node(category("Style"))]
fn opacity<T: MultiplyAlpha>(
	_: impl Ctx,
	#[implementations(
		GraphicGroupTable,
		VectorDataTable,
		RasterDataTable<CPU>,
	)]
	mut value: T,
	#[default(100.)] opacity: Percentage,
) -> T {
	// TODO: Find a way to make this apply once to the table's parent (i.e. its row in its parent table or Instance<T>) rather than applying to each row in its own table, which produces the undesired result
	value.multiply_alpha(opacity / 100.);
	value
}

#[node_macro::node(category("Style"))]
fn blending<T: SetBlendMode + MultiplyAlpha + MultiplyFill + SetClip>(
	_: impl Ctx,
	#[implementations(
		GraphicGroupTable,
		VectorDataTable,
		RasterDataTable<CPU>,
	)]
	mut value: T,
	blend_mode: BlendMode,
	#[default(100.)] opacity: Percentage,
	#[default(100.)] fill: Percentage,
	#[default(false)] clip: bool,
) -> T {
	// TODO: Find a way to make this apply once to the table's parent (i.e. its row in its parent table or Instance<T>) rather than applying to each row in its own table, which produces the undesired result
	value.set_blend_mode(blend_mode);
	value.multiply_alpha(opacity / 100.);
	value.multiply_fill(fill / 100.);
	value.set_clip(clip);
	value
}