PDA

Click to See Complete Forum and Search --> : Converting from RGB to HEX


PT Exorcist
Aug 27th, 2002, 11:14 AM
is ther any function in .NET to convert from rgb to hex and hex to rgb?

Buy2easy.com
Aug 27th, 2002, 08:51 PM
what is rgb? i know what hex is if you tell mre what rgb is then i can possibly help.

Dorothy
Aug 27th, 2002, 11:08 PM
Iam not sure,Thou.. here is my own function for that:


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


and this is how you call it.

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

Vahid
Aug 28th, 2002, 05:10 PM
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

Magiaus
Aug 28th, 2002, 07:47 PM
RGB is red green blue 0 - 255 for each 0r 00 - FF which ever