-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from leeclarke/develop
Develop
- Loading branch information
Showing
20 changed files
with
428 additions
and
227 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ build | |
.DS_Store | ||
.idea | ||
**/*.iml | ||
|
||
/out | ||
|
||
/*.svg | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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,45 +42,72 @@ 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 | ||
|
||
#### Unique Seeds | ||
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 ``` "[email protected]".getBytes()``` | ||
2. Use the Users Id and add on their ip address (must be >= 20 chars) ``` "[email protected]".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. | ||
- [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. |
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
60 changes: 25 additions & 35 deletions
60
src/main/groovy/org/meadowhawk/identicon/IdenticonGenerator.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/main/groovy/org/meadowhawk/identicon/pattern/Dots.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}")) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/groovy/org/meadowhawk/identicon/pattern/Monochrome.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}")) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/groovy/org/meadowhawk/identicon/pattern/Patchwork.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}")) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/groovy/org/meadowhawk/identicon/pattern/RandomPattern.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/groovy/org/meadowhawk/identicon/pattern/RenderPattern.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/groovy/org/meadowhawk/identicon/pattern/Trichrome.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/groovy/org/meadowhawk/identicon/util/IconSize.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
} |
Oops, something went wrong.