diff --git a/.gitignore b/.gitignore
index 47bcbc8..3058a93 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,7 +8,7 @@ build
.DS_Store
.idea
**/*.iml
-
+/out
/*.svg
diff --git a/README.md b/README.md
index 2837ef8..2d44d23 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,8 @@
![IdenticonGen Logo](./docs/img/IdenticonGen.png)
# IdenticonGenerator
-A lite weight tool for generating Identicons in Java and Groovy.
+A lite weight tool for generating Identicons in Java and Groovy.
+
+[ ![Download](https://api.bintray.com/packages/meadowhawk/utils/IdenticonGenerator/images/download.svg?version=1.1.0) ](https://bintray.com/meadowhawk/utils/IdenticonGenerator/1.1.0/link)
### A what?
An [Identicon](https://en.wikipedia.org/wiki/Identicon) is a visual representation of a hash value, usually of an IP address, that serves to identify a user of a computer system as a form of avatar while protecting the users' privacy. The original Identicon was a 9-block graphic, and the representation has been extended to other graphic forms by third parties.
@@ -40,19 +42,19 @@ implementation 'org.meadowhawk:IdenticonGenerator:1.0.0'
```
### Code Examples
-The interface is intended to be easy so all calls can be made on the static methods in [IdenticonGenerator.groovy](./src/main/groovy/org/meadowhawk/identicon/IdenticonGenerator.groovy) Unless you want to do something that isn't already coded, this is all you have to do!
+The interface is intended to be easy to use so all calls can be made on the static methods in [IdenticonGenerator.groovy](./src/main/groovy/org/meadowhawk/identicon/IdenticonGenerator.groovy) Unless you want to do something that isn't already coded, this is all you have to do!
#### Write a little Code to create an Identicon in a file!
```
-IdenticonGenerator.generateToFile("TRICHROME", "TEST.SVG")
+IdenticonGenerator.generateToFile(new Monochrome(), "TEST.SVG")
```
-#### Want more control where the image gores? Just get an OutputStream so yo can do what you want with it later
+#### Want more control where the image goes? Just get an OutputStream so you can do what you want with it later
```
-StringWriter out = new StringWriter()
+StringWriter out = IdenticonGenerator.generate("TheByteArrayFromaUserNameOrPublickKey".getBytes(), new Trichrome, IconSize.REGULAR)
-IdenticonGenerator.generate("TRICHROME", out)
```
+There are quire a few other static methods available to call, take a look at IdenticonGenerator for more.
### More Stuff
@@ -60,25 +62,52 @@ IdenticonGenerator.generate("TRICHROME", out)
To ensure that the users get unique Identicons you can base them of a byte array generated from their unique id. There are a few ways to generate this byte array:
1. User their Public Key: (This is what the no byte array method do but using a randomly generated Key. of corse its better to use an actual public key) ``` KeyPair.getPublic().encoded ``` see. [Helper.groovy](./src/main/groovy/org/meadowhawk/identicon/util/Helper.groovy) for an example.
-2. Use the Users Id and add on their ip address ``` "lees2bytes@188.192.9.109".getBytes()```
+2. Use the Users Id and add on their ip address (must be >= 20 chars) ``` "lees2bytes@188.192.9.109".getBytes()```
3. User their email address; really any unique identifier will work, just make sure its on the longer side (50+ chars is nice) or the uniqueness wills suffer.
#### Patterns
-There are a number of Patterns that can be provided to give the Identicon a unique looks. Some are based entirely off the seed byte array and others have varying degrees of randomness in them just to amp up the interest level.
+There are a number of default patterns that are provided to give the Identicon a unique looks. Some are based entirely off the seed byte array and others have varying degrees of randomness in them just to amp up the interest level.
* PATCHWORK - Seeded from byte array, provides a nifty patchwork look when using a users public key.
* MONOCHROME - Seeded from the 42nd - 44th bytes in the seed, it provides a single color on a white background similar to the gitHub Identicon.
* TRICHROME - Seeded Randomly, it provides 2 colors on a white background similar to MONOCHROME.
* DOTS - Seeded from byte array, it randomly throws varying sized dots on a dark or light background.
-* MIRRORED - Based on part of the seed then mirrored
* RANDOM - Comes out a lot like Patchwork but the colors are randomly selected. Not based on seed and probably less unique?
+#### Make your Own Pattern
+Want something different for you pattern? No problem, you can implement your own pattern by implementing the [RenderPattern](./src/main/groovy/org/meadowhawk/identicon/pattern/RenderPattern.groovy) interface and passing that as one of the params to the appropriate ```IdenticonGenerator``` method.
+
+To implement your own pattern you would simply implement the ```.render()``` method, and provided closures for fillColors and colorList which are sued by the SVGBuilder methods. If you don't want to render grids or circles you can write your own version of the the SVGBuider function and likely skip the closures. The best way to understand how to build a pattern is to look at one of the default patterns. a very simple example is the [RandomPattern](./src/main/groovy/org/meadowhawk/identicon/pattern/RandomPattern.groovy) shown below.
+
+```
+class RandomPattern implements RenderPattern {
+ @Override
+ void render(StringWriter writer, byte[] bytes, int width, int height) {
+ SVGBuilder.generateGrid(writer, bytes, this, width, height)
+ }
+
+ def fillColors = { byte[] bytes, int colorCt -> //ignoring for this
+ String[] colors = []
+ colorCt.times {colors += Math.random()}
+ colors
+ }
+
+ def renderColor = { hxColor -> SVGBuilder.hsbColor(hxColor)}
+}
+
+```
+* render() makes a call to the SVGBuilder to build a grid based svg file passing the writer, the seed bytes used to generate the colors (public key, username etc.) and of course dimensions. note: it expects ht & wt to be equal because its building a square.
+* fillColors is a closure that the render method will use to determine what colors will be used and puts them in the correct order, in this case one color per square to be rendered. (This is pretty simple and a little limiting, if you want something very different then I would skip this and build it out in the render method..)
+* renderColor - Provides a translation from the generated color to the SVG color. In most of the defaults the color is either a web hex color or an hsb color and they have to be translated to rbg colors for teh svg format.
+
+Just to reiterate, if you aren't going to use the SVGBuilder, you can set these closures to ={} and just do your own thing in the render(). This oddity will likely go on my todo list.
+
### Want to help?
Dive on in or give me a shout! There's a few issues below that need work and I'm open to any expansions etc.
### TODOs
-* Reactor the Pattern Enum into a base class. Including closures in an enum was pretty amazing but this would benefit from a default class that can be overridden and allow for more patterns to be created without messing with an enum thats getting kinda ugly/
-* Allow for icon size changes, default is currently 100x100
-* Refine Hex to color algorithm
-* MAke TRICHROME based off seed.
\ No newline at end of file
+- [x] Reactor the Pattern Enum into a base class. Including closures in an enum was pretty amazing but this would benefit from a default class that can be overridden and allow for more patterns to be created without messing with an enum thats getting kinda ugly/
+- [x] Allow for icon size changes, default is currently 100x100
+- [ ] Refine Hex to color algorithm
+- [x] Make TRICHROME based off seed.
diff --git a/build.gradle b/build.gradle
index 636c9db..df23a71 100644
--- a/build.gradle
+++ b/build.gradle
@@ -5,7 +5,7 @@ plugins {
}
group 'org.meadowhawk'
-version '1.0.0'
+version '1.1.0'
sourceCompatibility = 1.8
@@ -106,8 +106,8 @@ bintray {
licenses = ['MIT']
vcsUrl = 'https://github.com/leeclarke/IdenticonGenerator.git'
version {
- name = '1.0.0'
- desc = '1.0.0'
+ name = '1.1.0'
+ desc = '1.1.0'
released = new Date()
}
}
diff --git a/src/main/groovy/org/meadowhawk/identicon/IdenticonGenerator.groovy b/src/main/groovy/org/meadowhawk/identicon/IdenticonGenerator.groovy
index 5de739f..e5095c9 100644
--- a/src/main/groovy/org/meadowhawk/identicon/IdenticonGenerator.groovy
+++ b/src/main/groovy/org/meadowhawk/identicon/IdenticonGenerator.groovy
@@ -1,53 +1,43 @@
package org.meadowhawk.identicon
+import org.meadowhawk.identicon.pattern.RenderPattern
import org.meadowhawk.identicon.util.Helper
-import org.meadowhawk.identicon.util.Pattern
-import org.meadowhawk.identicon.util.SVGBuilder
+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, Pattern pattern, File file){
- FileWriter fileWriter = new FileWriter(file)
- fileWriter.withWriter {
- fileWriter.write(IdenticonGenerator.generate(bytes, pattern).toString())
+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(byte[] bytes, Pattern pattern, String filePath){
- generateToFile(bytes, pattern, new File(filePath))
+ static boolean generateToFile(String pattern, String filePath){
+ generateToFile(Helper.getRandomSeed(), pattern, new File(filePath), IconSize.REGULAR)
}
- static boolean generateToFile(byte[] bytes, String pattern, String filePath){
- generateToFile(bytes, Pattern.fromString(pattern), new File(filePath))
+ 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(String pattern, String filePath){
- generateToFile(Helper.getRandomSeed(), Pattern.fromString(pattern), new File(filePath))
+ static boolean generateToFile(byte[] bytes, RenderPattern pattern, String filePath){
+ generateToFile(bytes, pattern, new File(filePath), REGULAR)
}
- static StringWriter generate(byte[] bytes, Pattern pattern){
+
+ 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()
- switch (pattern){
- case Pattern.MIRRORED:
- SVGBuilder.generateGrid(writer, bytes, Pattern.MIRRORED)
- break
- case Pattern.MONOCHROME:
- SVGBuilder.generateGrid(writer, bytes, Pattern.MONOCHROME)
- break
- case Pattern.TRICHROME:
- SVGBuilder.generateGrid(writer, bytes, Pattern.TRICHROME)
- break
- case Pattern.PATCHWORK:
- SVGBuilder.generateGrid(writer, bytes, Pattern.PATCHWORK)
- break
- case Pattern.DOTS:
- SVGBuilder.generateCircles(writer, bytes, Pattern.DOTS)
- break
- case Pattern.RANDOM:
- default:
- SVGBuilder.generateGrid(writer, bytes, Pattern.RANDOM)
- break
- }
+ def width = size.getSize()
+ def height = size.getSize()
+ pattern.render(writer, bytes, width, height)
writer
}
+
+
}
diff --git a/src/main/groovy/org/meadowhawk/identicon/pattern/Dots.groovy b/src/main/groovy/org/meadowhawk/identicon/pattern/Dots.groovy
new file mode 100644
index 0000000..22ad5ac
--- /dev/null
+++ b/src/main/groovy/org/meadowhawk/identicon/pattern/Dots.groovy
@@ -0,0 +1,37 @@
+package org.meadowhawk.identicon.pattern
+
+import org.meadowhawk.identicon.util.SVGBuilder
+
+import java.awt.Color
+
+class Dots implements RenderPattern{
+
+ @Override
+ void render(StringWriter writer, byte[] bytes, int width, int height) {
+ SVGBuilder.generateCircles(writer, bytes, this, width, height)
+ }
+
+ def fillColors = {byte[] bytes, int colorCt ->
+ //Take every 3 bytes to generate a color.
+ String[] colors = []
+ int c = 0
+ String tmp = ""
+ bytes.each { b->
+ def hex = String.format("%02x", b)
+ if(c <3) {tmp += hex; c++}
+ else {
+ colors += tmp
+ c = 0; tmp = ""
+ }
+ }
+
+ while(colors.size() < colorCt){
+ colors += colors
+ }
+ colors.take(colorCt)
+ }
+
+ def renderColor = {hxColor ->
+ SVGBuilder.rbgColor(Color.decode("#${hxColor}"))
+ }
+}
diff --git a/src/main/groovy/org/meadowhawk/identicon/pattern/Monochrome.groovy b/src/main/groovy/org/meadowhawk/identicon/pattern/Monochrome.groovy
new file mode 100644
index 0000000..d69deea
--- /dev/null
+++ b/src/main/groovy/org/meadowhawk/identicon/pattern/Monochrome.groovy
@@ -0,0 +1,30 @@
+package org.meadowhawk.identicon.pattern
+
+import org.meadowhawk.identicon.util.SVGBuilder
+
+import java.awt.Color
+
+class Monochrome implements RenderPattern{
+ @Override
+ void render(StringWriter writer, byte[] bytes, int width, int height) {
+ SVGBuilder.generateGrid(writer, bytes, this, width, height)
+ }
+
+ def fillColors = { byte[] bytes, int colorCt ->
+ int start = (bytes.size()<45)?42:10
+ String[] colors = []
+ String theColor = SVGBuilder.getHexColor(bytes[start],bytes[start+1], bytes[start+2])
+ String white = "FFFFFF"
+ bytes.each { b->
+ colors += (b%2==0)? theColor: white
+ }
+ while(colors.size() < colorCt){
+ colors += colors
+ }
+ colors.take(colorCt)
+ }
+
+ def renderColor = {hxColor ->
+ SVGBuilder.rbgColor(Color.decode("#${hxColor}"))
+ }
+}
diff --git a/src/main/groovy/org/meadowhawk/identicon/pattern/Patchwork.groovy b/src/main/groovy/org/meadowhawk/identicon/pattern/Patchwork.groovy
new file mode 100644
index 0000000..e5c8cea
--- /dev/null
+++ b/src/main/groovy/org/meadowhawk/identicon/pattern/Patchwork.groovy
@@ -0,0 +1,36 @@
+package org.meadowhawk.identicon.pattern
+
+import org.meadowhawk.identicon.util.SVGBuilder
+
+import java.awt.Color
+
+class Patchwork implements RenderPattern{
+
+ @Override
+ void render(StringWriter writer, byte[] bytes, int width, int height) {
+ SVGBuilder.generateGrid(writer, bytes, this, width, height)
+ }
+
+ def fillColors = {byte[] bytes, int colorCt ->
+ //Take every 3 bytes to generate a color.
+ String[] colors = []
+ int c = 0
+ String tmp = ""
+ bytes.each { b->
+ def hex = String.format("%02x", b)
+ if(c <3) {tmp += hex; c++}
+ else {
+ colors += tmp
+ c = 0; tmp = ""
+ }
+ }
+ while(colors.size() < colorCt){
+ colors += colors
+ }
+ colors.take(colorCt)
+ }
+
+ def renderColor = {hxColor ->
+ SVGBuilder.rbgColor(Color.decode("#${hxColor}"))
+ }
+}
diff --git a/src/main/groovy/org/meadowhawk/identicon/pattern/RandomPattern.groovy b/src/main/groovy/org/meadowhawk/identicon/pattern/RandomPattern.groovy
new file mode 100644
index 0000000..0e571f2
--- /dev/null
+++ b/src/main/groovy/org/meadowhawk/identicon/pattern/RandomPattern.groovy
@@ -0,0 +1,18 @@
+package org.meadowhawk.identicon.pattern
+
+import org.meadowhawk.identicon.util.SVGBuilder
+
+class RandomPattern implements RenderPattern {
+ @Override
+ void render(StringWriter writer, byte[] bytes, int width, int height) {
+ SVGBuilder.generateGrid(writer, bytes, this, width, height)
+ }
+
+ def fillColors = { byte[] bytes, int colorCt -> //ignoring for this
+ String[] colors = []
+ colorCt.times {colors += Math.random()}
+ colors
+ }
+
+ def renderColor = { hxColor -> SVGBuilder.hsbColor(hxColor)}
+}
diff --git a/src/main/groovy/org/meadowhawk/identicon/pattern/RenderPattern.groovy b/src/main/groovy/org/meadowhawk/identicon/pattern/RenderPattern.groovy
new file mode 100644
index 0000000..26971b1
--- /dev/null
+++ b/src/main/groovy/org/meadowhawk/identicon/pattern/RenderPattern.groovy
@@ -0,0 +1,7 @@
+package org.meadowhawk.identicon.pattern
+
+interface RenderPattern {
+ void render(StringWriter writer, byte[] bytes, int width, int height )
+ def fillColors
+ def renderColor
+}
diff --git a/src/main/groovy/org/meadowhawk/identicon/pattern/Trichrome.groovy b/src/main/groovy/org/meadowhawk/identicon/pattern/Trichrome.groovy
new file mode 100644
index 0000000..a008346
--- /dev/null
+++ b/src/main/groovy/org/meadowhawk/identicon/pattern/Trichrome.groovy
@@ -0,0 +1,32 @@
+package org.meadowhawk.identicon.pattern
+
+import org.meadowhawk.identicon.util.SVGBuilder
+
+import java.awt.Color
+
+class Trichrome implements RenderPattern {
+ @Override
+ void render(StringWriter writer, byte[] bytes, int width, int height) {
+ SVGBuilder.generateGrid(writer, bytes, this, width, height)
+ }
+
+ def fillColors = { byte[] bytes, int colorCt ->
+ String[] colors = []
+ String theColor = Math.random()
+ String theOtherColor = Math.random()
+ String white = 0
+
+ bytes.each { b->
+ colors += (b%2==0)? theColor: ((b%3==0)? theOtherColor:white)
+ }
+ while(colors.size() < colorCt){
+ colors += colors
+ }
+ colors.take(colorCt)
+ }
+
+ def renderColor = {hxColor ->
+ SVGBuilder.hsbColor(hxColor)
+ }
+
+}
diff --git a/src/main/groovy/org/meadowhawk/identicon/util/IconSize.groovy b/src/main/groovy/org/meadowhawk/identicon/util/IconSize.groovy
new file mode 100644
index 0000000..1a59323
--- /dev/null
+++ b/src/main/groovy/org/meadowhawk/identicon/util/IconSize.groovy
@@ -0,0 +1,13 @@
+package org.meadowhawk.identicon.util
+
+//Remember, we are making svg files here so technically you can resize them to whatever you want.
+enum IconSize {
+ SMALL(50), REGULAR(100), LARGE(200)
+
+ int size
+ IconSize(int pixels){
+ this.size = pixels
+ }
+
+ int getSize(){ this.size}
+}
\ No newline at end of file
diff --git a/src/main/groovy/org/meadowhawk/identicon/util/Pattern.groovy b/src/main/groovy/org/meadowhawk/identicon/util/Pattern.groovy
deleted file mode 100644
index 13bfd49..0000000
--- a/src/main/groovy/org/meadowhawk/identicon/util/Pattern.groovy
+++ /dev/null
@@ -1,90 +0,0 @@
-package org.meadowhawk.identicon.util
-
-import java.awt.Color
-
-enum Pattern {
- PATCHWORK({byte[] bytes ->
- //Take every 3 bytes to generate a color.
- String[] colors = []
- int c = 0
- String tmp = ""
- bytes.each { b->
- def hex = String.format("%02x", b)
- if(c <3) {tmp += hex; c++}
- else {
- colors += tmp
- c = 0; tmp = ""
- }
- }
- if(colors.size() <= 100) {colors += colors.take(100 - colors.size())}
- colors
- }, {hxColor ->
- SVGBuilder.rbgColor(Color.decode("#${hxColor}"))
- }),
-
- MIRRORED({},{}),
-
- DOTS({byte[] bytes ->
- //Take every 3 bytes to generate a color.
- String[] colors = []
- int c = 0
- String tmp = ""
- bytes.each { b->
- def hex = String.format("%02x", b)
- if(c <3) {tmp += hex; c++}
- else {
- colors += tmp
- c = 0; tmp = ""
- }
- }
- if(colors.size() <= 100) {colors += colors.take(100 - colors.size())}
- colors
- },{hxColor ->
- SVGBuilder.rbgColor(Color.decode("#${hxColor}"))
- }),
- MONOCHROME({ byte[] bytes ->
- String hexFormat = "%02x"
- String[] colors = []
- String theColor = String.format(hexFormat, bytes[42]) + String.format(hexFormat, bytes[43]) + String.format(hexFormat, bytes[44])
- String white = "FFFFFF"
- bytes.each { b->
- colors += (b%2==0)? theColor: white
- }
- if(colors.size() > 100) {colors = colors.take(100)}
- colors
- },{hxColor ->
- SVGBuilder.rbgColor(Color.decode("#${hxColor}"))
- }),
- TRICHROME({ byte[] bytes ->
- String[] colors = []
- String theColor = Math.random()
- String theOtherColor = Math.random()
- String white = 0
- bytes.each { b->
- colors += (b%2==0)? theColor: ((b%3==0)? theOtherColor:white)
- }
- colors
- },{hxColor ->
- SVGBuilder.hsbColor(hxColor)
- }),
- RANDOM({ byte[] bytes -> //ignoring for this
- String[] colors = []
- 100.times {Math.random()}
- colors
- },{ hxColor -> SVGBuilder.hsbColor(hxColor)})
-
- Closure fillColors
- Closure buildColorValue
- Pattern(Closure fillColors, Closure colorList){
- this.fillColors = fillColors
- this.buildColorValue = colorList
- }
-
- static Pattern fromString(String patternName){
- try{
- Pattern.valueOf(patternName)
- } catch(IllegalArgumentException e){
- Pattern.RANDOM
- }
- }
-}
\ No newline at end of file
diff --git a/src/main/groovy/org/meadowhawk/identicon/util/RenderPatternFactory.groovy b/src/main/groovy/org/meadowhawk/identicon/util/RenderPatternFactory.groovy
new file mode 100644
index 0000000..2d76a80
--- /dev/null
+++ b/src/main/groovy/org/meadowhawk/identicon/util/RenderPatternFactory.groovy
@@ -0,0 +1,32 @@
+package org.meadowhawk.identicon.util
+
+import org.meadowhawk.identicon.pattern.Dots
+import org.meadowhawk.identicon.pattern.Monochrome
+import org.meadowhawk.identicon.pattern.Patchwork
+import org.meadowhawk.identicon.pattern.RandomPattern
+import org.meadowhawk.identicon.pattern.RenderPattern
+import org.meadowhawk.identicon.pattern.Trichrome
+
+class RenderPatternFactory {
+ static RenderPattern getPattern(String patternName){
+ def ptrnNameUpper =patternName.toUpperCase()
+ RenderPattern pattern = new RandomPattern()
+ switch (ptrnNameUpper){
+ case "MONOCHROME":
+ pattern = new Monochrome()
+ break
+ case "TRICHROME":
+ pattern = new Trichrome()
+ break
+ case "PATCHWORK":
+ pattern = new Patchwork()
+ break
+ case "DOTS":
+ pattern = new Dots()
+ break
+ case "RANDOM":
+ pattern = new RandomPattern()
+ }
+ pattern
+ }
+}
diff --git a/src/main/groovy/org/meadowhawk/identicon/util/SVGBuilder.groovy b/src/main/groovy/org/meadowhawk/identicon/util/SVGBuilder.groovy
index c2eb9eb..f37ca6c 100644
--- a/src/main/groovy/org/meadowhawk/identicon/util/SVGBuilder.groovy
+++ b/src/main/groovy/org/meadowhawk/identicon/util/SVGBuilder.groovy
@@ -1,6 +1,7 @@
package org.meadowhawk.identicon.util
import groovy.xml.MarkupBuilder
+import org.meadowhawk.identicon.pattern.RenderPattern
import java.awt.Color
@@ -36,27 +37,34 @@ class SVGBuilder {
static def rbgColor = { Color color ->
return "rgb(${color.red},${color.green},${color.blue})"
}
+ static String hexFormat = "%02x"
- static void generateCircles(Writer writer, byte[] bytes, Pattern pattern){
- String[] colors = pattern.fillColors(bytes)
+ static String getHexColor(byte byte1 , byte byte2, byte byte3){
+ String.format(hexFormat, byte1) + String.format(hexFormat, byte2) + String.format(hexFormat, byte3)
+ }
+
+ static void generateCircles(Writer writer, byte[] bytes, RenderPattern pattern, int width, int height){
+ int colorCt = width
+ String[] colors = pattern.fillColors(bytes, colorCt)
String bgColor = (bytes[2]%2==0)?"ffffff": "282828"
- SVGBuilder.createSvg(writer, 100, 100) { width, height ->
- rect(x: 0, y: 0, width: width, height: height, fill: pattern.buildColorValue(bgColor)) //White backfill
+ SVGBuilder.createSvg(writer, width, height) { w, h ->
+ rect(x: 0, y: 0, width: w, height: h, fill: pattern.renderColor(bgColor)) //White backfill
colors.each { color->
- circle(cx:Math.random() * width, cy:Math.random() * height,r:Math.min(width,height)/ (Math.abs(new Random().nextInt() % (25 - 10)) + 10) , fill:pattern.buildColorValue(color))
+ circle(cx:Math.random() * width, cy:Math.random() * height,r:Math.min(width,height)/ (Math.abs(new Random().nextInt() % (25 - 10)) + 10) , fill:pattern.renderColor(color))
}
}
}
- static void generateGrid(Writer writer, byte[] bytes, Pattern pattern){
- String[] colors = pattern.fillColors(bytes)
- SVGBuilder.createSvg(writer, 100, 100) { width, height ->
+ static void generateGrid(Writer writer, byte[] bytes, RenderPattern pattern, int width, int height){
+ int colorCt = width * height
+ String[] colors = pattern.fillColors(bytes, colorCt)
+ SVGBuilder.createSvg(writer, width, height) { w, h ->
int x = 0
int y = 0
int b = 0
- while (y < height) {
- while (x < width) {
- rect(x: x, y: y, width: 10, height: 10, fill: pattern.buildColorValue(colors[b]))
+ while (y < h) {
+ while (x < w) {
+ rect(x: x, y: y, width: 10, height: 10, fill: pattern.renderColor(colors[b]))
x += 10
b++
}
diff --git a/src/test/groovy/org/meadowhawk/identicon/IdenticonGeneratorSpec.groovy b/src/test/groovy/org/meadowhawk/identicon/IdenticonGeneratorSpec.groovy
index 98a99dc..f2464b1 100644
--- a/src/test/groovy/org/meadowhawk/identicon/IdenticonGeneratorSpec.groovy
+++ b/src/test/groovy/org/meadowhawk/identicon/IdenticonGeneratorSpec.groovy
@@ -1,7 +1,12 @@
package org.meadowhawk.identicon
+import org.meadowhawk.identicon.pattern.Dots
+import org.meadowhawk.identicon.pattern.Monochrome
+import org.meadowhawk.identicon.pattern.Patchwork
+import org.meadowhawk.identicon.pattern.RandomPattern
+import org.meadowhawk.identicon.pattern.Trichrome
import org.meadowhawk.identicon.util.Helper
-import org.meadowhawk.identicon.util.Pattern
+import org.meadowhawk.identicon.util.IconSize
import spock.lang.Shared
import spock.lang.Specification
@@ -12,13 +17,13 @@ class IdenticonGeneratorSpec extends Specification{
@Shared
KeyPair keys = Helper.getKeys()
- def "Generate a Random/rainbow Styled Identicon"(){
+ def "Generate a Random Styled Identicon"(){
given:
byte[] bytes = keys.getPublic().encoded
- String fileName = "rainbowIcon.svg"
+ String fileName = "rnd.svg"
when:
- IdenticonGenerator.generateToFile(bytes, Pattern.PATCHWORK, fileName)
+ IdenticonGenerator.generateToFile(bytes, new RandomPattern(), fileName)
then:
assert new File(fileName).exists()
@@ -30,22 +35,43 @@ class IdenticonGeneratorSpec extends Specification{
byte[] bytes = keys.getPublic().encoded
when:
- Writer writer = IdenticonGenerator.generate(bytes, Pattern.PATCHWORK)
+ StringWriter writer = IdenticonGenerator.generate(bytes,new Patchwork(), IconSize.SMALL)
then:
assert writer != null
+ String content = writer.toString()
+ def startStr = "")
+
+ File out = new File("patch.svg").withWriter { w->
+ w.write(content)
+ }
}
- def "Generate a MONOCHROME Styled Identicon"(){
+ def "Generate a MONOCHROME Styled Identicon using the RenderPattern"(){
given:
byte[] bytes = keys.getPublic().encoded
- String fileName = "monoIcon.svg"
+ String fileName = "monoIconRP.svg"
when:
- IdenticonGenerator.generateToFile(bytes, Pattern.MONOCHROME, fileName)
+ IdenticonGenerator.generateToFile(bytes, new Monochrome(), new File(fileName), IconSize.REGULAR)
then:
- assert new File(fileName).exists()
+ File monoFile = new File(fileName)
+ assert monoFile.exists()
+ String monoContent = new FileReader(monoFile).text
+ assert monoContent.startsWith("