mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-25 07:23:03 +00:00
Generate letter icons
This commit is contained in:
parent
240a30b1a3
commit
f2112d3268
2 changed files with 47 additions and 13 deletions
|
@ -133,13 +133,16 @@ public class ContactInfo {
|
|||
}
|
||||
|
||||
if (info.bitmap == null) {
|
||||
TypedValue tv = new TypedValue();
|
||||
context.getTheme().resolveAttribute(R.attr.themeName, tv, true);
|
||||
int dp = Helper.dp2pixels(context, 48);
|
||||
|
||||
boolean dark = !"light".equals(tv.string);
|
||||
boolean identicons = prefs.getBoolean("identicons", false);
|
||||
if (identicons) {
|
||||
TypedValue tv = new TypedValue();
|
||||
context.getTheme().resolveAttribute(R.attr.themeName, tv, true);
|
||||
int dp = Helper.dp2pixels(context, 48);
|
||||
info.bitmap = Identicon.generate(key, dp, 5, !"light".equals(tv.string));
|
||||
}
|
||||
if (identicons)
|
||||
info.bitmap = Identicon.icon(key, dp, 5, dark);
|
||||
else
|
||||
info.bitmap = Identicon.letter(key, dp, dark);
|
||||
}
|
||||
|
||||
if (info.displayName == null)
|
||||
|
|
|
@ -31,13 +31,8 @@ import java.security.MessageDigest;
|
|||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
class Identicon {
|
||||
static Bitmap generate(String email, int size, int pixels, boolean dark) {
|
||||
byte[] hash;
|
||||
try {
|
||||
hash = MessageDigest.getInstance("MD5").digest(email.getBytes());
|
||||
} catch (NoSuchAlgorithmException ignored) {
|
||||
hash = email.getBytes();
|
||||
}
|
||||
static Bitmap icon(String email, int size, int pixels, boolean dark) {
|
||||
byte[] hash = getHash(email);
|
||||
|
||||
int color = Color.argb(255, hash[0], hash[1], hash[2]);
|
||||
color = ColorUtils.blendARGB(color, dark ? Color.WHITE : Color.BLACK, 0.2f);
|
||||
|
@ -63,4 +58,40 @@ class Identicon {
|
|||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
static Bitmap letter(String email, int size, boolean dark) {
|
||||
byte[] hash = getHash(email);
|
||||
|
||||
int color = Color.argb(255, hash[0], hash[1], hash[2]);
|
||||
color = ColorUtils.blendARGB(color, dark ? Color.WHITE : Color.BLACK, 0.2f);
|
||||
|
||||
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawColor(color);
|
||||
|
||||
Paint paint = new Paint();
|
||||
paint.setColor(Color.argb(
|
||||
Color.alpha(color),
|
||||
(~Color.red(color)) & 0xff,
|
||||
(~Color.green(color)) & 0xff,
|
||||
(~Color.blue(color)) & 0xff));
|
||||
paint.setTextSize(size / 2f);
|
||||
paint.setFakeBoldText(true);
|
||||
|
||||
String text = email.substring(0, 1).toUpperCase();
|
||||
canvas.drawText(
|
||||
text,
|
||||
size / 2f - paint.measureText(text) / 2,
|
||||
size / 2f - ((paint.descent() + paint.ascent()) / 2), paint);
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
private static byte[] getHash(String email) {
|
||||
try {
|
||||
return MessageDigest.getInstance("MD5").digest(email.getBytes());
|
||||
} catch (NoSuchAlgorithmException ignored) {
|
||||
return email.getBytes();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue