-
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.
-
Re: hex colour
Would something like this work for you?
Code:
Private Sub Command1_Click()
Dim strHex As String
strHex = Hex(Me.BackColor)
Text1.Text = "&H" & IIf(Len(strHex) < 6, String(6 - Len(strHex), "0") & strHex, strHex) & "&"
End Sub
-
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.
-
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.
-
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
-
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)
-
Re: hex colour
-
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.
-
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.
-
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
-
Re: hex colour
Ok thanks. I'll try switching them around.