Skip to content

Instantly share code, notes, and snippets.

@NimaBoscarino
Created August 7, 2014 04:05
Show Gist options
  • Save NimaBoscarino/f6153ad32dfae6c526c0 to your computer and use it in GitHub Desktop.
Save NimaBoscarino/f6153ad32dfae6c526c0 to your computer and use it in GitHub Desktop.
Avatar Maker
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
public class avatar {
static int charSum = 0;
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println("Enter username: ");
imagemaker(scan.nextLine());
scan.close();
}
public static Color colorSeeder(String name) { //to create the seed color
int r; int g; int b; //color values
for (int i = 0; i<name.length(); i++) charSum += name.charAt(i); //add up all the chars
r = charSum%256;
g = (r + r)%256; // so that the values are pretty different, I guess.
b = (r + g)%256;
Color ret = new Color(r, g, b); //create Color with the generated values
return ret;
}
public static void imagemaker(String name) throws IOException {
BufferedImage img = new BufferedImage(9, 9, BufferedImage.TYPE_INT_RGB);
File f = new File(name + ".png");
Color seed = colorSeeder(name);
for (int i = 0; i<img.getWidth(); i++) {
for (int j = 0; j<img.getHeight(); j++) {
img.setRGB(i, j, seed.getRGB());
img.setRGB(j, i, seed.getRGB());
switch (charSum%3) { //for extra chaos
case 0: seed = new Color((seed.getRed() + 30)%256, seed.getGreen(), seed.getBlue());
case 1: seed = new Color(seed.getRed(), (seed.getGreen() + 30)%256, seed.getBlue());
case 2: seed = new Color(seed.getRed(), seed.getGreen(), (seed.getBlue() + 30)%256);
}
}
}
//this part here is just to enlarge the image.
BufferedImage enlargedImage = new BufferedImage(90, 90, img.getType());
for (int y = 0; y < 90; ++y){
for (int x = 0; x < 90; ++x){
enlargedImage.setRGB(x, y, img.getRGB(x / 10, y / 10));
}
}
ImageIO.write(enlargedImage, "PNG", f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment