|
14 | 14 | */
|
15 | 15 | public class IdenticonGenerator {
|
16 | 16 |
|
| 17 | + private static final String[] COLOUR_SPACE = new String[]{ |
| 18 | + "#1E88E5", |
| 19 | + "#7B99FA", |
| 20 | + "#53CDD8", |
| 21 | + "#F68787", |
| 22 | + "#96EAB7", |
| 23 | + }; |
| 24 | + |
| 25 | + private static final String DEFAULT_COLOUR_BLACK = "#ffffff"; |
17 | 26 |
|
18 | 27 | private enum CSSClass {
|
19 | 28 | ONE("identicon-one"),
|
@@ -62,9 +71,7 @@ public static String generateIdenticonSVG(String input) {
|
62 | 71 | }
|
63 | 72 |
|
64 | 73 | // the color is determined by the first three bytes
|
65 |
| - final String identiconColor = SvgBuilder.toHexColor((byte) (hashedInput[0] ^ hashedInput[1]), |
66 |
| - (byte) (hashedInput[1] ^ hashedInput[2]), |
67 |
| - (byte) (hashedInput[2] ^ hashedInput[3])); |
| 74 | + final String identiconColor = getHexColor(hashedInput); |
68 | 75 |
|
69 | 76 | // use the css class to overwrite the colors
|
70 | 77 | int classByteValue = Byte.toUnsignedInt(hashedInput[63]);
|
@@ -95,6 +102,17 @@ public static String generateIdenticonSVG(String input) {
|
95 | 102 | .build();
|
96 | 103 | }
|
97 | 104 |
|
| 105 | + private static String getHexColor(byte[] hashedInput) { |
| 106 | + if (hashedInput == null || hashedInput.length < 3) { |
| 107 | + return DEFAULT_COLOUR_BLACK; |
| 108 | + } |
| 109 | + // we are looking into the first 3 byte and create an XOR on them, to enhance the |
| 110 | + // variation of values a little bit |
| 111 | + var colourAssignmentByte = Byte.toUnsignedInt((byte) (hashedInput[0] ^ hashedInput[1] ^ hashedInput[2])); |
| 112 | + // Will always access with an index between 0 and the length of the colour space array minus 1 |
| 113 | + return COLOUR_SPACE[colourAssignmentByte % COLOUR_SPACE.length]; |
| 114 | + } |
| 115 | + |
98 | 116 | public static class IdenticonGenerationException extends RuntimeException {
|
99 | 117 |
|
100 | 118 | public IdenticonGenerationException(Throwable cause) {
|
|
0 commit comments