views:

332

answers:

2

I am sending a var to Flash:

// incoming
var pageColor:String = "rgb(81, 89, 112)";

I have this function to covert the RGB values to a HEX

function rgb2hex(r:Number, g:Number, b:Number) {
    return '0x'+(r << 16 | g << 8 | b).toString(16).toUpperCase();
}
// trace(rgb2hex(81, 89, 112));

Now I am looking for the best way to extract the numbers from the pageColor String and use them in the rgb2hex function..

+2  A: 
pageColor = pageColor.substring(4, pageColor.length - 1); // '81, 89, 112'
var colors:Array = pageColor.split(",");
rgb2hex(parseInt(colors[0]), parseInt(colors[1]), parseInt(colors[2]));

As an aside, you should change the arguments to your rbg2hex function to take int or uint instead of number.

Cory Petosky
+1. Bit shifts are much faster on int and uint.
sberry2A
Thanks Cory, the only thing I needed to change for AS3 compatibility was:var colors:Array = pageColor.split(",");
FFish
Oh, good call. Edited that in for posterity.
Cory Petosky
A: 
var pageColor:String = "rgb(81, 89, 112)";

pageColor = pageColor.substring(4, pageColor.length - 1);
var colors:Array = pageColor.split(",");

function rgb2hex(r:int, g:int, b:int) {
    return '0x'+(r << 16 | g << 8 | b).toString(16).toUpperCase();
}
trace(rgb2hex(parseInt(colors[0]), parseInt(colors[1]), parseInt(colors[2])));
FFish