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
// Copyright (c) 2023 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by Lesser General Public License that can be found
// in the LICENSE file.
//! Blends are operators that take in two colors (source, destination) and return a new color.
/// Many of color operators operate the same on all 4 components: red, green, blue, alpha. For these,
/// we just document what happens to one component, rather than naming each one separately.
///
/// Different `ColorTypes` have different representations for color components:
/// 8-bit: 0..255
/// 6-bit: 0..63
/// 5-bit: 0..31
/// 4-bit: 0..15
/// floats: 0...1
///
/// The documentation is expressed as if the component values are always 0..1 (floats).
///
/// For brevity, the documentation uses the following abbreviations
/// s : source
/// d : destination
/// sa : source alpha
/// da : destination alpha
///
/// Results are abbreviated
/// r : if all 4 components are computed in the same manner
/// ra : result alpha component
/// rc : result "color": red, green, blue components
#[repr(u8)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub enum BlendMode {
/// r = 0
Clear,
/// r = s
Src,
/// r = d
Dst,
/// r = s + (1-sa)*d
SrcOver,
/// r = d + (1-da)*s
DstOver,
/// r = s * da
SrcIn,
/// r = d * sa
DstIn,
/// r = s * (1-da)
SrcOut,
/// r = d * (1-sa)
DstOut,
/// r = s*da + d*(1-sa)
SrcATop,
/// r = d*sa + s*(1-da)
DstATop,
/// r = s*(1-da) + d*(1-sa)
Xor,
/// r = min(s + d, 1)
Plus,
/// r = s*d
Modulate,
/// r = s + d - s*d
Screen,
/// multiply or screen, depending on destination
Overlay,
/// rc = s + d - max(s*da, d*sa), ra = SrcOver
Darken,
/// rc = s + d - min(s*da, d*sa), ra = SrcOver
Lighten,
/// brighten destination to reflect source
ColorDodge,
/// darken destination to reflect source
ColorBurn,
/// multiply or screen, depending on source
HardLight,
/// lighten or darken, depending on source
SoftLight,
/// rc = s + d - 2*(min(s*da, d*sa)), ra = SrcOver
Difference,
/// rc = s + d - two(s*d), ra = SrcOver
Exclusion,
/// r = s*(1-da) + d*(1-sa) + s*d
Multiply,
/// hue of source with saturation and luminosity of destination
Hue,
/// saturation of source with hue and luminosity of destination
Saturation,
/// hue and saturation of source with luminosity of destination
Color,
/// luminosity of source with hue and saturation of destination
Luminosity,
}
/// last porter duff blend mode
pub const LAST_COEFF_MODE: BlendMode = BlendMode::Screen;
/// last blend mode operating separately on components
pub const LAST_SEPARABLE_MODE: BlendMode = BlendMode::Multiply;
/// last valid value
pub const LAST_MODE: BlendMode = BlendMode::Luminosity;
/// For Porter-Duff `BlendModes` (those <= `LAST_COEFF_MODE`), these coefficients describe the blend
/// equation used.
///
/// Coefficient-based blend modes specify an equation:
/// ('dstCoeff' * dst + 'srcCoeff' * src), where the coefficient values are constants, functions of
/// the src or dst alpha, or functions of the src or dst color.
#[repr(u8)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub enum BlendModeCoeff {
/// 0
Zero,
/// 1
One,
/// src color
SC,
/// inverse src color (i.e. 1 - sc)
ISC,
/// dst color
DC,
/// inverse dst color (i.e. 1 - dc)
IDC,
/// src alpha
SA,
/// inverse src alpha (i.e. 1 - sa)
ISA,
/// dst alpha
DA,
/// inverse dst alpha (i.e. 1 - da)
IDA,
}
impl BlendMode {
/// Returns true if `mode` is a coefficient-based blend mode (<= `LAST_COEFF_MODE`).
///
/// If true is returned, the mode's src and dst coefficient functions are set in `src` and `dst`.
pub fn as_coeff(self, _src: &mut BlendModeCoeff, _dst: &mut BlendModeCoeff) -> bool {
unimplemented!()
}
/// Returns name of blendMode.
#[must_use]
pub const fn name(self) -> &'static str {
unimplemented!()
}
}