Converting colour in Windows Phone

自古美人都是妖i 提交于 2019-12-12 20:08:29

问题


In my Windows Phone application I get the colour from xml and then bind it to some element.
I have found that I get the wrong colour in my case.

Here is my code:

 var resources = feedsModule.getResources().getColorResource("HeaderColor") ??
     FeedHandler.GetInstance().MainApp.getResources().getColorResource("HeaderColor");
     if (resources != null)
     {
      var colourText = Color.FromArgb(255,Convert.ToByte(resources.getValue().Substring(1, 2), 16),
                       Convert.ToByte(resources.getValue().Substring(3, 2), 16),
                      Convert.ToByte(resources.getValue().Substring(5, 2), 16));

So after converting the colour, I get the wrong result. In xml I have this one:

 <Color name="HeaderColor">#FFc50000</Color>

and it converts into #FFFFC500


回答1:


You should use some 3rd-party converter.

Here is one of them.

Then you can use it so:

Color color = (Color)(new HexColor(resources.GetValue());

Also you can use the method from this link, it works as well.

public Color ConvertStringToColor(String hex)
{
    //remove the # at the front
    hex = hex.Replace("#", "");

    byte a = 255;
    byte r = 255;
    byte g = 255;
    byte b = 255;

    int start = 0;

    //handle ARGB strings (8 characters long)
    if (hex.Length == 8)
    {
        a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
        start = 2;
    }

    //convert RGB characters to bytes
    r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
    g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
    b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);

    return Color.FromArgb(a, r, g, b);
}


来源:https://stackoverflow.com/questions/11737583/converting-colour-in-windows-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!