Reputation: 1860
My Goal is to add the color for text by dynamically.
var tf:TextField = new TextField();
tf.text ="jkg"
tf.textColor = chatData.user;
listChat.addChild(tf);
Here my chatData.user is 16777215 this format.But i need chatData.user (0xFF0000) this format.how do i convert the color code?
Upvotes: 0
Views: 7479
Reputation: 25490
Easy. Just parse the string into a uint.
var txtColor:uint=uint(chatData.user);
tf.textColor=txtColor;
Upvotes: 0
Reputation: 18546
It is very unclear what exactly your problem is/what you are trying to do.
But first, the hex representation of the decimal number 1677721510 is FFFFFF16. Which is white... Not red (which would be FF000016)
A uint
is just a unsigned interger. It can be represented bases.
1610 = 208 = 100002 = 1016 = G17
Those are all the same integer, just different ways of showing them, but to most programming languages its the same thing. In flash you represent literal hexidecimal numbers using the 0x
notation. In ActionScript's cousin JavaScript, you can create a octal literal by prepending it with a zero (that seriously throws some people off some times 010==8
)
But back to what you are asking. I tested it out real quick, and the textColor
setter will coalesce a string into uint if you give it a string.
var tf:TextField = new TextField();
tf.text = "Test";
var color;
color = 0xFF0000; // Red
color = 16711680; // Red
color = "16711680"; // Red
color = "0xFF0000"; // Red
tf.textColor = color;
this.addChild(tf);
Those are all acceptable "reds", but really .textColor
wants a uint
, so the first two are the most valid. But 0xFF0000 == 16711680
. They aren't 'different' numbers... They are the same number, just different ways of expressing them.
So, I don't understand what your problem is? Your code should work, and do exactly what you've told it to do...
Upvotes: 0
Reputation: 599
Check out the global function: parseInt(String[, radix])
It treats strings starting with 0x as hexadecimal ones. If there is no 0x; you can specify 16 for the radix parameter.
tf.textColor = parseInt(chatData.user);
tf.textColor = parseInt(chatData.user, 16);
Upvotes: 1