aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/utils/XEP0392Helper.java
blob: bbfe5089835119fb678ef40a90109e423fc934f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package de.pixart.messenger.utils;

import android.graphics.Color;

import org.hsluv.HUSLColorConverter;

import java.security.MessageDigest;

public class XEP0392Helper {

    private static double angle(String nickname) {
        try {
            MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
            byte[] digest = sha1.digest(nickname.getBytes("UTF-8"));
            int angle = ((int) (digest[0]) & 0xff) + ((int) (digest[1]) & 0xff) * 256;
            return angle / 65536.;
        } catch (Exception e) {
            return 0.0;
        }
    }

    public 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));
    }
}