how can I programmatically set control colors using hex values ?
-
Question was: how do I convert a hex string to a Media.Color, with the hope that .Net 3.5 was mature enough to provide a method. I found the solution and wanted to delete the thread... 1) add a reference to System.Drawing 2) System.Drawing contains overloaded fromArgb() so that I don't have to bit-shuffle EDIT: but this approach didn't work...
modified on Wednesday, September 16, 2009 8:43 PM
-
Question was: how do I convert a hex string to a Media.Color, with the hope that .Net 3.5 was mature enough to provide a method. I found the solution and wanted to delete the thread... 1) add a reference to System.Drawing 2) System.Drawing contains overloaded fromArgb() so that I don't have to bit-shuffle EDIT: but this approach didn't work...
modified on Wednesday, September 16, 2009 8:43 PM
abiemann wrote:
I don't want to split up the hex value into single bytes. I feel that I shouldn't have to.
Sometimes, a programmer has to actually program. Sometimes, they have to do some basic things like conversion of numbers. Sometimes, they have to look up stuff like the Colors enum. Your two colors here are Color.Black and Color.Transparent ( as it's ARGB ).
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Question was: how do I convert a hex string to a Media.Color, with the hope that .Net 3.5 was mature enough to provide a method. I found the solution and wanted to delete the thread... 1) add a reference to System.Drawing 2) System.Drawing contains overloaded fromArgb() so that I don't have to bit-shuffle EDIT: but this approach didn't work...
modified on Wednesday, September 16, 2009 8:43 PM
-
"Value of type 'System.Drawing.Brush' cannot be converted to 'System.Windows.Media.Brush'" BAH!!
Well, you need to explicitly scope it. I think if you want to use the build in ones, it's Brushes, not Brush, too. My bad. I would agree that MS having objects of the same name in different namespaces is a PITA.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
"Value of type 'System.Drawing.Brush' cannot be converted to 'System.Windows.Media.Brush'" BAH!!
I think I saw people doing this back in the VB6 days, here is the VB.Net version ;P
Private Function ConvertHexToColor(ByVal strInput As String) As Color strInput = strInput.Replace("#", "") Dim alph As Integer = 0 Dim red As Integer = 0 Dim green As Integer = 0 Dim blue As Integer = 0 Dim colToReturn As Color = Color.FromRgb(red, green, blue) 'default if input is invalid If (strInput.Length = 6) Then 'RRGGBB red = Convert.ToInt32(strInput.Substring(0, 2), 16) green = Convert.ToInt32(strInput.Substring(2, 2), 16) blue = Convert.ToInt32(strInput.Substring(4, 2), 16) colToReturn = Color.FromRgb(red, green, blue) ElseIf (strInput.Length = 8) Then 'AARRGGBB alph = Convert.ToInt32(strInput.Substring(0, 2), 16) red = Convert.ToInt32(strInput.Substring(2, 2), 16) green = Convert.ToInt32(strInput.Substring(4, 2), 16) blue = Convert.ToInt32(strInput.Substring(6, 2), 16) colToReturn = Color.FromArgb(alph, red, green, blue) End If Return colToReturn End Function
to use:Public MainMenu_Background As Brush MainMenu_Background = New SolidColorBrush(ConvertHexToColor("#FF000000")) 'Black myControl.Background = MainMenu_Background
hopefully .Net 4.0 will see an improvement like this -
Question was: how do I convert a hex string to a Media.Color, with the hope that .Net 3.5 was mature enough to provide a method. I found the solution and wanted to delete the thread... 1) add a reference to System.Drawing 2) System.Drawing contains overloaded fromArgb() so that I don't have to bit-shuffle EDIT: but this approach didn't work...
modified on Wednesday, September 16, 2009 8:43 PM
There is ColorConverter, which is the TypeConverter WPF uses.
this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00FF00"));