is ther any function in .NET to convert from rgb to hex and hex to rgb?
Printable View
is ther any function in .NET to convert from rgb to hex and hex to rgb?
what is rgb? i know what hex is if you tell mre what rgb is then i can possibly help.
Iam not sure,Thou.. here is my own function for that:
and this is how you call it.Code:
Public Sub HexMe(ByVal icolor As Color)
Dim hexval As String
hexval += IIf(Len(Hex(icolor.R)) = 1, Hex(icolor.R) & "0", Hex(icolor.R))
hexval += IIf(Len(Hex(icolor.G)) = 1, Hex(icolor.G) & "0", Hex(icolor.G))
hexval += IIf(Len(Hex(icolor.B)) = 1, Hex(icolor.B) & "0", Hex(icolor.B))
MsgBox(hexval)
End Sub
Public Sub RGBMe(ByVal icolor As Color)
MsgBox("R= " & icolor.R.ToString & " G= " & icolor.G.ToString & " B= " & icolor.B.ToString)
End Sub
Code:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RGBMe(Label1.BackColor)
HexMe(Label1.BackColor)
End Sub
Hey dude, Sorry, I don't really understand what you mean, RGB colors ARE hex values. If you mean ‘System.Drawing.Color’ structure, it has two function (FromArgb, ToArgb) that can help.
Generally to get Red, Green, Blue parts of a RGB color:
Dim myRGBColor As Integer
Dim myRedPart, myGreenPart, myBluePart As Byte
myBluePart = CByte(myRGBColor And &HFF)
myGreenPart = CByte((myRGBColor And &HFF00) \ &H100)
myRedPart = CByte((myRGBColor And &HFF0000) \ &H10000)
To create a RGB color from Red, Green and Blue parts:
myRGBColor = myBluePart Or (myGreenPart * &H100) Or (myRedPart * &H10000)
PS: .Net RGB colors include one more part, Alpha channel. This part is used to tell the machine that how much transparent your color is. 0 is complete transparent, 255 is opaque. To get the Alpha part:
Dim myAlphaPart As Byte
myAlphaPart = CByte(((myRGBColor And &HFF000000) \ &H1000000) And &HFF)
And to set it:
myRGBColor = (myRGBColor And &H00FFFFFF) Or &HFF000000 'Opaque
myRGBColor = (myRGBColor And &H00FFFFFF) Or &H80000000 '50% Transparent
myRGBColor = myRGBColor And &H00FFFFFF 'Full Transparent
RGB is red green blue 0 - 255 for each 0r 00 - FF which ever