Skip to content

Instantly share code, notes, and snippets.

@justindujardin
Last active September 24, 2015 23:07
Show Gist options
  • Save justindujardin/9747de1acca7e631f420 to your computer and use it in GitHub Desktop.
Save justindujardin/9747de1acca7e631f420 to your computer and use it in GitHub Desktop.
Actionscript HSL Color blend texture
public static function hslColorBlendMaterial(target:BitmapData,colorMask:BitmapData,color:uint,darkLuminance:Number=0.0):void
{
Profiler.enter("BitmapFeatureRenderer.colorBlendMaterial");
var hslColorMask:HSL = new HSL();
var hslTarget:HSL = new HSL();
if(colorMask != null && !colorMask.rect.equals(target.rect))
throw new Error("only supports color blending same size textures");
// Calculate a single HSL color for the mask.
HSL.convertRGBToHSL(color,hslColorMask);
var isDark:Boolean = (hslColorMask.luminance < darkLuminance);
for(var x:int = 0; x < target.width; x++)
{
for (var y:int = 0; y < target.height; y++)
{
var maskPixel:uint = (colorMask != null) ? colorMask.getPixel(x,y) : 0xFFFFFF;
if(maskPixel != 0)
{
HSL.convertRGBToHSL(target.getPixel(x,y),hslTarget);
hslTarget.hue = hslColorMask.hue;
hslTarget.saturation = hslColorMask.saturation;
hslTarget.luminance = Math.pow(hslTarget.luminance,2.1);
target.setPixel(x,y,HSL.convertHSLToRGB(hslTarget));
}
}
}
Profiler.exit("BitmapFeatureRenderer.colorBlendMaterial");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment