I'm trying to loop through all rgb colours in rainbow order. Currently I have this:
int state = 1;
int a = 255;
int r = 255;
int g = 0;
int b = 0;
if(g < 255 && state == 1){
g++;
r--;
if(g == 255)
state = 2;
}
if(b < 255 && state == 2){
b++;
g--;
if(b == 255)
state = 3;
}
if(state == 3){
r++;
b--;
if(b == 0)
state = 1;
}
int hex = (a << 24) + (r << 16) + (g << 8) + (b);
It works but it doesn't seem to get all the colours. I know this is probably a bad way of doing it and yes I know I can do 3 loops inside each other but does anybody know a better way of doing this that gets all the colours?
Also, the reason I'm not using the 3 loops is because it needs to update after every new RGB combination, not after the loops have finished, because that gives me the same outcome every time.
EDIT: Thanks to pbabcdefp I got it working and the solution is below.
int state = 0;
int a = 255;
int r = 255;
int g = 0;
int b = 0;
if(state == 0){
g++;
if(g == 255)
state = 1;
}
if(state == 1){
r--;
if(r == 0)
state = 2;
}
if(state == 2){
b++;
if(b == 255)
state = 3;
}
if(state == 3){
g--;
if(g == 0)
state = 4;
}
if(state == 4){
r++;
if(r == 255)
state = 5;
}
if(state == 5){
b--;
if(b == 0)
state = 0;
}
int hex = (a << 24) + (r << 16) + (g << 8) + (b);