svg-filters: seperate out abstractions and add flood abstraction
This commit is contained in:
parent
9ae8c2fbd3
commit
919a3bb377
5 changed files with 174 additions and 158 deletions
|
@ -138,158 +138,6 @@ impl FilterGraph {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod abstracted_inputs {
|
pub mod abstracted_inputs;
|
||||||
use petgraph::{data::Build, prelude::NodeIndex};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
types::nodes::primitives::{
|
|
||||||
blend::BlendMode, color_matrix::ColorMatrixType, component_transfer::TransferFn,
|
|
||||||
},
|
|
||||||
Node,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::{FilterGraph, NodeInput};
|
|
||||||
|
|
||||||
impl FilterGraph {
|
|
||||||
pub fn color_matrix(
|
|
||||||
&mut self,
|
|
||||||
r#in: impl Into<NodeInput>,
|
|
||||||
cm_type: ColorMatrixType,
|
|
||||||
) -> NodeIndex {
|
|
||||||
let node_idx = self.dag.add_node(Node::color_matrix(cm_type));
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
|
||||||
node_idx
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn offset(&mut self, r#in: impl Into<NodeInput>, dx: f32, dy: f32) -> NodeIndex {
|
|
||||||
let node_idx = self.dag.add_node(Node::offset(dx, dy));
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
|
||||||
node_idx
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn gaussian_blur_xy(
|
|
||||||
&mut self,
|
|
||||||
r#in: impl Into<NodeInput>,
|
|
||||||
x: u16,
|
|
||||||
y: u16,
|
|
||||||
) -> NodeIndex {
|
|
||||||
let node_idx = self.dag.add_node(Node::gaussian_blur_xy(x, y));
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
|
||||||
node_idx
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn blend(
|
|
||||||
&mut self,
|
|
||||||
r#in: impl Into<NodeInput>,
|
|
||||||
in2: impl Into<NodeInput>,
|
|
||||||
mode: BlendMode,
|
|
||||||
) -> NodeIndex {
|
|
||||||
let node_idx = self.dag.add_node(Node::blend(mode));
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(in2.into()), node_idx, ());
|
|
||||||
node_idx
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn composite_arithmetic(
|
|
||||||
&mut self,
|
|
||||||
r#in: impl Into<NodeInput>,
|
|
||||||
in2: impl Into<NodeInput>,
|
|
||||||
k1: f32,
|
|
||||||
k2: f32,
|
|
||||||
k3: f32,
|
|
||||||
k4: f32,
|
|
||||||
) -> NodeIndex {
|
|
||||||
let node_idx = self
|
|
||||||
.dag
|
|
||||||
.add_node(Node::composite_arithmetic(k1, k2, k3, k4));
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(in2.into()), node_idx, ());
|
|
||||||
node_idx
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn component_transfer_rgba(
|
|
||||||
&mut self,
|
|
||||||
r#in: impl Into<NodeInput>,
|
|
||||||
r: TransferFn,
|
|
||||||
g: TransferFn,
|
|
||||||
b: TransferFn,
|
|
||||||
a: TransferFn,
|
|
||||||
) -> NodeIndex {
|
|
||||||
let node_idx = self.dag.add_node(Node::component_transfer_rgba(r, g, b, a));
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
|
||||||
node_idx
|
|
||||||
}
|
|
||||||
pub fn component_transfer_rgb(
|
|
||||||
&mut self,
|
|
||||||
r#in: impl Into<NodeInput>,
|
|
||||||
r: TransferFn,
|
|
||||||
g: TransferFn,
|
|
||||||
b: TransferFn,
|
|
||||||
) -> NodeIndex {
|
|
||||||
let node_idx = self.dag.add_node(Node::component_transfer_rgb(r, g, b));
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
|
||||||
node_idx
|
|
||||||
}
|
|
||||||
pub fn component_transfer_r(
|
|
||||||
&mut self,
|
|
||||||
r#in: impl Into<NodeInput>,
|
|
||||||
func: TransferFn,
|
|
||||||
) -> NodeIndex {
|
|
||||||
let node_idx = self.dag.add_node(Node::component_transfer_r(func));
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
|
||||||
node_idx
|
|
||||||
}
|
|
||||||
pub fn component_transfer_g(
|
|
||||||
&mut self,
|
|
||||||
r#in: impl Into<NodeInput>,
|
|
||||||
func: TransferFn,
|
|
||||||
) -> NodeIndex {
|
|
||||||
let node_idx = self.dag.add_node(Node::component_transfer_g(func));
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
|
||||||
node_idx
|
|
||||||
}
|
|
||||||
pub fn component_transfer_b(
|
|
||||||
&mut self,
|
|
||||||
r#in: impl Into<NodeInput>,
|
|
||||||
func: TransferFn,
|
|
||||||
) -> NodeIndex {
|
|
||||||
let node_idx = self.dag.add_node(Node::component_transfer_b(func));
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
|
||||||
node_idx
|
|
||||||
}
|
|
||||||
pub fn component_transfer_a(
|
|
||||||
&mut self,
|
|
||||||
r#in: impl Into<NodeInput>,
|
|
||||||
func: TransferFn,
|
|
||||||
) -> NodeIndex {
|
|
||||||
let node_idx = self.dag.add_node(Node::component_transfer_a(func));
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
|
||||||
node_idx
|
|
||||||
}
|
|
||||||
pub fn component_transfer_single(
|
|
||||||
&mut self,
|
|
||||||
r#in: impl Into<NodeInput>,
|
|
||||||
func: TransferFn,
|
|
||||||
) -> NodeIndex {
|
|
||||||
let node_idx = self.dag.add_node(Node::component_transfer_single(func));
|
|
||||||
self.dag
|
|
||||||
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
|
||||||
node_idx
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod edge;
|
pub mod edge;
|
||||||
|
|
155
crates/svg-filters/src/types/graph/abstracted_inputs.rs
Normal file
155
crates/svg-filters/src/types/graph/abstracted_inputs.rs
Normal file
|
@ -0,0 +1,155 @@
|
||||||
|
use csscolorparser::Color;
|
||||||
|
use petgraph::{data::Build, prelude::NodeIndex};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
types::nodes::primitives::{
|
||||||
|
blend::BlendMode, color_matrix::ColorMatrixType, component_transfer::TransferFn,
|
||||||
|
},
|
||||||
|
Node,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{FilterGraph, NodeInput};
|
||||||
|
|
||||||
|
impl FilterGraph {
|
||||||
|
pub fn color_matrix(
|
||||||
|
&mut self,
|
||||||
|
r#in: impl Into<NodeInput>,
|
||||||
|
cm_type: ColorMatrixType,
|
||||||
|
) -> NodeIndex {
|
||||||
|
let node_idx = self.dag.add_node(Node::color_matrix(cm_type));
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
||||||
|
node_idx
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn offset(&mut self, r#in: impl Into<NodeInput>, dx: f32, dy: f32) -> NodeIndex {
|
||||||
|
let node_idx = self.dag.add_node(Node::offset(dx, dy));
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
||||||
|
node_idx
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn gaussian_blur_xy(&mut self, r#in: impl Into<NodeInput>, x: u16, y: u16) -> NodeIndex {
|
||||||
|
let node_idx = self.dag.add_node(Node::gaussian_blur_xy(x, y));
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
||||||
|
node_idx
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn blend(
|
||||||
|
&mut self,
|
||||||
|
r#in: impl Into<NodeInput>,
|
||||||
|
in2: impl Into<NodeInput>,
|
||||||
|
mode: BlendMode,
|
||||||
|
) -> NodeIndex {
|
||||||
|
let node_idx = self.dag.add_node(Node::blend(mode));
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(in2.into()), node_idx, ());
|
||||||
|
node_idx
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn composite_arithmetic(
|
||||||
|
&mut self,
|
||||||
|
r#in: impl Into<NodeInput>,
|
||||||
|
in2: impl Into<NodeInput>,
|
||||||
|
k1: f32,
|
||||||
|
k2: f32,
|
||||||
|
k3: f32,
|
||||||
|
k4: f32,
|
||||||
|
) -> NodeIndex {
|
||||||
|
let node_idx = self
|
||||||
|
.dag
|
||||||
|
.add_node(Node::composite_arithmetic(k1, k2, k3, k4));
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(in2.into()), node_idx, ());
|
||||||
|
node_idx
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn component_transfer_rgba(
|
||||||
|
&mut self,
|
||||||
|
r#in: impl Into<NodeInput>,
|
||||||
|
r: TransferFn,
|
||||||
|
g: TransferFn,
|
||||||
|
b: TransferFn,
|
||||||
|
a: TransferFn,
|
||||||
|
) -> NodeIndex {
|
||||||
|
let node_idx = self.dag.add_node(Node::component_transfer_rgba(r, g, b, a));
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
||||||
|
node_idx
|
||||||
|
}
|
||||||
|
pub fn component_transfer_rgb(
|
||||||
|
&mut self,
|
||||||
|
r#in: impl Into<NodeInput>,
|
||||||
|
r: TransferFn,
|
||||||
|
g: TransferFn,
|
||||||
|
b: TransferFn,
|
||||||
|
) -> NodeIndex {
|
||||||
|
let node_idx = self.dag.add_node(Node::component_transfer_rgb(r, g, b));
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
||||||
|
node_idx
|
||||||
|
}
|
||||||
|
pub fn component_transfer_r(
|
||||||
|
&mut self,
|
||||||
|
r#in: impl Into<NodeInput>,
|
||||||
|
func: TransferFn,
|
||||||
|
) -> NodeIndex {
|
||||||
|
let node_idx = self.dag.add_node(Node::component_transfer_r(func));
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
||||||
|
node_idx
|
||||||
|
}
|
||||||
|
pub fn component_transfer_g(
|
||||||
|
&mut self,
|
||||||
|
r#in: impl Into<NodeInput>,
|
||||||
|
func: TransferFn,
|
||||||
|
) -> NodeIndex {
|
||||||
|
let node_idx = self.dag.add_node(Node::component_transfer_g(func));
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
||||||
|
node_idx
|
||||||
|
}
|
||||||
|
pub fn component_transfer_b(
|
||||||
|
&mut self,
|
||||||
|
r#in: impl Into<NodeInput>,
|
||||||
|
func: TransferFn,
|
||||||
|
) -> NodeIndex {
|
||||||
|
let node_idx = self.dag.add_node(Node::component_transfer_b(func));
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
||||||
|
node_idx
|
||||||
|
}
|
||||||
|
pub fn component_transfer_a(
|
||||||
|
&mut self,
|
||||||
|
r#in: impl Into<NodeInput>,
|
||||||
|
func: TransferFn,
|
||||||
|
) -> NodeIndex {
|
||||||
|
let node_idx = self.dag.add_node(Node::component_transfer_a(func));
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
||||||
|
node_idx
|
||||||
|
}
|
||||||
|
pub fn component_transfer_single(
|
||||||
|
&mut self,
|
||||||
|
r#in: impl Into<NodeInput>,
|
||||||
|
func: TransferFn,
|
||||||
|
) -> NodeIndex {
|
||||||
|
let node_idx = self.dag.add_node(Node::component_transfer_single(func));
|
||||||
|
self.dag
|
||||||
|
.add_edge(self.resolve_input(r#in.into()), node_idx, ());
|
||||||
|
node_idx
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn flood(&mut self, flood_color: Color, flood_opacity: f32) -> NodeIndex {
|
||||||
|
self.dag.add_node(Node::flood(flood_color, flood_opacity))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn flood_opaque(&mut self, flood_color: Color) -> NodeIndex {
|
||||||
|
self.dag.add_node(Node::flood_opaque(flood_color))
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
use csscolorparser::Color;
|
||||||
use quick_xml::{events::attributes::Attribute, name::QName};
|
use quick_xml::{events::attributes::Attribute, name::QName};
|
||||||
|
|
||||||
use self::{
|
use self::{
|
||||||
|
@ -8,6 +9,7 @@ use self::{
|
||||||
color_matrix::{ColorMatrix, ColorMatrixType},
|
color_matrix::{ColorMatrix, ColorMatrixType},
|
||||||
component_transfer::{ComponentTransfer, TransferFn},
|
component_transfer::{ComponentTransfer, TransferFn},
|
||||||
composite::{Composite, CompositeOperator},
|
composite::{Composite, CompositeOperator},
|
||||||
|
flood::Flood,
|
||||||
gaussian_blur::GaussianBlur,
|
gaussian_blur::GaussianBlur,
|
||||||
offset::Offset,
|
offset::Offset,
|
||||||
FePrimitive,
|
FePrimitive,
|
||||||
|
@ -194,4 +196,15 @@ impl Node {
|
||||||
pub fn component_transfer_single(func: TransferFn) -> Self {
|
pub fn component_transfer_single(func: TransferFn) -> Self {
|
||||||
Self::component_transfer_rgb(func.clone(), func.clone(), func)
|
Self::component_transfer_rgb(func.clone(), func.clone(), func)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn flood(flood_color: Color, flood_opacity: f32) -> Self {
|
||||||
|
Self::simple(FePrimitive::Flood(Flood {
|
||||||
|
flood_color,
|
||||||
|
flood_opacity,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn flood_opaque(flood_color: Color) -> Self {
|
||||||
|
Self::flood(flood_color, 1.)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,7 +91,7 @@ impl WriteElement for FePrimitive {
|
||||||
FePrimitive::ConvolveMatrix(_) => todo!(),
|
FePrimitive::ConvolveMatrix(_) => todo!(),
|
||||||
FePrimitive::DiffuseLighting(_) => todo!(),
|
FePrimitive::DiffuseLighting(_) => todo!(),
|
||||||
FePrimitive::DisplacementMap(_) => todo!(),
|
FePrimitive::DisplacementMap(_) => todo!(),
|
||||||
FePrimitive::Flood(_) => todo!(),
|
FePrimitive::Flood(el) => el.attrs(),
|
||||||
FePrimitive::Image(_) => todo!(),
|
FePrimitive::Image(_) => todo!(),
|
||||||
FePrimitive::Merge(_) => todo!(),
|
FePrimitive::Merge(_) => todo!(),
|
||||||
FePrimitive::Morphology(_) => todo!(),
|
FePrimitive::Morphology(_) => todo!(),
|
||||||
|
@ -112,7 +112,7 @@ impl WriteElement for FePrimitive {
|
||||||
FePrimitive::ConvolveMatrix(_) => todo!(),
|
FePrimitive::ConvolveMatrix(_) => todo!(),
|
||||||
FePrimitive::DiffuseLighting(_) => todo!(),
|
FePrimitive::DiffuseLighting(_) => todo!(),
|
||||||
FePrimitive::DisplacementMap(_) => todo!(),
|
FePrimitive::DisplacementMap(_) => todo!(),
|
||||||
FePrimitive::Flood(_) => todo!(),
|
FePrimitive::Flood(el) => el.tag_name(),
|
||||||
FePrimitive::Image(_) => todo!(),
|
FePrimitive::Image(_) => todo!(),
|
||||||
FePrimitive::Merge(_) => todo!(),
|
FePrimitive::Merge(_) => todo!(),
|
||||||
FePrimitive::Morphology(_) => todo!(),
|
FePrimitive::Morphology(_) => todo!(),
|
||||||
|
@ -139,7 +139,7 @@ impl WriteElement for FePrimitive {
|
||||||
FePrimitive::ConvolveMatrix(_) => todo!(),
|
FePrimitive::ConvolveMatrix(_) => todo!(),
|
||||||
FePrimitive::DiffuseLighting(_) => todo!(),
|
FePrimitive::DiffuseLighting(_) => todo!(),
|
||||||
FePrimitive::DisplacementMap(_) => todo!(),
|
FePrimitive::DisplacementMap(_) => todo!(),
|
||||||
FePrimitive::Flood(_) => todo!(),
|
FePrimitive::Flood(el) => el.element_writer(writer, common, inputs, output),
|
||||||
FePrimitive::Image(_) => todo!(),
|
FePrimitive::Image(_) => todo!(),
|
||||||
FePrimitive::Merge(_) => todo!(),
|
FePrimitive::Merge(_) => todo!(),
|
||||||
FePrimitive::Morphology(_) => todo!(),
|
FePrimitive::Morphology(_) => todo!(),
|
||||||
|
|
|
@ -5,8 +5,8 @@ use super::WriteElement;
|
||||||
/// [feFlood](https://www.w3.org/TR/SVG11/filters.html#feFloodElement)
|
/// [feFlood](https://www.w3.org/TR/SVG11/filters.html#feFloodElement)
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Flood {
|
pub struct Flood {
|
||||||
flood_color: Color,
|
pub flood_color: Color,
|
||||||
flood_opacity: f32,
|
pub flood_opacity: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WriteElement for Flood {
|
impl WriteElement for Flood {
|
||||||
|
|
Loading…
Reference in a new issue