Get screen pixel color

Hi All,
Working in Perspective, Gateway 8.0.16.

I’m attempting to get the color of a pixel at the user’s mouse coordinates.

I found this forum post from over a decade ago, which solved the problem in Vision with a few lines of Java.

Updating the original solution for Perspective, I put the following code in my view.root’s mouse OnClick event:

	import java.awt.Robot 
	robot = java.awt.Robot() 

	try:
		x = event.clientX
		y = event.clientY
		colour = robot.getPixelColor(x, y)
		
		system.perspective.print('Color: ' + str(colour))
	except Exception as e:
		system.perspective.print(str(e))

The code doesn’t error out, but it always prints
Color: java.awt.Color[r=0,g=0,b=0],
even though the root is completely colored (not black). Anybody know if this solution should port over to Perspective, or another way to get color of a specific pixel?

Thanks in advance.

It does not. Someone would probably have to write a “Color Picker” component Module with the SDK to extend Perspective with this.

You can’t use Java methods directly, and even in a Perspective module you’ll have a hard time doing this in a satisfactory way. You can only retrieve colors from HTML canvas elements, or images converted into canvas elements, it looks like; and even then, only from the same origin (due to browser security restrictions).
A potentially better option, but one highly dependent on your environment, would be a color input element, which might, depending on your browser + platform, have an eyedropper:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/color
But that would be very flakey, and requires a custom module as well.

1 Like

Thanks for the help @pturmel and @PGriffith.