Skip to content

Instantly share code, notes, and snippets.

@MrP123
Created August 6, 2014 20:56
Show Gist options
  • Save MrP123/f14c5000a15b59cf4d0e to your computer and use it in GitHub Desktop.
Save MrP123/f14c5000a15b59cf4d0e to your computer and use it in GitHub Desktop.
package Challenge_174_Intermediate;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.imageio.ImageIO;
public class ForumAvatar{
private static byte[] hash;
private static Color[] colors;
private static String input = "MrP_123";
public static void main(String[] args){
try{
byte[] bytes = input.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
hash = md.digest(bytes);
}catch(UnsupportedEncodingException | NoSuchAlgorithmException e){
e.printStackTrace();
}
//gets all needed colors
getColors();
//generates 1/4 of the image
Color[] image = new Color[16];
image = makeImage(image);
//completes and saves the whole image
saveFullImage(image);
}
private static void getColors(){
colors = new Color[5]; //MD5 hash is 16 bytes --> 5 full colors
int index = 0;
for(int i = 0; i < hash.length - 1; i += 3){
int r = ((Byte) hash[i]).intValue() + 128;
int g = ((Byte) hash[i + 1]).intValue() + 128;
int b = ((Byte) hash[i + 2]).intValue() + 128;
colors[index] = new Color(r, g, b);
index++;
}
}
private static Color[] makeImage(Color[] image){
//255 (byte max value) / 5 (amount of colors) = 51
for(int i = 0; i < image.length; i++){
if((((Byte) hash[i]).intValue() + 128) < 51){
image[i] = colors[0];
}else if((((Byte) hash[i]).intValue() + 128) < 102){
image[i] = colors[1];
}else if((((Byte) hash[i]).intValue() + 128) < 153){
image[i] = colors[2];
}else if((((Byte) hash[i]).intValue() + 128) < 204){
image[i] = colors[3];
}else{
image[i] = colors[4];
}
}
return image;
}
private static void saveFullImage(Color[] image){
Color[] imageWhole = add(add(image, mirrorImage(image)), mirrorImage(add(image, mirrorImage(image))));
saveImage(imageWhole);
}
private static void saveImage(Color[] image){
int side = (int) Math.sqrt(image.length);
BufferedImage img = new BufferedImage(side, side, BufferedImage.TYPE_INT_RGB);
for(int x = 0; x < side; x++){
for(int y = 0; y < side; y++){
img.setRGB(x, y, image[x + y * side].getRGB());
}
}
File path = new File(System.getProperty("user.home") + "/Desktop/ForumAvatar-" + input + ".png");
try{
ImageIO.write(img, "png", path);
}catch(IOException e){
e.printStackTrace();
}
}
private static Color[] mirrorImage(Color[] image){
Color[] result = new Color[image.length];
for(int i = 0; i < image.length / 2; i++){
Color temp = image[i];
result[i] = image[image.length - i - 1];
result[image.length - i - 1] = temp;
}
return result;
}
private static Color[] add(Color[] c1, Color[] c2){
int len1 = c1.length;
int len2 = c2.length;
Color[] c = new Color[len1 + len2];
System.arraycopy(c1, 0, c, 0, len1);
System.arraycopy(c2, 0, c, len1, len2);
return c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment