Quad getPixel() or getPixels() not possible ?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
gcmartijn
Party member
Posts: 146
Joined: Sat Dec 28, 2019 6:35 pm

Quad getPixel() or getPixels() not possible ?

Post by gcmartijn »

H!,

I have a quad and need the get the pixels or getPixel, but that is not possible ?

https://love2d.org/wiki/Quad

And https://love2d.org/wiki/love.image.newImageData don't allow a Quad

https://love2d.org/wiki/ImageData

Thanks for the info?
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Quad getPixel() or getPixels() not possible ?

Post by pgimeno »

A quad is just a rectangle, so to say; it does not contain the image data itself, and you're right, ImageData does not support quads. But you can simulate it easily with Quad:getViewport:

Code: Select all

local function getQuadPixel(imgData, quad, x, y)
  local x0, y0 = quad:getViewport()
  return imgData:getPixel(x - x0, y - y0)
end
If that function could receive pixels outside the quad extents, and you want to do something about it, you'll have to account for the width and height of the quad as well:

Code: Select all

local function getQuadPixel(imgData, quad, x, y)
  local x0, y0, w, h = quad:getViewport()
  if x >= x0 and y >= y0 and x < x0 + w and y < y0 + h then
    return imgData:getPixel(x - x0, y - y0)
  end
  -- Do something to deal with coordinates out of the quad.
  -- In the example below, it reports an error.
  error("Coordinates out of the quad")
end
gcmartijn
Party member
Posts: 146
Joined: Sat Dec 28, 2019 6:35 pm

Re: Quad getPixel() or getPixels() not possible ?

Post by gcmartijn »

Ah din't know about the quad viewport. Thanks!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests