Desmond Hume
Desmond Hume

Reputation: 8617

How to change the alpha of a pixel without changing the resulting color?

Given: A pixel, with its color (denoted as PC0) and alpha value (denoted as PA0), which is layered over a background of some color (denoted as BC).

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

Upvotes: 2

Views: 1243

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308391

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

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

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

Edit: substitute 255 for 1 in the above if you're using the common convention of color values ranging from 0-255.

Upvotes: 3

Related Questions