How to convert Color object to HTML Hex code?

goodmorningsky

Well-known member
Joined
Aug 18, 2003
Messages
172
In vb I did this way..

-- dlg is CommonDialog
Private Sub Command1_Click()
Dim col
dlg.ShowColor
col = dlg.color
Text1 = ColorToHTML(col)
End Sub
Function ColorToHTML(ByVal color As Long) As String
HTML color codes are in the format #RRGGBB (red, green, blue)
while Hex(color) returns numbers in the format BBGGRR
therefore we just have to invert the order of the
hex values of red and blue
Dim tmp As String
tmp = Right$("00000" & Hex$(color), 6)

ColorToHTML = "#" & Right$(tmp, 2) & Mid$(tmp, 3, 2) & Left$(tmp, 2)

End Function


But in C#, ColorDialog return Color object!! How can I conver it to HTML color code? (how to write ColorToHTML method in C#?)
 
theres a default way of translating color to html you know? ie: the ColorTranslator class ...
Code:
        Dim cl As Color = Color.Red

        MessageBox.Show(ColorTranslator.ToHtml(cl))
to reverse it use ColorTranslator.FromHtml
 
Back
Top