Skip to main content

alpha2_render/
blend.rs

1/// Color space in which compositing arithmetic is evaluated.
2#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
3pub enum BlendSpace {
4    /// Convert sRGB-encoded inputs to linear light before blending.
5    #[default]
6    LinearLight,
7    /// Blend directly in sRGB-encoded component space.
8    SrgbEncoded,
9}
10
11impl BlendSpace {
12    pub(crate) const fn shader_value(self) -> f32 {
13        match self {
14            Self::LinearLight => 0.0,
15            Self::SrgbEncoded => 1.0,
16        }
17    }
18}
19
20/// Artistic RGB operation applied before Porter-Duff source-over coverage.
21///
22/// The modes and their formulas are those of W3C Compositing and Blending
23/// Level 1, evaluated in the renderer's [`BlendSpace`]. The first twelve are
24/// separable (each channel on its own); `Hue`, `Saturation`, `Color`, and
25/// `Luminosity` are not.
26#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq)]
27pub enum BlendMode {
28    /// Preserve the source color.
29    #[default]
30    Normal,
31    /// Multiply source and backdrop components.
32    Multiply,
33    /// Inverse-multiply source and backdrop components.
34    Screen,
35    /// Multiply dark backdrop regions and screen light regions.
36    Overlay,
37    /// The darker of source and backdrop, per channel.
38    Darken,
39    /// The lighter of source and backdrop, per channel.
40    Lighten,
41    /// Brighten the backdrop to reflect the source.
42    ColorDodge,
43    /// Darken the backdrop to reflect the source.
44    ColorBurn,
45    /// Multiply or screen, depending on the source.
46    HardLight,
47    /// Darken or lighten, depending on the source: a diffused hard light.
48    SoftLight,
49    /// The absolute difference of source and backdrop.
50    Difference,
51    /// Like difference, with lower contrast.
52    Exclusion,
53    /// The source's hue with the backdrop's saturation and luminosity.
54    Hue,
55    /// The source's saturation with the backdrop's hue and luminosity.
56    Saturation,
57    /// The source's hue and saturation with the backdrop's luminosity.
58    Color,
59    /// The source's luminosity with the backdrop's hue and saturation.
60    Luminosity,
61}
62
63impl BlendMode {
64    /// Every mode, in the order of W3C Compositing and Blending Level 1.
65    pub const ALL: [Self; 16] = [
66        Self::Normal,
67        Self::Multiply,
68        Self::Screen,
69        Self::Overlay,
70        Self::Darken,
71        Self::Lighten,
72        Self::ColorDodge,
73        Self::ColorBurn,
74        Self::HardLight,
75        Self::SoftLight,
76        Self::Difference,
77        Self::Exclusion,
78        Self::Hue,
79        Self::Saturation,
80        Self::Color,
81        Self::Luminosity,
82    ];
83
84    /// The index `composite.wesl` switches on.
85    pub(crate) const fn shader_value(self) -> f32 {
86        match self {
87            Self::Normal => 0.0,
88            Self::Multiply => 1.0,
89            Self::Screen => 2.0,
90            Self::Overlay => 3.0,
91            Self::Darken => 4.0,
92            Self::Lighten => 5.0,
93            Self::ColorDodge => 6.0,
94            Self::ColorBurn => 7.0,
95            Self::HardLight => 8.0,
96            Self::SoftLight => 9.0,
97            Self::Difference => 10.0,
98            Self::Exclusion => 11.0,
99            Self::Hue => 12.0,
100            Self::Saturation => 13.0,
101            Self::Color => 14.0,
102            Self::Luminosity => 15.0,
103        }
104    }
105}