Created
December 4, 2015 21:37
-
-
Save inez/6fd0049739261f9877a4 to your computer and use it in GitHub Desktop.
Generate image with similar colors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package colors; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import javax.imageio.ImageIO; | |
public class colors { | |
public static void main(String[] args) throws IOException { | |
System.out.println("starting"); | |
//Color baseColor = new Color(4, 253, 6); // green | |
Color baseColor = new Color(4, 0, 254); // blue | |
ArrayList<Color> similarColors = new ArrayList<Color>(); | |
for(int i = 0; i < 255; i++) { | |
for(int j = 0; j < 255; j++) { | |
for(int k = 0; k < 255; k++) { | |
Color color = new Color(i, j, k); // green | |
if(areColorsSimilar(baseColor, color)) { | |
//System.out.println(i + "," + j + "," + k); | |
similarColors.add(color); | |
} | |
} | |
} | |
} | |
System.out.println("similar colors count:" + similarColors.size()); | |
int height = 3; | |
int width = 100; | |
BufferedImage img = new BufferedImage(width, similarColors.size() * height /* so it is visible */, BufferedImage.TYPE_INT_RGB); | |
int index = 0; | |
for (Color similarColor : similarColors) { | |
for(int i = 0; i < height; i++) { | |
for(int j = 0; j < width; j++) { | |
img.setRGB(j, index + i, similarColor.getRGB()); | |
} | |
} | |
index += 3; | |
} | |
File outputfile = new File("/tmp/image.jpg"); | |
ImageIO.write(img, "jpg", outputfile); | |
System.out.println("done"); | |
} | |
private static boolean areColorsSimilar(Color c1, Color c2) { | |
double distance = Math.pow(c1.getRed() - c2.getRed(), 2) + Math.pow(c1.getGreen() - c2.getGreen(), 2) + Math.pow(c1.getBlue() - c2.getBlue(), 2); | |
return Math.sqrt(distance) < 10; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment