package { import aether.effects.shaders.ShaderEffect; import aether.utils.MathUtil; /** * Custom shader effect that allows you to isolate a single color in an image, * setting a hue and luminance threshold for the isolation. Pixels that do not * fall within these thresholds will either be hidden (no opacity) or made grayscale. */ public class graphic_flex_image_effects_10_Flex_IsolateColorEffect extends ShaderEffect { // update if the name of embedded class (if used) is changed public static var shaderClass:String = "IsolateColorKernel"; // update if the name of the file (if loaded) is changed public static var shaderFile:String = "isolateColor.pbj"; private var _red:uint; private var _green:uint; private var _blue:uint; private var _hueThreshold:uint; private var _luminanceThreshold:uint; private var _hideNonIsolated:uint; /** * Constructor. Sets initial properties. * * @param color The color to isolate in the image. * @param hueThreshold The variance from the color's hue that will still be considered a match. * @param luminanceThreshold The variance from the color's luminance that will still be considered a match. * @param hideNonIsolated True to set non-isolated pixels to be transparent, false to make pixels grayscale. * @param blendMode The blend mode to use when applying the effect. * @param alpha The alpha to use when applying the effect. */ public function graphic_flex_image_effects_10_Flex_IsolateColorEffect( color:uint=0xFF0000, hueThreshold:uint=10, luminanceThreshold:uint=60, hideNonIsolated:Boolean=false, blendMode:String=null, alpha:Number=1 ) { _shaderClass = shaderClass; _shaderFile = shaderFile; this.color = color; this.hueThreshold = hueThreshold; this.luminanceThreshold = luminanceThreshold; this.hideNonIsolated = hideNonIsolated; init(blendMode, alpha); } /** * Configures the shader data with this instance's property values. * * @para data The data for the shader on which parameters can be set. */ override protected function configureShader(data:Object):void { data.color.value = [_red, _green, _blue]; data.hueThreshold.value = [_hueThreshold]; data.luminanceThreshold.value = [_luminanceThreshold]; data.hideNonIsolated.value = [_hideNonIsolated]; } /** * Sets the color to isolate in the image. This splits up the color into component channel values. * * @param color The numeric value of the color to isolate. */ public function set color(color:uint):void { color = MathUtil.clamp(color, 0, 0xFFFFFF); _red = color >> 16 & 0xFF; _green = color >> 8 & 0xFF; _blue = color & 0xFF; } /** * Sets the threshold in which a pixel's hue is considered a match to the set color. * This ensures a value between 0 and 360. * * @param threshold The amount that a pixel's hue can vary from the color's hue and still be a match. */ public function set hueThreshold(threshold:uint):void { _hueThreshold = MathUtil.clamp(threshold, 0, 360); } /** * Sets the threshold in which a pixel's luminance is considered a match to the set color. * This ensures a value between 0 and 255. * * @param threshold The amount that a pixel's luminance can vary from the color's luminance and still be a match. */ public function set luminanceThreshold(threshold:uint):void { _luminanceThreshold = MathUtil.clamp(threshold, 0, 255); } /** * Sets whether non-isolated colors should be hidden or made grayscale. * This transforms the Boolean value to a 0 or 1 as used by the shader. * * @param hide True to set non-isolated pixels to be transparent, false to make pixels grayscale. */ public function set hideNonIsolated(hide:Boolean):void { _hideNonIsolated = hide ? 1 : 0; } } }