package { import flash.display.Sprite; /** * Class for testing blend mode pixel equations. * The output of this file are trace statements, so nothing will appear in SWF. */ public class graphic_flex_image_effects_02_Flex_BlendModeFunctions extends Sprite { /** * Constructor. Sends two pseudo-pixels to functions to determine * how pixels interact with each blend mode. */ public function graphic_flex_image_effects_02_Flex_BlendModeFunctions() { // the colors of the top and bottom pixel to test var topPixel:Object = {red:0xFF, green:0x33, blue:0x99}; var bottomPixel:Object = {red:0x00, green:0x00, blue:0x99}; // the method to apply var method:String = "difference"; // finds the new value for each channel based on the top and bottom // pixels and blend mode that was applied var red:Number = this[method](topPixel.red, bottomPixel.red); var green:Number = this[method](topPixel.green, bottomPixel.green); var blue:Number = this[method](topPixel.blue, bottomPixel.blue); // traces out the resulting pixel value var resultPixel:uint = (red << 16) | (green << 8) | (blue); trace(resultPixel.toString(16)); } public function normal(topPixel:uint, bottomPixel:uint):uint { return topPixel; } public function multiply(topPixel:uint, bottomPixel:uint):uint { return (bottomPixel * topPixel)/255; } public function screen(topPixel:uint, bottomPixel:uint):uint { return (255 - ((255 - topPixel) * (255 - bottomPixel))/255); } public function hardlight(topPixel:uint, bottomPixel:uint):uint { var color:uint; if (topPixel > 127.5) { color = screen(bottomPixel, 2 * topPixel - 255); } else { color = multiply(bottomPixel, 2 * topPixel); } return color; } public function overlay(topPixel:uint, bottomPixel:uint):uint { return hardlight(bottomPixel, topPixel); } public function add(topPixel:uint, bottomPixel:uint):uint { return Math.min(255, bottomPixel + topPixel); } public function subtract(topPixel:uint, bottomPixel:uint):uint { return Math.max(0, bottomPixel - topPixel); } public function lighten(topPixel:uint, bottomPixel:uint):uint { return Math.max(topPixel, bottomPixel); } public function darken(topPixel:uint, bottomPixel:uint):uint { return Math.min(topPixel, bottomPixel); } public function difference(topPixel:uint, bottomPixel:uint):uint { return Math.abs(topPixel - bottomPixel); } public function invert(topPixel:uint, bottomPixel:uint):uint { return (255 - bottomPixel); } } }