Results 1 to 4 of 4

Thread: Sample - RGB from long, byte retrieval

  1. #1
    jim mcnamara
    Guest

    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.

  2. #2
    Addicted Member Janus's Avatar
    Join Date
    Aug 2001
    Location
    California
    Posts
    221
    Using LSet is faster:
    VB Code:
    1. Public Type LongUDT
    2.     Value as Long
    3. End Type
    4. Public Type BGRAUDT
    5.     Blue as Byte
    6.     Green as Byte
    7.     Red as Byte
    8.     Alpha as Byte
    9. End Type
    10.  
    11. Dim LU as LongUDT, Color as BGRAUDT
    12. ' ...
    13. LU.Value = GetPixel(Me.hDC, X, Y)
    14. LSet Color = LU
    15. Color.Blue = Color.Blue \ 2
    16. Color.Green = Color.Green \ 2
    17. Color.Red = Color.Red \ 2
    18. LSet LU = Color
    19. Call SetPixelV(Me.hDC, X, Y, LU.Value)
    20. ' ...

    In most situations this is the fastest method. (Even faster than copymemory.)
    "1 4m 4 1337 #4xz0r!'
    Janus

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Here's another approach

    VB Code:
    1. Private Type colTriplet
    2.     r As Long: g As Long: b As Long
    3. End Type
    4.  
    5. Private Function longToTriplet(ByVal colLong As Long) As colTriplet
    6.     With longToTriplet
    7.         .r = (colLong And &HFF)
    8.         .g = (colLong \ &H100&) And &HFF&
    9.         .b = (colLong \ &H10000) And &HFF&
    10.     End With
    11. End Function

    ... which is basically the same as jim's
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    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
  •  



Click Here to Expand Forum to Full Width