Results 1 to 11 of 11

Thread: hex colour

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    8

    Post hex colour

    hi i am relatively new to VB, am using it at college.

    We have created a small colour mixer and i wanted to take it further by adding the hex values of the RGB decimal numbers. Was wondering if anybody new of code that ic ould use to turn RGB numbers into hex so that i can display them in another textbox kind of thing.

    Cheers,

    Tom.

  2. #2

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    8

    Re: hex colour

    Cheers,

    Might work if i knew how to use it, looks a little complicated for me, is there no easier way?

    Cheers all the sames.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: hex colour

    It isn't really that complicated, but here is a slightly different method

    ' Replace Me.BackColor with any valid color value
    Code:
    Text1.Text = Right$("0000000" & Hex(Me.BackColor), 8 )
    Edited: Most valid colors will only use 6 hex characters not 8, so feel free to change 8 to 6 above.
    The 7th & 8th characters are for opacity with normal colors. Negative colors used in VB, i.e., vbButtonFace = -2147483633, are not true colors, but are a masked reference to the pc's system color table/references.
    Last edited by LaVolpe; Mar 1st, 2009 at 11:58 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    8

    Re: hex colour

    just remembered, i also need the hex code textbox to edit the colour of the picturebox that is usually edited currently by sliders or textboxes, one of each for red blue and green. Would be great if the hex values were not only displayed but also could edit the colour and the values of the rgb text boxes and the sliders??

    Cheers

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: hex colour

    To convert Hex string to a number: CLng("&H" & Text1.Text)
    Assumption is that Text1.Text is a valid Hex string and the value can be used as a color property.

    Me.Text1.ForeColor = CLng("&H" & Text1.Text)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    8

    Re: hex colour

    cheers will have a go

  8. #8
    Member
    Join Date
    Aug 2008
    Posts
    39

    Re: hex colour

    LaVolpe, I tried your code. It failed.

    I have 3 sliders that you use to choose your RGB value. One slider for R, one for G and the last for B.

    Anyway, I set the sliders to create a gold color and clicked the "OK" button, in which I put your code (from post #4).
    I have it like this:
    Code:
    Private Sub Command1_Click()
    Form1.Text13.Text = Right$("0000000" & Hex(RGB(HScroll1.Value, HScroll2.Value, HScroll3.Value)), 6)
    End Sub
    When I tried gold, and then used that code for
    Code:
    <font color="code here">blablabla</font>
    in a HTML document, it became dark TEAL and NOT gold!

    I tried the same with a yellow color (255,255,0) and it became LIGHT teal, and not yellow! What's the problem? I thought HTML used the same Hex codes.

    Edit: I asked my HTML teacher, and he noticed your code makes the HEX backwards! When I put my three sliders to create red, your HEX code turns it into "0000FF" and not "FF0000"

    Edit 2: And yes I am 100% sure I have put the sliders in the right order.

    Edit 3:
    As you might have understood, I want to be able to get a Hex code from a Visual Basics program to be able to copy/paste that code into a HTML document and have it make the text (or background or whatever) the color I chose in the Visual Basics program.
    Last edited by Divran; Mar 10th, 2009 at 09:15 AM.

  9. #9
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: hex colour

    Html colours and VB colours use different byte ordering. They are both valid hex colour codes.

    Just change the scroll bar order... RGB(HScroll3.Value, HScroll2.Value, HScroll1.Value)

    The colours won't be right for VB but they will work in Html.

  10. #10
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: hex colour

    the only difference between html hex and vb hex color is that html is Red Green Blue and vb6 is blue green red

    like

    red in html
    FF0000

    red in vb6
    0000FF

  11. #11
    Member
    Join Date
    Aug 2008
    Posts
    39

    Re: hex colour

    Ok thanks. I'll try switching them around.

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