This is a follow up to yesterday’s post, addressing the first of the two caveats I mentioned then. I had said that the alpha associated with a color (whether it’s the color of the glow or shadow, or highlight of a bevel, or color stop in a gradient) was nowhere to be found in JSFL’s filter object, and that I had just used a value of 1.0 in the generated code.
Turns out I was wrong, but I’m glad that we do access to this information.
Honestly, I can’t believe I didn’t notice it before when I was writing the original script. If a given color has an alpha of anything other than 100%, the color string is 32-bit, like “#FF993366“. I think what threw me off was that the alpha channel is in the right-most bits, not the left-most bits, like we’re used to with specifying color-with-alpha in ActionScript, like when working with BitmapData. It’s a lame excuse, but I think I was scanning the color values of my test filters, looking for “#66FF9933“, but instead seeing the initial “FF” and dismissing the value as being 24-bit.
Anyway, there’s no handy way to extract the alpha information from the color information, but since it’s actually a string of a hex-format numbers, it’s fairly simple to do some string manipulation and get the values. I’ve updated the original JSFL script with a bit of logic to handle this extraction, as well as updating the use of the colors from the Filter object when converting to ActionScript code. The key color extraction function is this:
function getColorAndAlpha(color) {
var colorInfo = {};
if (color.length == 9) {
colorInfo.alpha = Math.round(parseInt(color.substr(7, 2), 16) / 0xFF * 100) / 100;
colorInfo.color = color.substr(0, 7).replace(/^#/, "0x")
} else if (color.length == 7) {
colorInfo.alpha = 1;
colorInfo.color = color.replace(/^#/, "0x")
} else {
fl.trace("Problem parsing color.")
}
return colorInfo;
}
Pass in the color string from a Filter object, and get back an object with color and alpha properties back, both ready for use in the ActionScript conversion.
The updated script can be found on the original post, or here is the link to the text file again.

Leave a comment
Comments feed for this article