2018-11-03 16:34:35 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
2018-12-31 08:04:33 +00:00
|
|
|
/*
|
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
|
|
|
|
*/
|
|
|
|
|
|
2018-11-03 16:34:35 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.graphics.RectF;
|
|
|
|
|
|
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
2018-11-03 17:12:37 +00:00
|
|
|
import androidx.core.graphics.ColorUtils;
|
|
|
|
|
|
2018-11-03 16:34:35 +00:00
|
|
|
class Identicon {
|
2018-11-03 17:12:37 +00:00
|
|
|
static Bitmap generate(String email, int size, int pixels, boolean dark) {
|
2018-11-03 16:34:35 +00:00
|
|
|
byte[] hash;
|
|
|
|
|
try {
|
|
|
|
|
hash = MessageDigest.getInstance("MD5").digest(email.getBytes());
|
|
|
|
|
} catch (NoSuchAlgorithmException ignored) {
|
|
|
|
|
hash = email.getBytes();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-03 17:12:37 +00:00
|
|
|
int color = Color.argb(255, hash[0], hash[1], hash[2]);
|
|
|
|
|
color = ColorUtils.blendARGB(color, dark ? Color.BLACK : Color.WHITE, 0.2f);
|
|
|
|
|
|
2018-11-03 16:34:35 +00:00
|
|
|
Paint paint = new Paint();
|
2018-11-03 17:12:37 +00:00
|
|
|
paint.setColor(color);
|
2018-11-03 16:34:35 +00:00
|
|
|
|
|
|
|
|
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
|
|
|
|
Canvas canvas = new Canvas(bitmap);
|
2018-11-03 17:12:37 +00:00
|
|
|
canvas.drawColor(Color.TRANSPARENT);
|
2018-11-03 16:34:35 +00:00
|
|
|
|
|
|
|
|
float psize = (float) size / pixels;
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x < pixels; x++) {
|
|
|
|
|
int i = (x > pixels / 2 ? pixels - x - 1 : x);
|
|
|
|
|
for (int y = 0; y < pixels; y++) {
|
|
|
|
|
if ((hash[i] >> y & 1) == 1) {
|
|
|
|
|
RectF rect = new RectF(x * psize, y * psize, (x + 1) * psize, (y + 1) * psize);
|
|
|
|
|
canvas.drawRect(rect, paint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
|
}
|
|
|
|
|
}
|