Dealing with new 0-1 color range
Posted: Wed Apr 11, 2018 10:05 am
How do I detect pixels in an image which are set to, say, (128, 0, 0, 0) in a paint program when newImageData:getPixel doesn't return 0.5 for the red component?
Code: Select all
function why(float, int) return math.abs(float - (int/255)) < 0.004 end
Code: Select all
function WHY(float) return math.floor(float*255) end
I'd recommend this approach but adding rounding (just in case default rounding falls short of an integer by just a tiny bit during the conversions).zorg wrote: ↑Wed Apr 11, 2018 10:17 am or if you prefer to just convert the value to whole numbers again:Code: Select all
function WHY(float) return math.floor(float*255) end
Code: Select all
function WHY(float) return math.floor(float*255+0.5) end
Code: Select all
local function rgba2raw(r, g, b, a)
return
math.floor(r * 255 + .5),
math.floor(g * 255 + .5),
math.floor(b * 255 + .5),
a and math.floor(a * 255 + .5)
end
local function color2raw(r, g, b, a)
if type(r) == 'table' then
r, g, b, a = unpack(r)
end
return rgba2raw(r, g, b, a)
end
local function raw2color(r, g, b, a)
if type(r) == 'table' then
r, g, b, a = unpack(r)
end
return r / 255, g / 255, b / 255, a and a / 255
end
local function mapRaw(fn, x, y, r, g, b, a)
return raw2color(fn(x, y, rgba2raw(r, g, b, a)))
end
local ImageData = debug.getregistry().ImageData
function ImageData:getRawPixel(x, y)
return color2raw(self:getPixel(x, y))
end
function ImageData:setRawPixel(x, y, r, g, b, a)
return self:setPixel(x, y, raw2color(r, g, b, a))
end
function ImageData:mapRawPixel(fn)
return self:mapPixel(function(x, y, r, g, b, a)
return mapRaw(fn, x, y, r, g, b, a)
end)
end
function love.graphics.setRawColor(r, g, b, a)
return love.graphics.setColor(raw2color(r, g, b, a))
end
function love.graphics.rawClear(r, g, b, a, stencil, depth)
r, g, b, a = raw2color(r, g, b, a)
return love.graphics.clear(r, g, b, a, stencil, depth)
end
I'm totally pro-normalization: 0-1 allows for any image bit depth while 0-255 is 24-bit specific and archaic, and of course it means no translation to/from shaders, etc.