# Why is my texture's alpha not being applied correctly?

I have a PNG image with alpha, which I'm attempting to overlay on a scene as a kind of 3D compositing. However I've noticed that semi-transparent parts of the image don't seem to be as occluded as they should be.

For example, in Blender 3.4.1 (scene):

Compared to how this looks in my photo-editor:

This occurs regardless of using Eevee or Cycles render.

Color Management is configured as follows:

• Display Device: sRGB
• View Transform: Standard
• Look: None
• Exposure: 0.000
• Gamma: 1.000

I suspect what might be happening is that the 8-bit per-channel sRGB image is being converted to Blender's linear colourspace (which is fine), except I'm now applying my 8-bit alpha channel in a linear colour space (which is not).

And indeed, if I convert my photo-editor document from 8-bit per-channel sRGB to 32-bit per-channel linear sRGB, the result looks almost identical to what I see in Blender:

So my question is, can I apply the alpha channel as-if it was in the non-linear sRGB colour space?

edit: I suspect what I might need to do is apply gamma compression. A value of 0.22 looks about right, but that value is entirely empirical.

• I believe you are correct, but AFAIK you can't change it. Mar 19, 2023 at 21:19
• I investigated your idea of changing the alpha so it will have the equivalent effect when done in linear space. See this fiddle. Because, as you can see, the correct alpha depends heavily on what the foreground/background colors are, there isn't a way to change it that will always work. Mar 20, 2023 at 3:19

Thanks to @scurest, I now have a Nodes setup that calculates the correct linear alpha for a non-linear sRGB image (scene):

The maths behind this can be found below:

Blending a colour $$b$$ with colour $$a$$ by alpha $$\alpha$$ is defined as:

$$c = (1 - \alpha)a + \alpha{}b$$

This can be rearranged into:

$$\alpha = \frac{c - a}{b - a}; \text{ if } a \ne b$$

So, given gamma-expansion $$\gamma(x)$$ and gamma-compression $$\gamma^{-1}(x_{linear})$$:

$$\begin{eqnarray*} && \alpha_{linear} &=& \frac{c_{linear} - a_{linear}}{b_{linear} - a_{linear}}; \text{ if } a_{linear} \ne b_{linear}\\ \text{where: } && c_{linear} &=& \gamma\bigl(c\bigr) \\ && &=& \gamma\Bigl((1 - \alpha) a + \alpha b\Bigr) \\ && &=& \gamma\Bigl((1 - \alpha) \gamma^{-1}\bigl(a_{linear}\bigr) + \alpha \gamma^{-1}\bigl(b_{linear}\bigr)\Bigr) \end{eqnarray*}$$

note: sRGB images use a gamma-expansion factor of $$2.2$$.

• This is doing the blending entirely in the material, not using the Alpha Blend mode. If that's okay for you, it will be much easier to just convert the two colors to sRGB (two gamma nodes), blend (a mix node), and convert back (one gamma node). Mar 22, 2023 at 16:35
• @scurest It wasn't shown in the previous blend file (just updated it), but I worked out that you can use a Transparent BSDF with the calculated "Linear Alpha" so it uses Alpha Blend. The Glass BSDF with IOR = 1 is a hack to sample the colours behind, but only works for one plane with no Alpha Blend or Screen Space Reflections behind it. Mar 23, 2023 at 2:23