-
Notifications
You must be signed in to change notification settings - Fork 1
/
NetBlockies-Core.cs
182 lines (160 loc) · 6.16 KB
/
NetBlockies-Core.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.DrawingCore;
using System.Linq;
namespace NetBlockies
{
public class Blockies : IDisposable
{
#region Disposable implementation
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
//Free other managed objects that implement IDisposable only
}
_randSeed = null;
_iconPixels = null;
_disposed = true;
}
#endregion
private bool _disposed;
private Int32[] _randSeed = new Int32[4];
private Color[] _iconPixels;
private int _size;
/// <summary>
/// Creates identicons for ethereum addresses. The standard size of identicon is 8.
/// </summary>
/// <param name="Seed">Ethereum address</param>
/// <param name="Size">Size of the identicon</param>
public Blockies(string Seed, int size = 8)
{
_size = size;
CreateImageData(Seed);
}
/// <summary>
/// Returns identicon bitmap with desired resolution (nearest smaller multiple of BlockiesHelper.Size)
/// </summary>
/// <param name="resolution">Use multiples of BlockiesHelper.Size</param>
/// <returns></returns>
public Bitmap GetBitmap(int resolution) => Create(resolution / _size);
/// <summary>
/// Returns a base64 encoded identicon jpeg with desired resolution (nearest smaller multiple of BlockiesHelper.Size)
/// </summary>
/// <param name="resolution">Use multiples of BlockiesHelper.Size</param>
/// <returns></returns>
public string GetBase64Image(int resolution)
{
using (var ms = new System.IO.MemoryStream())
{
var image = GetBitmap(resolution);
image.Save(ms, System.DrawingCore.Imaging.ImageFormat.Jpeg);
return $"data:image/png;base64, {Convert.ToBase64String(ms.ToArray())}";
}
}
private void Seedrand(string seed)
{
char[] seedArray = seed.ToCharArray();
for (int i = 0; i < _randSeed.Length; i++)
_randSeed[i] = 0;
for (int i = 0; i < seed.Length; i++)
_randSeed[i % 4] = ((_randSeed[i % 4] << 5) - _randSeed[i % 4]) + seedArray[i];
}
private double Rand()
{
var t = _randSeed[0] ^ (_randSeed[0] << 11);
_randSeed[0] = _randSeed[1];
_randSeed[1] = _randSeed[2];
_randSeed[2] = _randSeed[3];
_randSeed[3] = (_randSeed[3] ^ (_randSeed[3] >> 19) ^ t ^ (t >> 8));
return Convert.ToDouble(_randSeed[3]) / Convert.ToDouble((UInt32)1 << 31);
}
private double HueTorgb(double p, double q, double t)
{
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1D / 6) return p + (q - p) * 6 * t;
if (t < 1D / 2) return q;
if (t < 2D / 3) return p + (q - p) * (2D / 3 - t) * 6;
return p;
}
private Color HslToRgb(double h, double s, double l)
{
double r, g, b;
if (s == 0)
r = g = b = l;
else
{
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = HueTorgb(p, q, h + 1D / 3);
g = HueTorgb(p, q, h);
b = HueTorgb(p, q, h - 1D / 3);
}
return Color.FromArgb((int)Math.Round(r * 255), (int)Math.Round(g * 255), (int)Math.Round(b * 255));
}
private Color CreateColor()
{
var h = (Rand());
var s = ((Rand() * 0.6) + 0.4);
var l = ((Rand() + Rand() + Rand() + Rand()) * 0.25);
return HslToRgb(h, s, l);
}
private void CreateImageData(string seed)
{
Seedrand(seed.ToLower());
var mainColor = CreateColor();
var bgColor = CreateColor();
var spotColor = CreateColor();
int width = _size;
int height = _size;
int mirrorWidth = width / 2;
int dataWidth = width - mirrorWidth;
double[] data = new double[width * height];
for (int i = 0; i < height; i++)
{
double[] row = new double[dataWidth];
for (int x = 0; x < dataWidth; x++)
row[x] = Math.Floor(Rand() * 2.3);
Array.Copy(row, 0, data, i * width, dataWidth);
Array.Copy(row.Reverse().ToArray(), 0, data, i * width + dataWidth, mirrorWidth);
}
_iconPixels = new Color[data.Length];
for (int i = 0; i < data.Length; i++)
{
if (data[i] == 1)
_iconPixels[i] = mainColor;
else if (data[i] == 0)
_iconPixels[i] = bgColor;
else
_iconPixels[i] = spotColor;
}
}
private Bitmap Create(int scale)
{
Bitmap pic = new Bitmap(_size * scale, _size * scale, System.DrawingCore.Imaging.PixelFormat.Format32bppArgb);
for (int i = 0; i < _iconPixels.Length; i++)
{
int x = i % _size;
int y = i / _size;
for (int xx = x * scale; xx < x * scale + scale; xx++)
for (int yy = y * scale; yy < y * scale + scale; yy++)
pic.SetPixel(xx, yy, _iconPixels[i]);
}
return pic;
}
}
}