user564042
user564042

Reputation: 185

Converting Hex Value to SolidColorBrush in Silverlight

I'm trying to write an IValueConverter in Silverlight. This IValueConverter will return a SolidColorBrush. The converter will be passed a hex value like "FFFF5300". Because Silverlight does not have the BrushConverter class, I needed to parse this value manually. In an attempt to do this, I have the following code:

byte a = (byte)(Convert.ToUInt32(color.Substring(0, 2), 16));
byte r = (byte)(Convert.ToUInt32(color.Substring(2, 2), 16));
byte g = (byte)(Convert.ToUInt32(color.Substring(4, 2), 16));
byte b = (byte)(Convert.ToUInt32(color.Substring(6, 2), 16));

My problem is, I can't use the Convert.ToX methods in an IValueConverter. Because of this, I'm not sure how to convert a two character string into a byte value. Can someone tell me how to do this?

Upvotes: 2

Views: 3956

Answers (3)

jeeradej
jeeradej

Reputation: 21

Actually, you can use the Convert.ToXXX() methods in an IValueConverter. You just need to add the System namespace in front of Convert: System.Convert.ToXXX().

Upvotes: 2

A_Nabelsi
A_Nabelsi

Reputation: 2574

The value must be in the following format "#xxxxxxxx",

System.Drawing.ColorTranslator.FromHtml(Value);

Upvotes: 0

Domenic
Domenic

Reputation: 112897

I'm not sure if this what you're asking, but the following code doesn't use Convert:

byte a = byte.Parse(color.Substring(0, 2), NumberStyles.HexNumber);
// etc.

Upvotes: 1

Related Questions