ReadPixel ( x,y[,buffer] )

Parameters

x - x coordinate of the pixel

y - y coordinate of the pixel

buffer (optional) - buffer to read from, e.g. BackBuffer(); the current buffer set with SetBuffer (default)

Description

Returns the colour of a pixel as an ARGB integer.

The result packs alpha, red, green and blue into one integer - blue in the low byte, then green, then red, with alpha at the top. To pull out the parts:

red = (argb Shr 16) And 255
green = (argb Shr 8) And 255
blue = argb And 255

Handy for colour-keyed logic: reading a level layout from an image, sampling a terrain map, checking what a bullet just hit on a paint-style destructible landscape. The coordinates respect the current Origin, and reading outside the Viewport safely returns the buffer's mask colour rather than an error.

ReadPixel is fine for occasional lookups, but it is slow for whole-image work - for tight loops over many pixels, use ReadPixelFast between LockBuffer and UnlockBuffer.

See also: WritePixel, ReadPixelFast, GetColor, LockBuffer.

Example

; ReadPixel Example
; -----------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

; A colourful photo to inspect
logo=LoadImage("media/b3dlogo.jpg")

While Not KeyDown(1)

    Cls
    DrawImage logo,120,140

    ; ReadPixel returns the packed ARGB value of the pixel under the mouse
    argb=ReadPixel(MouseX(),MouseY())

    ; Split the packed value into its colour components
    r=(argb Shr 16) And 255
    g=(argb Shr 8) And 255
    b=argb And 255

    ; Show the colour we read as a swatch next to the pointer
    Color r,g,b
    Rect MouseX()+12,MouseY()+12,24,24,1
    Color 255,255,255
    Rect MouseX()+11,MouseY()+11,26,26,0

    Text 0,0,"Move the mouse over the photo   Esc: exit"
    Text 0,20,"ReadPixel("+MouseX()+","+MouseY()+") = "+argb+"  ->  RGB "+r+","+g+","+b

    Flip

Wend

End

Index