Results 1 to 7 of 7

Thread: alpha blend math

  1. #1

    Thread Starter
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111

    alpha blend math

    Alpha blending isn't that hard for me except when the destination also has an alpha channel.

    I've tried multiplying with the destination alpha channel into the RGB calculation.

    My problem currently is semi-transparent pixels, either opaque or transparent destination alpha works ok.

    VB Code:
    1. ' RGBA = destination
    2. 'c(r,g,b) = source
    3. 'Opacity = source opacity
    4.              Sa(0) = Opacity / 255
    5.              Sa(1) = A / 255
    6.  
    7.                
    8.                clsDIB.Red = cR * Sa(0) + cR * (1 - Sa(1)) + R * (1 - Sa(0)) * Sa(1)
    9.                clsDIB.Green = cG * Sa(0) + cG * (1 - Sa(1)) + G * (1 - Sa(0)) * Sa(1)
    10.                clsDIB.Blue = cB * Sa(0) + cB * (1 - Sa(1)) + B * (1 - Sa(0)) * Sa(1)
    11.                clsDIB.Alpha = Opacity * (1 - Sa(1)) + A

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Let me see if I understand the question. You have a semi transparent object, which you want to move under another semi-transparent object. You want to find how transparent the 1st object will be under the 2nd. right?

    Well, if you could convert the two objects transparencies to 0 to 1 (50% would be 0.5) then I would think you could simply multiply the numbers together to get the transparency of the bottom object.
    Or is that what you've already tried?
    Have I helped you? Please Rate my posts.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    you should just add R1*alpha2 to R2, R2 being the top layer red channel and R1 the bottom layer red channel and alpha2 the top layer alpha channel, similar for the other channels, alpha2 should be multiplid by alpha1
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Originally posted by kedaman
    you should just add R1*alpha2 to R2, R2 being the top layer red channel and R1 the bottom layer red channel and alpha2 the top layer alpha channel, similar for the other channels, alpha2 should be multiplid by alpha1
    R2 can be transparent though.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    ok these equations should do, where layer 2 is on top of layer 1 and R any colour channel, A alpha channel:
    Anew=A1A2
    Rnew=((R1(1-A1)-1)A2 +1)/(1-Anew)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6

    Thread Starter
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Originally posted by kedaman
    ok these equations should do, where layer 2 is on top of layer 1 and R any colour channel, A alpha channel:
    Anew=A1A2
    Rnew=((R1(1-A1)-1)A2 +1)/(1-Anew)
    Anew isn't right, because if the old alpha is 0, and the new is 255, it becomes 0.

  7. #7

    Thread Starter
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Ok... so I'm thinking...

    ConstAlpha = SourceOpacity * (1 - Sa(1)) + A

    And then...

    FinalR = Sr * (SourceOpacity/ConstAlpha) + Dr * (A/ConstAlpha)

    Yea... I don't see any problems with that.

    Except Alpha is actually 1-256.

    Oh well, minor detail!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width