

red.prototype.color = {
			
	/* red.color.to_rgb("rgb(255,255,255)"),[255,255,255] oder "#cccccc" oder farbname*/
	
	to_rgb : function(color){
		if(color.charAt(0) == '#'){
			if(color.length == 4){
				color_tmp = color.charAt(1)	+color.charAt(2)+color.charAt(3);
				color_tmp += color_tmp;
				color = '#'+color_tmp;
				return red.prototype.color.hex2rgb(color);
			}
			else if(color.length == 7){
				return red.prototype.color.hex2rgb(color);
			}
			else{
				return false;
			}
		}
		else if (red.prototype.is_string(color)) {
			var result = color.match(/^\s*rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*/);
			if (result == null) { 
				var farbname = red.prototype.array.in_array(red.prototype.color.farbarray,color,'farbe');
													
				if(farbname){
					var hex = farbname['hex'];
					return red.prototype.color.hex2rgb(hex);
				}
				else{				
					return false;
				}
			}
			else{
				return [result[1],result[2],result[3]];
			}
		}
		else if (red.prototype.is_array(color)) {
			var result = [color[0],color[1],color[2]];
		}
		else{
			return false;
		}
	},
				
	rgb2hex : function ( value ) {
		var rgb = +value[0] << 16 | +value[1] << 8 | +value[2];
		var hex = "";
		var digits = "0123456789abcdef";
		while (rgb != 0) { 
			hex = digits.charAt(rgb&0xf) + hex; 
			rgb >>>= 4; 
		} 
		while (hex.length < 6) { 
			hex = '0' + hex; 
		}
		return "#" + hex;
	},

	hex2rgb : function ( hex ) {
		R = hexToR("#FFFFFF");
		G = hexToG("#FFFFFF");
		B = hexToB("#FFFFFF");
		function removeHexMark(hex) { return (hex.charAt(0)=="#") ? hex.substring(1,7) : hex}
		function hexToR(hex) { return parseInt((removeHexMark(hex)).substring(0,2),16) }
		function hexToG(hex) { return parseInt((removeHexMark(hex)).substring(2,4),16) }
		function hexToB(hex) { return parseInt((removeHexMark(hex)).substring(4,6),16) }
		return rgb = [hexToR(hex) ,hexToG(hex),hexToB(hex)];
	},
			
	rgb2hsb : function (v) {
		var max = Math.max(Math.max(v[0], v[1]), v[2]);
		var min = Math.min(Math.min(v[0], v[1]), v[2]);
		var brightness = Math.round(max*20/51);
		if (min == max) return new Array(0, 0, brightness);
		var saturation = Math.round((1 - min/max)*100);
		var d = max - min;
		var hue = Math.round((max == v[0] ? 6 + (v[1] - v[2])/d :
							max == v[1] ? 2 + (v[2] - v[0])/d :
							4 + (v[0] - v[1])/d
							)*60) % 360;
		return new Array(hue, saturation, brightness);
	},
	
	/*
	// using integer ranges:
	//   hue: 0 .. 360
	//   saturation, brightness: 0 .. 100
	*/

	hsb2rgb : function (v) {
		var max = Math.round(v[2]*51/20);
		var min = Math.round(max*(1 - v[1]/100));
		if (min == max) return new Array(max, max, max);
		var d = max - min;
		var h6 = v[0]/60;
		if (h6 <= 1) return new Array(max, Math.round(min + h6*d), min);
		if (h6 <= 2) return new Array(Math.round(min - (h6 - 2)*d), max, min);
		if (h6 <= 3) return new Array(min, max, Math.round(min + (h6 - 2)*d));
		if (h6 <= 4) return new Array(min, Math.round(min - (h6 - 4)*d), max);
		if (h6 <= 5) return new Array(Math.round(min + (h6 - 4)*d), min, max);
		return new Array(max, min, Math.round(min - (h6 - 6)*d));
	},
	
	hex2hsb : function(hex){
		var rgb = red.prototype.color.hex2rgb(hex);
		return red.prototype.color.rgb2hsb(rgb);
	},
	
	hsb2hex : function (hsb){
		var rgb = red.prototype.color.hsb2rgb(hsb);
		return red.prototype.color.rgb2hex(rgb);
	},
			
	merge : function (a, b, f) {
		if (f == 0) return b;
		if (a == b) return a;
		return Math.round(a + f/(f + 1)*(b - a));
	},

	mergecolors : function (h1, h2, f) {
		if (f == 0) return h2;
		var c1 = red.prototype.color.hex2rgb(h1);
		var c2 = red.prototype.color.hex2rgb(h2);
		return red.prototype.color.rgb2hex(new Array(
		red.prototype.color.merge(c1[0], c2[0], f),	red.prototype.color.merge(c1[1], c2[1], f), red.prototype.color.merge(c1[2], c2[2], f)));
	},
		
	invertcolor : function (v) {
		v = red.prototype.color.to_rgb(v);		
		return red.prototype.color.rgb2hex([255 - v[0], 255 - v[1], 255 - v[2]]);
	},
	
	randcolor : function(hue,sat,bri){
		if(hue == null){
			hue = red.prototype.randint(0,360);
		}	
		if(sat == null){
			sat	= red.prototype.randint(0,100);
		}
		if(bri == null){
			bri	= red.prototype.randint(0,100);
		}
		
		var hex = r.color.hsb2hex([hue,sat,bri])
		return hex;
	},	
	
	/*color: hex o. rgb, schritte : int, grad : 0-360, richtung : 'l' o. 'r' */
	
	farbkreis : function (color,schritte,grad,richtung) {
		if(color==null){
			color = '#ff0000';
		}
		if(schritte==null){
			schritte = 5;
		}
		if(grad==null){
			grad = 360;
		}
		if(richtung==null){
			richtung = 'r';
		}
		var rgb = red.prototype.color.to_rgb(color);
		var hsb = red.prototype.color.rgb2hsb(rgb);
		var farbarray = new Array();
		var starthue = hsb[0];
		var hueweite = grad/(schritte);
		farbarray[farbarray.length] = red.prototype.color.rgb2hex(rgb);
		for(var i=1;i<schritte;i++){
			if(richtung == 'r'){
				var huewert = starthue+(hueweite*i);
			}
			else if(richtung == 'l'){
				var huewert = starthue-(hueweite*i);
			}
			if(huewert > 360){
				huewert = huewert-360;
			}
			else if(huewert < 0){
				huewert = huewert+360;
			}
			farbarray[farbarray.length] = red.prototype.color.hsb2hex([huewert,hsb[1],hsb[2]]);
		}
		return farbarray;
	},
	
	/* typ : 'hsb' oder 'rgb' */
	/* nur 2 farben : von, bis*/
	
	farbverlauf1 : function (von,bis,schritte,typ){
		schritte = schritte-1;
		if(typ == null){typ = 'rgb';}
				
		var vonrgb = red.prototype.color.to_rgb(von);
		var bisrgb = red.prototype.color.to_rgb(bis);
		var verlaufsarray = new Array();
		
		if(typ == 'hsb'){
			var vonhsb = red.prototype.color.rgb2hsb(vonrgb);
			var bishsb = red.prototype.color.rgb2hsb(bisrgb);
			var vonhue = vonhsb[0];
			var vonsat = vonhsb[1];
			var vonbri = vonhsb[2];
			var bishue = bishsb[0];
			var bissat = bishsb[1];
			var bisbri = bishsb[2];
		}
					
		for(var i=0;i<=schritte;i++){
			if(typ == 'hsb'){
				var hue = red.prototype.util.fromto(vonhue,bishue,schritte,i,360);
				var sat = red.prototype.util.fromto(vonsat,bissat,schritte,i);
				var bri = red.prototype.util.fromto(vonbri,bisbri,schritte,i);
				verlaufsarray[verlaufsarray.length] = red.prototype.color.hsb2hex([hue,sat,bri]);
			}
			else if(typ == 'rgb'){
				var r = red.prototype.util.fromto(vonrgb[0],bisrgb[0],schritte,i);
				var g = red.prototype.util.fromto(vonrgb[1],bisrgb[1],schritte,i);
				var b = red.prototype.util.fromto(vonrgb[2],bisrgb[2],schritte,i);
				verlaufsarray[verlaufsarray.length] = red.prototype.color.rgb2hex([r,g,b]);
			}
						
		}
					
		return verlaufsarray;
	},
	
	/* typ : 'hsb' oder 'rgb' */
	/* beliebig viele farben als array reingeben*/
	
	farbverlauf : function (farbarray,schritte,typ){
		if(typ == null){typ = 'rgb';}
		var schritt_pro_farbe = Math.floor(schritte/(farbarray.length-1));
		var rest = (schritte%schritt_pro_farbe);
		var verlaufsarray = new Array();
				
		for(var i=0;i<farbarray.length;i++){
			if(farbarray[i+1]){
				if(i==farbarray.length-2){schritt_pro_farbe=schritt_pro_farbe+rest;}
				var thisverlauf = red.prototype.color.farbverlauf1(farbarray[i],farbarray[i+1],schritt_pro_farbe);
				verlaufsarray = red.prototype.array.push(thisverlauf,verlaufsarray,true);
			}
		
		}
		return verlaufsarray;
	},
	
	farbarray : [
				{farbe : 'aliceblue', hex : '#f0f8ff'},
				{farbe : 'antiquewhite', hex : '#faebd7'},
				{farbe : 'aqua', hex : '#00ffff'},
				{farbe : 'aquamarine', hex : '#7fffd4'},
				{farbe : 'azure', hex : '#f0ffff'},
				{farbe : 'beige', hex : '#f5f5dc'},
				{farbe : 'bisque', hex : '#ffe4c4'},
				{farbe : 'blanchedalmond', hex : '#ffebcd'},
				{farbe : 'blue', hex : '#0000ff'},
				{farbe : 'blueviolet', hex : '#8a2be2'},
				{farbe : 'brown', hex : '#a52a2a'},
				{farbe : 'burlywood', hex : '#deb887'},
				{farbe : 'cadetblue', hex : '#5f9ea0'},
				{farbe : 'chartreuse', hex : '#7fff00'},
				{farbe : 'chocolate', hex : '#d2691e'},
				{farbe : 'coral', hex : '#ff7f50'},
				{farbe : 'cornflowerblue', hex : '#6495ed'},
				{farbe : 'cornsilk', hex : '#fff8dc'},
				{farbe : 'crimson', hex : '#dc143c'},
				{farbe : 'cyan', hex : '#00ffff'},
				{farbe : 'darkblue', hex : '#00008b'},
				{farbe : 'darkcyan', hex : '#008b8b'},
				{farbe : 'darkgoldenrod', hex : '#b8860b'},
				{farbe : 'darkgreen', hex : '#006400'},
				{farbe : 'darkkhaki', hex : '#bdb76b'},
				{farbe : 'darkmagenta', hex : '#8b008b'},
				{farbe : 'darkolivegreen', hex : '#556B2F'},
				{farbe : 'darkorange', hex : '#ff8c00'},
				{farbe : 'darkorchid', hex : '#9932cc'},
				{farbe : 'darkred', hex : '#8b0000'},
				{farbe : 'darksalmon', hex : '#e9967a'},
				{farbe : 'darkseagreen', hex : '#8fbc8f'},
				{farbe : 'darkslateblue', hex : '#483d8b'},
				{farbe : 'darkslategray', hex : '#2f4f4f'},
				{farbe : 'darkturquoise', hex : '#00ced1'},
				{farbe : 'darkviolet', hex : '#9400d3'},
				{farbe : 'deeppink', hex : '#ff1493'},
				{farbe : 'deepskyblue', hex : '#00bfff'},
				{farbe : 'dodgerblue', hex : '#1e90ff'},
				{farbe : 'firebrick', hex : '#b22222'},
				{farbe : 'floralwhite', hex : '#fffaf0'},
				{farbe : 'forestgreen', hex : '#228b22'},
				{farbe : 'fuchsia', hex : '#ff00ff'},
				{farbe : 'ghostwhite', hex : '#f8f8ff'},
				{farbe : 'gold', hex : '#ffd700'},
				{farbe : 'goldenrod', hex : '#daa520'},
				{farbe : 'green', hex : '#008000'},
				{farbe : 'greenyellow', hex : '#adff2f'},
				{farbe : 'honeydew', hex : '#f0fff0'},
				{farbe : 'hotpink', hex : '#ff69b4'},
				{farbe : 'indianred', hex : '#cd5c5c'},
				{farbe : 'indigo', hex : '#4b0082'},
				{farbe : 'ivory', hex : '#fffff0'},
				{farbe : 'khaki', hex : '#f0e68c'},
				{farbe : 'lavender', hex : '#e6e6fa'},
				{farbe : 'lavenderblush', hex : '#fff0f5'},
				{farbe : 'lawngreen', hex : '#7cfc00'},
				{farbe : 'lemonchiffon', hex : '#fffacd'},
				{farbe : 'lightblue', hex : '#add8e6'},
				{farbe : 'lightcoral', hex : '#f08080'},
				{farbe : 'lightcyan', hex : '#e0ffff'},
				{farbe : 'lightgoldenrodyellow', hex : '#fafad2'},
				{farbe : 'lightgreen', hex : '#90ee90'},
				{farbe : 'lightpink', hex : '#ffb6c1'},
				{farbe : 'lightsalmon', hex : '#ffa07a'},
				{farbe : 'lightseagreen', hex : '#20b2aa'},
				{farbe : 'lightskyblue', hex : '#87cefa'},
				{farbe : 'lightslategray', hex : '#778899'},
				{farbe : 'lightsteelblue', hex : '#b0c4de'},
				{farbe : 'lightyellow', hex : '#ffffe0'},
				{farbe : 'lime', hex : '#00ff00'},
				{farbe : 'limegreen', hex : '#32cd32'},
				{farbe : 'linen', hex : '#faf0e6'},
				{farbe : 'magenta', hex : '#ff00ff'},
				{farbe : 'maroon', hex : '#800000'},
				{farbe : 'mediumaquamarine', hex : '#66cdaa'},
				{farbe : 'mediumblue', hex : '#0000cd'},
				{farbe : 'mediumorchid', hex : '#ba55d3'},
				{farbe : 'mediumpurple', hex : '#9370db'},
				{farbe : 'mediumseagreen', hex : '#3cb371'},
				{farbe : 'mediumslateblue', hex : '#7b68ee'},
				{farbe : 'mediumspringgreen', hex : '#00fa9a'},
				{farbe : 'mediumturquoise', hex : '#48d1cc'},
				{farbe : 'mediumvioletred', hex : '#c71585'},
				{farbe : 'midnightblue', hex : '#191970'},
				{farbe : 'mintcream', hex : '#f5fffa'},
				{farbe : 'mistyrose', hex : '#ffe4e1'},
				{farbe : 'moccasin', hex : '#ffe4b5'},
				{farbe : 'navajowhite', hex : '#ffdead'},
				{farbe : 'navy', hex : '#000080'},
				{farbe : 'oldlace', hex : '#fdf5e6'},
				{farbe : 'olive', hex : '#808000'},
				{farbe : 'olivedrab', hex : '#6b8e23'},
				{farbe : 'orange', hex : '#ffa500'},
				{farbe : 'orangered', hex : '#ff4500'},
				{farbe : 'orchid', hex : '#da70d6'},
				{farbe : 'palegoldenrod', hex : '#eee8aa'},
				{farbe : 'palegreen', hex : '#98fb98'},
				{farbe : 'paleturquoise', hex : '#afeeee'},
				{farbe : 'palevioletred', hex : '#db7093'},
				{farbe : 'papayawhip', hex : '#ffefd5'},
				{farbe : 'peachpuff', hex : '#ffdab9'},
				{farbe : 'peru', hex : '#cd853f'},
				{farbe : 'pink', hex : '#ffc0cb'},
				{farbe : 'plum', hex : '#dda0dd'},
				{farbe : 'powderblue', hex : '#b0e0e6'},
				{farbe : 'purple', hex : '#800080'},
				{farbe : 'red', hex : '#ff0000'},
				{farbe : 'rosybrown', hex : '#bc8f8f'},
				{farbe : 'royalblue', hex : '#4169e1'},
				{farbe : 'saddlebrown', hex : '#8b4513'},
				{farbe : 'salmon', hex : '#fa8072'},
				{farbe : 'sandybrown', hex : '#f4a460'},
				{farbe : 'seagreen', hex : '#2e8b57'},
				{farbe : 'seashell', hex : '#fff5ee'},
				{farbe : 'sienna', hex : '#a0522d'},
				{farbe : 'skyblue', hex : '#87ceeb'},
				{farbe : 'slateblue', hex : '#6a5acd'},
				{farbe : 'slategray', hex : '#708090'},
				{farbe : 'snow', hex : '#fffafa'},
				{farbe : 'springgreen', hex : '#00ff7f'},
				{farbe : 'steelblue', hex : '#4682b4'},
				{farbe : 'tan', hex : '#d2b48c'},
				{farbe : 'teal', hex : '#008080'},
				{farbe : 'thistle', hex : '#d8bfd8'},
				{farbe : 'tomato', hex : '#ff6347'},
				{farbe : 'turquoise', hex : '#40e0d0'},
				{farbe : 'violet', hex : '#ee82ee'},
				{farbe : 'wheat', hex : '#f5deb3'},
				{farbe : 'whitesmoke', hex : '#f5f5f5'},
				{farbe : 'yellow', hex : '#ffff00'},
				{farbe : 'yellowgreen', hex : '#9acd32'},
				{farbe : 'white', hex : '#ffffff'},
				{farbe : 'gainsboro', hex : '#dcdcdc'},
				{farbe : 'lightgrey', hex : '#d3d3d3'},
				{farbe : 'silver', hex : '#c0c0c0'},
				{farbe : 'darkgray', hex : '#a9a9a9'},
				{farbe : 'gray', hex : '#808080'},
				{farbe : 'dimgray', hex : '#696969'},
				{farbe : 'black', hex : '#000000'}
				]
	
	

};