anotherim/src/main/java/eu/siacs/conversations/utils/XEP0392Helper.java

32 lines
982 B
Java
Raw Normal View History

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;
import java.security.MessageDigest;
2019-07-02 09:10:21 +00:00
class XEP0392Helper {
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));
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) {
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));
}
}