public class IdentIcon
extends java.lang.Object
Default size of the icon is 32x32 pixel. The icons are generated from a grid of 4x4 patches.
This is intended to be used for generating individual icons for uses bases on their unique user name / OU combination. Based on the original Identicon implementation of Don Park, see https://github.com/donpark/identicon.
Original copyright notice:
Copyright (c) 2007-2012 Don Park <donpark@docuverse.com>
| Modifier and Type | Field and Description |
|---|---|
static java.awt.Color |
COLOR_TRANSPARENT
Constant to identify a transparent background fill color.
|
static java.lang.String |
IDENTICON_SALT
Salt String for generation better hashes.
|
| Constructor and Description |
|---|
IdentIcon()
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
drawPatch(java.awt.Graphics2D g,
float x,
float y,
float size,
int patch,
int turn,
boolean invert,
java.awt.Color fillColor,
java.awt.Color strokeColor) |
protected java.awt.image.BufferedImage |
drawShape(int shape)
Method to draw a single shape, useful for visual testing the shape output.
|
java.awt.Color |
getBackgroundColor()
Returns the background color of the IdentIcon.
|
protected float |
getColorDistance(java.awt.Color c1,
java.awt.Color c2)
Returns the distance between two colors.
|
protected java.awt.Color |
getComplementaryColor(java.awt.Color color)
Returns the complementary color for the given color.
|
float |
getPatchSize()
Returns the size in pixels at which each patch will be rendered before
they are scaled down to requested IdentIcon size.
|
java.awt.Color |
getReservedColor()
Returns the reserved color for the IdentIcon renderer.
|
int |
getSize()
Returns the target image size (height, width) for the rendered patch, default is 32.
|
protected byte[] |
hash(java.lang.String input)
Generates an MD5 hash array from the given input String.
|
java.awt.image.BufferedImage |
render(java.lang.String input)
Renders the IdentIcon for the given input String.
|
java.awt.image.BufferedImage |
render(java.lang.String input,
boolean allowProtected)
Renders the IdentIcon for the given input String.
|
java.awt.image.BufferedImage |
render(java.lang.String input,
boolean allowProtected,
int size)
Renders the IdentIcon for the given input String with the given size.
|
java.awt.image.BufferedImage |
render(java.lang.String input,
int size)
Renders the IdentIcon for the given input String.
|
java.awt.image.BufferedImage |
render(java.lang.String input,
int red,
int green,
int blue)
Renders the IdentIcon for the given input String in the given color.
|
protected java.awt.image.BufferedImage |
renderIcon(byte[] hash,
int size)
Renders the IdentIcon based on the input hash, using the internal parameters.
|
void |
setBackgroundColor(java.awt.Color backgroundColor)
The background color to be used for the IdentIcon.
|
void |
setPatchSize(float size)
Set the size in pixels at which each patch will be rendered before they
are scaled down to requested identicon size.
|
void |
setReservedColor(java.awt.Color reservedColor)
Returns the reserved color for the IdentIcon renderer.
|
void |
setSize(int size)
Sets the target image size (height, width) for the rendered patch, default is 32.
|
public static final java.awt.Color COLOR_TRANSPARENT
public static final java.lang.String IDENTICON_SALT
public java.awt.Color getBackgroundColor()
public float getPatchSize()
public java.awt.Color getReservedColor()
The default is null which disables the reserved color feature.
The reserved color can be set to make sure a certain color is only used for specific input Strings. A practical example would be "draw all admin users in orange, but no other users". In case the color calculated for the user name would be to close to the reserved color, the opposite color from the spectrum is used instead.
public int getSize()
public java.awt.image.BufferedImage render(java.lang.String input)
The protected color in this case is NOT allowed to be used for the generated icon.
input - the input String to render the IdentIcon forpublic java.awt.image.BufferedImage render(java.lang.String input,
boolean allowProtected)
input - the input String to render the IdentIcon forallowProtected - controls if the reserves color set by getReservedColor() can be used or notpublic java.awt.image.BufferedImage render(java.lang.String input,
boolean allowProtected,
int size)
Good values for the size parameter should set in dependency with the patch size set with
getPatchSize(). The largest size should be the patch size multiplied by four.
input - the input String to render the IdentIcon forallowProtected - controls if the reserves color set by getReservedColor() can be used or notsize - the target size of the output imagepublic java.awt.image.BufferedImage render(java.lang.String input,
int size)
Good values for the size parameter should set in dependency with the patch size set with
getPatchSize(). The largest size should be the patch size multiplied by four.
The protected color in this case is NOT allowed to be used for the generated icon.
input - the input String to render the IdentIcon forsize - the target size of the output imagepublic java.awt.image.BufferedImage render(java.lang.String input,
int red,
int green,
int blue)
The protected color in this case is NOT allowed to be used for the generated icon, as this method exists mostly for test purposes.
input - the input String to render the IdentIcon forred - the red color componentgreen - the green color componentblue - the blue color componentpublic void setBackgroundColor(java.awt.Color backgroundColor)
backgroundColor - the background color to setpublic void setPatchSize(float size)
Default size is 16 pixels which means, for 16-block identicon, a 64x64 image will be rendered and scaled down.
size - patch size in pixelspublic void setReservedColor(java.awt.Color reservedColor)
The default is null which disables the reserved color feature.
The reserved color can be set to make sure a certain color is only used for specific input Strings. A practical example would be "draw all admin users in orange, but no other users". In case the color calculated for the user name would be to close to the reserved color, the opposite color from the spectrum is used instead.
reservedColor - the reserved color for the IdentIcon renderer to setpublic void setSize(int size)
size - the target image size to setprotected void drawPatch(java.awt.Graphics2D g,
float x,
float y,
float size,
int patch,
int turn,
boolean invert,
java.awt.Color fillColor,
java.awt.Color strokeColor)
g - the graphic to draw onx - x positiony - y positionsize - size of the tilepatch - patch type to drawturn - turn of the patchinvert - invert color or notfillColor - fill colorstrokeColor - stroke colorprotected java.awt.image.BufferedImage drawShape(int shape)
shape - the shape to drawprotected float getColorDistance(java.awt.Color c1,
java.awt.Color c2)
c1 - the first colorc2 - the second colorprotected java.awt.Color getComplementaryColor(java.awt.Color color)
color - the base to calculate the complementary color forprotected byte[] hash(java.lang.String input)
The IDENTICON_SALT is added to the String for better results.
input - the input to generate the hash forprotected java.awt.image.BufferedImage renderIcon(byte[] hash,
int size)
hash - the hash to rendersize - the size of the image to generate