# Opacity alpha off by 1 and RGB off by one when using Blend mode normal

Hello,

We have a tool which extract the layer images from the .pyxel archive and rebuild the resulting image. We noticed the formula to properly compute the expected blended image is different from expected. I have attached the file to repro.

Assume an image with 2 layers, first layer (BaseLayer) opacity is set at 255, second layer (Opacity_100) opacity is set at 100.
If we look at the docData.json inside the .pyxel archive, we will see the layers alpha tagged as 255 and 100, respectively, as expected.

We'll use the pixel at coordinate (40,70) to demonstrate the bug.

If we look at the images inside the .pyxel archive we have:
- layer0.png: RGBA(167,154,95, 99)
- layer1.png: RGBA(197,184,125, 255)

Now if we export these layers using File / Export / Export Images ... / Layers as separate files CHECKED, we get:
- Base_Layer(167,154,95,99) - Exactly as expected
- Opacity_100(196,183,124,99) - Notice this one is off by one on the RGB values compared to the internal png inside the .pyxel archive; especially notice the alpha value is 99, not 100.

Now let's take a look at the composite image using File / Export / Export Images ... / Layers as separate files UNCHECKED, we get:
RGBA(178,165,106,159)

Now, if we want to obtain the composite pixel from above we have to do the off by 1, otherwise we will get the wrong value.
If we use the raw data from layer?.png + json:
R: 167*100/255+197*(255-100)/255 = 179
G: 154*100/255+184*(255-100)/255 = 166
B: 95*100/255+125*(255-100)/255 = 107

Notice how this is incorrect RGB(179,166,107) != RGB(178,165,106)

But if we do the off by-1 trick only on layers which have opacity < 255:
R: (167-1)*100/255+197*(255-100)/255 = 178
G: (154-1)*100/255+184*(255-100)/255 = 165
B: (95-1)*100/255+125*(255-100)/255 = 106

Now it works.

I have played with multiple opacity value, and it seems that alpha+1=opacity holds up for all values; except for the edge cases (255 stays at 255, and 0 stays at 0).

Can anyone shed some light on what is going here?

Thank you,