summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/ColorTypeConverter.cs
blob: 547adf3be6269ef01e7de11422d0fbfc014967eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Linq;
using System.Reflection;

namespace Xamarin.Forms
{
	public class ColorTypeConverter : TypeConverter
	{
		public override object ConvertFromInvariantString(string value)
		{
			if (value != null)
			{
				if (value.Trim().StartsWith("#", StringComparison.Ordinal))
					return Color.FromHex(value);
				string[] parts = value.Split('.');
				if (parts.Length == 1 || (parts.Length == 2 && parts[0] == "Color"))
				{
					string color = parts[parts.Length - 1];
					switch (color)
					{
						case "Default":
							return Color.Default;
						case "Transparent":
							return Color.Transparent;
						case "Aqua":
							return Color.Aqua;
						case "Black":
							return Color.Black;
						case "Blue":
							return Color.Blue;
						case "Fuchsia":
							return Color.Fuchsia;
						case "Gray":
							return Color.Gray;
						case "Green":
							return Color.Green;
						case "Lime":
							return Color.Lime;
						case "Maroon":
							return Color.Maroon;
						case "Navy":
							return Color.Navy;
						case "Olive":
							return Color.Olive;
						case "Purple":
							return Color.Purple;
						case "Pink":
							return Color.Pink;
						case "Red":
							return Color.Red;
						case "Silver":
							return Color.Silver;
						case "Teal":
							return Color.Teal;
						case "White":
							return Color.White;
						case "Yellow":
							return Color.Yellow;
					}
					FieldInfo field = typeof(Color).GetFields().FirstOrDefault(fi => fi.IsStatic && fi.Name == color);
					if (field != null)
						return (Color)field.GetValue(null);
					PropertyInfo property = typeof(Color).GetProperties().FirstOrDefault(pi => pi.Name == color && pi.CanRead && pi.GetMethod.IsStatic);
					if (property != null)
						return (Color)property.GetValue(null, null);
				}
			}

			throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(Color)));
		}
	}
}