Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. how can I programmatically set control colors using hex values ?

how can I programmatically set control colors using hex values ?

Scheduled Pinned Locked Moved WPF
questioncsharpgraphics
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abiemann
    wrote on last edited by
    #1

    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

    C A I 3 Replies Last reply
    0
    • A abiemann

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A abiemann

        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

        A Offline
        A Offline
        abiemann
        wrote on last edited by
        #3

        "Value of type 'System.Drawing.Brush' cannot be converted to 'System.Windows.Media.Brush'" BAH!!

        C A 2 Replies Last reply
        0
        • A abiemann

          "Value of type 'System.Drawing.Brush' cannot be converted to 'System.Windows.Media.Brush'" BAH!!

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • A abiemann

            "Value of type 'System.Drawing.Brush' cannot be converted to 'System.Windows.Media.Brush'" BAH!!

            A Offline
            A Offline
            abiemann
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • A abiemann

              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

              I Offline
              I Offline
              Insincere Dave
              wrote on last edited by
              #6

              There is ColorConverter, which is the TypeConverter WPF uses. this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00FF00"));

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups