Important alert: (current site time 7/16/2013 1:27:16 AM EDT)
 

article

[[ RGB to HEX to COLOR conversion

Email
Submitted on: 11/12/2003 7:00:08 AM
By: Pamela RAI 
Level: Beginner
User Rating: By 5 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 21295
 
     Color conversion. Vote if you like it....

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				Public Function hextorgb(hexcolor As String)
'input format = #FFCC00
Dim r, g, b As Byte
r = CByte("&H" & Mid(hexcolor, 2, 2))
g = CByte("&H" & Mid(hexcolor, 4, 2))
b = CByte("&H" & Mid(hexcolor, 6, 2))
hextorgb = r & "," & g & "," & b
End Function


Public Function rgbtohex(r As Byte, g As Byte, b As Byte)
'input format = 255,255,255

'get the r value
If r < 16 Then
hex1 = 0 & Hex(r)
Else
hex1 = Hex(r)
End If

'get the g value
If r < 16 Then
hex2 = 0 & Hex(g)
Else
hex2 = Hex(g)
End If

'get the b value
If b < 16 Then
hex3 = 0 & Hex(b)
Else
hex3 = Hex(b)
End If
rgbtohex = "#" & hex1 & hex2 & hex3
End Function

Public Function rgbtocolor(r As Byte, g As Byte, b As Byte)
rgbtocolor = r + (g * 256) + (b * 65536)
End Function

Public Function colortorgb(color As Long)
Dim r, g, b As Byte
r = color And 255
g = (color \ 256) And 255
b = (color \ 65536) And 255
colortorgb = r & "," & g & "," & b
End Function


Other 20 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 article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

12/3/2003 6:32:54 AMFade (Amit BS)

My dear Pamela,
Thank you so much for this submission,
I was desperatly looking for a ColorToRGB translation.
You have hepled me a lot :)
5 glbs for you!
(If this comment was disrespectful, please report it.)

 
2/25/2004 3:36:19 AM

-
(If this comment was disrespectful, please report it.)

 
7/28/2004 2:28:20 AMMichael Vainshtein

Gr8. but can you plz add explanation for all the weird numbers. we need to UNDERSTAND it not Copy+Paste it!!!
(If this comment was disrespectful, please report it.)

 
7/28/2004 2:31:53 AMMichael Vainshtein

ERROR!! this won't work!
what if i do HexToRGB("#1F15C")
HAH?
(If this comment was disrespectful, please report it.)

 
11/12/2004 10:54:17 PM

pamela 1 apluse, pamela 2 i salute you,
pamela 3 plz help me find something about words number conversion like cheque, pamela 4 thanks man.
(If this comment was disrespectful, please report it.)

 
11/19/2004 8:41:24 AM

nice program and has good logic technique

(If this comment was disrespectful, please report it.)

 
3/23/2005 9:27:00 AM

hi i don't know what text boxes or labels to add to the program and what names to call them... i tried making 2 textboxes... 1 called rgbtocolor and the other called colortorgb but no success can someone please tell me or send me a copy WITH the project!
(If this comment was disrespectful, please report it.)

 
6/12/2005 4:07:00 PMZim Dale

Umm I found 1 thing that would make it a little bit better for stuff like web colors. If ya make it like this:

Public Function RGBtoHex(R as byte, G as byte, B as byte)

If R < 16 then
If R = 0 then
Hex1 = 0 & 0
end if
Hex1 = 0 & Hex(R)
Else
Hex1 = Hex(R)
end if

If G < 16 then
If G = 0 then
Hex2 = 0 & 0
end if
Hex2 = 0 & Hex(G)
Else
Hex2 = Hex(G)
end if

If B < 16 then
If B = 0 then
Hex3 = 0 & 0
end if
Hex3 = 0 & Hex(B)
Else
Hex3 = Hex(B)
end if

RGBtoHex = "#" & Hex1 & hex2 & hex3
end function

That makes it so if the RGB is 0 instead of just doing plane 0 it will do 00 so you will stay with the 6 characters oh the hex colors :)

Still nice code helped a lot :)
(If this comment was disrespectful, please report it.)

 
6/8/2006 7:57:01 AMjalapandiyan

ok good
(If this comment was disrespectful, please report it.)

 

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 article, please click here instead.)
 

To post feedback, first please login.