using UnityEngine;
using System.Collections;

public static class Color32Extensions 
{
	static public string ToString2(this Color32 value)
	{
		return string.Format("({0}, {1}, {2}, {3})", value.r, value.g, value.b, value.a);
	}
	
	static public bool TryParse(string toParse, out Color32 parsed)
	{
		parsed = default(Color32);
		
		if(!toParse.StartsWith("(") || !toParse.EndsWith(")"))
		{
			return false;
		}
		var trimmedToken = toParse.Substring(1, toParse.Length - 2);
		
		var vectorComponentTokens = trimmedToken.Split(',');
		if(vectorComponentTokens.Length != 3 && vectorComponentTokens.Length != 4)
		{
			return false;
		}
		
		byte r = 0;
		byte g = 0;
		byte b = 0;
		byte a = 255;
		bool success = true;
		success &= byte.TryParse(vectorComponentTokens[0], out r);
		success &= byte.TryParse(vectorComponentTokens[1], out g);
		success &= byte.TryParse(vectorComponentTokens[2], out b);
		if(vectorComponentTokens.Length > 3)
		{
			success &= byte.TryParse(vectorComponentTokens[3], out a);
		}
		if(!success)
		{
			return false;
		}
		
		parsed = new Color32(r, g, b, a);
		return true;
	}
	
}
