|
-
Aug 21st, 2000, 02:41 PM
#1
Thread Starter
Lively Member
Now then, I know how to convert from RGB to long by using RGB(Red As Integer, Green As Integer, Blue As Integer), but how can I convert from a long value back to RGB?
It's puzzled me for a while and the only way I can think to do it is like this:
Code:
Dim redVal As Integer, greenVal As Integer, blueVal As Integer
Dim lngColour As Long
For redVal = 0 To 255
For greenVal = 0 To 255
For blueVal = 0 To 255
If RGB(redVal, greenVal, blueVal) = lngColour Then
'the RGB version of lngColour is redVal, greenVal, blueVal
Exit Sub
Else:
'wrong colour, keep trying
End If
Next blueVal
Next greenVal
Next redVal
This will work, but surely there's an easier way of doing it - this loops through 16 and a half million colours, and checks each one, and my computer crashes every time I try. I need to find it at the click of a mouse button, otherwise the purpose of the application is defied.
Anybody?
-
Aug 21st, 2000, 02:48 PM
#2
Monday Morning Lunatic
I answered this here:
http://forums.vb-world.net/showthrea...threadid=27064
although you may need to swap the blue and red around in the function, because it parses BGR values from the API.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 21st, 2000, 05:21 PM
#3
Fanatic Member
there are several ways of doing it. i can think of two, but this is my favorite:
Code:
Sub Value2RGB(color As Long, r As Long, g As Long, b As Long)
Dim a As String
a = Hex(color)
a = String(6 - Len(a), "0") + a
r = Val("&h" + Right(a, 2))
g = Val("&h" + Mid(a, 3, 2))
b = Val("&h" + Left(a, 2))
End Sub
you call it like this
Code:
Value2RGB color, r, g, b
'r, g, and b now contain the value of red, green, and blue (0 to 255)
-
Aug 21st, 2000, 05:58 PM
#4
Monday Morning Lunatic
agent - I don't like that method. Mine is safer, and considerably faster, because there is no need for string conversions - it's all done on the number, with integer arithmetic.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 21st, 2000, 06:03 PM
#5
So Unbanned
When I need RGB conversion I wrote my own code for it, pretty much the same as agent's and it worked perfectly.
-
Aug 21st, 2000, 06:37 PM
#6
Monday Morning Lunatic
Yes, but if you do it a lot, it really slows it down. Anyway, it's best to use the integer manipulations just from a tidiness point.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
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
|