PROWAREtech

articles » current » c-plus-plus » algorithms » alpha-compositing-algorithm

C/C++: Alpha Compositing Algorithm

How to convert an image with an alpha channel to an image without an alpha channel.
Choose Background Color

First, choose a background color to be used for the transparent pixels.

Apply Alpha Compositing Algorithm

The formula for alpha compositing a single pixel typically involves blending each of the RGB values of the foreground image with those of the background color based on the alpha value. The alpha value ranges from 0 (completely transparent) to 255 (completely opaque).


float ConvertAlphaChannelToBackground(float desiredBackgroundColor, float foregroundColor, float alpha)
{
	return (foregroundColor * alpha / 255) + (desiredBackgroundColor * (255 - alpha) / 255);
}

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE