# Color Conversion Functions

Posted: 2002-06-01
By: ArchiveBot
Viewed: 77

Filed Under:

VB6 Code Cache

No attachments for this post

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.

Original Author: Simon Price

### Code

`' *** 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 StringDim i As ByteOn 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 SubPublic Function RGB2Hex(r As Byte, g As Byte, b As Byte) As StringOn Error Resume Next  ' convert to long using vb's rgb function, then use the long2rgb function  RGB2Hex = Long2Hex(RGB(r, g, b))End FunctionPublic 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 SubPublic Function RGB2Long(r As Byte, g As Byte, b As Byte) As LongOn Error Resume Next  ' use vb's rgb function  RGB2Long = RGB(r, g, b)End FunctionPublic Function Long2Hex(LongColor As Long) As StringOn Error Resume Next  ' use vb's hex function  Long2Hex = Hex(LongColor)End FunctionPublic Function Hex2Long(strHexColor As String) As LongDim r As ByteDim g As ByteDim b As ByteOn 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`
{"ft_lang_label":"__label__en","ft_lang_prob":0.5803124,"math_prob":0.8219725,"size":2367,"snap":"2023-40-2023-50","text_gpt3_token_len":683,"char_repetition_ratio":0.16377486,"word_repetition_ratio":0.12470588,"special_character_ratio":0.28559357,"punctuation_ratio":0.1085595,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95983046,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-22T19:09:03Z\",\"WARC-Record-ID\":\"<urn:uuid:9df73680-5345-4892-9a39-e25593ba647a>\",\"Content-Length\":\"8333\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b6fb55a7-3a10-49e6-8666-ba98552ce3c3>\",\"WARC-Concurrent-To\":\"<urn:uuid:334b92be-7494-4382-9919-17c4629b652d>\",\"WARC-IP-Address\":\"162.250.75.145\",\"WARC-Target-URI\":\"https://www.fortypoundhead.com/showcontent.asp?artid=47598\",\"WARC-Payload-Digest\":\"sha1:EKSGIWUNGTVX7Y2YXOUBEZH7DXBQUCBH\",\"WARC-Block-Digest\":\"sha1:XRPDNOOT6UGQ5FLXDCMF3VPJETZL4CGQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506421.14_warc_CC-MAIN-20230922170343-20230922200343-00764.warc.gz\"}"}