How to change alpha of a pixel without changing color?

Considering: a pixel with its color (denoted as PC0) and an alpha value (denoted as PA0), which is superimposed on top of a background of any color (denoted as BC).

Question: How would you change the alpha value of a pixel (PA0) for another value (designated as PA1) so that the final composite color of the pixel and background does not change (PBC0 == PBC1)? In other words, how would you find a PC1 that makes the original and final composite colors (PBC0 and PBC1) the same?

+2
source share
1 answer
PBC0 = PC0*PA0 + BC*(1-PA0) = PC1*PA1 + BC*(1-PA1) 

If you know both PA0 and PA1, you can decide for PC1.

 PC1 = (PC0*PA0 + BC*(1-PA0) - BC*(1-PA1)) / PA1 

Edit: replace 255 with 1 in the above example if you use the general color convention from 0 to 255.

+3
source

Source: https://habr.com/ru/post/908452/


All Articles