05.28TIP - Converting a textual HEX color value to int
Hi Everyone,
just a short tip today on how to convert values like this : #00FF00 from a config file, letsay, to something you can use inside of your AS3 code.
Here it is
Actionscript:
-
function digestHexColor(color:String):Number
-
{
-
var col:Number = parseInt(color.substr(1), 16);
-
if (isNaN(col))
-
col = 0xffffff;
-
-
return col;
-
}
What this enables you to do, is read a xml file, lets say something like this
XML:
-
<?xml version="1.0" encoding="utf-8" ?>
-
<config>
-
<colors>
-
<bg>#000000</bg>
-
<btnHigh>#CCCCCC</btnHigh>
-
<btnLow>#5F5F5F</btnLow>
-
<text>#FFFFFF</text>
-
</colors>
-
<thumbBar>
-
<padding>
-
<left>30</left>
-
<right>30</right>
-
<top>22</top>
-
<bottom>22</bottom>
-
</padding>
-
<spacing>
-
<horizontal>12</horizontal>
-
<vertical>12</vertical>
-
</spacing>
-
</thumbBar>
-
</config>
And extract the values for the colors, then use them.. Let us pretend that the XML file is in a variable named xmlConfig, you could do this:
Actionscript:
-
var bgColor:Number = digestHexColor(xmlConfig.colors.bg.toString());
And then use this value to draw, assign colors to a textField or .... anything you like!
Martin

Leave a Reply