Results 1 to 3 of 3

Thread: [*resolved]Hex to Color?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    [*resolved]Hex to Color?

    I wanted to convert Hex to System.Color and I had to write everything my own... right now I have a function that has to convert all the hex values to rgb values and then it converts it to system.color.
    doesnt the .NET framework do this by any chance?
    Last edited by MrPolite; Feb 3rd, 2003 at 07:51 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    maybe this would help ?

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    aaah thanks!
    just two linex!
    dim c as new colorconverter
    c.convertfromstring(hex)

    look at what I had to write!
    VB Code:
    1. Public Shared Sub HexToRGB(ByVal hex As String, ByRef r As Integer, ByRef g As Integer, ByRef b As Integer)
    2.         If hex Is Nothing Then Throw New ArgumentNullException()
    3.        
    4.         ' Remove the "#" sign
    5.         If hex.StartsWith("#") Then
    6.             hex = hex.Substring(1)
    7.         End If
    8.  
    9.         Dim chars() As Char = StringToChar(hex)
    10.  
    11.  
    12.         r = HexCharToDec(chars(0)) * 16
    13.         r += HexCharToDec(chars(1))
    14.  
    15.         g = HexCharToDec(chars(2)) * 16
    16.         g += HexCharToDec(chars(3))
    17.  
    18.         b = HexCharToDec(chars(4)) * 16
    19.         b += HexCharToDec(chars(5))
    20.     End Sub
    21.  
    22.     Public Shared Function HexToColor(ByVal hex As String) As Color
    23.         Dim r, g, b As Integer
    24.         HexToRGB(hex, r, g, b)
    25.         Return Color.FromArgb(r, g, b)
    26.     End Function
    27.  
    28.     ' Gets a hex character and returns its decimal value
    29.     Private Shared Function HexCharToDec(ByVal hex As Char) As Integer
    30.         Dim dec As Integer
    31.         hex = Char.ToLower(hex)
    32.  
    33.         Try
    34.             dec = CInt(hex.ToString)
    35.             Return dec
    36.         Catch   ' Not a number
    37.             Select Case hex
    38.                 Case "a"c
    39.                     Return 10
    40.                 Case "b"c
    41.                     Return 11
    42.                 Case "c"c
    43.                     Return 12
    44.                 Case "d"c
    45.                     Return 13
    46.                 Case "e"c
    47.                     Return 14
    48.                 Case "f"c
    49.                     Return 15
    50.                 Case Else
    51.                     Throw New ArgumentException()
    52.             End Select
    53.         End Try
    54.     End Function
    55.  
    56. Public Shared Function StringToChar(ByVal s As String) As Char()
    57.         If s Is Nothing Then Return Nothing
    58.  
    59.         Dim chars(s.Length - 1) As Char
    60.         Dim i As Integer
    61.         For i = 0 To s.Length - 1
    62.             chars(i) = CChar(s.Substring(i, 1))
    63.         Next
    64.  
    65.         Return chars
    66.     End Function
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width