From ae47e0fe6e49483f627a481279c50dd7680fa338 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 3 Nov 2018 17:12:37 +0000 Subject: [PATCH] Lighten or darken identicon --- .../main/java/eu/faircode/email/AdapterMessage.java | 6 +++++- app/src/main/java/eu/faircode/email/Identicon.java | 11 ++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index b2d7e04dee..326c684fc5 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -118,6 +118,7 @@ public class AdapterMessage extends PagedListAdapter selectionTracker = null; @@ -292,7 +293,9 @@ public class AdapterMessage extends PagedListAdapter 0) - ivAvatar.setImageBitmap(Identicon.generate(((InternetAddress) message.from[0]).getAddress(), dp24, 5)); + ivAvatar.setImageBitmap( + Identicon.generate(((InternetAddress) message.from[0]).getAddress(), + dp24, 5, "light".equals(theme))); else ivAvatar.setImageDrawable(null); photo = true; @@ -1409,6 +1412,7 @@ public class AdapterMessage extends PagedListAdapter DIFF_CALLBACK = diff --git a/app/src/main/java/eu/faircode/email/Identicon.java b/app/src/main/java/eu/faircode/email/Identicon.java index 46a161d79e..eb3f67c36a 100644 --- a/app/src/main/java/eu/faircode/email/Identicon.java +++ b/app/src/main/java/eu/faircode/email/Identicon.java @@ -9,8 +9,10 @@ import android.graphics.RectF; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import androidx.core.graphics.ColorUtils; + class Identicon { - static Bitmap generate(String email, int size, int pixels) { + static Bitmap generate(String email, int size, int pixels, boolean dark) { byte[] hash; try { hash = MessageDigest.getInstance("MD5").digest(email.getBytes()); @@ -18,12 +20,15 @@ class Identicon { hash = email.getBytes(); } + int color = Color.argb(255, hash[0], hash[1], hash[2]); + color = ColorUtils.blendARGB(color, dark ? Color.BLACK : Color.WHITE, 0.2f); + Paint paint = new Paint(); - paint.setColor(Color.argb(255, hash[0], hash[1], hash[2])); + paint.setColor(color); Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); - canvas.drawColor(Color.argb(255, 128, 128, 128)); + canvas.drawColor(Color.TRANSPARENT); float psize = (float) size / pixels;