Skip to content

Commit 32d500e

Browse files
authored
Use fixed colour palette for identicons (#687)
1 parent 5cfe8c0 commit 32d500e

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

user-interface/src/main/java/life/qbic/datamanager/views/identicon/IdenticonGenerator.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
*/
1515
public class IdenticonGenerator {
1616

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";
1726

1827
private enum CSSClass {
1928
ONE("identicon-one"),
@@ -62,9 +71,7 @@ public static String generateIdenticonSVG(String input) {
6271
}
6372

6473
// 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);
6875

6976
// use the css class to overwrite the colors
7077
int classByteValue = Byte.toUnsignedInt(hashedInput[63]);
@@ -95,6 +102,17 @@ public static String generateIdenticonSVG(String input) {
95102
.build();
96103
}
97104

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+
98116
public static class IdenticonGenerationException extends RuntimeException {
99117

100118
public IdenticonGenerationException(Throwable cause) {

0 commit comments

Comments
 (0)