|
-
Oct 30th, 2001, 08:05 PM
#1
Thread Starter
Member
HEX , HTML & CommonDialog1.Color
Code:
txtcolor(0).text = "#" & Right("00000" & Hex(CommonDialog1.Color), 6)
The code above is what I am using to convert commondialog1.color to html hex values. it works on most colors but like on yellow it gives #00FFFF and on darkblue it give #80400 anyone know of a way to make the conversion work all of the time?
-
Oct 30th, 2001, 11:31 PM
#2
PowerPoster
This works, and for your own reference, #00FFFF is yellow.
HexColorString = "#" & Right(Format("000000" & CStr(Hex(CommonDialog1.Color)), "000000"), 6)
The main prob you had was that Hex() doesn't give you the right RGB string. It was the CStr which fixed it up nicely.
I have thoroughly tested this, but it gave the right answer for some standard colours.
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Oct 31st, 2001, 12:54 AM
#3
Thread Starter
Member
if #00FFFF is yellow why do I get skyblue when using this code on a web page?
-
Oct 31st, 2001, 12:58 AM
#4
Thread Starter
Member
I just tried your code and did not fixe the problem I compared the color codes in 2 different graphic editing programs and not all of the colors get the right values. Any other Ideas?
-
Oct 31st, 2001, 03:07 AM
#5
PowerPoster
hi
you must remember that RGB in VB has to be switched to BGR for html hex as it uses reverse notation. I have posted some conversions on other threads so u can do a search on RGB and my name and see some
regards
Stuart
-
Oct 31st, 2001, 03:33 AM
#6
PowerPoster
Hi
Here is some code
If u print Hex(vbBlue) in the debug window u get the reverse notation of FF0000. For html u need #0000FF.
VB Code:
Private Sub Command1_Click()
Dim MyString As String
Dim MyNum As Long
Dim MyCalc As Long
Dim MyRemainder As Long
MyNum = vbBlue
MyRemainder = MyNum
MyString = ""
For x = 2 To 0 Step -1
MyCalc = MyRemainder \ 256 ^ x
MyString = Format$(Hex(MyCalc), "00") & MyString
MyRemainder = MyRemainder - MyCalc * 256 ^ x
Next
MyString = "#" & MyString
Debug.Print MyString
End Sub
You could also do it by flipping the string if u wanted to eg
VB Code:
MyNum = vbYellow
MyString = Right$("000000" & Hex(MyNum), 6)
MyString = "#" & Right$(MyString, 2) & Mid$(MyString, 3, 2) & Left$(MyString, 2)
Debug.Print MyString
-
Oct 31st, 2001, 04:11 AM
#7
PowerPoster
If everything Stuart says is true (as I am sure it is), then convert my previous answer to:
HexColorString = "#" & Left(Format(CStr(Hex(CommonDialog1.Color) & "000000"), "000000"), 6)
And see what happens.
I am half-drunk right now, so this may not work at all...
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
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
|