Dynamically change asset color using ColorTransform
Usually, changing colors inside your application is easy. You can set just about everything from textColor to backgroundColor, inline (backgroundColor = “#FFFFFF”), in a stylesheet (background-color: #FFFFFF) or using the setStyle method( uic.setStyle(”borderColor”, “green”) ). But what if you are trying to change the color of an embedded asset?
Say your designer gives you an asset, like a black funky shaped blob for a logo or icon. Then, later on, your company changes the icons to white. Why wait around for 2 weeks while your designer finds time to change the graphic when you could change in the code? Or perhaps a better example would be if your application requires applying different stylesheets at run-time for users to pick custom colors and layouts.
This is where using transforms comes in handy. The ColorTransform class lets you do some cool things to set RGB and alpha offsets or multipliers as well as set the color explicitly, which is what this example does. All you do is create a new instance, set the color value and assign the instance.
ct.color = event.color;
img_funky.transform.colorTransform = ct;
This is not just limited to embedded assets. You can use transforms on almost any DisplayObject.
Example Application – Click Here
No commentsNo comments yet. Be the first.
Leave a reply