[Resolved] Color as an object

I’m not sure if this is a Corona or Lua question. I would like to be able to have some standard colors I use through out my app. For example, a green for when good things happen and a red for when bad things happen.

Something like,

local GREEN = {r=34, g=224, b=27, a=200}  
local RED = {r=209, g=29, b=41, a=150}  

So, instead of writing

text:setTextColor(34, 224, 27, 200)  

I’d much rather write something like

text:setTextColor(GREEN)  

That way when a client comes back and says the green is hurting their eyes and they simply must have 2 more bits of blue, I can make the change in one spot instead of the 27 spots where that color appears and all is good.

Is there a simple way of doing this that I’m overlooking due to my overwhelming Lua Newbiosity?

Thanks. [import]uid: 161992 topic_id: 32301 reply_id: 332301[/import]

I think most Corona SDK API calls want each color as individual parameters. I know some external libraries and modules use table data for it. Lua functions can return multiple parameters and you should be able to do something like this:

local C\_RED = {255,0,0}  
local C\_GREEN = {0, 255, 0}  
local C\_BLUE = {0,0,255}  
  
local function getColor(color)  
 return color[1], color[2], color[3]  
end  
  
someTextObject:setTextColor(getColor(C\_RED), 255) -- keep the alpha channel separate  

Now to be honest, I’ve not tested this and I don’t know if you can have a function return multiple parameters and they get passed on the stack to another function, but you could give this a try and see if it does what you want to do

[import]uid: 19626 topic_id: 32301 reply_id: 128496[/import]

Example, not the best by any means- but just to share as a sample-

[lua]–main.lua
local colors = require “colors”

local bg = display.newRect( 0, 0, 320, 480 )
bg:setFillColor( colors.white() )

local test = display.newRect( 100, 200, 100, 100 )
test:setFillColor(colors.blue())[/lua]

[lua]–colors.lua
function black ()
return 0, 0, 0
end

function white ()
return 255, 255, 255
end

function red ()
return 255, 0, 0
end

function blue ()
return 0, 0, 255
end

function green ()
return 0, 255, 0
end[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 32301 reply_id: 128498[/import]

Thank you both. That’s what I was looking for. [import]uid: 161992 topic_id: 32301 reply_id: 128503[/import]

unpack will work

http://www.lua.org/pil/5.1.html

local GREEN = {r=34, g=224, b=27, a=200} rect:setFillColor(unpack(GREEN)) [import]uid: 160496 topic_id: 32301 reply_id: 128509[/import]

Mike is correct… “unpack” will work nicely and it looks clean in code. I’ll just note that it’s one of the “slower” native functions in Lua, so be careful where you use it. You won’t notice any performance hit if you use this in some kind of scene setup… just don’t use it in some in-action sequence that has to loop through 400 objects and fill them all with the color you’re specifying.

Brent [import]uid: 9747 topic_id: 32301 reply_id: 128567[/import]

I think most Corona SDK API calls want each color as individual parameters. I know some external libraries and modules use table data for it. Lua functions can return multiple parameters and you should be able to do something like this:

local C\_RED = {255,0,0}  
local C\_GREEN = {0, 255, 0}  
local C\_BLUE = {0,0,255}  
  
local function getColor(color)  
 return color[1], color[2], color[3]  
end  
  
someTextObject:setTextColor(getColor(C\_RED), 255) -- keep the alpha channel separate  

Now to be honest, I’ve not tested this and I don’t know if you can have a function return multiple parameters and they get passed on the stack to another function, but you could give this a try and see if it does what you want to do

[import]uid: 19626 topic_id: 32301 reply_id: 128496[/import]

Example, not the best by any means- but just to share as a sample-

[lua]–main.lua
local colors = require “colors”

local bg = display.newRect( 0, 0, 320, 480 )
bg:setFillColor( colors.white() )

local test = display.newRect( 100, 200, 100, 100 )
test:setFillColor(colors.blue())[/lua]

[lua]–colors.lua
function black ()
return 0, 0, 0
end

function white ()
return 255, 255, 255
end

function red ()
return 255, 0, 0
end

function blue ()
return 0, 0, 255
end

function green ()
return 0, 255, 0
end[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 32301 reply_id: 128498[/import]

Thank you both. That’s what I was looking for. [import]uid: 161992 topic_id: 32301 reply_id: 128503[/import]

unpack will work

http://www.lua.org/pil/5.1.html

local GREEN = {r=34, g=224, b=27, a=200} rect:setFillColor(unpack(GREEN)) [import]uid: 160496 topic_id: 32301 reply_id: 128509[/import]

Mike is correct… “unpack” will work nicely and it looks clean in code. I’ll just note that it’s one of the “slower” native functions in Lua, so be careful where you use it. You won’t notice any performance hit if you use this in some kind of scene setup… just don’t use it in some in-action sequence that has to loop through 400 objects and fill them all with the color you’re specifying.

Brent [import]uid: 9747 topic_id: 32301 reply_id: 128567[/import]