Welcome back to my tutorial series on 2D dynamic shadows. In this article I'm going to explain what blend modes are available in flash, and what each of the blend modes does. Why blend modes? They are extrememly useful for controlling the color of pixels and as you'll see, crucial to my shadow implementation. I'll go into that detail in a later article, where we are going to examine which blend modes are useful to create shadows.
Some of the following information is gleaned from wikipedia, some from random websites. The accompanying image to each blend mode is representation of how the blend mode looks when combining against two different images. Top left is original image while top right is blended with a gradient using the appropriate blend mode. Bottom right( I know, reveresed... ) is the gradient on its own while bottom left is it blended with a flat grey color( 0x666666. ) I'm going to discuss their usefulness as a filter for image manipulation, the specific formula that is involved and how it affects the end result.
One thing to keep in mind with all blend modes: The formula( or an optimized version of it ) is perfomed on each pixel to be blended! That means that an ADD blend will take less of your frame than an OVERLAY blend. However, since flash executes all of its code on the CPU, you are more often than not bottlenecked by pixel fill rates than anything else. Still, its worth remembering if performance is required.
A final note before I get right into it; my represenation for color here is in the form of RGB with 0-255 as possible values for each. To make a compatible flash color, this can be packed into an unsinged int using:
Color = r << 16 | g << 8 | b;
Normal blend mode
This is the standard blend mode which simply takes each pixel from the top layer if present. Otherwise the bottom layer is used. Flash uses this blend mode by default. I don't think this requires an image.
Simple arithmetic blend modes
Addition
This blend mode simply adds pixel values of one layer with the other. In case of values above 255, white is displayed. Commonly used to create an animated lightening dissolve effect between two images. I've also seen it used many times in particle emmitters to create a 'glow' look to the particles when they overlap. Even though it is a simple operation to perform care should be taken not to overuse it.
Formula: Result Color = Clamp((Top Color + Bottom Color), 255)
The result is a whiter picture that is technically 'brighter' but has no deeper contrast to show it.
Subtract
This blend mode simply subtracts pixel values of one layer with the other. In case of negative values, black is displayed. Commonly used to create an animated darkening dissolve effect between two images. It can also be used in particles to create a 'draining' of color. Like ADD mode it has uses in specific scenarios, but is not a great utility function for image manipulation. Why? Well it really just shifts the color spectrum in one direction which results in capping of the colors. With Additive blending it caps to white while with with Subtractive it caps to black.
Formula: Result Color = Clamp((Top Color - Bottom Color), 0)
The result is a darker picture with no contrast boost. If image enhancement is your goal, check out Multiply.
Difference
If Difference mode both colors are subtracted from one another and then the absolute value is taken. This means that the result shows the distance between both colors. The result can look a bit strange in many cases but has a few good uses. First, this mode can be used to invert parts of the base image depending on the blend image. Also, it can be used to compare two images (if the results are black, they are equal.)
Formula: Result Color = Absolute Value( Top Color - Bottom Color)
The results are... strange but useful.
Darken
Replaces only the areas that are lighter than the blend color. Areas darker than the blend color don’t change. Colors can seem to behave oddly with this blend mode. However, if you understand the underlying formula then it all makes sense.
Formula: Result Color = If ( Top Color < Bottom Color )
then Result = Top Color
else Result = Bottom Color
An important thing to keep in mind is this formula is run for each color value seperately, not on the color value as a whole. This may seem counter-intuitive to some. For instance, the color Red is 0xFF0000 while Blue is 0x0000FF. If you compared the values straight across( In decimal format Red = 16711680 while Blue is 255 ) then Blue should be the darkest color. Yet, when these two blend together using BlendMode?.DARKEN the result is Black. What? Yeah... that's simply a result of the component comparisons. If TopColor?.R = 255 and BottomColor?.R = 0, the result is 0 and the same applies to the other colors.
The result is a flattening of the baseline color of the image. Usually results in a much darker image.
Lighten
Replaces only pixels that are darker than the blend color. Areas lighter than the blend color don’t change. This is exactly the opposite of Darken and has a similar formula. The effect produced is also of a 'flattening' of the images color but in this case towards a lighter spectrum.
Formula: Result Color = If ( Top Color < Bottom Color )
then Result = Top Color
else Result = Bottom Color
The result is a flattened image with less color depth than before.
Erase
Removes all base color pixels, including those in the background image. In this example image I changed the shape of the gradient to a circle just to illustrate the point a bit better. This works like a reverse mask in flash, hiding what it overlaps instead of revealing it. Alpha is respected and if you have an alpha gradient then only part of the resulting image is masked out. I'm not going to speak much on this one here and instead keep it for later.
Formula: Honestly, I can only guess at this one. I suspect it uses a few conditionals as the result is different based on where you are using it.( example, drawing to a bitmap with BlendMode?.ERASE when the bitmap has no transparency )
Multiply and Screen
Multiply and Screen blend modes are basic blend modes for lightening or darkening images. Overlay and Hard Light can be used to improve image saturation and contrast. All of these blend modes involve some form of multiplication and are slightly more complex.
Multiply
Multiply blend mode multiplies the values for each pixel of the top layer with the corresponding values of the pixel for the bottom layer. As a simple multiplication can get values as high as 65025. Since this is far higher than the maximum allowed value, 255, the result is divided by 255. Many image editing software programs offer this option. It can be used to both darken and enhance an overexposed image by blending a copy of the image with the original. Since white(0xffffff) multiplied with white is( you guessed it ) white and black(0x000000) with black produces black, dark and light parts of the picture are retained while the rest of the image is darkened.
Formula: Result Color = (Top Color * Bottom Color)/255
The result is a darker, 'enhanced', picture. It can also result in a dark picture if overused or used incorrectly.
Screen
With Screen blend mode the values of the pixels in the two layers are inverted, multiplied, and then inverted again. This is in some way the opposite of multiply. Where the top layer is black the underlying colour is the same, where it is white, the result is white and all other pixels are lightened. Screen is the typical fix for an underexposed image because it lightens it.
Formula: Result Color = 255 - ((255 - Top Color)*(255 - Bottom Color))/255
The result is a brighter picture. It can also result in a washed out picture.
Overlay
One of the most useful blend modes for improving saturation in an otherwise correctly exposed image is the Overlay mode. Use it on the top layer of an image with two layers the same and it boosts the colours in it. Light parts of the picture become lighter and dark parts become darker. It works by applying a multiply or screen effect depending on the base colour in the image. As you can imagine, branching code is much more complex than a simple addition or multiplication. Use this with caution in any runtime code.
Formula: Result Color = If (Top Color < 128)
then Result = 2 * (Top Color * Bottom Color)
else Result = 255 - (2 * (255 - Top Color)) * (255 - Bottom Color);
The result is a picture with greater saturation.
Hard Light
The Hard Light blend mode also multiplies or screens the image but this time the result depends on the lightness or darkness of the colours on the blend layer (not the base layer). It is handy for adding lights and shadows to an image because light pixels on the top layer are lightened and dark ones are darkened. Much like Overlay, this can get expensive during runtime.
Formula: Result Color = If (Bottom Color < 128)
then Result = 2 * (Top Color * Bottom Color)
else Result = 255 - (2 * (255 - Bottom Color)) * (255 - Top Color);
The result is a picture with greater contrast and saturation.
What's next?
Apologies for this taking so long to get out. I wanted to make sure my information was both accurate and informative. Also, I didn't want to just reprint other people's words exactly. Now the next thing to tackle is how these blend modes can help create shadows!
EDIT: If you stumbled here for the blend modes, check out Lost in Actionscript










