New creation methods for object libs.

This commit is contained in:
jussi
2024-04-18 13:33:58 +03:00
parent 70b40f6782
commit bdd660be01
7 changed files with 197 additions and 37 deletions

View File

@@ -65,6 +65,17 @@ function Color:newT( t )
return object
end
function Color:newC( c )
local object = setmetatable( {}, metatable )
object.r = c.r
object.g = c.g
object.b = c.b
object.a = c.a
return object
end
function Color:set( r, g, b, a )
self.r = r or 255
self.g = g or 255
@@ -76,6 +87,13 @@ function Color:setT( t )
self.r, self.g, self.b, self.a = table.unpack( t )
end
function Color:setC( c )
self.r = c.r
self.g = c.g
self.b = c.b
self.a = c.a
end
function Color:arr()
return { self.r, self.g, self.b, self.a }
end
@@ -145,6 +163,8 @@ function Color:lerp( color, amount )
)
end
-- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100
local tempPool = {}
local curTemp = 1
@@ -153,7 +173,6 @@ for _ = 1, TEMP_COUNT do
table.insert( tempPool, Color:new( 255, 255, 255, 255 ) )
end
-- Uses pre generated objects to avoid "slow" table generation.
function Color:temp( r, g, b, a )
local object = tempPool[ curTemp ]
curTemp = curTemp + 1
@@ -170,7 +189,6 @@ function Color:temp( r, g, b, a )
return object
end
-- Uses pre generated objects to avoid "slow" table generation.
function Color:tempT( t )
local object = tempPool[ curTemp ]
curTemp = curTemp + 1
@@ -184,6 +202,22 @@ function Color:tempT( t )
return object
end
function Color:tempC( c )
local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then
curTemp = 1
end
object.r = c.r
object.g = c.g
object.b = c.b
object.a = c.a
return object
end
function Color:getTempId()
return curTemp
end