Monday, January 20, 2014

Uint Color Converter Utility

This was long overdue to program, as I have this code inlined in many places. Where performance is not critical, it would be good to use this utility.
package JM_LIB.graphics.color 
{
	/**
	 * Basic color conversion that I do far too often. Tired of retyping all the time.
	 * Use this class when you do not require optimized color conversions.
	 * @author 
	 */
	public class UintRGBandBackConverter 
	{
		//Each time a function is called, all of these variables are set to properly correlate
		//to the correct color type.
		public static var color:uint;
		public static var colorNoAlphaChannel:uint;
		public static var alphaAsPercentage:Number;
		public static var a:int;
		public static var r:int;
		public static var g:int;
		public static var b:int;
		
		public function UintRGBandBackConverter() 
		{
			
		}
		
		public static function convertUintColor(inColor:uint):void
		{
			//extract each color:
			a = (inColor >> 24) & 0xFF;
			r = (inColor >> 16) & 0xFF;
			g = (inColor >> 8 ) & 0xFF;
			b = (inColor >> 0 ) & 0xFF;
			
			//So color and colorNoAlpha can be set:
			convertARGBColor(a, r, g, b);
			
		}
		
		/** Returns uint color WITH alpha channel **/
		public static function convertARGBColor(inA:int, inR:int, inG:int, inB:int):uint
		{
			a = inA;
			r = inR;
			g = inG;
			b = inB;
			color = ((a << 24) | (r << 16) | (g << 8) | (b << 0));
			colorNoAlphaChannel = ((0x00 << 24) | (r << 16) | (g << 8) | (b << 0));
			alphaAsPercentage = (a / 255);
			
			return color;
		}//convertARGBColor
		
		
	}//class
}//package

No comments:

Post a Comment