|
-
Apr 9th, 2003, 06:24 AM
#1
Thread Starter
Fanatic Member
HTML Hex Colours > VB Colours
I found this code on the forum a little time ago. It converts VB colours to HTML Colours (some kind of Hexadecimal value). Unfortunately I don't really understand its functionality, and I need a code that does the opposite: convert HTML Colours to VB Colours. Does anyone have such a code?
Thanks.
VB Code:
Public Function ForeColorHTML(Number As Long) As String
Dim MyString As String, MyNum As Long, MyCalc As Long, MyRemainder As Long, X As Integer
MyRemainder = Number
MyString = ""
For X = 2 To 0 Step -1
MyCalc = MyRemainder \ 256 ^ X
MyString = Format$(Hex(MyCalc), "00") & MyString
MyRemainder = MyRemainder - MyCalc * 256 ^ X
Next
txtForeColor.Text = MyString
End Function
-
Apr 9th, 2003, 07:18 AM
#2
Hyperactive Member
To convert HTML colors to VB colors is pretty straight forward VB has a function called RGB........ the hex value from HTML represents this.... for example #ffffff is white
this represents Red = ff = (15*16) + 15 = 255
Blue = ff = (15*16) + 15 = 255
Green = ff = (15*16) + 15 = 255
So RGB(255,255,255) = color code white in VB which can be used to set the color in VB.
See ya later,
-=XQ=-
"Reality is merely an illusion, albeit a very persistent one. "
- Albert Einstein (1879-1955)
This is the coolest site ever!!!
-
Apr 9th, 2003, 07:23 AM
#3
Hyperactive Member
An easyway to set the color is to use the following:
Code:
public function returnColorCode(byval HexColor as String) as long
returnColorCode = val("&h" & HexColor)
end function
This seems to be what you are after
See ya later,
-=XQ=-
"Reality is merely an illusion, albeit a very persistent one. "
- Albert Einstein (1879-1955)
This is the coolest site ever!!!
-
Apr 9th, 2003, 08:03 AM
#4
Thread Starter
Fanatic Member
Yeah, indeed. Thanks very much. I needed that code bad. 
By the way, HTML reads Hexadecimal colours from right to left instead of left to right. So I slightly changed the code to this:
VB Code:
Val("&h" & Mid(strColor, 5, 2) & Mid(strColor, 3, 2) & Mid(strColor, 1, 2))
Last edited by TheVader; Apr 9th, 2003 at 08:10 AM.
-
Apr 9th, 2003, 08:12 AM
#5
That code will not give you the correct result since on a webpage you put the hex rgb code in this manner: RRGGBB while Windows store colors in the following manner: BBGGRR so if you would use that code to convert FF0000 that would be red on a webpage it would be shown as blue in your app.
VB Code:
Public Function RGBconvert(ByVal sHexColor) As Long
'First make sure the hex color is 6 digit long
sHexColor = Right$("000000" & sHexColor, 6)
'Now switch the red and the blue values
sHexColor = Right$(sHexColor, 2) & Mid$(sHexColor, 3, 2) & Left$(sHexColor, 2)
'Convert to long
RGBconvert = CLng("&H" & sHexColor)
End Function
Best regards
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|