Results 1 to 5 of 5

Thread: Pixel colour

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2000
    Location
    Peterborough, Cambs, England
    Posts
    176
    Hi

    I need to find out the RGB of a pixel from a picture box control.

    When I use Picture1.Point(x, y) it return a long integer.

    Is there anyway to convert this into seperate RGB colours to store in variables?

  2. #2
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Sedgefield
    Posts
    337

    Talking

    The valid range for a normal RGB color is 0 to 16,777,215 (&HFFFFFF&). Each color setting (property or argument) is a 4-byte integer. The high byte of a number in this range equals 0. The lower 3 bytes, from least to most significant byte, determine the amount of red, green, and blue, respectively. The red, green, and blue components are each represented by a number between 0 and 255 (&HFF).

    Consequently, you can specify a color as a hexadecimal number using this syntax:

    &HBBGGRR&

    The BB specifies the amount of blue, GG the amount of green, and RR the amount of red. Each of these fragments is a two-digit hexadecimal number from 00 to FF. The median value is 80. Thus, the following number specifies gray, which has the median amount of all three colors:

    &H808080&

    Setting the most significant bit to 1 changes the meaning of the color value: It no longer represents an RGB color, but an environment-wide color specified through the Windows Control Panel. The values that correspond to these system-wide colors range from &H80000000 to &H80000015.

    Note Although you can specify over 16 million different colors, not all systems are capable of displaying them accurately. For more information on how Windows represents colors, see "Working with 256 Colors" later in this chapter.
    From the horses mouth

  3. #3
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    the best way is to use the copymemory API.


    Code:
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    
    Private Type RGBTRIPLE
            rgbtRed As Byte
            rgbtGreen As Byte
            rgbtBlue As Byte
    End Type
    
    
    Private Function SplitColour(ByVal Colour As Long) As RGBTRIPLE
    
    
    CopyMemory SplitColour, Colour, 3
    
    
    End Function
    hope this helps

  4. #4
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Sedgefield
    Posts
    337

    Cool Or the non-api route....

    Code:
    Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
        Dim res As Long
        Dim str As String
        Dim sRed As String
        Dim sGreen As String
        Dim sBlue As String
        Dim iRed As Integer
        Dim iGreen As Integer
        Dim iBlue As Integer
        
        res = Picture1.Point(x, Y)
        str = Hex(res)
        
        sRed = Right(str, 2): iRed = CHex(sRed)
        sGreen = Mid(str, 3, 2): iGreen = CHex(sGreen)
        sBlue = Left(str, 2): iBlue = CHex(sBlue)
        
    End Sub
    Private Function CHex(ByVal x As String) As Long
    Dim lDec As Long
    Dim i As Long
    Dim j As Long
    
    For i = Len(x) To 1 Step -1
        j = 16 ^ (Len(x) - i)
        Select Case Mid$(x, i, 1)
            Case "0"
                lDec = lDec + (j * 0)
            Case "1"
                lDec = lDec + (j * 1)
            Case "2"
                lDec = lDec + (j * 2)
            Case "3"
                lDec = lDec + (j * 3)
            Case "4"
                lDec = lDec + (j * 4)
            Case "5"
                lDec = lDec + (j * 5)
            Case "6"
                lDec = lDec + (j * 6)
            Case "7"
                lDec = lDec + (j * 7)
            Case "8"
                lDec = lDec + (j * 8)
            Case "9"
                lDec = lDec + (j * 9)
            Case "A"
                lDec = lDec + (j * 10)
            Case "B"
                lDec = lDec + (j * 11)
            Case "C"
                lDec = lDec + (j * 12)
            Case "D"
                lDec = lDec + (j * 13)
            Case "E"
                lDec = lDec + (j * 14)
            Case "F"
                lDec = lDec + (j * 15)
        End Select
    Next i
    CHex = lDec
    End Function
    You should be able to do something now!!!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2000
    Location
    Peterborough, Cambs, England
    Posts
    176
    Thanks for the help.

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