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 :-\
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.