|
-
Jun 7th, 2002, 10:31 AM
#1
Sample - RGB from long, byte retrieval
Efficient way to get R,G,B out of a color value
Or to whack a long or integer into bytes....
[code]
In a .BAS module--------------
Code:
Private Declare Function LoByte Lib "TLBINF32" Alias "lobyte" _
(ByVal Word As Integer) As Byte
Private Declare Function HiByte Lib "TLBINF32" Alias "hibyte" _
(ByVal Word As Integer) As Byte
Private Declare Function loword Lib "TLBINF32" (ByVal DWord As Long) _
As Integer
Private Declare Function hiword Lib "TLBINF32" (ByVal DWord As Long) _
As Integer
Public Sub From_RGB(Color as Long, R as Byte, G as Byte,B as Byte)
R = LoByte(loword(Color)) ' Byte 1
G = HiByte(loword(Color)) ' Byte 2
B = LoByte(hiword(Color)) ' Byte 3
' HiByte(hiword(Color)) Byte 4 - no meaning here just a comment
End Sub
You can also use the api calls above to get any byte out of a two or four byte integer or long.
-
Jun 7th, 2002, 10:42 AM
#2
Addicted Member
Using LSet is faster:
VB Code:
Public Type LongUDT
Value as Long
End Type
Public Type BGRAUDT
Blue as Byte
Green as Byte
Red as Byte
Alpha as Byte
End Type
Dim LU as LongUDT, Color as BGRAUDT
' ...
LU.Value = GetPixel(Me.hDC, X, Y)
LSet Color = LU
Color.Blue = Color.Blue \ 2
Color.Green = Color.Green \ 2
Color.Red = Color.Red \ 2
LSet LU = Color
Call SetPixelV(Me.hDC, X, Y, LU.Value)
' ...
In most situations this is the fastest method. (Even faster than copymemory.)
"1 4m 4 1337 #4xz0r!'
Janus
-
Jun 7th, 2002, 10:47 AM
#3
Retired VBF Adm1nistrator
Here's another approach
VB Code:
Private Type colTriplet
r As Long: g As Long: b As Long
End Type
Private Function longToTriplet(ByVal colLong As Long) As colTriplet
With longToTriplet
.r = (colLong And &HFF)
.g = (colLong \ &H100&) And &HFF&
.b = (colLong \ &H10000) And &HFF&
End With
End Function
... which is basically the same as jim's
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jun 7th, 2002, 04:28 PM
#4
Good Ol' Platypus
The best thing about LSet or CopyMemory (btw, LSet wins by about 3ms for every 100 times called, I did a test) is that it isn't done with VB's slow math.
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
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
|