2

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?

Desmond Hume
  • 8,037
  • 14
  • 65
  • 112
  • So, BC=original background, PBC0=overlay pixel color, PA0=original alpha value, PA1=new alpha value. Which of these are given? PBC0, PA0 and PA1, but not BC? – Igor ostrovsky Feb 14 '12 at 20:50
  • @Igorostrovsky Given: PC0 = original color, PA0 = original alpha, BC = constant background color, PA1 = new alpha, that is everything except PC1, which is the color that is supposed to make the pixel look unchanged against the given background after the alpha of the pixel transits from PA0 to PA1. And PBC0 with PBC1 are the two *identical* opaque colors produced by the composition of the pixel and the background, PBC0 before the alpha transition took place, and PBC1 after. – Desmond Hume Feb 14 '12 at 21:06
  • Can I ask you what the application is? I am really curious – MyCarta Feb 14 '12 at 23:42
  • @MyCarta The application of this is performing the second stage of the process of generating transparency information for an image based on a specified color, known as the key color, with the condition that the resulting image should look the same as the original one if layered over a background filled with the key color. The first stage would be determining alpha values by comparing each pixel of the image with the key color and somehow estimating similarity between the two. The second stage is the modification of the colors of semi-transparent pixels meant to satisfy the mentioned condition. – Desmond Hume Feb 15 '12 at 10:57
  • Interesting stuff Desmond, I will research it a bit. Thank you – MyCarta Feb 19 '12 at 16:25

1 Answers1

3
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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 1
    I've just tested it with vector algebra and with values ranging from 0.0 to 1.0 and it was success! Thank you so much for this great answer, sir. – Desmond Hume Feb 14 '12 at 21:36
  • I was having the same question. This works wonderfully! Thank you! – Wouter Oct 08 '16 at 12:21