Clamp all color arguments to [0, 1] in cases where values outside that range don't make sense (fixed-point color values & sRGB colors). Closes issue #1315.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2017-11-21 19:49:52 -04:00
parent 1cd1a8d32e
commit 6dea2ab714
9 changed files with 59 additions and 36 deletions
+4 -6
View File
@@ -88,22 +88,20 @@ int w_ImageData_getDimensions(lua_State *L)
return 2;
}
// TODO: rgba16f
static void luax_checkpixel_rgba8(lua_State *L, int startidx, Pixel &p)
{
for (int i = 0; i < 3; i++)
p.rgba8[i] = (uint8) (luaL_checknumber(L, startidx + i) * 255.0);
p.rgba8[i] = (uint8) (luax_checknumberclamped01(L, startidx + i) * 255.0);
p.rgba8[3] = (uint8) (luaL_optnumber(L, startidx + 3, 1.0) * 255.0);
p.rgba8[3] = (uint8) (luax_optnumberclamped01(L, startidx + 3, 1.0) * 255.0);
}
static void luax_checkpixel_rgba16(lua_State *L, int startidx, Pixel &p)
{
for (int i = 0; i < 3; i++)
p.rgba16[i] = (uint16) (luaL_checknumber(L, startidx + i) * 65535.0);
p.rgba16[i] = (uint16) (luax_checknumberclamped01(L, startidx + i) * 65535.0);
p.rgba16[3] = (uint16) (luaL_optnumber(L, startidx + 3, 1.0) * 65535.0);
p.rgba16[3] = (uint16) (luax_optnumberclamped01(L, startidx + 3, 1.0) * 65535.0);
}
static void luax_checkpixel_rgba16f(lua_State *L, int startidx, Pixel &p)
+14 -9
View File
@@ -27,10 +27,15 @@ local ImageData = ImageData_mt.__index
local tonumber, assert, error = tonumber, assert, error
local type, pcall = type, pcall
local floor = math.floor
local floor = math.floor
local min, max = math.min, math.max
local function inside(x, y, w, h)
return x >= 0 and x < w and y >= 0 and y < h
end
local function clamp01(x)
return min(max(x, 0), 1)
end
-- Implement thread-safe ImageData:mapPixel regardless of whether the FFI is
@@ -111,10 +116,10 @@ local conversions = {
return tonumber(self.r) / 255, tonumber(self.g) / 255, tonumber(self.b) / 255, tonumber(self.a) / 255
end,
fromlua = function(self, r, g, b, a)
self.r = r * 255
self.g = g * 255
self.b = b * 255
self.a = a == nil and 255 or a * 255
self.r = clamp01(r) * 255
self.g = clamp01(g) * 255
self.b = clamp01(b) * 255
self.a = a == nil and 255 or clamp01(a) * 255
end,
},
rgba16 = {
@@ -123,10 +128,10 @@ local conversions = {
return tonumber(self.r) / 65535, tonumber(self.g) / 65535, tonumber(self.b) / 65535, tonumber(self.a) / 65535
end,
fromlua = function(self, r, g, b, a)
self.r = r * 65535
self.g = g * 65535
self.b = b * 65535
self.a = a == nil and 65535 or a * 65535
self.r = clamp01(r) * 65535
self.g = clamp01(g) * 65535
self.b = clamp01(b) * 65535
self.a = a == nil and 65535 or clamp01(a) * 65535
end,
},
rgba16f = {