From 6302602a458b03eeabc5ee9023a478330efe47f2 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Tue, 18 Dec 2018 00:07:39 -0400 Subject: [PATCH] Add love.math.colorToBytes and colorFromBytes, simple functions to convert between 0-1 and 0-255 (byte) colors. --- src/modules/math/wrap_Math.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/modules/math/wrap_Math.lua b/src/modules/math/wrap_Math.lua index ffb6a16d3..84527d174 100644 --- a/src/modules/math/wrap_Math.lua +++ b/src/modules/math/wrap_Math.lua @@ -58,6 +58,28 @@ function love_math.getRandomState() return rng:getState() end +function love_math.colorToBytes(r, g, b, a) + if type(r) == "table" then + r, g, b, a = r[1], r[2], r[3], r[4] + end + r = floor(clamp01(r) * 255 + 0.5) + g = floor(clamp01(g) * 255 + 0.5) + b = floor(clamp01(b) * 255 + 0.5) + a = a ~= nil and floor(clamp01(a) * 255 + 0.5) or nil + return r, g, b, a +end + +function love_math.colorFromBytes(r, g, b, a) + if type(r) == "table" then + r, g, b, a = r[1], r[2], r[3], r[4] + end + r = clamp01(floor(r + 0.5) / 255) + g = clamp01(floor(g + 0.5) / 255) + b = clamp01(floor(b + 0.5) / 255) + a = a ~= nil and clamp01(floor(a + 0.5) / 255) or nil + return r, g, b, a +end + if type(jit) ~= "table" or not jit.status() then -- LuaJIT's FFI is *much* slower than LOVE's regular methods when the JIT -- compiler is disabled.