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
|
/// <summary>
/// Returns a <see cref="Color"/> object based on its color name. The 'color name'
/// is of the color from <see cref="Color.KnownColor"/> or a hexadecimal value as
/// returned by <see cref="Color.Name"/> (hex format: 'aarrggbb').
/// <para>
/// This can be very easily used, for example, for XML serialisation
/// where type <see cref="Color"/> can not be used:
/// <pre>
/// [XmlIgnore]
/// public Color MyColor { get; set; }
/// [EditorBrowsable( EditorBrowsableState.Never )]
/// public string MyColorXml
/// {
/// get { return MyColor.Name; }
/// set { MyColor = value.ToColor(); }
/// }
/// </pre>
/// </para>
/// </summary>
/// <returns>A <see cref="Color"/> object.</returns>
public static Color ToColor( this string thisValue )
{
Color color = Color.FromName( thisValue );
if( !color.IsKnownColor )
{
try
{
// try to convert from hex format: aarrggbb
int iResult = Convert.ToInt32( thisValue, 16 );
color = Color.FromArgb( iResult >> 24 & 0xff, iResult >> 16 & 0xff, iResult >> 8 & 0xff, iResult & 0xff );
}
catch { }
}
return color;
}
|