Using getPixel and setPixel

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
Maholain
Prole
Posts: 12
Joined: Tue Jun 23, 2009 4:29 am

Using getPixel and setPixel

Post by Maholain »

I was looking at the getPixel API and saw it's synopsis as this:

Code: Select all

r, g, b, a = ImageData:getPixel( x, y )
I was wondering, how would I compare what it returns in an if statement? I'm not sure what to do when it returns more than one thing.

Also, when I used setPixel in the update method, I got a 'attempt to access index 'ImageData'. Can someone tell me what is going wrong here?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Using getPixel and setPixel

Post by Robin »

Code: Select all

local r, g, b, a = ImageData:getPixel(x, y)
-- compare r, g, b, a
-- OR
local t = {ImageData:getPixel(x, y)} -- d'oh!
-- compare t[1], t[2], t[3], t[4]
Last edited by Robin on Mon Sep 27, 2010 6:22 am, edited 1 time in total.
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Using getPixel and setPixel

Post by bartbes »

That second one is wrong

Code: Select all

local t = {ImageData:getPixel(x, y)}
Maholain
Prole
Posts: 12
Joined: Tue Jun 23, 2009 4:29 am

Re: Using getPixel and setPixel

Post by Maholain »

I'm trying to set the pixel below the cursor to say green if the pixel below my cursor is black.

Code: Select all

--In load
coverData = love.image.newImageData("overlay.png");
--in Update
if love.mouse.isDown("l") then
		local r, g, b, a = coverData:getPixel(love.mouse.getX(), love.mouse.getY());
		if (r,g,b,a == 0, 0, 0, 255) then
			coverData:setPixel(love.mouse.getX(), love.mouse.getY(), 40, 40, 40, 255);
		end
	end
It doesn't work and I'm wondering if anyone can provide me with a working sample.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Using getPixel and setPixel

Post by thelinx »

what am I thinking
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Using getPixel and setPixel

Post by vrld »

thelinx wrote:dont look please
This won't work, because t is a different table than {0,0,0,255} even if t = {0,0,0,255}.
This should work:

Code: Select all

local r, g, b = coverData:getPixel(love.mouse.getX(), love.mouse.getY());
if r + g + b == 0 then
    coverData:setPixel(love.mouse.getX(), love.mouse.getY(), 40, 40, 40)
end
But expect it to be very, very slow...
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Maholain
Prole
Posts: 12
Joined: Tue Jun 23, 2009 4:29 am

Re: Using getPixel and setPixel

Post by Maholain »

Thanks. I still can't get my initial idea to work though. I think it's because

Code: Select all

coverData:setPixel(love.mouse.getX(), love.mouse.getY(), 40, 40, 40, 255);
is only setting the pixel on coverData. I can't draw a ImageData though, so how can I show this coverData on the screen?
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Using getPixel and setPixel

Post by thelinx »

Convert it into an image, draw that image.
Maholain
Prole
Posts: 12
Joined: Tue Jun 23, 2009 4:29 am

Re: Using getPixel and setPixel

Post by Maholain »

thelinx wrote:Convert it into an image, draw that image.
Neat! Thanks, problem solved. The code I used if anyone is interested:

Code: Select all

--This code will reveal what's behind it because of setPixel being set to invisible
function love.update(dt) 
	if love.mouse.isDown("l") then
		local r, g, b = coverData:getPixel(love.mouse.getX(), love.mouse.getY());
			if r + g + b == 0 then
				coverData:setPixel(love.mouse.getX(), love.mouse.getY(), 0, 0, 0, 0)
		end
	end
	imagesData = love.graphics.newImage(coverData);
end
function love.draw() 
	love.graphics.draw(imagesData, 0, 0);
end
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: Using getPixel and setPixel

Post by leiradel »

Maholain wrote:

Code: Select all

if (r,g,b,a == 0, 0, 0, 255) then
You can't compare multiple values like that AFAIK. Use

Code: Select all

if r == 0 and g == 0 and b == 0 and a == 255 then[/quote]

Summing r, g and b and comparing against zero is more efficient I think, but it only works for black (sum == 0) and white (sum == 765).

Cheers,

Andre
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 10 guests