Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: Nick on November 11, 2011, 04:50:21 am

Title: Random color select in a faster way
Post by: Nick on November 11, 2011, 04:50:21 am
I was looking for a fast way to switch between colors for my tetris project, so i tried to find a faster way of choosing a color and setting it with setColorRGB(). I don't know if it's the fastest way, but it goes nice and the colde is tidy :)
It's only to get the most basic colors: Red, Green, Blue, turquoise, pink, orange and yellow.

This is what you would have normally (or better, i had):
Code: [Select]
color = math.random(1,7)
if color == 1 then
gc:setColorRGB(255,0,0)
elseif color == 2 then
gc:setColorRGB(0,255,0)
elseif color == 3 then
gc:setColorRGB(0,0,255)
elseif color == 4 then
gc:setColorRGB(255,0,255)
elseif color == 5 then
gc:setColorRGB(0,255,255)
elseif color == 6 then
gc:setColorRGB(255,255,0)
elseif color == 7 then
gc:setColorRGB(255,150,0)
end

and this is what you get now:
Code: [Select]
color=math.random(1,7)
colortable={0,0,0}
if color<=3 then
         colortable[color]=255
elseif color==4 or color==5 then
         colortable[3]=255
         colortable[color-3]=255
elseif color==6 then
         colortable[1]=255
         colortable[2]=255
else
       colortable[1]=255
         colortable[2]=150
end
gc:setColorRGB(colortable[1],colortable[2],colortable[3])

both results are the same (luckily xp )

(http://img.removedfromgame.com/imgs/ChooseColor.gif)

does anyone know if this is more relevant than the upper one?
Title: Re: Random color select in a faster way
Post by: Jim Bauwens on November 11, 2011, 06:03:18 am
You could use this:
Code: [Select]
colortable = {{255,0,0}, {0,255,0}, {0,0,255}, {255,0,255}, {0,255,255}, {255,255,0}, {255,150,0}}
setColorRGB(unpack(colortable[math.random(#colortable)]))

This way you can easily add colors, and it will continue to work :)
Title: Re: Random color select in a faster way
Post by: Nick on November 11, 2011, 06:46:41 am
wow, that looks really easy :) thanks! now my code will finally get a little more tidy :)

but i do not understand the unpack method, what does it do? (always wanting to learn something new xp)
Title: Re: Random color select in a faster way
Post by: Chockosta on November 11, 2011, 06:49:50 am
When you type unpack({1,2,3}), the function returns 1,2,3.
It basically unpacks a table.
Title: Re: Random color select in a faster way
Post by: Nick on November 11, 2011, 07:15:35 am
oh, thanks a lot, that might be useful for some other functions too :)
Title: Re: Random color select in a faster way
Post by: apcalc on November 13, 2011, 10:17:38 am
Good method for doing this!  The unpack command looks quite helpful.
Title: Re: Random color select in a faster way
Post by: DJ Omnimaga on November 18, 2011, 07:16:02 pm
Apcalc, mind sharing your technique for your Axe tunnel? Becuase your game seemed to flash between colors even faster than in  the screenshot above. Both are nice nonetheless, though.