简体   繁体   English

更改颜色/循环种类(java)

[英]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) . 我试图让Mosaic在一个重复的循环中添加红色,然后是绿色,然后是蓝色,直到颜色为白色,又名(r/g/b/ >= 255)为止。

I have written various if statements, since I believe that's the loop I'm looking for. 我写了各种if语句,因为我相信这就是我要寻找的循环。 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. 我假设这是因为循环在main()例程的while(Mosaic.isOpen())循环中重新循环后再也不会变成蓝色。 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. 这是由main()例程运行和重新运行的for循环。

  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 . 如有其他任何问题,请philecarpenter@gmail.com评论或给我发送电子邮件: 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. 在某个时候,红色,绿色和蓝色都将大于240。现在蓝色将重置为0,然后又增加到240,而红色和绿色保持不变。 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); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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