在java中浏览所有颜色
我正在尝试编写一种符合以下逻辑的方法: 我有3个整数,r,g和b 开始时:在java中浏览所有颜色,java,algorithm,colors,cycle,Java,Algorithm,Colors,Cycle,我正在尝试编写一种符合以下逻辑的方法: 我有3个整数,r,g和b 开始时: r = 255; g = 0; b = 0; 如你们所见,r是255,现在g应该一个接一个地上升,直到它达到255 r = 255; g = 255; b = 0; 这三个整数加起来就是一种颜色。第一个是红色的,现在是黄色的。不是,它应该变成绿色,所以r应该减小,直到只有g是255: r = 0; g = 255; b = 0; 下一个颜色应该是青色,等等 命令如下: red - yellow - green -
r = 255;
g = 0;
b = 0;
如你们所见,r是255,现在g应该一个接一个地上升,直到它达到255
r = 255;
g = 255;
b = 0;
这三个整数加起来就是一种颜色。第一个是红色的,现在是黄色的。不是,它应该变成绿色,所以r应该减小,直到只有g是255:
r = 0;
g = 255;
b = 0;
下一个颜色应该是青色,等等
命令如下:
red - yellow - green - cyan - blue - violet - red
-->从一开始
我尝试通过使用if方法来实现这一点:
e.g.: if(r == 255 && g == 0) g++;
等等,但我意识到这将是一个漫长而复杂的过程
有人对如何循环使用颜色有其他想法吗
我的目标是在每次更新时更改方形对象的颜色:
public void update() {
-->color change code here<--
color = new Color(r, g, b, alpha);
}
public void update(){
-->这里的颜色更改代码java.awt.color
类提供了一个静态函数
使用HSB颜色空间,使色调分量从0变为1,同时保持其他分量不变
color = Color.getHSBColor(hue, 1, 1);
java.awt.Color
类提供了一个静态函数
使用HSB颜色空间,使色调分量从0变为1,同时保持其他分量不变
color = Color.getHSBColor(hue, 1, 1);
for(int-red=0;redfor(int-red=0;red以下是一个示例,介绍了您使用for循环所做的更改,希望对您有所帮助
public class rgb {
public static void main(String[] args) {
// establish your starting conditions
int r, g, b;
r = 255;
g = 0;
b = 0;
// increment g by 1 until it reaches 255
for (int i = 0; i <= 255; i++) {
g = i;
/* Do whatever you
* want with this
* color here */
}
// de-increment r by 1 until it's 0
for (int i = 254; i >= 0; i--) {
r = i;
/* Do whatever you
* want with this
* color here */
}
}
}
公共类rgb{
公共静态void main(字符串[]args){
//确定你的出发条件
int r,g,b;
r=255;
g=0;
b=0;
//将g增加1,直到达到255
对于(inti=0;i,这里有一个示例,说明了您使用for循环所做的更改,希望这对您有所帮助
public class rgb {
public static void main(String[] args) {
// establish your starting conditions
int r, g, b;
r = 255;
g = 0;
b = 0;
// increment g by 1 until it reaches 255
for (int i = 0; i <= 255; i++) {
g = i;
/* Do whatever you
* want with this
* color here */
}
// de-increment r by 1 until it's 0
for (int i = 254; i >= 0; i--) {
r = i;
/* Do whatever you
* want with this
* color here */
}
}
}
公共类rgb{
公共静态void main(字符串[]args){
//确定你的出发条件
int r,g,b;
r=255;
g=0;
b=0;
//将g增加1,直到达到255
对于(int i=0;i根据您的周期,您有6个转换,每个转换256步。每个转换在所有三个RGB通道上工作,但以移位方式
例如,假设您从255,0,0
开始,您将得到如下结果:
R 255 --- \ / ---
\ /
0 \ --- --- /
G 255 / --- --- \
/ \
0 / \ --- ---
B 255 / --- --- \
/ \
0 --- --- / \
R Y G C B M R..
从这里你可以很容易地看到,整个周期是6个片段x 256个步骤。在每个片段中,每个通道都可以是稳定的,上升或下降。同时你可以看到绿色和蓝色通道就像红色通道刚刚移动一样
因此,我们只能关注红色通道:
int redValueForStep(int step) {
step %= 256*6; // let's make step wrap around
int fragment = step/256; // which fragment 0-5
int fragmentStep = step%256; // which step inside fragment 0-255
switch (fragment) {
case 0: case 5:
return 255; // first and last fragment have steady high value
case 2: case 3:
return 0; // fragments 2 and 3 have steady low value
case 1: return 255-fragmentStep; // falling
case 2: return fragmentStep; // rising
}
一旦你有了红色通道,你就可以很容易地计算出另一个,绿色通道在前面被红移了两个完整的片段,所以你加了4个来环绕(以避免出现负值):
同样的事情也适用于蓝色通道。根据您的周期,您有6个转换,每个转换256步。每个转换在所有三个RGB通道上工作,但以移位的方式
例如,假设您从255,0,0
开始,您将得到如下结果:
R 255 --- \ / ---
\ /
0 \ --- --- /
G 255 / --- --- \
/ \
0 / \ --- ---
B 255 / --- --- \
/ \
0 --- --- / \
R Y G C B M R..
从这里你可以很容易地看到,整个周期是6个片段x 256个步骤。在每个片段中,每个通道都可以是稳定的,上升或下降。同时你可以看到绿色和蓝色通道就像红色通道刚刚移动一样
因此,我们只能关注红色通道:
int redValueForStep(int step) {
step %= 256*6; // let's make step wrap around
int fragment = step/256; // which fragment 0-5
int fragmentStep = step%256; // which step inside fragment 0-255
switch (fragment) {
case 0: case 5:
return 255; // first and last fragment have steady high value
case 2: case 3:
return 0; // fragments 2 and 3 have steady low value
case 1: return 255-fragmentStep; // falling
case 2: return fragmentStep; // rising
}
一旦你有了红色通道,你就可以很容易地计算出另一个,绿色通道在前面被红移了两个完整的片段,所以你加了4个来环绕(以避免出现负值):
同样的事情也适用于蓝色通道。你想从一种颜色逐渐过渡到另一种颜色,对吗?也许你可以创建一个大的硬编码color
对象数组。它只有1500个元素长,大约:-)你想从一种颜色逐渐过渡到另一种颜色,对吗?也许你可以创建一个大的硬编码color
对象数组。它只有1500个元素长,大约:-)不,他/她想要所有的最大饱和颜色,不是所有可能的颜色,而不仅仅是成分为0或255的八种颜色。这不符合他/她想要的。不,他/她想要所有的最大饱和颜色,不是所有可能的颜色,而不仅仅是成分为0或255的八种颜色。这不起什么作用他/她想要。那是绝对正确的,我一定一直在考虑另一个API,其中色调被指定为度数(1-360)值。那是绝对正确的,我一定一直在考虑另一个API,其中色调被指定为度数(1-360)值。哇,谢谢你的回答,听起来比我第一次使用“如果”时想到的要简单…但是我更喜欢使用Color.GetHSBColor()。但是谢谢你的想法!哇,谢谢你的回答,听起来比我第一次想到的使用“如果”更容易…但是我更喜欢使用Color.GetHSBColor()。但是谢谢你的想法!