/////////////////////////////////////////////////////////////////////////////// // Copyright (c) 2008 Vincent Petithory - http://blog.lunar-dev.net/ // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. /////////////////////////////////////////////////////////////////////////////// package net.lunar.filters { import flash.display.Shader; import flash.display.ShaderData; import flash.display.ShaderParameter; import flash.filters.ShaderFilter; /** * The ContrastLightnessFilter class allows * filtering display objects by adjusting their lightness and contrast. * It behaves like Photoshop's Lightness/Contrast adjustement. * *

You may use this class as an usual filter class :

*
 * var filter:ContrastLightnessFilter = new ContrastLightnessFilter(-20, 10);
 * 
 * // with a DisplayObject or a subclass
 * myDisplayObject.filters = [filter];
 * 
 * // or with a BitmapData object
 * myBitmapData.applyFilter(myBitmapData, myBitmapData.rect, 
 * 							new Point(), filter);
 * 
* */ public class ContrastLightnessFilter extends ShaderFilter { /** * @private * The bytecode of the shader. */ [Embed( source="contrastLightnessFilter/ContrastLightnessFilter.pbj", mimeType="application/octet-stream")] private static var ShaderByteCode:Class; /** * The description of the shader, as it is defined in the shader source. */ public static var description:String; /** * The minimum lightness defined in the shader source. * *

You may change it to allow lesser values to all * ContrastLightnessFilter objects

*/ public static var minLightness:int; /** * The maximum lightness defined in the shader source. * *

You may change it to allow higher values to all * ContrastLightnessFilter objects

*/ public static var maxLightness:int; /** * The default lightness defined in the shader source. * *

It is not automatically applied as default values of this * ContrastLightnessFilter filter. * If this value is passed, it will remain the input image unchanged.

*/ public static var defaultLightness:int; /** * The minimum contast defined in the shader source. * *

You may change it to allow lesser values to all * ContrastLightnessFilter objects

*/ public static var minContrast:int; /** * The maximum contrast defined in the shader source. * *

You may change it to allow higher values to all * ContrastLightnessFilter objects

*/ public static var maxContrast:int; /** * The default contrast defined in the shader source. * *

It is not automatically applied as default values of this * ContrastLightnessFilter filter. * If this value is passed, it will remain the input image unchanged.

*/ public static var defaultContrast:int; /** * @private * Class initialized flag. */ private static const isClassInit:Boolean = initClass(); /** * @private * Initiliazes the class variables. * @return true */ private static function initClass():Boolean { // we create a shader instance to define the values of the parameters var s:Shader = new Shader(new ShaderByteCode()); var data:ShaderData = s.data; var lightnessMetadata:ShaderParameter = data.lightness; minLightness = lightnessMetadata.minValue[0]; maxLightness = lightnessMetadata.maxValue[0]; defaultLightness = lightnessMetadata.defaultValue[0]; var contrastMetadata:ShaderParameter = data.contrast; minContrast = contrastMetadata.minValue[0]; maxContrast = contrastMetadata.maxValue[0]; defaultContrast = contrastMetadata.defaultValue[0]; description = data.description; return true; } /** * @private * A reference to the internal shader. */ private var _shader:Shader; /** * Constructor. * * @param lightness increases or decreases the filtered image lightness. * Between -100 and * 100. * @param contrast increases or decreases the filtered image contrast. * Between -100 and * 100 */ public function ContrastLightnessFilter( contrast:int = 0, lightness:int = 0 ) { super(); _shader = new Shader(new ShaderByteCode()); this.contrast = contrast; this.lightness = lightness; } /** * Increases or decreases the filtered image lightness. *

Valid values ar between -100 * and 100.

*/ public function get lightness():int { return this.shader.data.lightness.value[0]; } /** * @private */ public function set lightness(value:int):void { if (value > maxLightness) { value = maxLightness; } else if (value < minLightness) { value = minLightness; } _shader.data.lightness.value[0] = value; this.shader = _shader; } /** * Increases or decreases the filtered image contrast. *

Valid values ar between -100 * and 100.

*/ public function get contrast():int { return this.shader.data.contrast.value[0]; } /** * @private */ public function set contrast(value:int):void { if (value > maxContrast) { value = maxContrast; } else if (value < minContrast) { value = minContrast; } _shader.data.contrast.value[0] = value; this.shader = _shader; } } }