Results 1 to 10 of 10

Thread: I need to convert Hex into RGB anyone know how ??

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    219

    Lightbulb

    I need to convert Hex into RGB anyone know how ??
    me

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Where did Hex come from?

    Not sure what you are asking.

    Is hex supplied as a text string from a Text Box? Is hex from a long? From some Bytes?

    Are you familiar with the RGB Function? Hex not needed if you use it to create Color Values.

    ' Set background to green.
    Form1.BackColor = RGB(0, 128, 0)
    ' Set background to yellow.
    Form2.BackColor = RGB(255, 255, 0)
    ' Set point to dark blue.
    PSet (100, 100), RGB(0, 0, 64)

    RGB Color values are Longs, but I am not sure about the order of the 4 bytes.

    Live long & Prosper --- The dinosaur ([email protected])



  3. #3
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    you've learnt to spell, Nice one.
    (I retract that comment on the grounds that I've forgotten how to spell dosn't) (This dosn't make sense anymore but there was a bit on this message that I deleted because it dosn't apply)

    Code:
    bytRed =     lngColour And vbRed
    bytGreen =  (lngColour And vbGreen) / 255
    bytBlue =   (lngColour And vbBlue) / (255 * 255)
    you can also use copymemory to copy the long into an RGB quad structure bu I hardly ever do it this way so I'm not doing it this way.

    Hope this helps

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    219
    the hex is a string from a text box
    me

  5. #5
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Thumbs down

    I guess that I was thinking about different meaning of the word HEX

    What do you mean by:
    The hex is a string from a text box
    ???

    [Edited by QWERTY on 04-24-2000 at 11:06 PM]

  6. #6
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    I imagine you mean that in a text box you write
    FF0000

    and a "something" goes Blue

    Try this:

    Code:
    Dim MyColour as Long
    MyColour = Clng("&H" & Text1.text)
    then use Sam Finches algorithm above to cut the long into RGB

    [Edited by Paul282 on 04-24-2000 at 11:14 PM]

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    219
    no i want to take if someone type a hex like #FFFFFF in a text box i want to turn that in to RGB Code of the color of it?
    me

  8. #8
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Text to Hex

    To convert a Text string like "9FA4" to hex, you will need to build a simple 16-item Array to relate Hex Characters to corresponding Hex Digits [Dim HexCharacters(0 to 15) as String*1]. Assign values: "0", "1", "2", ... "E", "F".

    Using the Mid Function in a loop, pick characters from your text string, one at a time from left to right. Look each character up in the array. Use position number of each character as a value ("F" is in position 15, use 15 for "F", "9" is in position 9, use 9 for "9", et cetera).

    The conversion algorithm for "9FA4" to hex is
    [9*16+15)*16+10]*16+4

    Multiply high order digit by 16, add next digit, multiply sum by 16, add next digit, multiply sum by 16, add next digit, et cetera until string is exhausted. Obviously you must have code barf if character is not found in your array, or if the string is too long.

    If a Long is used for the result, you should have your RGB color value (assuming input text is in correct order). Note RGB Function (see my first reply) can be used if you have three separate Hex Strings, one for each color. In this case, each conversion is: Multiply high order digit by 16, then add low order digit.

    The code is actually shorter than above description, but if I wrote it out without debugging it, I would probably make some dumb mistake.

    Live long & prosper --- The Dinosaur



  9. #9
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    Same thing, different event code.

    Code:
    Private Sub Command1_Click()
    
        Dim MyColour As Long
        Dim bytRed As Byte
        Dim bytGreen As Byte
        Dim bytBlue As Byte
        Dim X As Long
        
        X = 256 ^ 2
        
        MyColour = CLng("&H" & Text1.Text)
        
        bytRed = MyColour And vbRed
        bytGreen = (MyColour And vbGreen) / 256
        bytBlue = (MyColour And vbBlue) / (X)
        
        Label1.Caption = "RED = " & bytRed & " Green = " & bytGreen & " Blue = " & bytBlue
        
    
    End Sub

  10. #10
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455
    Hello XxEvilxX,

    Look at our answers on site:

    http://forums.vb-world.net/showthrea...threadid=10324

    Good luck,

    Michelle.

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