Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
22 May, 2013, 07:21:54 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1]   Go Down
  Print  
Author Topic: Random color select in a faster way -  (Read 612 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
Nick
LV9 Veteran (Next: 1337)
*********
Offline Offline

Gender: Male
Last Login: 20 May, 2013, 20:44:19
Date Registered: 05 June, 2011, 20:01:07
Location: 51° 12′ 34″ N, 3° 13′ 31″ E
Posts: 1178


Topic starter
Total Post Ratings: +158

View Profile WWW
« on: 11 November, 2011, 11:50:21 »
0

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 Smiley
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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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?

* ChooseColor.lua (0.55 KB - downloaded 22 times.)
* ChooseColor.tns (1.03 KB - downloaded 21 times.)
Logged

Jim Bauwens
Lua! Nspire! Linux!
Editor
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Yesterday at 22:53:52
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« Reply #1 on: 11 November, 2011, 13:03:18 »
0

You could use this:

1
2
3
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 Smiley
Logged

Nick
LV9 Veteran (Next: 1337)
*********
Offline Offline

Gender: Male
Last Login: 20 May, 2013, 20:44:19
Date Registered: 05 June, 2011, 20:01:07
Location: 51° 12′ 34″ N, 3° 13′ 31″ E
Posts: 1178


Topic starter
Total Post Ratings: +158

View Profile WWW
« Reply #2 on: 11 November, 2011, 13:46:41 »
0

wow, that looks really easy Smiley thanks! now my code will finally get a little more tidy Smiley

but i do not understand the unpack method, what does it do? (always wanting to learn something new xp)
« Last Edit: 11 November, 2011, 13:48:02 by Nick » Logged

Chockosta
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: Yesterday at 18:41:08
Date Registered: 03 June, 2011, 20:14:17
Location: France
Posts: 440


Total Post Ratings: +159

View Profile
« Reply #3 on: 11 November, 2011, 13:49:50 »
0

When you type unpack({1,2,3}), the function returns 1,2,3.
It basically unpacks a table.
« Last Edit: 11 November, 2011, 13:50:03 by Chockosta » Logged

Chockosta (Loic Pujet) - Sorry for my poor English...
Look at my projects :
in C code : Periodic table, Space invaders, Fall, Snake, Minesweeper, nCraft (WIP)
in Lua : Snake, Space invaders, Bobby Carrot, Minesweeper, Mazes 3D, nSpaint, FreeCell, Tiny3D-Viewer, CubeField, Gravity Guy
Nick
LV9 Veteran (Next: 1337)
*********
Offline Offline

Gender: Male
Last Login: 20 May, 2013, 20:44:19
Date Registered: 05 June, 2011, 20:01:07
Location: 51° 12′ 34″ N, 3° 13′ 31″ E
Posts: 1178


Topic starter
Total Post Ratings: +158

View Profile WWW
« Reply #4 on: 11 November, 2011, 14:15:35 »
0

oh, thanks a lot, that might be useful for some other functions too Smiley
Logged

apcalc
The Game
Coder Of Tomorrow
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: 19 May, 2013, 16:59:33
Date Registered: 20 March, 2010, 16:31:47
Location: 2003 UB313
Posts: 1393


Total Post Ratings: +118

View Profile
« Reply #5 on: 13 November, 2011, 17:17:38 »
0

Good method for doing this!  The unpack command looks quite helpful.
Logged


DJ Omnimaga
Retired Omnimaga founder (Site issues must be PM'ed to Netham45, Eeems, Shmibs, Deep Thought and AngelFish, not me.)
Editor
LV15 Omnimagician (Next: --)
*
Offline Offline

Gender: Male
Last Login: Today at 06:42:09
Date Registered: 25 August, 2008, 07:00:21
Location: Québec (Canada)
Posts: 50214


Total Post Ratings: +2613

View Profile WWW
« Reply #6 on: 19 November, 2011, 02:16:02 »
0

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.
Logged

Retired 83+ coder, Omnimaga/TIMGUL founder. Now doing power metal music (formerly did electronica)

Follow me on Bandcamp|Facebook|Reverbnation|Youtube|Twitter|Myspace
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.207 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.