Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The short way to do it in a tweet:</p> <pre><code>(Color)XamlReader.Load(string.Format("&lt;Color xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation …\"&gt;{0}&lt;/Color&gt;", c)); </code></pre> <p>The recommended way is to get <a href="http://nuget.org/packages/winrtxamltoolkit" rel="nofollow">WinRT XAML Toolkit</a> from NuGet and call</p> <pre><code>WinRTXamlToolkit.Imaging.ColorExtensions.FromString(c); </code></pre> <p>This runs way faster than using <code>XamlReader</code>, so it is recommended if you need to call it more than once. You can also clone it from <a href="https://github.com/xyzzer/WinRTXamlToolkit/blob/master/WinRTXamlToolkit/Imaging/ColorExtensions.cs" rel="nofollow">GitHub</a> or copy and paste from here:</p> <pre><code>#region FromString() /// &lt;summary&gt; /// Returns a Color based on XAML color string. /// &lt;/summary&gt; /// &lt;param name="c"&gt;The color string. Any format used in XAML should work.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static Color FromString(string c) { if (string.IsNullOrEmpty(c)) throw new ArgumentException("Invalid color string.", "c"); if (c[0] == '#') { switch (c.Length) { case 9: { //var cuint = uint.Parse(c.Substring(1), NumberStyles.HexNumber); var cuint = Convert.ToUInt32(c.Substring(1), 16); var a = (byte)(cuint &gt;&gt; 24); var r = (byte)((cuint &gt;&gt; 16) &amp; 0xff); var g = (byte)((cuint &gt;&gt; 8) &amp; 0xff); var b = (byte)(cuint &amp; 0xff); return Color.FromArgb(a, r, g, b); } case 7: { var cuint = Convert.ToUInt32(c.Substring(1), 16); var r = (byte)((cuint &gt;&gt; 16) &amp; 0xff); var g = (byte)((cuint &gt;&gt; 8) &amp; 0xff); var b = (byte)(cuint &amp; 0xff); return Color.FromArgb(255, r, g, b); } case 5: { var cuint = Convert.ToUInt16(c.Substring(1), 16); var a = (byte)(cuint &gt;&gt; 12); var r = (byte)((cuint &gt;&gt; 8) &amp; 0xf); var g = (byte)((cuint &gt;&gt; 4) &amp; 0xf); var b = (byte)(cuint &amp; 0xf); a = (byte)(a &lt;&lt; 4 | a); r = (byte)(r &lt;&lt; 4 | r); g = (byte)(g &lt;&lt; 4 | g); b = (byte)(b &lt;&lt; 4 | b); return Color.FromArgb(a, r, g, b); } case 4: { var cuint = Convert.ToUInt16(c.Substring(1), 16); var r = (byte)((cuint &gt;&gt; 8) &amp; 0xf); var g = (byte)((cuint &gt;&gt; 4) &amp; 0xf); var b = (byte)(cuint &amp; 0xf); r = (byte)(r &lt;&lt; 4 | r); g = (byte)(g &lt;&lt; 4 | g); b = (byte)(b &lt;&lt; 4 | b); return Color.FromArgb(255, r, g, b); } default: throw new FormatException(string.Format("The {0} string passed in the c argument is not a recognized Color format.", c)); } } else if ( c.Length &gt; 3 &amp;&amp; c[0] == 's' &amp;&amp; c[1] == 'c' &amp;&amp; c[2] == '#') { var values = c.Split(','); if (values.Length == 4) { var scA = double.Parse(values[0].Substring(3)); var scR = double.Parse(values[1]); var scG = double.Parse(values[2]); var scB = double.Parse(values[3]); return Color.FromArgb( (byte)(scA * 255), (byte)(scR * 255), (byte)(scG * 255), (byte)(scB * 255)); } else if (values.Length == 3) { var scR = double.Parse(values[0].Substring(3)); var scG = double.Parse(values[1]); var scB = double.Parse(values[2]); return Color.FromArgb( 255, (byte)(scR * 255), (byte)(scG * 255), (byte)(scB * 255)); } else { throw new FormatException(string.Format("The {0} string passed in the c argument is not a recognized Color format (sc#[scA,]scR,scG,scB).", c)); } } else { var prop = typeof(Colors).GetTypeInfo().GetDeclaredProperty(c); return (Color)prop.GetValue(null); } } #endregion </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload