ARTICLE

How to write and read a Color in VB.NET

Posted by Tiberiu Ionescu Articles | Visual Basic 2010 December 09, 2004
This article shows how to transform a color in hexadecimal representation.
Download Files:
 
Reader Level:

Transforming a color in hexa representation using following function:

Private Function WriteHexString(ByVal aobjColR As Byte, ByVal aobjColG As Byte, ByVal aobjColB As Byte) As String
Dim strRet As
String
Dim btR As Byte
() = {aobjColR}
Dim strR As String
= ToHexString(btR)
strRet = strR
Dim btG As Byte() = {Me
.colorDialog1.Color.G}
Dim strG As String
= ToHexString(btG)
strRet &= strG
Dim btB As Byte() = {Me
.colorDialog1.Color.B}
Dim strB As String
= ToHexString(btB)
strRet &= strB
Return
strRet
End Function 'WriteHexString

This function transform a Color (defined as Color.R, Color.G, Color.B) in hexa representation (as string) and use following helper functions: 

Private Shared Function ToHexString(ByVal bytes As Byte()) As String
Dim chars As Char() = New Char
(bytes.Length * 2 - 1) {}
Dim i As Integer
= 0
Do While
i < bytes.Length
Dim b As Integer
= bytes(i)
chars(i * 2) = hexDigits(b >> 4)
chars(i * 2 + 1) = hexDigits(b
And
&HF)
i += 1
Loop
Return New String
(chars)
End Function 'ToHexString

Shared hexDigits() As Char = {"0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c, "A"c, "B"c, "C"c, "D"c, "E"c, "F"c}

To transform "hexa" representation back in color we can use following function:

Private Function GetColor(ByVal astrHexString As String) As Color
Dim strR As
String
Dim strG As
String
Dim strB As
String
Dim strRL As
String
Dim strRR As
String
Dim strGL As
String
Dim strGR As
String
Dim strBL As
String
Dim strBR As
String
Dim iR As
Integer
Dim iG As
Integer
Dim iB As
Integer
Dim iRL As
Integer
Dim iRR As
Integer
Dim iGL As
Integer
Dim iGR As
Integer
Dim iBL As
Integer
Dim iBR As
Integer
Dim c As
Color

strR = astrHexString.Substring(0, 2)
strG = astrHexString.Substring(2, 2)
strB = astrHexString.Substring(4, 2)
strRL = strR.Substring(0, 1)
strRR = strR.Substring(1, 1)
strGL = strG.Substring(0, 1)
strGR = strG.Substring(1, 1)
strBL = strB.Substring(0, 1)
strBR = strB.Substring(1, 1)
iRL = GetIntFromHex(strRL)
iRR = GetIntFromHex(strRR)
iGL = GetIntFromHex(strGL)
iGR = GetIntFromHex(strGR)
iBL = GetIntFromHex(strBL)
iBR = GetIntFromHex(strBR)
iR = 16 * iRL + iRR
iG = 16 * iGL + iGR
iB = 16 * iBL + iBR
c = Color.FromArgb(iR, iG, iB)
Return c

End Function 'GetColour

The complete code is in zip file.

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM).

Login to add your contents and source code to this article
share this article :
post comment
 

Public Function HexToColor(ByVal strHex As String) As Color

Return Color.FromArgb(Convert.ToUInt32(strHex, 16))

End Function

Public Function ColorToHex(ByVal cColor As Color) As String

Return Hex(cColor.ToArgb)

End Function

Public Function ColorToHex(ByVal R As Byte, ByVal G As Byte, ByVal B As Byte) As String

Return Hex(CUInt((R << 16) + (G << 8) + B))

End Function

Posted by Epic Fail Jul 04, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor