2018-08-19 20:33:20 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
|
|
|
import org.hsluv.HUSLColorConverter;
|
|
|
|
|
2021-01-23 08:25:34 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2018-08-19 20:33:20 +00:00
|
|
|
import java.security.MessageDigest;
|
|
|
|
|
2019-07-02 09:10:21 +00:00
|
|
|
class XEP0392Helper {
|
2018-08-19 20:33:20 +00:00
|
|
|
|
|
|
|
private static double angle(String nickname) {
|
|
|
|
try {
|
|
|
|
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
|
2021-01-23 08:25:34 +00:00
|
|
|
byte[] digest = sha1.digest(nickname.getBytes(StandardCharsets.UTF_8));
|
2018-08-19 20:33:20 +00:00
|
|
|
int angle = ((int) (digest[0]) & 0xff) + ((int) (digest[1]) & 0xff) * 256;
|
|
|
|
return angle / 65536.;
|
|
|
|
} catch (Exception e) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 09:10:21 +00:00
|
|
|
static int rgbFromNick(String name) {
|
2018-08-19 20:33:20 +00:00
|
|
|
double[] hsluv = new double[3];
|
|
|
|
hsluv[0] = angle(name) * 360;
|
|
|
|
hsluv[1] = 100;
|
|
|
|
hsluv[2] = 50;
|
|
|
|
double[] rgb = HUSLColorConverter.hsluvToRgb(hsluv);
|
|
|
|
return Color.rgb((int) Math.round(rgb[0] * 255), (int) Math.round(rgb[1] * 255), (int) Math.round(rgb[2] * 255));
|
|
|
|
}
|
|
|
|
}
|