-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathColorBlending.pas
275 lines (216 loc) · 6.44 KB
/
ColorBlending.pas
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
unit ColorBlending;
{$R-}
interface
uses
{$IFNDEF FMX}
graphics,
{$ENDIF}
colorconversion, graphicsx;
function ColorBlend(cBackGround, cForeGround: TGiantcolor; alpha: advancedFloat): TGiantcolor; overload;
function ColorAdd(cBackGround, cForeGround: TColor; alpha: advancedFloat): TColor;
overload;
function ColorSubtract(cBackGround, cForeGround: TColor;
alpha: advancedFloat): TColor; overload;
function ColorBlend(cBackGround, cForeGround: TColor; alpha: advancedFloat): TColor;overload;
function ColorBlend_ForegroundSourceAlpha(cBackGround, cForeGround: TColor; alpha: advancedFloat): TColor;
function ColorBlend_PreserveSourceAlpha(cBackGround, cForeGround: TColor; alpha: advancedFloat): TColor;
function ColorBlendRGBA(cBackGround, cForeGround: TColor; alpha: advancedFloat): TColor;inline;overload;
function ColorOp(cBackGround, cForeGround: TColor; alpha: advancedFloat;
AlphaOp: TAlphaOp): TColor;inline;
function ColorReverse(c: TColor): TColor;inline;
function ColorAlpha(c: TColor; rAlpha: advancedFloat): TColor;inline;
function ColorMultiply(c: TColor; c2: TColor): TColor;inline;
implementation
function ColorBlend(cBackGround, cForeGround: TGiantcolor;
alpha: advancedFloat): TGiantcolor;
begin
if alpha > 1 then
alpha := 1;
if alpha < 0 then
alpha := 0;
cBackGround := cBackGround * (1 - alpha);
cForeGround := cForeGround * (alpha);
result := cBackGround + cForeGround;
end;
function ColorBlendRGBA(cBackGround, cForeGround: TGiantcolor;
alpha: advancedFloat): TGiantcolor; overload;
begin
if alpha > 1 then
alpha := 1;
if alpha < 0 then
alpha := 0;
cBackGround := cBackGround * (1 - alpha);
cForeGround := cForeGround * (alpha);
result := cBackGround + cForeGround;
end;
function ColorAdd(cBackGround, cForeGround: TColor; alpha: advancedFloat): TColor;
overload;
var
r1, g1, b1, r2, g2, b2: integer;
r, g, b: integer;
begin
r1 := (cBackGround shr 0) and 255;
g1 := (cBackGround shr 8) and 255;
b1 := (cBackGround shr 16) and 255;
r2 := (cForeGround shr 0) and 255;
g2 := (cForeGround shr 8) and 255;
b2 := (cForeGround shr 16) and 255;
r := Round((r2 * alpha) + r1);
g := Round((g2 * alpha) + g1);
b := Round((b2 * alpha) + b1);
if r > 255 then
r := 255;
if g > 255 then
g := 255;
if b > 255 then
b := 255;
result := (b shl 16) + (g shl 8) + r;
end;
function Colorsubtract(cBackGround, cForeGround: TColor; alpha: advancedFloat): TColor;
overload;
var
r1, g1, b1, r2, g2, b2: integer;
r, g, b: integer;
begin
r1 := (cBackGround shr 0) and 255;
g1 := (cBackGround shr 8) and 255;
b1 := (cBackGround shr 16) and 255;
r2 := (cForeGround shr 0) and 255;
g2 := (cForeGround shr 8) and 255;
b2 := (cForeGround shr 16) and 255;
r := Round(r1-(r2 * alpha));
g := Round(g1-(g2 * alpha));
b := Round(b1-(b2 * alpha));
if r < 0 then
r := 0;
if g < 0 then
g := 0;
if b < 0 then
b := 0;
result := (b shl 16) + (g shl 8) + r;
end;
function ColorBlend(cBackGround, cForeGround: TColor; alpha: advancedFloat): TColor;
{$IFDEF USE_SSE3}
asm
err
end;
{$ELSE}
var
r1, g1, b1, r2, g2, b2: nativeint;
r, g, b: nativeint;
begin
r1 := (cBackGround shr 0) and 255;
g1 := (cBackGround shr 8) and 255;
b1 := (cBackGround shr 16) and 255;
r2 := (cForeGround shr 0) and 255;
g2 := (cForeGround shr 8) and 255;
b2 := (cForeGround shr 16) and 255;
r := Round(((r2 - r1) * alpha) + r1);
g := Round(((g2 - g1) * alpha) + g1);
b := Round(((b2 - b1) * alpha) + b1);
result := (b shl 16) + (g shl 8) + r;
end;
{$ENDIF USE_SSE2}
function ColorBlendRGBA(cBackGround, cForeGround: TColor; alpha: advancedFloat): TColor;
var
r1, g1, b1, r2, g2, b2,a1,a2: nativeint;
r, g, b,a: nativeint;
begin
r1 := (cBackGround shr 0) and 255;
g1 := (cBackGround shr 8) and 255;
b1 := (cBackGround shr 16) and 255;
a1 := (cBackground shr 24) and 255;
r2 := (cForeGround shr 0) and 255;
g2 := (cForeGround shr 8) and 255;
b2 := (cForeGround shr 16) and 255;
a2 := (cForeGround shr 24) and 255;
r := Round(((r2 - r1) * alpha) + r1);
g := Round(((g2 - g1) * alpha) + g1);
b := Round(((b2 - b1) * alpha) + b1);
a := Round(((a2 - a1) * alpha) + a1);
result := (a shl 24)+(b shl 16) + (g shl 8) + r;
end;
function ColorBlend_PreserveSourceAlpha(cBackGround, cForeGround: TColor; alpha: advancedFloat): TColor;
var
r1, g1, b1, r2, g2, b2,a1: nativeint;
r, g, b,a: nativeint;
begin
r1 := (cBackGround shr 0) and 255;
g1 := (cBackGround shr 8) and 255;
b1 := (cBackGround shr 16) and 255;
a1 := (cBackGround shr 24) and 255;
r2 := (cForeGround shr 0) and 255;
g2 := (cForeGround shr 8) and 255;
b2 := (cForeGround shr 16) and 255;
// a2 := (cForeGround shr 24) and 255;
// alpha := (a1/255) * alpha;
r := Round(((r2 - r1) * alpha) + r1);
g := Round(((g2 - g1) * alpha) + g1);
b := Round(((b2 - b1) * alpha) + b1);
a := a1;
result := (a shl 24)+(b shl 16) + (g shl 8) + r;
end;
function ColorBlend_ForegroundSourceAlpha(cBackGround, cForeGround: TColor; alpha: advancedFloat): TColor;
var
r1, g1, b1, r2, g2, b2,a1,a2: nativeint;
r, g, b,a: nativeint;
begin
r1 := (cBackGround shr 0) and 255;
g1 := (cBackGround shr 8) and 255;
b1 := (cBackGround shr 16) and 255;
r2 := (cForeGround shr 0) and 255;
g2 := (cForeGround shr 8) and 255;
b2 := (cForeGround shr 16) and 255;
a2 := (cForeGround shr 24) and 255;
alpha := (a2/255) * alpha;
r := Round(((r2 - r1) * alpha) + r1);
g := Round(((g2 - g1) * alpha) + g1);
b := Round(((b2 - b1) * alpha) + b1);
result := (b shl 16) + (g shl 8) + r;
end;
function ColorOp(cBackGround, cForeGround: TColor; alpha: advancedFloat;
AlphaOp: TAlphaOp): TColor;
begin
case AlphaOp of
aoAdd: begin
result := ColorAdd(cBackGround, cForeGround, alpha);
end;
aoSubtract: begin
result := ColorSubtract(cBackGround, cForeGround, alpha);
end;
aoNone: begin
result := cForeGround;
end;
else
result := clBlack;
end;
end;
function ColorMultiply(c: TColor; c2: TColor): TColor;
var
cc1,cc2,cc3: TRGBNativeFloatColor;
begin
cc1.FromColor(c);
cc2.FromColor(c2);
cc3 := cc1*cc2;
result := cc3.ToColor;
end;
function ColorReverse(c: TColor): TColor;
var
p: pbyte;
r: byte;
begin
result := c;
p := Pbyte(@result);
r := p[0];
p[0] := p[2];
p[2] := r;
end;
function ColorAlpha(c: TColor; rAlpha: advancedFloat): TColor;
var
p: pbyte;
begin
result := c;
p := Pbyte(@result);
p[3] := round(rAlpha * 255);
end;
end.