Java-创建离散的彩虹颜色数组 [英] Java- Creating a discrete rainbow colour array

查看:253
本文介绍了Java-创建离散的彩虹颜色数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很难描述我要做什么。我基本上想创建一个离散的彩虹渐变,以便对于任何数量的JButton的任何行,它们之间的颜色渐变都将看起来是彩虹。

Hard to describe what I'm trying to do. I basically want to create a discrete rainbow gradient so that for any line of i number of JButtons, the colour gradient across them will look rainbow.

我已经完成了以下工作,但它只会真正创建一个红色渐变,然后创建一个绿色渐变,再创建一个蓝色渐变:

I have done the following but it only really creates a red gradient followed by green gradient followed by blue gradient:

Color[] c = new Color[i];
    for(int n = 0; n < i; n++) {
        if(n < i/3) {
            c[n] = new Color(255, 255/(n+1), 255/(n+1));
        } else if(n < 2*i/3) {
            c[n] = new Color(255/(n-(i/3)), 255, 255/(n-(i/3)));
        } else {
            c[n] = new Color(255/(n+1), 255/(n+1), 255);
        }
    }

任何想法我将如何获得某种类型彩虹效果?

Any Idea how I would be able to get some type of rainbow effect?

谢谢

编辑:

使用的Sine函数似乎效果更好,但不确定如何定义它,因此我在想要的区域收到一个彩虹波:

Used a Sine function which seems to work slightly better, but not sure how to define it so I get one "rainbow wave" in the region I want:

for(int n = 0; n < i; n++) {
        c[n] = new Color((int)(Math.sin(n) * 127 + 128), (int)(Math.sin(n + Math.PI/2) * 127 + 128), (int)(Math.sin(n + Math.PI) * 127 + 128));
    }


推荐答案

您的代码具有正确的思路,但是您需要对颜色进行一些改动。

Your code has the right idea, but you need to run through your colors a little bit differently.

假设您从绿色开始:

Color(  0, 255,   0)

慢慢地开始添加一些红色变成黄色:

Slowly start adding in some red to get to yellow:

Color( 51, 255,   0)
Color(102, 255,   0)
Color(153, 255,   0)
Color(204, 255,   0)
Color(255, 255,   0)

然后,拿出绿色变成红色:

Then, take out the green to get to red:

Color(255, 204,   0)
Color(255, 153,   0)
Color(255, 102,   0)
Color(255,  51,   0)
Color(255,   0,   0)

现在,添加蓝色以获得紫色:

Now, add blue to get to purple:

Color(255,   0,  51)
Color(255,   0, 102)
Color(255,   0, 153)
Color(255,   0, 204)
Color(255,   0, 255)

然后,删除重新d变为蓝色:

Then, remove red to get to blue:

Color(204,   0, 255)
Color(153,   0, 255)
Color(102,   0, 255)
Color( 51,   0, 255)
Color(  0,   0, 255)

重新添加绿色以获取青色:

Add the green back in to get to cyan:

Color(  0,  51, 255)
Color(  0, 102, 255)
Color(  0, 153, 255)
Color(  0, 204, 255)
Color(  0, 255, 255)

最后删除蓝色以恢复为绿色:

And finally remove the blue to get back to green:

Color(  0, 255, 204)
Color(  0, 255, 153)
Color(  0, 255, 102)
Color(  0, 255,  51)
Color(  0, 255,   0)

当然,在这个圈子中,您可以从任何地方开始并向任一方向前进。

In this circle, of course, you can start anywhere and go in either direction.

在代码中,它看起来像这样简单:

In code, it could look as simple as this:

List<Color> colors = new ArrayList<Color>();
for (int r=0; r<100; r++) colors.add(new Color(r*255/100,       255,         0));
for (int g=100; g>0; g--) colors.add(new Color(      255, g*255/100,         0));
for (int b=0; b<100; b++) colors.add(new Color(      255,         0, b*255/100));
for (int r=100; r>0; r--) colors.add(new Color(r*255/100,         0,       255));
for (int g=0; g<100; g++) colors.add(new Color(        0, g*255/100,       255));
for (int b=100; b>0; b--) colors.add(new Color(        0,       255, b*255/100));
                          colors.add(new Color(        0,       255,         0));

这里,100是每次淡入淡出的步数,您可以调整。

Here, the 100's are the number of steps for each fade, which you can adjust.

如果您需要数组中的颜色,请在最后执行以下操作:

If you need the colors in an array instead, do this at the end:

Color[] c = colors.toArray(new Color[colors.size()]);

需要注意的一件事:人眼对绿色的敏感度要高于对红色和蓝色的敏感度。因此,与使用红色和蓝色相比,您可能希望以较小的步长添加和删除绿色。只需使用不同的步长大小,直到获得看起来均匀的东西即可。

One thing to note: The human eye is MUCH more sensitive to green than to red and blue. So you might want to add and remove green with smaller steps than you would with red and blue. Just play with the different step-sizes until you get something that looks even.

PS:对于我使用的方法,如上所述,线性衰落完全可以满足您的需求。我个人认为您不需要通过使用sin和cos或其他数学运算来使事情复杂化。

这篇关于Java-创建离散的彩虹颜色数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆