I wrote simple lua-script that allows you to use hex and rgb colors (#ffaaff or 198, 0, 198 etc.). It works with Corona SDK too.
Here it is: GitHub - andrewyavors/Lua-Color-Converter: Converts your hex or RGB values into Corona/Defold-compatible format · GitHub
I’ll be glad if it helps you.
Super useful, great work! ![]()
Nice! Thank you for sharing!
This does not work.
You should return alpha as fourth component. Something like this:
function to_color(hex, alpha)
local r, g, b = hex:match("(%w%w)(%w%w)(%w%w)")
r = (tonumber(r, 16) or 0) / 255
g = (tonumber(g, 16) or 0) / 255
b = (tonumber(b, 16) or 0) / 255
return r, g, b, alpha or 1
end
Ah, yes, @dmitriy is correct. The example in the readme will not behave properly:
go.set("#label", "color", vmath.vector4(color.rgb(255, 16, 174), 1)
The reason is that with multiple return values only the first value will be used if there are more arguments, in this case the 1 for alpha. It will result in “bad argument #3 to ‘vector4’ (number expected, got no value)”
Hi!
Thank you for this useful script, but it looks like the conversion from hex doesn’t work, or at least I didn’t manage to make it work:
local converted_color = vmath.vector4(color.hex("#ff00ff", 0.5))
go.set("#sprite", "tint", converted_color)
An error message is generated:

It works as intended with the conversion from RGB/RGBA.
Am I missing something? ![]()
Seems to be an error in the library code on L4 it captures 4 groups but only unpacks 3. Changing the line to local redColor,greenColor,blueColor=hex:match('#?(..)(..)(..)') would work better. There is also this color module that I made, but it might be a bit excesive for your user case however.
I forgot to answer, but thanks, you were right ![]()
I’ve been working with this function and it seems to work without any issues, hopefuly this will help someone
local function to_color(hex, alpha)
local r, g, b = hex:match("(%w%w)(%w%w)(%w%w)")
r = (tonumber(r, 16) or 0) / 255
g = (tonumber(g, 16) or 0) / 255
b = (tonumber(b, 16) or 0) / 255
local a = alpha or 1
return vmath.vector4(r, g, b, a)
end
And in any script that has this function we can use it as:
function init(self)
-- Change background color on initialization of this script
msg.post("@render:", "clear_color", { color = to_color("#ffd833", 1) })
-- Change player color sprite
go.set("#sprite","tint", to_color("#389aff", 1))
}