0

I am trying to create a simple class that will help me organize my hundred's of text files that represent distinct color palettes in hex format. No size restrictions but in most cases colors are less than 256 per file and support transparency also (6 or 8 hex digits).

the format is simply a text file like this (a value per line):

546A817F
A7AC827F
ACBFB97F
9DB3BA7F
...

The goal is to create a preview png image per every palette file in a direcory. (format is PNG in order to support transparency, and when viewed in image viewers easily see/get the actual color). Preferably constructed in colored rectangles side by side, preferably with the option to set a title with the hex value.

I have tried with the following code, but there are problems in the constriction of the Graphics2D object.

The functions goes like this: 1.scan a directory for all the files, 2.for every file, we scan contents and create a list of the hex values that holds 3. we count those colors and create a png image with those colors as filled rectangles, side by side. (and maybe print the hex value also inside/top/bottom of every rectangle) 4. The best would be not to have a fixed size image, but a variable one, taking care the amount of colors per list/file.

a simple example of placing colors can be found here: http://www.java2s.com/Code/Java/2D-Graphics-GUI/ColorGradient.htm

I am not a java developer, just trying to develop simple tools to make my life easier for my personal projects. I do not care a about good code practices, I am interested only in the final result, so I'm sorry for the mixed up code or mistakes!

code till now, the alignment of rectangles is broken, loops for scanning I think work simply ok, but images I think overlay the previous graphics object on top of each other (best seen with transparency). any contribution welcomed. Thanks in advance.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import static java.nio.charset.Charset.defaultCharset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import javax.imageio.ImageIO;


/**
 *
 * @author
 */
public class ConvertFromHexColorListToImage {
   
   
     public      Color c = new Color (0,0,0,0);

public  List<String> list = new ArrayList<>(); 
   int count;
       private  BufferedImage buffer;

     // the size of our image
   private   int IMAGE_SIZE = 800; 
   public ConvertFromHexColorListToImage() throws IOException {

   }
   
  

    protected BufferedImage getBuffer() throws IOException {
        if (buffer == null) {
            buffer = new BufferedImage(IMAGE_SIZE, IMAGE_SIZE/2, BufferedImage.TYPE_INT_ARGB);

try (Stream<Path> filePathStream=Files.walk(Paths.get("data/multi_hex/hex_only/"))) {
    filePathStream.forEach(filePath -> {
       
     
        if (Files.isRegularFile(filePath)) {
          
              System.out.println(filePath);
              count++;              
               System.out.println("count = " +count);
           try {
//list = new ArrayList<>();
             list = Files.readAllLines(filePath); 
              System.out.println("list = "+list);
              
        // one image per list index     

     for (int i = 0; i < count; i++) {
       
        
         for(int j = 0; j < list.size(); j++){     
         
         Graphics2D   g = (Graphics2D) buffer.getGraphics();


//c = new Color(0,0,0,0);
c = HexToColor(list.get(j));
Random rand = new Random();


        // Show all the predefined colors.
        
                float size = 25;
        float x = -size * list.size() / 8;
        float y = -size * 3 / 2;

        
        
                Rectangle2D r = new Rectangle2D.Float(
              x + IMAGE_SIZE/12 * (float)j, y, IMAGE_SIZE/12, IMAGE_SIZE/12);
          
//          c = HexToColor(list.get(j));
          
          g.setPaint(c);
          g.fill(r);
        
        
g.dispose();

}

     }
        
        
                                     File f = new File("data/image_pal/MyFile"+""+count+".png"); // i=files, j=colors
    System.out.println("Created IMAGE = "+f);
   if (!ImageIO.write(buffer, "PNG", f)) {
  throw new RuntimeException("Unexpected error writing image");

         }
        
        
        
        
        
           }catch (IOException ex) {
              Logger.getLogger(ConvertFromHexColorListToImage.class.getName()).log(Level.SEVERE, null, ex);
           }

        }
    });
   
   
}          
            
        }
        return buffer;
    }
   
    //https://stackoverflow.com/questions/4129666/how-to-convert-hex-to-rgb-using-java
   /**
    * Converts a hex string to a color. If it can't be converted null is
    * returned.
    *
    * @param hex (i.e. #CCCCCCFF or CCCCCC)
    * @return Color
    */
   public static Color HexToColor(String hex) 
   {
      hex = hex.replace("#", "");
      switch (hex.length()) {
         case 6:
            return new Color(
                    Integer.valueOf(hex.substring(0, 2), 16),
                    Integer.valueOf(hex.substring(2, 4), 16),
                    Integer.valueOf(hex.substring(4, 6), 16));
         case 8:
            return new Color(
                    Integer.valueOf(hex.substring(0, 2), 16),
                    Integer.valueOf(hex.substring(2, 4), 16),
                    Integer.valueOf(hex.substring(4, 6), 16),
                    Integer.valueOf(hex.substring(6, 8), 16)); //alpha FF
      }
      //return null;
      return new Color(0, 0, 0, 0);
   }
   
       
    public static void main(String[] args) throws IOException {

        ConvertFromHexColorListToImage test0 = new ConvertFromHexColorListToImage();
        test0.getBuffer(); 
           
        System.out.println("bufferedimage = "+test0.buffer);
   
    }
   }
jazznbass
  • 24
  • 6

0 Answers0