|
-
Apr 24th, 2000, 07:58 AM
#1
Thread Starter
Addicted Member
I need to convert Hex into RGB anyone know how ??
-
Apr 24th, 2000, 08:38 AM
#2
Frenzied Member
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])
-
Apr 24th, 2000, 08:43 AM
#3
Frenzied Member
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
-
Apr 24th, 2000, 09:19 AM
#4
Thread Starter
Addicted Member
the hex is a string from a text box
-
Apr 24th, 2000, 10:04 AM
#5
Fanatic Member
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]
-
Apr 24th, 2000, 10:13 AM
#6
Fanatic Member
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]
-
Apr 24th, 2000, 10:52 AM
#7
Thread Starter
Addicted Member
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?
-
Apr 24th, 2000, 12:14 PM
#8
Frenzied Member
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
-
Apr 24th, 2000, 12:25 PM
#9
Fanatic Member
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
-
Apr 24th, 2000, 02:30 PM
#10
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|