开发者

Flash AS3 extract numbers from String (regexp?)

开发者 https://www.devze.com 2022-12-21 19:31 出处:网络
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

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 p开发者_如何转开发ageColor string and use them in the rgb2hex function..


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.


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])));
0

精彩评论

暂无评论...
验证码 换一张
取 消