Advertisement
Sorceress

RGB Blending

Feb 13th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. Let C by the destination background
  2.  
  3. Firstly, layer 0 (RGB0, A0) blends with the destination like this:
  4. RGBBlend0 = C * (1 - A0) + RGB0 * A0
  5.  
  6. Then layer 1 (RGB1, A1) blends with the destination like this:
  7. RGBBlend1 = RGBBlend0 * (1 - A1) + RGB1 * A1
  8.  
  9. -------------------------------------------------
  10.  
  11. Let's combine these two steps (substituting):
  12. RGBBlend1 = C * (1-A0)*(1-A1) + RGB0*A0*(1-A1) + RGB1*A1
  13.  
  14. What we want is a layer that achieves this in one blend, ie (RGB, A) such that:
  15. RGBBlend1 = C * (1 - A) + RGB * A
  16.  
  17. Equating C terms,
  18. 1-A = (1-A0)*(1-A1)
  19.  
  20. A = 1 - (1-A0)*(1-A1) = A0 + A1 - A0*A1 -- (equation Alpha)
  21.  
  22. Equating RGB terms,
  23. RGB = ( RGB0*A0*(1-A1) + RGB1*A1 ) / A -- (equation for RGB)
  24.  
  25. ------------------------------------------------
  26.  
  27. With some rearrangement, we get:
  28. RGB = RGB0 + (RGB1 - RGB0) * F(A0,A1)
  29.  
  30. where F(A0,A1) is a lookup table given by
  31. F(A0,A1) = A1 / (A0 + A1 - A0*A1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement