Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Ugly code and rainbows  (Read 1229 times)
0 Members and 1 Guest are viewing this topic.
Offline Rorkien
« Posted 2012-05-12 23:09:55 »

So, i was kinda bored and started working on a rainbow function. Actually, it's just a function that returns me color based on an integer.

It looks pretty good on screen, quite efficient too (you can puke later)


But the code is ugly and full of magic numbers; Even though the colors are perfect (I checked them), it still looks wrong  Undecided

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
   public int colorChart(int color) {
      color = color % 256;
      int rcolor;
     
      if (color <= 42) {
         rcolor = (0xFF << 16) + ((6 * color) << 8);
      }
      else if (color > 42 && color <= 84) {
         rcolor = (0xFF - (6 * (color - 43)) << 16) + (0xFF << 8);
      }
      else if (color > 84 && color <= 127) {
         rcolor = (0xFF << 8) + (6 * (color - 85));
      }
      else if (color > 127 && color <= 169) {
         rcolor = (0xFF - (6 * (color - 128)) << 8) + 0xFF;
      }
      else if (color > 169 && color <= 212) {
         rcolor = 0xFF + ((6 * (color - 170)) << 16);
      }
      else {
         rcolor = (0xFF << 16) + (0xFF - (6 * (color - 213)));
      }      
         
      return rcolor;
   }


The prototypes weren't worse tho, i miscalculated the number of colors and the result was a kinda-LSD type illusion. My head still aches.
Maybe that is causing some sort of brain blockage, because i don't know how to make the code any better

Ideas? Thanks.  Wink
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2012-05-12 23:42:33 »

Before looking at your code in depth....what exactly is wrong?

Offline Rorkien
« Reply #2 - Posted 2012-05-12 23:46:07 »

Nothing at all, i just thought i could have used a different design pattern

The thing is that i just hate to hard code numbers, even on conditional statements
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #3 - Posted 2012-05-12 23:55:05 »

If it works...don't mess with it Grin

Offline sproingie

JGO Kernel


Medals: 200



« Reply #4 - Posted 2012-05-13 00:54:11 »

The code itself looks fine, I'd only suggest one improvement: You could store the magic constants like 42, 84, 127 etc in an array.  That lets you do two things: one, if you change the numbers, you don't have to worry about forgetting to update the code in three different places per number.  Two, it would let you tweak the array itself with another function in order to animate the effect.
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #5 - Posted 2012-05-13 03:56:36 »

As a veteran of many years of coding it looks great to me.

Your code is a pure function i.e. given the same inputs it always gives the same output.  That's a good thing and it's easy to read and understand.  You could come back to this function years from now and it won't take you long to figure out what's going on.

If you're happy with the output then no need to change anything.

[edit] if you insist on some changes then: add a one-liner comment saying this produces a rainbow and you could make the function static.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 613
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2012-05-13 04:27:14 »

What you have there is not actually a linear hue gradient. You should take a look at HSL (not HSV):
http://en.wikipedia.org/wiki/HSL_and_HSV
You can simply 'pick' a Hue by specifying an angle.

As you only have 256 possible values in your current code, you can optionally create a lookup table.
IMHO, with HSL you'll endup with much better code, and no magic constants whatsoever.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Rorkien
« Reply #7 - Posted 2012-05-13 04:53:47 »

That is really interesting, Riven, i'll take a look

Thank you guys  Cheesy
Offline ReBirth
« Reply #8 - Posted 2012-05-13 05:24:26 »

It's not late to polish your code after everything run and ready to published.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (216 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!