From 268fcd3838f35050008bc03a81c0873e515e75f6 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 19 Aug 2018 22:33:20 +0200 Subject: [PATCH] disabled by default variant of XEP-0392 --- build.gradle | 1 + .../java/eu/siacs/conversations/Config.java | 2 ++ .../siacs/conversations/utils/UIHelper.java | 3 ++ .../conversations/utils/XEP0392Helper.java | 31 +++++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 src/main/java/eu/siacs/conversations/utils/XEP0392Helper.java diff --git a/build.gradle b/build.gradle index 8c3596ade..d347faeeb 100644 --- a/build.gradle +++ b/build.gradle @@ -51,6 +51,7 @@ dependencies { implementation 'net.ypresto.androidtranscoder:android-transcoder:0.2.0' implementation 'rocks.xmpp:xmpp-addr:0.8.0' implementation 'org.osmdroid:osmdroid-android:6.0.1' + implementation 'org.hsluv:hsluv:0.2' } ext { diff --git a/src/main/java/eu/siacs/conversations/Config.java b/src/main/java/eu/siacs/conversations/Config.java index 22ae9179a..acd45fd3c 100644 --- a/src/main/java/eu/siacs/conversations/Config.java +++ b/src/main/java/eu/siacs/conversations/Config.java @@ -68,6 +68,8 @@ public final class Config { public static final int CONNECT_DISCO_TIMEOUT = 20; public static final int MINI_GRACE_PERIOD = 750; + public static final boolean XEP_0392 = false; //enables a variant of XEP-0392 that is based on HSLUV + public static final int AVATAR_SIZE = 192; public static final Bitmap.CompressFormat AVATAR_FORMAT = Bitmap.CompressFormat.JPEG; public static final int AVATAR_CHAR_LIMIT = 9400; diff --git a/src/main/java/eu/siacs/conversations/utils/UIHelper.java b/src/main/java/eu/siacs/conversations/utils/UIHelper.java index d889c9e95..0c5311208 100644 --- a/src/main/java/eu/siacs/conversations/utils/UIHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/UIHelper.java @@ -230,6 +230,9 @@ public class UIHelper { } public static int getColorForName(String name, boolean safe) { + if (Config.XEP_0392) { + return XEP0392Helper.rgbFromNick(name); + } if (name == null || name.isEmpty()) { return 0xFF202020; } diff --git a/src/main/java/eu/siacs/conversations/utils/XEP0392Helper.java b/src/main/java/eu/siacs/conversations/utils/XEP0392Helper.java new file mode 100644 index 000000000..a482c80ac --- /dev/null +++ b/src/main/java/eu/siacs/conversations/utils/XEP0392Helper.java @@ -0,0 +1,31 @@ +package eu.siacs.conversations.utils; + +import android.graphics.Color; +import android.util.Log; + +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)); + } +}