00:00
00:00
Newgrounds Background Image Theme

TecNoir just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Convert Hexidecimal To Rgb

2,095 Views | 2 Replies
New Topic Respond to this Topic

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.

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.


Come join music competitions on Chips Compo and hang on our Discord!

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

BBS Signature

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.