如何为 java 中 ARGB 光谱中的每种颜色创建图像?

How do I create an image for every color in the ARGB spectrum in java?

我想为每种颜色制作一个像素图像,但这段代码只能制作 (255,255,255,255) 张图像。在使用 int 值创建图像之前,它会遍历整个 for 循环。我如何在 for 循环中将它停在每个整数处,这样我就可以制作从 (0,0,0,0) 开始然后转到 (0,0,0,1) 然后 (0,0,0, 2)等等一直到(255,255,255,255)?所以,我总共需要制作4,294,967,296张图片。


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException{

        int width = 1;
        int height = 1;

        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        File f = null;

        try{
            for(int i = 0; i < 4294967297; i++) {
                for(int y = 0; y < height; y++) {
                    for(int x = 0; x < width; x++){
                        for(int alpha = 0; alpha < 256; alpha++){
                            for(int red = 0; red < 256; red++){
                                for(int green = 0; green < 256; green++){
                                    for(int blue = 0; blue < 256; blue++) {

                                        int a = alpha;
                                        int r = red;
                                        int g = green;
                                        int b = blue;

                                        int p = (a << 24) | (r << 16) | (g << 8) | b;

                                        img.setRGB(x, y, p);
                                    }
                                }
                            }
                        }
                    }
                }
                f = new File("/Users/dimensionalengineer/Downloads/Colors/Color" + i + ".png");
                ImageIO.write(img, "png", f);
            }
        } catch(IOException e) {

            System.out.println("Error: " + e);

        }

    }
}

如果您更改 for 循环的顺序,它将为每种可能的颜色创建一个图像。但请注意,您的文件管理器可能无法处理一个目录中的那么多文件。

BufferedImage img = null;
File f = null;

int width = 1;
int height = 1;
int i = 0;

// loop for every possible color
for(int alpha = 0; alpha < 256; alpha++){
    for(int red = 0; red < 256; red++){
        for(int green = 0; green < 256; green++){
            for(int blue = 0; blue < 256; blue++) {
                
                // create one image filled with one color
                img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

                int a = alpha;
                int r = red;
                int g = green;
                int b = blue;

                int p = (a << 24) | (r << 16) | (g << 8) | b;

                // loop every pixel
                for(int y = 0; y < height; y++) {
                    for(int x = 0; x < width; x++){
                        img.setRGB(x, y, p);
                    }
                }
                
                // save to file
                f = new File("/Users/dimensionalengineer/Downloads/Colors/Color" + i++ + ".png");
                ImageIO.write(img, "png", f);
                
                // free ram
                img.dispose();
            }
        }
    }
}