Results 1 to 5 of 5

Thread: Converting from RGB to HEX

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    Converting from RGB to HEX

    is ther any function in .NET to convert from rgb to hex and hex to rgb?

  2. #2
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    what is rgb? i know what hex is if you tell mre what rgb is then i can possibly help.

  3. #3
    Hyperactive Member Dorothy's Avatar
    Join Date
    Feb 2001
    Posts
    310

    Wink

    Iam not sure,Thou.. here is my own function for that:

    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
    and this is how you call it.
    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

  4. #4
    Member Vahid's Avatar
    Join Date
    Aug 2002
    Location
    Iran
    Posts
    37

    Question

    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

  5. #5
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    RGB is red green blue 0 - 255 for each 0r 00 - FF which ever
    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width