How to blend alpha values?
In Java's AWT, alpha (opacity) is expressed using values from 0 (completely transparent) to 255 (completely opaque). How do I correctly blend alpha values, e.g. t开发者_StackOverflowo implement a fade effect? If the child element has an alpha of 200 relative to its parent, and the parent element has an alpha of 100, what is the alpha of the pixels that are ultimately drawn?
How much control do you have over pixel alpha values? What kind of fade effect are you looking for?
Different graphics systems might implement different alpha operations by default. I think the one that is most intuitive and thus is usually the default is to:
DestColor = DestColor * (1 - SourceAlpha) + SourceColor * SourceAlpha
DestAlpha left as-is
Using this scheme, the source alpha only affects the color component of the destination surface, and not the destination surface alpha itself.
I'm not sure how AWT behaves or if it gives you any level of control over alpha blending operations, but alpha can be applied in many different ways in theory.
I think this is right: alpha of 200 for child
means
blended = 200/255 * child + (1 - 200/255) * parent
We're used to this for a pixel value, but we could also interpolate the alpha channel in the same way. We have to pretend that the "real" alpha value of child
is 255:
blended = 255 * 200/255 + 100 * (1 - 200/255)
child.alpha parent.alpha
which is approximately 222.
This question might also be of use.
精彩评论