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

Time:05-28

I wanna make a pixel image for every color, but this code only makes (255,255,255,255) images. It loops through the entire for loop before it uses the int values for the creation of the images. How do I stop it at each integer during the for loop so I can make images that start at (0,0,0,0) then go to (0,0,0,1) and then (0,0,0,2) and so on all the way to (255,255,255,255)? so, I need to make 4,294,967,296 images in total.


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);

        }

    }
}

CodePudding user response:

If you change the order of the for loops it will create one image for each possible colors. But beware that your file manager might not be able to handle that many files inside of one directory.

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();
            }
        }
    }
}
  • Related