Corona SDK Pro Tip of the Day #14
Colors by name

Set colors by strings like 'green' or 'button'.

You can use object:setFillColor(1, 0, 0) to set red color, you can tell what color is it because you know by RGB definition (1, 0, 0) is pure red. Something harder than that will look more cryptic like object:setFillColor(0.65, 0.32, 0.47), what color is that?

Another problem with setting all the colors with numbers is that when you use the same color in different places and later you want to change it, you have to find all these places and change them manually.

It would be much easier to use something like CSS from HTML world. Define all the styles in one place and let your app use it throughout your app.

So I strongly advise not to use numeric values for colors, only as a part of a "style."

Another set of functions from my app.lua.

local _M = {}

local colors = require('lib.colors')  
function _M.setFillColor(object, color)  
    local rgb = colors[color]
    if rgb then
        object:setFillColor(unpack(rgb))
    else
        error('No such color: ' .. tostring(color), 2)
    end
end

function _M.setStrokeColor(object, color)  
    local rgb = colors[color]
    if rgb then
        object:setStrokeColor(unpack(rgb))
    else
        error('No such color: ' .. tostring(color), 2)
    end
end

return _M  

Just like in my previous pro tip, I take a color in RGB from a table and call setFillColor or setStrokeColor with help of unpack() function.

Color value can be in any format supported by Corona: {grey}, {r, g, b} or {r, g, b, a}.

I store all common color values in a separate file called colors.lua. You can grab it here. And I usually put all my Lua libraries in the lib directory, hence lib.colors.

Now you can write something like

app.setFillColor(myCircle, 'aquamarine')  

It's less pretty than myCircle:setFillColor('aquamarine'), but to achieve such functionality an extra hacking required. Each function that accepts colors in Corona API must be replaced with a wrapper, such hacking is just too much, I won't like it.

All extra colors that are missing from colors.lua you should add either directly to colors variable after requiring colors.lua or in a separate Lua file and extend colors with it. Which way you choose is a matter of taste, if you have a lot of extra colors, make a separate file for them.

First way:

local colors = require('lib.colors')  
colors['button-fill'] = colors.blue  
colors['button-stroke'] = colors.grey  
colors['background'] = {0.2}  
colors['bullet'] = {0.6, 0.5, 0.1, 0.4}  
colors['text'] = {0.8, 0.9, 0.9}  

Second way:
In app.lua

local colors = require('lib.colors')  
for k, v in pairs(require('lib.colors_style')) do  
    colors[k] = v
end  

colors_style.lua

local colors = require('lib.colors')  
return {  
    ['button-fill'] = colors.blue,
    ['button-stroke'] = colors.grey,
    background = {0.2},
    bullet = {0.6, 0.5, 0.1, 0.4},
    text = {0.8, 0.9, 0.9}
}

Notice that extra colors don't have usual color names, instead they are named by their use in an app. This is just like CSS, we can use normal color names from colors.lua or define new colors for our UI.

Say you had everywhere red buttons. You can write app.setFillColor(button, 'red'), but later you changed your design and now you want all your buttons to be orange. You can simply change definition of 'red' to become orange, but it will look really strange.

Instead name colors by their use: app.setFillColor(button, 'button') and define 'button' in colors_style.lua or directly in colors table.

Indie Game Developer