; ReadPixelFast Example
; ---------------------

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

t#=0

While Not KeyDown(1)

    Cls
    t=t+4

    ; The fast pixel commands require a locked buffer
    LockBuffer BackBuffer()

    ; Write a plasma pattern pixel by pixel into a small panel
    For y=0 To 95
        For x=0 To 127
            v#=Sin(x*4+t)+Sin(y*5-t)+Sin((x+y)*3+t*2)
            r=128+v*42
            g=128+Sin(v*60+t)*127
            b=255-r
            WritePixelFast 256+x,192+y,$FF000000 Or (r Shl 16) Or (g Shl 8) Or b
        Next
    Next

    ; Clamp the mouse position into the plasma panel...
    mx=MouseX()
    my=MouseY()
    If mx<256 Then mx=256
    If mx>383 Then mx=383
    If my<192 Then my=192
    If my>287 Then my=287

    ; ...and read the ARGB value there back with ReadPixelFast
    argb=ReadPixelFast(mx,my)

    ; Unlock before using any other drawing command
    UnlockBuffer BackBuffer()

    ; 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
    Color r,g,b
    Rect 20,60,40,40,1
    Color 255,255,255
    Rect 255,191,130,98,0
    Text 0,0,"Move the mouse over the plasma panel   Esc: exit"
    Text 0,20,"ReadPixelFast("+mx+","+my+") = RGB "+r+","+g+","+b

    Flip

Wend

End
