## Convert Hexidecimal To Rgb

• 1,503 Views
• 2 Replies
matrix5565
matrix5565
• Member since: Feb. 28, 2006
• Offline.
Forum Stats
Member
Level 16
Game Developer
Convert Hexidecimal To Rgb 2009-07-05 23:56:59

In a game I'm making right now, I found it necessary to convert hex to RGB for color changing, so I created the following function and share it with you guys.

``````function hexToRGB(hex:*):Object
{
if (hex is Number) hex.toString(16).toLowerCase();
else if (hex is String) hex.toLowerCase();
else throw new TypeError("Expected Number or String);

var red:Number = toNum(hex.charAt(0))*16+toNum(hex.charAt(1));
var green:Number = toNum(hex.charAt(2))*16+toNum(hex.charAt(3));
var blue:Number = toNum(hex.charAt(4))*16+toNum(hex.charAt(5));

var RGB:Object = new Object;
RGB.red = red;
RGB.green = green;
RGB.blue = blue;
return RGB;
function toNum(letter:String):Number
{
switch (letter)
{
case "a" :
return 10;
break;
case "b" :
return 11;
break;
case "c" :
return 12;
break;
case "d" :
return 13;
break;
case "e" :
return 14;
break;
case "f" :
return 15;
break;
default :
return Number(letter);
break;
}
}
}``````

Hope you all find it useful.

johnfn
johnfn
• Member since: Aug. 16, 2003
• Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to Convert Hexidecimal To Rgb 2009-07-06 00:35:25

Ah, very nice. There needs to be a place to upload good AS3 code like this.

Anyway, hardcore code never gets enough appreciation in the forums, so here you are.

Good artists copy. Great artists get banned from the Audio Portal.

matrix5565
matrix5565
• Member since: Feb. 28, 2006
• Offline.
Forum Stats
Member
Level 16
Game Developer
Response to Convert Hexidecimal To Rgb 2009-07-06 01:33:18

At 7/6/09 12:35 AM, johnfn wrote: Ah, very nice. There needs to be a place to upload good AS3 code like this.

Anyway, hardcore code never gets enough appreciation in the forums, so here you are.

Thanks, I was afraid this was going to fade away without so much as a single post.