9

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);
user3166950
  • 315
  • 3
  • 6
  • 15
  • 8
    I think you should take a look at [color spaces](https://en.wikipedia.org/wiki/List_of_color_spaces_and_their_uses), in particular the HSV or HSL family, which are far easier to use for this purpose. RGB isn't a very good color space for smooth hue transitions. (Please note that HS* color spaces are far from perfect themselves, but at least they are very simple and you can traverse the rainbow with a simple loop on `hue`.) – biziclop Aug 03 '15 at 10:15
  • @rakeb.void because of `g++`. – Karthik Aug 03 '15 at 10:15
  • @biziclop Could you provide any links to examples of this being implemented and used in java? Would help a lot, thanks! – user3166950 Aug 03 '15 at 10:22
  • @user3166950 `java.awt.Color` has a static factory method called [`getHSBColor()`](http://docs.oracle.com/javase/8/docs/api/java/awt/Color.html#getHSBColor-float-float-float-), that should do the job. – biziclop Aug 03 '15 at 10:25
  • 2
    You can do it this way, but there should be 6 loops, not 3: red->yellow->green->cyan->blue->magenta->red. – Paul Boddington Aug 03 '15 at 10:41
  • @pbabcdefp I managed to get it looping properly with this, thankyou. I'll update the original post with the solution. – user3166950 Aug 03 '15 at 11:37

2 Answers2

0

You need to find all combinations. The problem with your code is that in one loop you modify two values. In this case you skip a color. For example you have G=100 and R=100:

g++; r--; After this line G will be 101 and R will be 99. Next time it will be G102, R98 You now got three combinations:

[g100,r100], g[101,r99], g[102,r98].

But you miss lots of other combinations here like [g100,r99] and g[101,r100], or g[101,r98] etc.

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
0

You can create an RGB class to hold the ARGB values, and create a method to increase the value by 1.

Consider the following class.

class RGB {
    int alpha, red, green, blue;

    void next() {
        if (blue != 255) blue++;
        else if (green != 255) green++;
        else if (red != 255) red++;
        else if (alpha != 255) alpha++;
        else throw new IndexOutOfBoundsException();
    }

    @Override
    public String toString() {
        return "%02x%02x%02x%02x".formatted(alpha, red, green, blue);
    }
}

Example of output

00000001
00000002
00000003
00000004
00000005
...
fbffffff
fcffffff
fdffffff
feffffff
ffffffff
Reilas
  • 3,297
  • 2
  • 4
  • 17