forked from MetaMask/jazzicon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (134 loc) · 4.03 KB
/
index.js
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
var MersenneTwister = require('mersenne-twister');
var paperGen = require('./paper')
var colors = require('./colors')
var shapeCount = 4
var svgns = 'http://www.w3.org/2000/svg'
module.exports = generateIdenticon
var generator
function generateIdenticon(diameter, seed) {
generator = new MersenneTwister(seed);
var remainingColors = hueShift(colors.slice(), generator)
var elements = paperGen(diameter, genColor(remainingColors))
var container = elements.container
var svg = document.createElementNS(svgns, 'svg')
svg.setAttributeNS(null, 'x', '0')
svg.setAttributeNS(null, 'y', '0')
svg.setAttributeNS(null, 'width', diameter)
svg.setAttributeNS(null, 'height', diameter)
container.appendChild(svg)
for(var i = 0; i < shapeCount - 1; i++) {
genShape(remainingColors, diameter, i, shapeCount - 1, svg)
}
return container
}
function genShape(remainingColors, diameter, i, total, svg) {
var center = diameter / 2
var shape = document.createElementNS(svgns, 'rect')
shape.setAttributeNS(null, 'x', '0')
shape.setAttributeNS(null, 'y', '0')
shape.setAttributeNS(null, 'width', diameter)
shape.setAttributeNS(null, 'height', diameter)
var firstRot = generator.random()
var angle = Math.PI * 2 * firstRot
var velocity = diameter / total * generator.random() + (i * diameter / total)
var tx = (Math.cos(angle) * velocity)
var ty = (Math.sin(angle) * velocity)
var translate = 'translate(' + tx + ' ' + ty + ')'
// Third random is a shape rotation on top of all of that.
var secondRot = generator.random()
var rot = (firstRot * 360) + secondRot * 180
var rotate = 'rotate(' + rot.toFixed(1) + ' ' + center + ' ' + center + ')'
var transform = translate + ' ' + rotate
shape.setAttributeNS(null, 'transform', transform)
var fill = genColor(remainingColors)
shape.setAttributeNS(null, 'fill', fill)
svg.appendChild(shape)
}
function genColor(colors) {
var rand = generator.random()
var idx = Math.floor(colors.length * generator.random())
var color = colors.splice(idx,1)[0]
return color
}
var wobble = 30
function hueShift(colors, generator) {
var amount = (generator.random() * 30) - (wobble / 2)
var rotate = (hex) => colorRotate(hex, amount)
return colors.map(rotate)
}
function colorRotate(hex, degrees) {
var hsl = hexToHSL(hex)
var hue = hsl.h
hue = (hue + degrees) % 360;
hue = hue < 0 ? 360 + hue : hue;
hsl.h = hue;
return HSLToHex(hsl);
}
function hexToHSL(hex) {
// Convert hex to RGB first
var r = "0x" + hex[1] + hex[2];
var g = "0x" + hex[3] + hex[4];
var b = "0x" + hex[5] + hex[6];
// Then to HSL
r /= 255;
g /= 255;
b /= 255;
var cmin = Math.min(r,g,b),
cmax = Math.max(r,g,b),
delta = cmax - cmin,
h = 0,
s = 0,
l = 0;
if (delta == 0)
h = 0;
else if (cmax == r)
h = ((g - b) / delta) % 6;
else if (cmax == g)
h = (b - r) / delta + 2;
else
h = (r - g) / delta + 4;
h = Math.round(h * 60);
if (h < 0)
h += 360;
l = (cmax + cmin) / 2;
s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
s = +(s * 100).toFixed(1);
l = +(l * 100).toFixed(1);
return {h, s, l}
}
function HSLToHex(hsl) {
var {h, s, l} = hsl
s /= 100;
l /= 100;
let c = (1 - Math.abs(2 * l - 1)) * s,
x = c * (1 - Math.abs((h / 60) % 2 - 1)),
m = l - c/2,
r = 0,
g = 0,
b = 0;
if (0 <= h && h < 60) {
r = c; g = x; b = 0;
} else if (60 <= h && h < 120) {
r = x; g = c; b = 0;
} else if (120 <= h && h < 180) {
r = 0; g = c; b = x;
} else if (180 <= h && h < 240) {
r = 0; g = x; b = c;
} else if (240 <= h && h < 300) {
r = x; g = 0; b = c;
} else if (300 <= h && h < 360) {
r = c; g = 0; b = x;
}
// Having obtained RGB, convert channels to hex
r = Math.round((r + m) * 255).toString(16);
g = Math.round((g + m) * 255).toString(16);
b = Math.round((b + m) * 255).toString(16);
// Prepend 0s, if necessary
if (r.length == 1)
r = "0" + r;
if (g.length == 1)
g = "0" + g;
if (b.length == 1)
b = "0" + b;
return "#" + r + g + b;
}