Author Topic: Random color select in a faster way  (Read 2994 times)

0 Members and 1 Guest are viewing this topic.

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Random color select in a faster way
« 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 )



does anyone know if this is more relevant than the upper one?

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Random color select in a faster way
« Reply #1 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 :)

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Random color select in a faster way
« Reply #2 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)
« Last Edit: November 11, 2011, 06:48:02 am by Nick »

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: Random color select in a faster way
« Reply #3 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.
« Last Edit: November 11, 2011, 06:50:03 am by Chockosta »

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Random color select in a faster way
« Reply #4 on: November 11, 2011, 07:15:35 am »
oh, thanks a lot, that might be useful for some other functions too :)

Offline apcalc

  • The Game
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1393
  • Rating: +120/-2
  • VGhlIEdhbWUh (Base 64 :))
    • View Profile
Re: Random color select in a faster way
« Reply #5 on: November 13, 2011, 10:17:38 am »
Good method for doing this!  The unpack command looks quite helpful.


Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Random color select in a faster way
« Reply #6 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.