erdos/src/draw/color.lua

33 lines
689 B
Lua

local f = (love.getVersion or
function() return love._version_major
end)() == 0 and 1 or 255
return function(draw)
function draw:color(r, g, b, a)
a = a or 255
love.graphics.setColor(r / f, g / f, b / f, a / f)
return self
end
function draw:colorT(t)
return self:color(t[1], t[2], t[3], t[4])
end
function draw:colorH(hex)
hex = (tostring(hex) or ''):lower()
local r, g, b, a = hex:match '#?(%x%x?)(%x%x?)(%x%x?)'
r = #r == 1 and r..r or r
g = #g == 1 and g..g or g
b = #b == 1 and b..b or b
if a then a = #a == 1 and a..a or a end
return self:color(
tonumber(r, 16), tonumber(g, 16),
tonumber(b, 16),
not a and 255 or tonumber(a, 16)
)
end
end