Download node-graph/gcore/src/blending_nodes.rs from openfree/graphite2: direct link, hf CLI and curl.
- Browser
- Download file 4.89 kB
-
https://huggingface.co/spaces/openfree/graphite2/resolve/main/node-graph/gcore/src/blending_nodes.rs
- Command line
-
hf download hf://spaces/openfree/graphite2/node-graph/gcore/src/blending_nodes.rs
-
curl -L -o blending_nodes.rs https://huggingface.co/spaces/openfree/graphite2/resolve/main/node-graph/gcore/src/blending_nodes.rs
4.89 kB
| 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; | |
| } | |
| } | |
| } | |
| fn blend_mode<T: SetBlendMode>( | |
| _: impl Ctx, | |
| 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 | |
| } | |
| fn opacity<T: MultiplyAlpha>( | |
| _: impl Ctx, | |
| mut value: T, | |
| 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 | |
| } | |
| fn blending<T: SetBlendMode + MultiplyAlpha + MultiplyFill + SetClip>( | |
| _: impl Ctx, | |
| mut value: T, | |
| blend_mode: BlendMode, | |
| opacity: Percentage, | |
| fill: Percentage, | |
| 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 | |
| } | |