简体   繁体   中英

changing colors/cycling kind of (java)

I'm trying to get the Mosaic to add red, then green, then blue, in a repeated cycle until the colors are white, aka (r/g/b/ >= 255) .

I have written various if statements, since I believe that's the loop I'm looking for. I've come to a stopping point. The problem is that the loop cycles all the way to white, then stays blue (resets the red, I'm assuming. it's more cyan than magenta). I'm assuming this is because the loop never gets to blue after it is relooped in a while(Mosaic.isOpen()) loop in the main() routine. The other version (The one I've posted hereafter, wherein I've become stumped.) repeatedly adds then resets blue (I believe, because it cycles to yellow, then resets to green.).

Here is the for loop being ran and reran by the main() routine.

  static void colorChange(int x,int y) {
        int red = Mosaic.getRed(x,y);
        int green = Mosaic.getGreen(x,y);
        int blue = Mosaic.getBlue(x,y);
        if (red <= green || red <= blue) {
            if (red <= 240) {
                Mosaic.setColor(x, y, red + 6, green, blue);
            } else {
                Mosaic.setColor(x, y, 0, green, blue);
            } //
        } else if (green <= blue || green <= red) {
            if (green <= 240) {
                Mosaic.setColor(x, y, red, green + 6, blue);
            } else {
                Mosaic.setColor(x,y,red,0,blue);
            } //
        }  else if (blue <= red && blue <= green) {
            if (blue <= 240) {
                Mosaic.setColor(x, y, red, green, blue + 6);
            } else {
                 Mosaic.setColor(x, y, red, green, 0);
            }  //
        }

Can anyone see what I might be missing? In case it wasn't clear before, I would like to add red, then the next cycle (since red should be greater than green) it should add green, then the same with blue, until finally resetting the values when they're about to max out.

Comment or email me with any further questions: philecarpenter@gmail.com .

boolean redSmallerGreen = (red <= green);
boolean greenSmallerBlue = (green <= blue);

//What happens here?!
boolean allGreater240 = (red >= 240 && blue >= 240 && green >= 240);

if (redSmallerGreen) {
    Mosaic.setColor(x, y, red<=240?red+6:0,green,blue); 
} else if (greenSmallerBlue) {
    Mosaic.setColor(x, y, red,green<=240?green+6:0,blue);   
} else if (allgreater240) {
    //?!
} else {
    Mosaic.setColor(x, y, red,green,blue<=240?blue+6:0);    
}

What happens? At some point, red, green and blue will all be greater 240. Now blue will reset to 0 and increased to 240 again while red and green stay the same. Maybe this helps you solving your problem. I would have wirrten a comment but it would have been unreadable.

I did not test my code, just wrote it down to get a imagination of what you / I are doing here :)

Edit: Reverse

int step = 6;

public void yourFunction() {

    (...)

    boolean redSmallerGreen = (red <= green);
    boolean greenSmallerBlue = (green <= blue);

    boolean allSmaller0 = (red <= 0 && blue <= 0 && green <= 0); 
    boolean allGreater240 = (red >= 240 && blue >= 240 && green >= 240);

    if (allSmaller0) {
        step = 6;
    }
    if (allGreater240) {
        step = -6;
    }

    if (redSmallerGreen) {
        Mosaic.setColor(x, y, red+step,green,blue); 
    } else if (greenSmallerBlue) {
        Mosaic.setColor(x, y, red,green+step,blue); 
    } else {
        Mosaic.setColor(x, y, red,green,blue+step); 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM