context: using UnityEngine;
  using System.Reflection;
  using System.Collections;
  using System.Collections.Generic;
  using Frontiers;
  using System;
  
  namespace Frontiers
  {
  	public class Colors : Manager
  	{
  		//used mostly to manipulate colors on the fly
  		//will also store / load color schemes
  		public static Colors Get;
  		public static PathColorManager PathColors;
  		public static BannerColorManager BannerColors;
  		public FontColorManager FontColors;
  		public List <FlagsetColor> FlagsetColors = new List <FlagsetColor>();
  		public Texture2D [] CameraLUTs;
  
  		[HideInInspector]
  		public string [] ColorNames {
  			get {
  				if (mColorNames == null) {
  					FindColorNames();
  				}
  				return mColorNames;
  			}
  		}
  
  		[HideInInspector]
  		protected string[] mColorNames = null;
  
  		public bool CameraLUT (ref Texture2D cameraLut, string lutName) {
  			for (int i = 0; i < CameraLUTs.Length; i++) {
  				if (CameraLUTs [i].name.Equals (lutName)) {
  					cameraLut = CameraLUTs [i];
  					return true;
  				}
  			}
  			return false;
  		}
  
  		public void SaveColors()
  		{		//find fields
  			FieldInfo[] fields = this.GetType().GetFields();
  			SDictionary <string, SColor> saveData = new SDictionary <string, SColor>();
  			foreach (FieldInfo field in fields) {
  				if (field.FieldType == typeof(Color)) {	//TODO find best way to clean fields
  					string colorName = field.Name;
  					Color color = (Color)field.GetValue(this);
  					mStandardColorLookup.Add(colorName, color);
  				}
  			}
  		}
  
  		public void LoadColors()
  		{
  
  		}
  
  		public void PushInterfaceColors(IEnumerable<ColorKey> interfaceColors)
  		{
  			if (interfaceColors == null) {
  				return;
  			}
  			Type type = typeof(Colors);
  			foreach (ColorKey key in interfaceColors) {
  				FieldInfo field = type.GetField(key.Name);
  				if (field != null && field.FieldType == typeof(Color)) {
  					field.SetValue(this, key.color);
  				} else {
  					Debug.Log("Couldn't set field " + key.Name + ", was null or not type of color");
  				}
  			}
  
  			//get all label and button setup components
  			//set them all to the new colors
  			UILabel[] labels = GameObject.FindObjectsOfType <UILabel>();
  			UITiledSprite[] bgSprites = GameObject.FindObjectsOfType <UITiledSprite>();
  			UISlicedSprite[] borderSprites = GameObject.FindObjectsOfType <UISlicedSprite>();
  			GUI.GUIButtonSetup[] buttonSetup = GameObject.FindObjectsOfType <GUI.GUIButtonSetup>();
  			GUI.GUISliderSetColors[] sliderColors = GameObject.FindObjectsOfType <GUI.GUISliderSetColors>();
  			GUI.GUIPopupSetup[] popupSetup = GameObject.FindObjectsOfType <GUI.GUIPopupSetup>();
  
  			for (int i = 0; i < labels.Length; i++) {
  				if (labels[i].useDefaultLabelColor) {
  					labels[i].color = Colors.Alpha(MenuButtonTextColorDefault, labels[i].alpha);
  				}
  			}
  
  			for (int i = 0; i < bgSprites.Length; i++) {
  				if (bgSprites[i].useDefaultBackgroundColor) {
  					bgSprites[i].color = Colors.Alpha(TiledBackgroundColorDefault, bgSprites[i].alpha);
  				}
  			}
  
  			for (int i = 0; i < borderSprites.Length; i++) {
  				if (borderSprites[i].useDefaultBorderSpriteColor) {
  					borderSprites[i].color = Colors.Alpha(WindowBorderColorDefault, borderSprites[i].alpha);
  				}
  			}
  
  			for (int i = 0; i < buttonSetup.Length; i++) {
  				buttonSetup[i].RefreshColors();
  			}
  
  			for (int i = 0; i < sliderColors.Length; i++) {
  				sliderColors[i].RefreshColors();
  			}
  
  			for (int i = 0; i < popupSetup.Length; i++) {
  				popupSetup[i].RefreshColors();
  			}
  		}
  
  		public List <ColorKey> DefaultInterfaceColors()
  		{
  			return mDefaultInterfaceColors;
  		}
  
  		public void FindColorNames()
  		{
  			FieldInfo[] fields = this.GetType().GetFields();
  			List <string> colorNames = new List<string>() { "" };
  			foreach (FieldInfo field in fields) {
  				if (field.FieldType == typeof(Color)) {	//TODO find best way to clean fields
  					colorNames.Add(field.Name);
  				}
  			}
  			foreach (ColorKey colorKey in ColorKeys) {
  				colorNames.Add(colorKey.Name);
  			}
  			colorNames.Sort();
  			colorNames.Insert(0, "[Default]");
  			mColorNames = colorNames.ToArray();
  		}
  
  		public override void WakeUp()
  		{
  			base.WakeUp();
  
  			PathColors = gameObject.GetComponent <PathColorManager>();
  			BannerColors = gameObject.GetComponent <BannerColorManager>();
  
  			if (mStandardColorLookup.Count == 0) {
  				//build the color lookup
  				FieldInfo[] fields = this.GetType().GetFields();
  				foreach (FieldInfo field in fields) {
  					if (field.FieldType == typeof(Color)) {	//TODO find best way to clean fields
  						string colorName = field.Name;
  						Color color = (Color)field.GetValue(this);
  						if (field.IsDefined(typeof(InterfaceColorAttribute), true)) {
  							ColorKey cc = new ColorKey();
  							cc.Name = field.Name;
  							cc.color = color;
  							mDefaultInterfaceColors.Add(cc);
  						}
  						mStandardColorLookup.Add(colorName, color);
  					}
  				}
  
  				foreach (FlagsetColor flagsetColor in FlagsetColors) {
  					Dictionary <int, Color> lookup = null;
  					if (!mColorFromFlagsetLookup.TryGetValue(flagsetColor.Flagset, out lookup)) {
  						lookup = new Dictionary <int, Color>();
  						mColorFromFlagsetLookup.Add(flagsetColor.Flagset, lookup);
  					}
  					lookup.Add(flagsetColor.Flags, flagsetColor.Color);
  				}
  
  				foreach (ColorKey colorKey in ColorKeys) {
  					mStandardColorLookup.Add(colorKey.Name, colorKey.color);
  				}
  			}
  			Get = this;
  		}
  
  		public Color SkillGroupColor(string skillGroup)
  		{
  			Color color = Color.white;
  			mSkillGroupColorLookup.TryGetValue(skillGroup, out color);
  			return color;
  		}
  
  		public Color ColorFromFlagset(int flags, string flagset)
  		{		//TODO replace this we're no longer using it
  			Color color = Color.white;
  			Dictionary <int, Color> lookup = null;
  			if (mColorFromFlagsetLookup.TryGetValue(flagset, out lookup)) {
  				////Debug.Log ("Found flagset lookup " + flagset);
  				if (lookup.TryGetValue(flags, out color)) {
  					////Debug.Log ("Found credentials " + flagset + ", " + flags);
  				}
  			}
  			return color;
  		}
  		#if UNITY_EDITOR
  		public string GetOrCreateColor(Color existingColor, int tolerance, string newColorName)
  		{
  			string closest = string.Empty;
  			int closestSoFar = Int32.MaxValue;
  			foreach (KeyValuePair <string,Color> colorPair in mCustomColorLookup) {
  				int rDiff = (int)Mathf.Abs(existingColor.r - colorPair.Value.r) * 255;
  				int gDiff = (int)Mathf.Abs(existingColor.g - colorPair.Value.g) * 255;
  				int bDiff = (int)Mathf.Abs(existingColor.b - colorPair.Value.b) * 255;
  				if (rDiff < closestSoFar && rDiff <= tolerance
  				        && gDiff < closestSoFar && gDiff <= tolerance
  				        && bDiff < closestSoFar && bDiff <= tolerance) {
  					closestSoFar = Mathf.Max(rDiff, gDiff);
  					closestSoFar = Mathf.Max(closestSoFar, bDiff);
  					closest = colorPair.Key;
  				}
  			}
  
  			if (string.IsNullOrEmpty(closest)) {
  				ColorKey ck = new ColorKey();
  				ck.Name = newColorName;
  				ck.color = existingColor;
  				ColorKeys.Add(ck);
  				UnityEditor.EditorUtility.SetDirty(gameObject);
  				UnityEditor.EditorUtility.SetDirty(this);
  				closest = newColorName;
  			}
  
  			return closest;
  		}
  		#endif
  		public static Color RandomColor()
  		{
  			return new Color(UnityEngine.Random.Range(0.1f, 1f), UnityEngine.Random.Range(0.1f, 1f), UnityEngine.Random.Range(0.1f, 1f));
  		}
  
  		public static Color RandomColor(float alpha)
  		{
  			return new Color(UnityEngine.Random.Range(0.1f, 1f), UnityEngine.Random.Range(0.1f, 1f), UnityEngine.Random.Range(0.1f, 1f), alpha);
  		}
  
  		public static float InverseLuminance(Color result)
  		{
  			return 1.0f - ((result.r / 3) + (result.g / 3) + (result.b / 3));
  		}
  
  		public static int MaxIndexRGBA(Color color)
  		{
  			int index = 0;
  			index = (color.g > color.r) ? 1 : index;
  			index = (color.b > color.g) ? 2 : index;
  			index = (color.a > color.b) ? 3 : index;
  			return index;
  		}
  
  		public static int MaxIndexARGB(Color color)
  		{
  			int index = 0;
  			index = (color.r > color.a) ? 1 : index;
  			index = (color.g > color.r) ? 2 : index;
  			index = (color.b > color.g) ? 3 : index;
  			return index;
  		}
  
  		public static Color ColorFromString(string prefabName, int minValue)
  		{
  
gt: 			System.Random rnd = new System.Random(prefabName.GetHashCode());