Get color of pixel under mouse cursor

BlitzMax Forums/BlitzMax Beginners Area/Get color of pixel under mouse cursor

I would like to be able to get the RGB value of the pixel under the mouse cursor, but I can't seem to find a command that will help me do this.

What I had in mind is similar to the 'eyedropper' tool in most paint and photo editing programs, allowing the user to copy the color clicked on.

Is there a reciprocal to the 'Plot' command?

Thanks,

-Medicine Storm

Grab the screen to a pixmap then use readpixel?

Think you need to grabpixmap and then readpixel.
<edit> Argh

SuperStrict

Framework brl.blitz
Import brl.glmax2d

SetGraphicsDriver(GLMax2DDriver())

Graphics(800, 600, 0)


While Not KeyDown(KEY_ESCAPE) And Not AppTerminate()
	
	Cls()
	
	SetColor(225, 10, 80)
	DrawRect(150, 150, 100, 100)
	
	DoColorPicker()
	
	Flip()
	
Wend


Function DoColorPicker()
  Local temp:TPixmap, rgba:Int
	
	temp = GrabPixmap(MouseX(), MouseY(), 1, 1)
	rgba = temp.ReadPixel(0, 0)
	temp = Null
	
	SetColor(255, 255, 255)
	DrawText("Color: " + RGBA_GetRed(rgba) + ", " + RGBA_GetGreen(rgba) + ", " + RGBA_GetBlue(rgba), 0, 0)
	
End Function

Function RGBA_GetRed:Int(rgba:Int)
	
	Return((rgba Shr 16) & $FF)
	
End Function
              
Function RGBA_GetGreen:Int(rgba:Int)
	
	Return((rgba Shr 8) & $FF)
	
End Function
	
Function RGBA_GetBlue:Int(rgba:Int)
	
	Return(rgba & $FF)
	
End Function
	
Function RGBA_GetAlpha:Int(rgba:Int)
	
	Return((rgba:Int Shr 24) & $FF)
	
End Function



@Plash:

Fast, easy and exactly what I was looking for.

Thank you!

-Medicine Storm

Color box..
SuperStrict

Framework brl.blitz
Import brl.glmax2d

SetGraphicsDriver(GLMax2DDriver())

Graphics(800, 600, 0)


While Not KeyDown(KEY_ESCAPE) And Not AppTerminate()
	
	Cls()
	
	SetColor(225, 10, 80)
	DrawRect(150, 150, 100, 100)
	SetColor(29, 150, 40)
	DrawRect(220, 350, 175, 100)
	
	DoColorPicker()
	
	Flip()
	
Wend


Function DoColorPicker()
  Local temp:TPixmap, rgba:Int, cred:Int, cgreen:Int, cblue:Int
	
	temp = GrabPixmap(MouseX(), MouseY(), 1, 1)
	rgba = temp.ReadPixel(0, 0)
	temp = Null
	
	cred = RGBA_GetRed(rgba)
	cgreen = RGBA_GetGreen(rgba)
	cblue = RGBA_GetBlue(rgba)
	
	SetColor(255, 255, 255)
	DrawText("Color: " + cred + ", " + cgreen + ", " + cblue, 0, 0)
	
	DrawRect(9, 39, 52, 52)
	SetColor(cred, cgreen, cblue)
	DrawRect(10, 40, 50, 50)
	
End Function

Function RGBA_GetRed:Int(rgba:Int)
	
	Return((rgba Shr 16) & $FF)
	
End Function
              
Function RGBA_GetGreen:Int(rgba:Int)
	
	Return((rgba Shr 8) & $FF)
	
End Function
	
Function RGBA_GetBlue:Int(rgba:Int)
	
	Return(rgba & $FF)
	
End Function
	
Function RGBA_GetAlpha:Int(rgba:Int)
	
	Return((rgba:Int Shr 24) & $FF)
	
End Function


Note that reading the backbuffer into a pixmap is NOT a fast process, relatively speaking. Data has to be transferred over the graphics bus. Even reading a small area as a pixmap can affect your framerate, so it's not something that you want to do lots of times. If you need to read many pixels, read a rectangular area and then pick out the pixels you want.