background-blending-alpha-ref.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <!--
  2. Any copyright is dedicated to the Public Domain.
  3. http://creativecommons.org/publicdomain/zero/1.0/
  4. -->
  5. <html>
  6. <style>
  7. .reftest {
  8. background-color: rgba(0, 255, 0, 0.5);
  9. }
  10. .child {
  11. width: 100px;
  12. height: 100px;
  13. opacity: 0.5;
  14. background-color: rgb(0, 127, 0);
  15. }
  16. .topleft {
  17. float:left;
  18. width: 100px;
  19. height: 100px;
  20. /*First blend black rect with green in place*/
  21. /* Cs = (1 - αb) x Cs + αb x B(Cb, Cs)*/
  22. /* B(Cb, Cs) = | Cb - Cs | = (0, 0, 0) */
  23. /* 0.5 * (0, 255, 0) + 0.5 * (0, 0, 0) = (0, 127.5, 0)*/
  24. /* Now, composite the resulting color with src-over; the alpha is the original alpha for the top layer
  25. while the color is the blending result*/
  26. /* co = αs x Fa x Cs + αb x Fb x Cb - this is premultiplied */
  27. /* αo = αs + αb x (1 – αs) */
  28. /* Source over: Fa = 1; Fb = 1 – αs */
  29. /*co = 0.5 * 1 * (0, 127.5, 0) + 0.5 * 0.5 * (0, 255, 0) = (0, 63.5, 0) + 0.25 * (0, 255, 0) = (0, 127.5, 0)*/
  30. /*ao = 0.5 + 0.5 * 0.5 = 0.75*/
  31. /* Co = co/ao = (0, 127.5, 0) / 0.75*/
  32. /* Now alpha composite on white background */
  33. /*co = 0.75 * 1 * (0, 127.5, 0) / 0.75 + 1 * 0.25 * (255, 255, 255) = (0, 127.5, 0) + (63.75, 63.75, 63.75) = (63.75, 159, 63.75) = (64, 191, 64) */
  34. background-color: rgb(64, 191, 64);
  35. }
  36. .topright {
  37. float:left;
  38. width: 100px;
  39. height: 100px;
  40. }
  41. .bottom {
  42. width:200px;
  43. height: 100px;
  44. clear:both;
  45. }
  46. </style>
  47. <div class="topleft"></div>
  48. <div class="reftest topright"></div>
  49. <div class="reftest bottom"></div>
  50. </html>