• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 978
  • Last Modified:

Convert String to Color in Silverlight

I am building a windows phone app that the user is able to put in a hex value like "FFFFFF" and the value that they put in gets recalculated to a couple related values, each of which is displayed as text.

I am trying to make rectangles next to them that change to the color they state.however, i am having issues setting the colors to the string values. How do i go about this? I have googled around, and Haven't gotten very far :/

There are some great ways introduced in .Net 3.0, but they are not in silverlight.

Thanks in advance
0
dashifire
Asked:
dashifire
  • 2
1 Solution
 
dashifireAuthor Commented:
Yeah, looks like there isn't an inherent way to to do it. I ended up having to do custom coding similar to your third link. I found it online, but am struggling t find the page i got it from...
0
 
dashifireAuthor Commented:
Here is the code
 public static SolidColorBrush GetBrushFromHexString(string aarrggbb)
        {
            String xamlString = "<Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Background=\"" + aarrggbb + "\"/>";
            Canvas c = (Canvas)System.Windows.Markup.XamlReader.Load(xamlString);
            return (SolidColorBrush)c.Background;
        }

        
        private Color GetColorFromHexa(string hexaColor)
        {
            return Color.FromArgb(
                    Convert.ToByte(hexaColor.Substring(1, 2), 16),
                    Convert.ToByte(hexaColor.Substring(3, 2), 16),
                    Convert.ToByte(hexaColor.Substring(5, 2), 16),
                    Convert.ToByte(hexaColor.Substring(7, 2), 16));
        }

Open in new window

I use this to invoke it
Brush colorname = GetBrushFromHexString("#FF" + textBlock3.Text);

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now