Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ColorConverter.cs
Go to the documentation of this file.
5using System.Linq;
7
8namespace System.Drawing;
9
11{
12 private sealed class ColorComparer : IComparer<Color>
13 {
14 public int Compare(Color left, Color right)
15 {
16 return string.CompareOrdinal(left.Name, right.Name);
17 }
18 }
19
21 {
23 return new StandardValuesCollection(source.OrderBy((Color c) => c, new ColorComparer()).ToList());
24 });
25
26 public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
27 {
28 if (!(sourceType == typeof(string)))
29 {
30 return base.CanConvertFrom(context, sourceType);
31 }
32 return true;
33 }
34
36 {
38 {
39 return base.CanConvertTo(context, destinationType);
40 }
41 return true;
42 }
43
44 public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
45 {
46 if (value is string strValue)
47 {
49 }
50 return base.ConvertFrom(context, culture, value);
51 }
52
53 public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
54 {
55 if (destinationType == null)
56 {
57 throw new ArgumentNullException("destinationType");
58 }
59 if (value is Color color)
60 {
61 if (destinationType == typeof(string))
62 {
63 if (color == Color.Empty)
64 {
65 return string.Empty;
66 }
68 {
69 return color.Name;
70 }
71 if (color.IsNamedColor)
72 {
73 return "'" + color.Name + "'";
74 }
75 if (culture == null)
76 {
78 }
79 string separator = culture.TextInfo.ListSeparator + " ";
81 int num = 0;
82 string[] array;
83 if (color.A < byte.MaxValue)
84 {
85 array = new string[4];
86 array[num++] = converterTrimUnsafe.ConvertToString(context, culture, color.A);
87 }
88 else
89 {
90 array = new string[3];
91 }
92 array[num++] = converterTrimUnsafe.ConvertToString(context, culture, color.R);
93 array[num++] = converterTrimUnsafe.ConvertToString(context, culture, color.G);
94 array[num++] = converterTrimUnsafe.ConvertToString(context, culture, color.B);
95 return string.Join(separator, array);
96 }
98 {
100 object[] arguments = null;
101 if (color.IsEmpty)
102 {
103 memberInfo = typeof(Color).GetField("Empty");
104 }
105 else if (System.Drawing.ColorTable.IsKnownNamedColor(color.Name))
106 {
107 memberInfo = typeof(Color).GetProperty(color.Name) ?? typeof(SystemColors).GetProperty(color.Name);
108 }
109 else if (color.A != byte.MaxValue)
110 {
111 memberInfo = typeof(Color).GetMethod("FromArgb", new Type[4]
112 {
113 typeof(int),
114 typeof(int),
115 typeof(int),
116 typeof(int)
117 });
118 arguments = new object[4] { color.A, color.R, color.G, color.B };
119 }
120 else if (color.IsNamedColor)
121 {
122 memberInfo = typeof(Color).GetMethod("FromName", new Type[1] { typeof(string) });
123 arguments = new object[1] { color.Name };
124 }
125 else
126 {
127 memberInfo = typeof(Color).GetMethod("FromArgb", new Type[3]
128 {
129 typeof(int),
130 typeof(int),
131 typeof(int)
132 });
133 arguments = new object[3] { color.R, color.G, color.B };
134 }
135 if (memberInfo != null)
136 {
138 }
139 return null;
140 }
141 }
142 return base.ConvertTo(context, culture, value, destinationType);
143 }
144
146 {
147 return s_valuesLazy.Value;
148 }
149
151 {
152 return true;
153 }
154}
static TypeConverter GetConverterTrimUnsafe([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type)
static Color ConvertFromString(string strValue, CultureInfo culture)
override? object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
override bool GetStandardValuesSupported(ITypeDescriptorContext? context)
override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
override StandardValuesCollection GetStandardValues(ITypeDescriptorContext? context)
static readonly Lazy< StandardValuesCollection > s_valuesLazy
override? object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
static bool IsKnownNamedColor(string name)
Definition ColorTable.cs:38
static Dictionary< string, Color > Colors
Definition ColorTable.cs:11
static CultureInfo CurrentCulture
static readonly Color Empty
Definition Color.cs:15