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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use num_traits::Float;

use {Blend, ComponentWise};
use blend::{BlendFunction, PreAlpha};

///A pair of blending equations and corresponding parameters.
///
///The `Equations` type is similar to how blending works in OpenGL, where a
///blend function has can be written as `e(sp * S, dp * D)`. `e` is the
///equation (like `s + d`), `sp` and `dp` are the source and destination
///parameters, and `S` and `D` are the source and destination colors.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Equations {
    ///The equation for the color components.
    pub color_equation: Equation,

    ///The equation for the alpha component.
    pub alpha_equation: Equation,

    ///The parameters for the color components.
    pub color_parameters: Parameters,

    ///The parameters for the alpha component.
    pub alpha_parameters: Parameters,
}

impl Equations {
    ///Create a pair of blending equations, where all the parameters are
    ///`One`.
    pub fn from_equations(color: Equation, alpha: Equation) -> Equations {
        Equations {
            color_equation: color,
            alpha_equation: alpha,
            color_parameters: Parameters {
                source: Parameter::One,
                destination: Parameter::One,
            },
            alpha_parameters: Parameters {
                source: Parameter::One,
                destination: Parameter::One,
            },
        }
    }

    ///Create a pair of additive blending equations with the provided
    ///parameters.
    pub fn from_parameters(source: Parameter, destination: Parameter) -> Equations {
        Equations {
            color_equation: Equation::Add,
            alpha_equation: Equation::Add,
            color_parameters: Parameters {
                source: source,
                destination: destination,
            },
            alpha_parameters: Parameters {
                source: source,
                destination: destination,
            },
        }
    }
}

impl<C: Blend<Color = C> + ComponentWise + Clone> BlendFunction<C> for Equations
where
    C::Scalar: Float,
{
    fn apply_to(
        self,
        source: PreAlpha<C, C::Scalar>,
        destination: PreAlpha<C, C::Scalar>,
    ) -> PreAlpha<C, C::Scalar> {
        let col_src_param = self.color_parameters
            .source
            .apply_to(source.clone(), destination.clone());
        let col_dst_param = self.color_parameters
            .destination
            .apply_to(source.clone(), destination.clone());
        let alpha_src_param = self.alpha_parameters
            .source
            .apply_to(source.clone(), destination.clone());
        let alpha_dst_param = self.alpha_parameters
            .destination
            .apply_to(source.clone(), destination.clone());

        let src_color = col_src_param.mul_color(source.color.clone());
        let dst_color = col_dst_param.mul_color(destination.color.clone());
        let src_alpha = alpha_src_param.mul_constant(source.alpha);
        let dst_alpha = alpha_dst_param.mul_constant(destination.alpha);

        let color = match self.color_equation {
            Equation::Add => src_color.component_wise(&dst_color, |a, b| a + b),
            Equation::Subtract => src_color.component_wise(&dst_color, |a, b| a - b),
            Equation::ReverseSubtract => dst_color.component_wise(&src_color, |a, b| a - b),
            Equation::Min => source
                .color
                .component_wise(&destination.color, |a, b| a.min(b)),
            Equation::Max => source
                .color
                .component_wise(&destination.color, |a, b| a.max(b)),
        };

        let alpha = match self.alpha_equation {
            Equation::Add => src_alpha + dst_alpha,
            Equation::Subtract => src_alpha - dst_alpha,
            Equation::ReverseSubtract => dst_alpha - src_alpha,
            Equation::Min => source.alpha.min(destination.alpha),
            Equation::Max => source.alpha.max(destination.alpha),
        };

        PreAlpha {
            color: color,
            alpha: alpha,
        }
    }
}

///A blending equation.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Equation {
    ///Add the source and destination, according to `sp * S + dp * D`.
    Add,

    /// Subtract the destination from the source, according to `sp * S - dp *
    /// D`.
    Subtract,

    /// Subtract the source from the destination, according to `dp * D - sp *
    /// S`.
    ReverseSubtract,

    ///Create a color where each component is the smallest of each of the
    ///source and destination components. A.k.a. component wise min. The
    ///parameters are ignored.
    Min,

    ///Create a color where each component is the largest of each of the
    ///source and destination components. A.k.a. component wise max. The
    ///parameters are ignored.
    Max,
}

///A pair of source and destination parameters.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Parameters {
    ///The source parameter.
    pub source: Parameter,

    ///The destination parameter.
    pub destination: Parameter,
}

///A blending parameter.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Parameter {
    ///A simple 1.
    One,

    ///A simple 0.
    Zero,

    ///The source color, or alpha.
    SourceColor,

    ///One minus the source color, or alpha.
    OneMinusSourceColor,

    ///The destination color, or alpha.
    DestinationColor,

    ///One minus the destination color, or alpha.
    OneMinusDestinationColor,

    ///The source alpha.
    SourceAlpha,

    ///One minus the source alpha.
    OneMinusSourceAlpha,

    ///The destination alpha.
    DestinationAlpha,

    ///One minus the destination alpha.
    OneMinusDestinationAlpha,
}

impl Parameter {
    fn apply_to<C, T: Float>(
        &self,
        source: PreAlpha<C, T>,
        destination: PreAlpha<C, T>,
    ) -> ParamOut<C, T>
    where
        PreAlpha<C, T>: ComponentWise<Scalar = T>,
    {
        match *self {
            Parameter::One => ParamOut::Constant(T::one()),
            Parameter::Zero => ParamOut::Constant(T::zero()),
            Parameter::SourceColor => ParamOut::Color(source),
            Parameter::OneMinusSourceColor => {
                ParamOut::Color(source.component_wise_self(|a| T::one() - a))
            }
            Parameter::DestinationColor => ParamOut::Color(destination),
            Parameter::OneMinusDestinationColor => {
                ParamOut::Color(destination.component_wise_self(|a| T::one() - a))
            }
            Parameter::SourceAlpha => ParamOut::Constant(source.alpha),
            Parameter::OneMinusSourceAlpha => ParamOut::Constant(T::one() - source.alpha),
            Parameter::DestinationAlpha => ParamOut::Constant(destination.alpha),
            Parameter::OneMinusDestinationAlpha => ParamOut::Constant(T::one() - destination.alpha),
        }
    }
}

enum ParamOut<C, T: Float> {
    Color(PreAlpha<C, T>),
    Constant(T),
}

impl<C: ComponentWise<Scalar = T>, T: Float> ParamOut<C, T> {
    fn mul_constant(self, other: T) -> T {
        match self {
            ParamOut::Color(c) => c.alpha * other,
            ParamOut::Constant(c) => c * other,
        }
    }

    fn mul_color(self, other: C) -> C {
        match self {
            ParamOut::Color(c) => other.component_wise(&c.color, |a, b| a * b),
            ParamOut::Constant(c) => other.component_wise_self(|a| a * c),
        }
    }
}