Issue with the alpha blending arithmatic
in
Core Library Questions
•
1 year ago
The results of drawing shapes with alpha are not entirely what I would expect. Say I draw white boxes over a black background as below:
White box (alpha = 50)
over
White box (alpha = 50)
over
Black background
I would expect one box to have a color of 50, and the next box to have a color of 100. That is, the alpha's are additive and if I layer 5 white boxes at alpha=50, then I would have the color 250. This isn't what I'm seeing. See the code and results below. You can see that the first box is close to my expectation at 49. But it trails off from there, and after 21 layers, it tops out at 245. It never actually gets to full white.
I've done some reading about Alpha blending, and I don't think this is a bug as such, but it doesn't meet my expectations. Is there a way to mimic my simpler additive blending approach? Is there a way to change the blending mode of the renderer? My way would certainly save some compute cycles :) Seems ashame for the renderer to be doing all these computations that really just make it more difficult to create things the way I'd like them.
Any help is appreciated,
-- John
<code>
int width = 600;
void setup() {
size(width, 100, P2D);
background(0);
fill(0xff,50);
noLoop();
}
void draw() {
for (int i=0; i<width; i+=20) {
rect(i,0,width-i,100);
}
int dropper;
for (int i=0; i<width; i+=20) {
dropper = get(i+10,10)&0x000000ff;
System.out.format("%d\n",dropper);
}
}
</code>
<results>
49
89
121
146
166
182
195
205
213
220
225
229
233
236
238
240
241
242
243
244
245
245
245
245
245
245
245
245
245
245
</results>
White box (alpha = 50)
over
White box (alpha = 50)
over
Black background
I would expect one box to have a color of 50, and the next box to have a color of 100. That is, the alpha's are additive and if I layer 5 white boxes at alpha=50, then I would have the color 250. This isn't what I'm seeing. See the code and results below. You can see that the first box is close to my expectation at 49. But it trails off from there, and after 21 layers, it tops out at 245. It never actually gets to full white.
I've done some reading about Alpha blending, and I don't think this is a bug as such, but it doesn't meet my expectations. Is there a way to mimic my simpler additive blending approach? Is there a way to change the blending mode of the renderer? My way would certainly save some compute cycles :) Seems ashame for the renderer to be doing all these computations that really just make it more difficult to create things the way I'd like them.
Any help is appreciated,
-- John
<code>
int width = 600;
void setup() {
size(width, 100, P2D);
background(0);
fill(0xff,50);
noLoop();
}
void draw() {
for (int i=0; i<width; i+=20) {
rect(i,0,width-i,100);
}
int dropper;
for (int i=0; i<width; i+=20) {
dropper = get(i+10,10)&0x000000ff;
System.out.format("%d\n",dropper);
}
}
</code>
<results>
49
89
121
146
166
182
195
205
213
220
225
229
233
236
238
240
241
242
243
244
245
245
245
245
245
245
245
245
245
245
</results>
1