
package com.netgrind.core {
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.utils.*;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author Cale Bradbury(@netgrind)
	 */
	public class UtilsArt {
		
		public static function interpolateColors(c1:uint, c2:uint, amount:Number):uint {
			if (amount < 0) amount *= -1;
			var r1:* = HEXtoRGB(c1);
			var r2:* = HEXtoRGB(c2);
			r1.r *= amount;
			r1.g *= amount;
			r1.b *= amount;
			r1.r += r2.r * (1 - amount);
			r1.g += r2.g * (1 - amount);
			r1.b += r2.b * (1 - amount);
			
			return RGBtoHEX(r1 );
		}
		
		public static function addAlpha(color:uint, alpha:Number):uint {
			return (0xff / alpha) << 24 | color;
		}
		
		public static function makeCircle(diameter:Number=100, color:uint = 0xff0000, x:Number=0,y:Number=0):Sprite {
			var mc:Sprite = new Sprite();
			mc.graphics.beginFill(color);
			mc.graphics.drawCircle(0, 0, diameter);
			mc.graphics.endFill();
			mc.x = x;
			mc.y = y;
			return mc;
		}
		
		public static function makeRectangle(width:Number = 100,height:Number = 100, color:uint = 0xff0000, alpha:Number = 1, x:Number = 0, y:Number = 0):Sprite {
			var mc:Sprite = new Sprite();
			mc.graphics.beginFill(color, alpha);
			mc.graphics.drawRect(x,y, width, height);
			mc.graphics.endFill();
			return mc;
		}//end makeRectangle
	
		/*public static function makeButton(func:Function, text:String='click', color:uint = 0x0000ff, textColor:uint = 0x00ff00, width:int=0, height:int = 0, hoverTransform:ColorTransform = null):Sprite {				
			var textBox:Text = new Text();
			textBox.defaultColor = addAlpha(textColor,1);
			textBox.text = text;
			if (width == 0) {
				width = textBox.width + 10;
			}
			if (height == 0) {
				height = textBox.height + 10;
			}
			var bg:Sprite = makeRectangle(width, height, color);
			bg.addChild(textBox);
			textBox.x = bg.width * .5 - textBox.width * .5;
			textBox.y = bg.height * .5 - textBox.height * .5;
			if (hoverTransform){
				bg.addEventListener(MouseEvent.MOUSE_OVER, function():void {
					bg.transform.colorTransform = hoverTransform;
				});
				bg.addEventListener(MouseEvent.MOUSE_OUT, function():void {
					bg.transform.colorTransform = new ColorTransform();
				});
			}
			bg.addEventListener(MouseEvent.CLICK, function():void {
				func();
			});
			return bg;
		}*/
		
		public static function HSBtoRGB(h:int, s:int, b:int):Object
		{
			var rgb:Object = new Object();
			var max:Number = (b * 0.01) * 255;
			var min:Number = max * (1 - (s * 0.01));
			h %= 360;
			while (h <0) {
				h +=360;
			}
			
		 
			if (s == 0) {
				rgb.r = rgb.g = rgb.b = b*(255*0.01) ;
			} else {
				var _h:Number = Math.floor(h / 60);
		 
				switch(_h){
					case 0:
						rgb.r = max	;
						rgb.g = min+h * (max-min)/ 60;
						rgb.b = min;
						break;
					case 1:
						rgb.r = max-(h-60) * (max-min)/60;
						rgb.g = max;
						rgb.b = min;
						break;
					case 2:
						rgb.r = min ;
						rgb.g = max;
						rgb.b = min+(h-120) * (max-min)/60;
						break;
					case 3:
						rgb.r = min;
						rgb.g = max-(h-180) * (max-min)/60;
						rgb.b =max;
						break;
					case 4:
						rgb.r = min+(h-240) * (max-min)/60;
						rgb.g = min;
						rgb.b = max;
						break;
					case 5:
						rgb.r = max;
						rgb.g = min;
						rgb.b = max-(h-300) * (max-min)/60;
						break;
					case 6:
						rgb.r = max;
						rgb.g = min+h  * (max-min)/ 60;
						rgb.b = min;
						break;
				}
		 
				rgb.r = Math.min(255, Math.max(0, Math.round(rgb.r)))
				rgb.g = Math.min(255, Math.max(0, Math.round(rgb.g)))
				rgb.b = Math.min(255, Math.max(0, Math.round(rgb.b)))
			}
			return rgb;
		}
		
		public static function HSLtoRGB(h:Number, s:Number, l:Number):Array{
			h = h / 360;
			var r:Number;
			var g:Number;
			var b:Number;
 
			if(l==0)
			{
				r=g=b=0;
			}
			else
			{
				if(s == 0)
				{
					r=g=b=l;
				}
				else
				{
					var t2:Number = (l<=0.5)? l*(1+s):l+s-(l*s);
					var t1:Number = 2*l-t2;
					var t3:Vector.<Number> = new Vector.<Number>();
					t3.push(h+1/3);
					t3.push(h);
					t3.push(h-1/3);
					var clr:Vector.<Number> = new Vector.<Number>();
					clr.push(0);
					clr.push(0);
					clr.push(0);
					for(var i:int=0;i<3;i++)
					{
						if(t3[i]<0)
							t3[i]+=1;
						if(t3[i]>1)
							t3[i]-=1;
 
						if(6*t3[i] < 1)
							clr[i]=t1+(t2-t1)*t3[i]*6;
						else if(2*t3[i]<1)
							clr[i]=t2;
						else if(3*t3[i]<2)
							clr[i]=(t1+(t2-t1)*((2/3)-t3[i])*6);
						else
							clr[i]=t1;
					}
					r=clr[0];
					g=clr[1];
					b=clr[2];
				}
			}
			return new Array(int(r*255),int(g*255),int(b*255));
		}//edn hsltorgb
		
		public static function HSLtoHEX(hsl:Object):uint {
			return RGBtoHEX(HSLtoRGB(hsl.h,hsl.s,hsl.l));
		}//end hsltohex
		
		public static function RGBtoHEX(rgb:Object):uint {
			 return rgb.r << 16 | rgb.g << 8 | rgb.b;
		}//end rgbToHex
		
		public static function HEXtoRGB(hex:Number):Object{
			var rgbObj:Object = {
				r: ((hex & 0xFF0000) >> 16),
				g: ((hex & 0x00FF00) >> 8),
				b: ((hex & 0x0000FF))
			};		 
			return rgbObj;
		}//end HEXToRGB
		
		public static function RGBtoHSB(rgb:Object):Object {		
			var hsb:Object = new Object;
			var _max:Number = Math.max(rgb.r,rgb.g, rgb.b);
			var _min:Number = Math.min(rgb.r,rgb.g,rgb.b);
			hsb.s = (_max != 0) ? (_max - _min) / _max * 100: 0;
			hsb.b = _max / 255 * 100;
		 
			if(hsb.s == 0){
				hsb.h = 0;
			}else{
				switch(_max)
				{
					case rgb.r:
						hsb.h = (rgb.g - rgb.b)/(_max - _min)*60 + 0;
						break;
					case rgb.g:
						hsb.h = (rgb.b - rgb.r)/(_max - _min)*60 + 120;
						break;
					case rgb.b:
						hsb.h = (rgb.r - rgb.g)/(_max - _min)*60 + 240;
						break;
				}
			}
		 
			hsb.h = Math.min(360, Math.max(0, Math.round(hsb.h)))
			hsb.s = Math.min(100, Math.max(0, Math.round(hsb.s)))
			hsb.b = Math.min(100, Math.max(0, Math.round(hsb.b)))
		 
			return hsb;
		}//edn rgbtohsb		
		
		public static function createRandomColorFromRGB(rMin:int=0x00, rMax:int = 0xFF, gMin:int=0x00, gMax:int = 0xFF, bMin:int=0x00, bMax:int = 0xFF):uint {
			return int(Math.random()*(rMax-rMin)+rMin) << 16 | int(Math.random()*(bMax-bMin)+bMin) << 8 | int(Math.random()*(bMax-bMin)+bMin)
		}
		
		public static function createRandomColorFromHSB(hMin:int = 0, hMax:int = 360, sMin:int=0, sMax:int =255, bMin:int = 0, bMax:int = 255):uint {
			return RGBtoHEX(HSBtoRGB(Math.random()*(hMax-hMin)+hMin, Math.random()*(sMax-sMin)+sMin, Math.random()*(bMax-bMin)+bMin));
		}
		
		static public function HSBtoHEX(h:int, s:int, b:int):uint {
			return RGBtoHEX(HSBtoRGB(h, s, b));
		}
		
		static public function makeGrid(width:int, height:int, columns:int, rows:int, color:uint=0x888888, thickness:int=2):Sprite {
			var s:Sprite = new Sprite();
			s.graphics.lineStyle(thickness, color);
			
			var w:Number = width / columns;
			var h:Number = height / rows;
			
			for (var i:int = 0; i <= columns; i++) {
				s.graphics.moveTo(i * w, 0);
				s.graphics.lineTo(i * w, height);
			}
			for (var j:int = 0; j <= rows; j++) {
				s.graphics.moveTo(0, j * h);
				s.graphics.lineTo(width, j * h);
			}
			return s;
		}
		
		
	}	
}