Converting a string HEX to color in Windows Phone Runtime c#

前端 未结 8 1673
执念已碎
执念已碎 2021-02-09 07:30

I am working on a windows phone game, and I got stuck when I wanted to convert a HEX string into Color. On windows phone 8 silverlight it is not a problem but I cannot find a so

8条回答
  •  粉色の甜心
    2021-02-09 08:11

    Converting Hex to Color in C# for Universal Windows Platform (UWP)

    Create a method to Convert Hex string to SolidColorBrush:

      public SolidColorBrush GetSolidColorBrush(string hex)
        {
            hex = hex.Replace("#", string.Empty);
            byte a = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16));
            byte r = (byte)(Convert.ToUInt32(hex.Substring(2, 2), 16));
            byte g = (byte)(Convert.ToUInt32(hex.Substring(4, 2), 16));
            byte b = (byte)(Convert.ToUInt32(hex.Substring(6, 2), 16));
            SolidColorBrush myBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
            return myBrush;
        }
    

    Now all that left is to get the color by Calling the method and pass the hex string to it as parameter:

      var color = GetSolidColorBrush("#FFCD3927").Color;  
    

    Reference: http://www.joeljoseph.net/converting-hex-to-color-in-universal-windows-platform-uwp/

提交回复
热议问题