Alpha Blend if Background Alpha is not 255/1.0f

190 views Asked by At

I am trying to do an alpha blending in c#. So my actual code for this is:

final.Red = (pencil.Red * pencil.Alpha) + (background.Red * (1.0f - pencil.Alpha));
final.Green = (pencil.Green * pencil.Alpha) + (background.Green * (1.0f - pencil.Alpha));
final.Blue = (pencil.Blue * pencil.Alpha) + (background.Blue * (1.0f - pencil.Alpha));

This is working fine if the background pixel has no opacity. But what is the calculation for the colors if the background pixel has opacity?

1

There are 1 answers

0
AudioBubble On BEST ANSWER

Ok I managed it by myself. Was not as complicated as I thought. Heres is my solution:

final.Red = (pencil.Red * pencil.Alpha) + (background.Red * (1.0f - pencil.Alpha));
final.Green = (pencil.Green * pencil.Alpha) + (background.Green * (1.0f - pencil.Alpha));
final.Blue = (pencil.Blue * pencil.Alpha) + (background.Blue * (1.0f - pencil.Alpha));
final.Alpha = background.Alpha + (1.0f - background.Alpha * pencil.Alpha);

With this it is working on every backgound opacity.