Converting imageData:getPixel() numbers to the 0-255 range

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
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Converting imageData:getPixel() numbers to the 0-255 range

Post by Doctory »

So I recently came back to love after quite some time and decided to look at my old project. For some reason it didn't work and the culprit is imageData:getPixel() returning numbers from 0-1 instead of 0-255. This is a big deal because I created maps from reading image files and it depended on the color of the pixel to create tiles as you can see in these files: https://github.com/stefankoxd/overly-en ... verter.lua and https://github.com/stefankoxd/overly-en ... ainter.lua

Is there any way to fix this?
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Converting imageData:getPixel() numbers to the 0-255 range

Post by grump »

Code: Select all

math.floor(c * 255 +.5)
should do the trick.

Or use cindy. Either call require('cindy').applyPatch() to have the old behavior restored in all relevant functions, or use the provided *Bytes alternatives.

Code: Select all

function converter:imagetomap(img)
	local pixelData = {}
	local function map(x, y, r, g, b, a) 
		pixelData[#pixelData + 1] = { x = x, y = y, r = r, g = g, b = b }
		return r, g, b, a
	end

	img:mapPixelBytes(map)
	return pixelData
end
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Converting imageData:getPixel() numbers to the 0-255 range

Post by Doctory »

grump wrote: Mon Feb 18, 2019 10:15 am -snip-
Cindy worked perfectly, thanks for the recommendation
Post Reply

Who is online

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