Puzzle - effect of alpha value depends on RGB value of pixel?

Here is a little test case that illustrates my puzzle.

  • In both c1 and c3, alpha is set to 255. We should see the fill value showing over the background as declared via the RGB values, and this is what happens. In c1 we see a black rectangle, and in c3 we see a white one. (Hurrah.)

  • In both c2 and c4, alpha is set to 0. I think we should NOT be seeing the fill value showing up at all in both cases. What happens should be the opposite of what happens with the alpha value at the other extreme. c4 behaves as expected. c2 puzzles me. I think it should be the same as c4.

What am I missing here? Thanks for any hints and tips…

p.s. I can make it “behave” by setting c2 to color(1, 1, 1, 0); or to color(0, 0, 0, 1) - but this makes zero sense to me

xx

background(#E4E3E8);
size(300,300);
background(#E4E3E8);

color c1 = color(0, 0, 0, 255);
color c2 = color(0, 0, 0, 0);

color c3 = color(255, 255, 255, 255);
color c4 = color(255, 255, 255, 0);

fill(c1);
rect(75, 25, 50, 100);

fill(c2);
rect(150, 25, 50, 100);

fill(c3);
rect(75, 150, 50, 100);

fill(c4); 
rect(150, 150, 50, 100);

fill(0);

text("c1: RGB0+MaxAlpha", 50, 20);
text("c2: RGB0+MinAlpha", 175, 20);
text("c3: RGB255+MaxAlpha", 50, 195);
text("c4: RGB255+MinAlpha", 175, 195);

I’ve tracked back the issue here to using pixels containing an alpha, as a fill.

  • A pixel with transparent alpha works as expected as a fill if the RGB values are (255,255,255), i.e.white. A fill of (255,255,255,0) means the fill has no impact on the image displayed. It gives a different result from specifying a fill of (255,255,255,255), in this case you see white. This is as expected.

  • Transparent alpha does not work as expected as a fill if the RGB values are (0,0,0), i.e. black. A fill of (0,0,0,0) results in the display of an object filled with black. This is not as expected. It yields the same result as a fill of (0,0,0, 255). This is not as expected.

The display behaviour of fill with an ARGB pixel is anomalous when RGB is (0,0,0) and alpha is 0, and is inconsistent with behaviour when other RBG or Alpha values are specified. Except for this case, ARGB pixels seem to works as expected, as fills: they can display gradations of transparency through the gamut of allowable values.

However, if I redo this example using Pimages (instead of fill+drawing a rectangle), for my test of test pixel color specs, when overlaying the Pimage on the background, the behaviour of pixels colored (0,0,0,0) when displayed is as expected.

I’m marking this as a solution although it is still puzzling I have enough to be able work around the behaviour. Which may well be “as designed”… but doesn’t seem consistent.

Interesting observations! I just noticed that color(0,1,0,0) ends up almost transparent while color(0,0,1,0) is almost solid black.

When you print them out, color(0,1,0,0) is 256 and color(0,0,1,0) is 1. color in Processing is just an integer. Here’s the problem - because fill can take either grayscale (0-255) or a color, in your case, color(0,0,0,0) is interpreted as grayscale (0: black) with maximum opacity (same as calling fill(0)).

If you know that the value is represented by color, you can always unpack it with red(), green() functions and so on, pass it to fill. But I understand this is totally confusing.

1 Like

So… the integer representation is ambiguous between " I am an integer representing a greyscale" and “I am an integer representing a tuple decomposable into an RGB with an alpha”? And Processing just takes a punt?

But …you’d expect these to have different hex representations, due to different prefixes, and thus to map onto different integers? Strange that they map onto the same integer. (Gah! Good thing I’m already grey.)

When I dig into it, I see that this mixup is acknowledged in the code:

2 Likes

Yes. Thank you. In a related vein, the representational choice of stuffing “alpha” into the blue channel for the GRAYSCALE Pimage format type also has interesting consequences. Took me ages to figure out what was going on, when I was trying to round trip masks on and off Pimages. Do you think it could be useful for me to write this stuff up somewhere more accessible? I certainly have “invested” a lot of time in trying to make sense of the system’s behaviour around colour and alpha representation.