-
Notifications
You must be signed in to change notification settings - Fork 0
/
IdenticonGenerator.groovy
43 lines (32 loc) · 1.56 KB
/
IdenticonGenerator.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package org.meadowhawk.identicon
import org.meadowhawk.identicon.pattern.RenderPattern
import org.meadowhawk.identicon.util.Helper
import org.meadowhawk.identicon.util.IconSize
import org.meadowhawk.identicon.util.RenderPatternFactory
import static org.meadowhawk.identicon.util.IconSize.*
class IdenticonGenerator {
static boolean generateToFile(byte[] bytes, String pattern, File file, IconSize size){
file.withWriter { writer ->
writer.write(IdenticonGenerator.generate(bytes, RenderPatternFactory.getPattern(pattern), size).toString())
}
}
static boolean generateToFile(String pattern, String filePath){
generateToFile(Helper.getRandomSeed(), pattern, new File(filePath), IconSize.REGULAR)
}
static boolean generateToFile(byte[] bytes, RenderPattern pattern, File file, IconSize size){
file.withWriter { writer ->
writer.write(IdenticonGenerator.generate(bytes, pattern, size).toString())
}
}
static boolean generateToFile(byte[] bytes, RenderPattern pattern, String filePath){
generateToFile(bytes, pattern, new File(filePath), REGULAR)
}
static StringWriter generate(byte[] bytes, RenderPattern pattern, IconSize size){
if (bytes.size() < 20) throw new IllegalArgumentException("Input seed should be 20 chars at least to produce good randomness. Seed Len= ${bytes.size()}")
def writer = new StringWriter()
def width = size.getSize()
def height = size.getSize()
pattern.render(writer, bytes, width, height)
writer
}
}