循环 256*256*256 颜色,这样颜色会逐渐变化,并且类似的阴影不会在以后的循环迭代中重复?
Posted
技术标签:
【中文标题】循环 256*256*256 颜色,这样颜色会逐渐变化,并且类似的阴影不会在以后的循环迭代中重复?【英文标题】:Loop for 256*256*256 colors such that colors are gradually changing and similar shades don't repeat in later iterations of loop? 【发布时间】:2019-01-17 16:15:30 【问题描述】:当您使用以下循环生成所有可能的颜色时。
for (int red = 0; red < 256; red++)
for (int green = 0; green < 256; green++)
for (int blue = 0; blue < 256; blue++)
它会生成类似这样的东西
我想要一个循环,使颜色逐渐变化(即渐变不能重复。例如,所有可能的绿色阴影都在一起,然后所有可能的蓝色阴影等等)和所有可能的颜色被循环覆盖。
【问题讨论】:
LinearGradientPaint
将是我的第一个停靠港,如果这不能完全满足您的要求,那么可能像 this 这样的东西会更适合
【参考方案1】:
这是我使用了一段时间的“颜色混合”算法的一部分。它基本上采用一系列颜色和一系列归一化点,并自动计算这些颜色/点之间的“混合”
因此,在解决方案的核心,我们拥有从黑色到完全“彩色”(即绿色/蓝色/红色)的颜色
private Color[] colors = new Color[]
new Color(0, 0, 0), new Color(0, 255, 0), // Green
new Color(0, 0, 0), new Color(0, 0, 255), // Blue
new Color(0, 0, 0), new Color(255, 0, 0), // Red
;
然后我们有我们希望这些颜色覆盖的区域的一部分,注意,每次颜色变化都有一个点,然后算法会自动混合从起点到终点的颜色。
private float[] fractions = new float[]
0f, 0.33f,
0.34f, 0.66f,
0.67f,1f;
该示例然后简单地生成多个波段(在本例中为 100 个)来“伪造”混合效果
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ColorFade
public static void main(String[] args)
new ColorFade();
public ColorFade()
EventQueue.invokeLater(new Runnable()
@Override
public void run()
try
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
catch (ClassNotFoundException ex)
catch (InstantiationException ex)
catch (IllegalAccessException ex)
catch (UnsupportedLookAndFeelException ex)
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ColorFadePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
);
public class ColorFadePane extends JPanel
private float[] fractions = new float[]
0f, 0.33f,
0.34f, 0.66f,
0.67f,1f;
private Color[] colors = new Color[]
new Color(0, 0, 0), new Color(0, 255, 0),
new Color(0, 0, 0), new Color(0, 0, 255),
new Color(0, 0, 0), new Color(255, 0, 0),
;
public ColorFadePane()
@Override
public Dimension getPreferredSize()
return new Dimension(200, 100);
@Override
protected void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int width = getWidth();
int height = getHeight();
int bandWidth = width / 100;
for (int index = 0; index < 100; index++)
float progress = (float) index / (float) 100;
Color color = blendColors(fractions, colors, progress);
int x = bandWidth * index;
int y = 0;
g2d.setColor(color);
g2d.fillRect(x, y, bandWidth, height);
g2d.dispose();
public static Color blendColors(float[] fractions, Color[] colors, float progress)
Color color = null;
if (fractions != null)
if (colors != null)
if (fractions.length == colors.length)
int[] indicies = getFractionIndicies(fractions, progress);
float[] range = new float[]fractions[indicies[0]], fractions[indicies[1]];
Color[] colorRange = new Color[]colors[indicies[0]], colors[indicies[1]];
float max = range[1] - range[0];
float value = progress - range[0];
float weight = value / max;
color = blend(colorRange[0], colorRange[1], 1f - weight);
else
throw new IllegalArgumentException("Fractions and colours must have equal number of elements");
else
throw new IllegalArgumentException("Colours can't be null");
else
throw new IllegalArgumentException("Fractions can't be null");
return color;
public static int[] getFractionIndicies(float[] fractions, float progress)
int[] range = new int[2];
int startPoint = 0;
while (startPoint < fractions.length && fractions[startPoint] <= progress)
startPoint++;
if (startPoint >= fractions.length)
startPoint = fractions.length - 1;
range[0] = startPoint - 1;
range[1] = startPoint;
return range;
public static Color blend(Color color1, Color color2, double ratio)
float r = (float) ratio;
float ir = (float) 1.0 - r;
float rgb1[] = new float[3];
float rgb2[] = new float[3];
color1.getColorComponents(rgb1);
color2.getColorComponents(rgb2);
float red = rgb1[0] * r + rgb2[0] * ir;
float green = rgb1[1] * r + rgb2[1] * ir;
float blue = rgb1[2] * r + rgb2[2] * ir;
if (red < 0)
red = 0;
else if (red > 255)
red = 255;
if (green < 0)
green = 0;
else if (green > 255)
green = 255;
if (blue < 0)
blue = 0;
else if (blue > 255)
blue = 255;
Color color = null;
try
color = new Color(red, green, blue);
catch (IllegalArgumentException exp)
NumberFormat nf = NumberFormat.getNumberInstance();
System.out.println(nf.format(red) + "; " + nf.format(green) + "; " + nf.format(blue));
exp.printStackTrace();
return color;
LinearGradientPaint
现在,如果你真的很无聊,你可以改用LinearGradientPaint
...
public class ColorFadePane extends JPanel
private float[] fractions = new float[]
0f, 0.33f,
0.34f, 0.66f,
0.67f, 1f;
private Color[] colors = new Color[]
new Color(0, 0, 0), new Color(0, 255, 0),
new Color(0, 0, 0), new Color(0, 0, 255),
new Color(0, 0, 0), new Color(255, 0, 0),;
public ColorFadePane()
@Override
public Dimension getPreferredSize()
return new Dimension(200, 100);
@Override
protected void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int width = getWidth();
int height = getHeight();
LinearGradientPaint lgp = new LinearGradientPaint(new Point(0, 0), new Point(width, 0), fractions, colors);
g2d.setPaint(lgp);
g2d.fillRect(0, 0, width, height);
g2d.dispose();
但我需要将结果生成为图像/文件
好的,好的,所以,创建一个 BufferedImage
并对其进行绘制,然后使用 ImageIO.write
保存它
【讨论】:
【参考方案2】:如果你真的想展示总共 1600 万种颜色,那么你必须放弃一些标准。你不能拥有所有的红色阴影,然后是所有的绿色阴影,然后是所有的蓝色阴影,因为这并不能解释颜色的混合。所有深浅不一的红色和绿色混合在一起——就是所有深浅不一的红色或所有深浅不一的绿色。特别是因为现在您要添加一些蓝色。
你也说过你希望它逐渐改变,所以你可以做的是(在元代码中):
for (int red = 0; red < 256; ++red)
bool redIsEven = (red % 2) == 0;
int greenStart = redIsEven ? 0 : 255;
int greenEnd = redIsEven ? 256 : -1;
int greenDirection = redIsEven ? 1 : -1;
for (int green = greenStart; green != greenEnd; green += greenDirection)
... similar code for blue, but based on green, not red
也就是说...您将执行红色 = 0,绿色将从 0 变为 255。对于绿色 = 0,蓝色也变为 0...255,但当它为 1 时,它会向后移动,所以它在流动均匀。然后绿色向下计数,然后向上,然后向下......
因此,您可以顺利完成 1600 万个选择中的任何一个选择。
这就是你想要做的吗?
【讨论】:
以上是关于循环 256*256*256 颜色,这样颜色会逐渐变化,并且类似的阴影不会在以后的循环迭代中重复?的主要内容,如果未能解决你的问题,请参考以下文章