Discord
Login
Community
DARK THEME

Get pixel color at coordinate ?

Hello Is there way to get color pixel at 100,100(example) ??

You can do it putting what you want to get the pixel from in an Image, like this

init = function()
  i = new Image(100, 100, true)
  i.clear("rgb(255,0,0)")
  i.fillRect(0, 0, 25, 25, "rgb(0,255,0)" )
end

update = function()
  a = i.getRGB(0, 0)
  print( a )
end

draw = function()
  screen.clear()
  screen.drawImage(i, 0, 0, i.width, i.height)
end

The variable a will be an object with the attributes R, G, B (and A if you use getRGBA). This function is documented in QuickReference->API Cheatsheet->Images.

Be careful because the x and y of getRGB start from the top left of the image

If you don't want to be tied to a specific image area you could combine above mentioned with Gilles Screen Capture example.
That would capture the whole screen and using the getRGB() function as iviaurizio showed you you could check any pixel.

https://microstudio.dev/i/gilles/screencapture2/

There's no way to embed function like getPixelRGB() ???

Yes, you can, the principle is explained above. But be aware:

The screen coordinates are in units, not pixels. So they are independent of the target devices (tablet, phone, pc) screen resolution.

So it really depends on what you actually try to do. How do you create your screen. You use the drawing functions (line, circle, etc.), or you place sprites? Again, each is drawn in units, not pixels.

Where are the coordinates you would like to check comming from? Are you checking the whole screen?

The above mentioned suggestion do what you ask for, they get the color from a screen position, or from an image area.

Maybe check the discord, there are discossions about this. Or otherwise tell us in more detail about what you try to do.

:)

P.S. check the Screen Size thread here on the forum too.

https://microstudio.dev/community/help/screen-width-and-height/93/

Here a simple example :
https://microstudio.io/i/TinkerSmith/getrgb/

:embed https://microstudio.io/TinkerSmith/getrgb/

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community