2015-08-14 17:25:05 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Christian Basler
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package ch.dissem.apps.abit;
|
|
|
|
|
|
|
|
import android.graphics.*;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2016-10-10 21:57:46 +02:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
2015-08-14 17:25:05 +02:00
|
|
|
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
|
|
|
|
|
|
|
/**
|
2016-01-19 20:50:58 +01:00
|
|
|
* @author Christian Basler
|
2015-08-14 17:25:05 +02:00
|
|
|
*/
|
|
|
|
public class Identicon extends Drawable {
|
|
|
|
private static final int SIZE = 9;
|
|
|
|
private static final int CENTER_COLUMN = 5;
|
|
|
|
|
|
|
|
private final Paint paint;
|
|
|
|
private int color;
|
|
|
|
private int background;
|
|
|
|
private boolean[][] fields;
|
|
|
|
|
|
|
|
public Identicon(BitmessageAddress input) {
|
|
|
|
paint = new Paint();
|
|
|
|
paint.setStyle(Paint.Style.FILL);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
2016-01-19 20:50:58 +01:00
|
|
|
byte[] hash = input.getRipe();
|
2015-08-14 17:25:05 +02:00
|
|
|
|
|
|
|
fields = new boolean[SIZE][SIZE];
|
2016-10-10 21:57:46 +02:00
|
|
|
color = Color.HSVToColor(new float[]{
|
|
|
|
Math.abs(hash[0] * hash[1] + hash[2]) % 360,
|
|
|
|
0.8f,
|
|
|
|
1.0f
|
|
|
|
});
|
|
|
|
background = Color.HSVToColor(new float[]{
|
|
|
|
Math.abs(hash[1] * hash[2] + hash[0]) % 360,
|
|
|
|
0.8f,
|
|
|
|
1.0f
|
|
|
|
});
|
2015-08-14 17:25:05 +02:00
|
|
|
|
|
|
|
for (int row = 0; row < SIZE; row++) {
|
|
|
|
for (int column = 0; column < SIZE; column++) {
|
2016-10-10 21:57:46 +02:00
|
|
|
fields[row][column] = hash[(row * (column < CENTER_COLUMN ? column : SIZE -
|
|
|
|
column - 1)) % hash.length] >= 0;
|
2015-08-14 17:25:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-10-10 21:57:46 +02:00
|
|
|
public void draw(@NonNull Canvas canvas) {
|
2015-08-14 17:25:05 +02:00
|
|
|
float x, y;
|
2016-10-10 21:57:46 +02:00
|
|
|
float width = canvas.getWidth();
|
|
|
|
float height = canvas.getHeight();
|
|
|
|
float cellWidth = width / (float) SIZE;
|
|
|
|
float cellHeight = height / (float) SIZE;
|
2015-08-14 17:25:05 +02:00
|
|
|
paint.setColor(background);
|
2016-10-10 21:57:46 +02:00
|
|
|
canvas.drawCircle(width / 2, height / 2, width / 2, paint);
|
2015-08-14 17:25:05 +02:00
|
|
|
paint.setColor(color);
|
|
|
|
for (int row = 0; row < SIZE; row++) {
|
|
|
|
for (int column = 0; column < SIZE; column++) {
|
|
|
|
if (fields[row][column]) {
|
|
|
|
x = cellWidth * column;
|
|
|
|
y = cellHeight * row;
|
|
|
|
|
|
|
|
canvas.drawCircle(x + cellWidth / 2, y + cellHeight / 2, cellHeight / 2, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setAlpha(int alpha) {
|
|
|
|
paint.setAlpha(alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setColorFilter(ColorFilter cf) {
|
|
|
|
paint.setColorFilter(cf);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getOpacity() {
|
2015-09-04 08:19:07 +02:00
|
|
|
return PixelFormat.TRANSPARENT;
|
2015-08-14 17:25:05 +02:00
|
|
|
}
|
2016-10-10 21:57:46 +02:00
|
|
|
}
|