Last active
January 20, 2017 15:07
-
-
Save AndrewReitz/2a64b1fa014e393469808fc1979c6115 to your computer and use it in GitHub Desktop.
Code Golf: Git Hub Avatar Generator
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
apply plugin: 'groovy' | |
apply plugin: 'application' | |
mainClassName = 'cash.andrew.GitHubAvatar' | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
compile 'org.codehaus.groovy:groovy-all:2.4.8' | |
} |
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 cash.andrew | |
import javax.imageio.ImageIO | |
import java.awt.image.BufferedImage | |
import java.time.LocalDateTime | |
// 0xffffff + 1 | |
// so that random returns a value from 0 to 0xffffff | |
final int MAX_COLOR_VALUE = 16777216 | |
def random = new Random() | |
def backgroundColor = 0xf0f0f0 | |
def avatarColor = random.nextInt(MAX_COLOR_VALUE) | |
// highly unlikely but just to make sure | |
while (avatarColor == backgroundColor) { | |
avatarColor = random.nextInt(MAX_COLOR_VALUE) | |
} | |
def image = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB) | |
// outside 2 columns | |
for (x in 0..1) { | |
for (y in 0..4) { | |
def color = random.nextBoolean() ? backgroundColor : avatarColor | |
image.setRGB(x, y, color) | |
image.setRGB(4 - x, y, color) | |
} | |
} | |
// center row | |
for (y in 0..4) { | |
def color = random.nextBoolean() ? backgroundColor : avatarColor | |
image.setRGB(2, y, color) | |
} | |
def fileName = "avatar_${LocalDateTime.now()}.png" | |
def file = new File(fileName) | |
ImageIO.write(image, 'png', file) | |
println "New avatar created! $fileName" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment