Set a textColor in a variable?

I may have overlooked something, but I’ve checked the documentation for newText:setTextColor.

Am I right in assuming that the only way to store a color in 1 variable as opposed to 3, and passing it as one parameter as opposed to 3, is to create a gradient?

I’m also a bit weak on how these “interpreted parameters depending on their count and value” overloaded(?) variants of setTextColor work. Is it really a lua feature, since variables arent type’d in lua? Or does the setTextColor method interpret the parameters passed explicitly inside the one method and switch to different code behaviors ?

I was also wondering if a generic RGBA color object has been considered for use with other graphics objects at one point?

Hey,

I’m not too sure if I understand what you’re asking.  The overloaded nature of this method is that it accepts 3 arguments for the RGB value or 4 aguments if you also include the A (Alpha) value.  When you set the RGB or the RGBA of the particular display object you are only setting one colour on that object.

e.g.

[lua]

text1:setTextColor(255,0,0) – sets text to red

text2:setTextColor(255,0,0,100) – sets text to red, and makes the variable slightly transparent

[/lua]

If you provide only the setTextColor() function with 3 aguments then it will just default the A (Alpha) property to 255 (meaning that the color is completely opaque).

But this all depends on whether I have fully understood your question or not…  :slight_smile:  Please elaborate further if I haven’t

Rich

To set color, you must provide at least 3 subcolors of RGB pallete (red, green and blue). You cannot pass other color format as for example hex one.

However you can wrap this 3 values with table and then in function unpack it. Short remind, unpack() function is not the fastest one so probably try to not use it in time critical parts of code.

[lua]
local text = display.newText (…)
local myColor = { 255, 10, 120 }
text:setTextColor( unpack(myColor) )
[/lua]

Exactly as @piotrz55 says, “unpack()” is fine to use, but it’s not the fastest Lua function. If for some reason you’re using this in time-critical code, something like this may be better (because you know there are 3 color channel parameters, or 4 if you opt to add alpha to the mix):

[lua]

local myColor = { 255, 10, 120 }

text:setTextColor( myColor[1], myColor[2], myColor[3] )

[/lua]

Best regards,

Brent

Hey,

I’m not too sure if I understand what you’re asking.  The overloaded nature of this method is that it accepts 3 arguments for the RGB value or 4 aguments if you also include the A (Alpha) value.  When you set the RGB or the RGBA of the particular display object you are only setting one colour on that object.

e.g.

[lua]

text1:setTextColor(255,0,0) – sets text to red

text2:setTextColor(255,0,0,100) – sets text to red, and makes the variable slightly transparent

[/lua]

If you provide only the setTextColor() function with 3 aguments then it will just default the A (Alpha) property to 255 (meaning that the color is completely opaque).

But this all depends on whether I have fully understood your question or not…  :slight_smile:  Please elaborate further if I haven’t

Rich

To set color, you must provide at least 3 subcolors of RGB pallete (red, green and blue). You cannot pass other color format as for example hex one.

However you can wrap this 3 values with table and then in function unpack it. Short remind, unpack() function is not the fastest one so probably try to not use it in time critical parts of code.

[lua]
local text = display.newText (…)
local myColor = { 255, 10, 120 }
text:setTextColor( unpack(myColor) )
[/lua]

Exactly as @piotrz55 says, “unpack()” is fine to use, but it’s not the fastest Lua function. If for some reason you’re using this in time-critical code, something like this may be better (because you know there are 3 color channel parameters, or 4 if you opt to add alpha to the mix):

[lua]

local myColor = { 255, 10, 120 }

text:setTextColor( myColor[1], myColor[2], myColor[3] )

[/lua]

Best regards,

Brent