I need to convert Hex into RGB anyone know how ??
Printable View
I need to convert Hex into RGB anyone know how ??
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])
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)
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.Code:bytRed = lngColour And vbRed
bytGreen = (lngColour And vbGreen) / 255
bytBlue = (lngColour And vbBlue) / (255 * 255)
Hope this helps
the hex is a string from a text box
I guess that I was thinking about different meaning of the word HEX :D
What do you mean by:
???Quote:
The hex is a string from a text box
[Edited by QWERTY on 04-24-2000 at 11:06 PM]
I imagine you mean that in a text box you write
FF0000
and a "something" goes Blue
Try this:
then use Sam Finches algorithm above to cut the long into RGBCode:Dim MyColour as Long
MyColour = Clng("&H" & Text1.text)
[Edited by Paul282 on 04-24-2000 at 11:14 PM]
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?
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
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
Hello XxEvilxX,
Look at our answers on site:
http://forums.vb-world.net/showthrea...threadid=10324
Good luck,
Michelle.