VB icon

Color Conversion Functions

Email
Submitted on: 1/19/2015 4:53:00 AM
By: Simon Price (from psc cd)  
Level: Beginner
User Rating: By 8 Users
Compatibility: VB 6.0
Views: 1043
 
     Convert any color format (hex, long, rgb) to any other color format. There may be other examples of this on PSC, but I checked them and they do not use the same algorithm. This is not the fastest way to convert colors, but it is simple and reliable.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Color Conversion Functions
' Description:Convert any color format (hex, long, rgb) to any other color format. There may be other examples of this on PSC, but I checked them and they do not use the same algorithm. This is not the fastest way to convert colors, but it is simple and reliable.
' By: Simon Price (from psc cd)
'**************************************

' *** COLOR CONVERSTION FUNCTIONS ***
' this is the main function, all the other converstion functions play off this 1
' accepted input hex formats: &H######, ######, #*****
' NOT: &H#***** !!! (i hope no1 would use that anyway)
Public Sub Hex2RGB(strHexColor As String, r As Byte, g As Byte, b As Byte)
Dim HexColor As String
Dim i As Byte
On Error Resume Next
' make sure the string is 6 characters long
' (it may have been given in &H###### format, we want ######)
strHexColor = Right((strHexColor), 6)
' however, it may also have been given as or #***** format, so add 0's in front
For i = 1 To (6 - Len(strHexColor))
HexColor = HexColor & "0"
Next
HexColor = HexColor & strHexColor
' convert each set of 2 characters into bytes, using vb's cbyte function
r = CByte("&H" & Right$(HexColor, 2))
g = CByte("&H" & Mid$(HexColor, 3, 2))
b = CByte("&H" & Left$(HexColor, 2))
End Sub
Public Function RGB2Hex(r As Byte, g As Byte, b As Byte) As String
On Error Resume Next
' convert to long using vb's rgb function, then use the long2rgb function
RGB2Hex = Long2Hex(RGB(r, g, b))
End Function
Public Sub Long2RGB(LongColor As Long, r As Byte, g As Byte, b As Byte)
On Error Resume Next
' convert to hex using vb's hex function, then use the hex2rgb function
Hex2RGB (Hex(LongColor))
End Sub
Public Function RGB2Long(r As Byte, g As Byte, b As Byte) As Long
On Error Resume Next
' use vb's rgb function
RGB2Long = RGB(r, g, b)
End Function
Public Function Long2Hex(LongColor As Long) As String
On Error Resume Next
' use vb's hex function
Long2Hex = Hex(LongColor)
End Function
Public Function Hex2Long(strHexColor As String) As Long
Dim r As Byte
Dim g As Byte
Dim b As Byte
On Error Resume Next
' use the hex2rgb function to get the red green and blue bytes
Hex2RGB strHexColor, r, g, b
' convert to long using vb's rgb function
Hex2Long = RGB(r, g, b)
End Function


Other 35 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.