-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
69 lines (57 loc) · 2.17 KB
/
convert.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
/**
* identicon 2.0.0
* http://github.com/Rajveer2009/identicon/
*
* Libraries
* elrumordelaluz/svgson, mathiasbynens/utf8.js,
* mathiasbynens/base64, stewartlord/identicon.js
*
* Copyright (c) 2022 Rajveer Singh Saggu, https://rajveer2009.github.io/ <[email protected]>
* Released under the MIT license
* https://rajveer2009.github.io/LICENSE/
*/
function Genrate() {
// Checking if the image is there an if there removing it
element1 = document.getElementById("img");
if (typeof (element1) != 'undefined' && element1 != null) {
const element1 = document.getElementById("img");
element1.remove();
}
// Getting "hash" value
var hash = document.getElementById("seeed").value;
// Options for the Identicon
var options = {
background: [240, 240, 240, 255],
margin: 0.2,
size: 420,
format: 'svg'
};
// Genrate the Identicon
var data = new Identicon(hash, options).toString();
// Decode the base64 ecoded svg data of the Identicon to get the color value
var bytes = base64.decode(data);
var text = utf8.decode(bytes);
svgson.parse(String(text)).then((json) => {
style = json.children[0].attributes.style
var myArr = String(style).split("").map((style) => {
return String(style)
})
if (myArr[21] == ")") {
color = myArr[10] + myArr[11] + myArr[12] + myArr[13] + myArr[14] + myArr[15] + myArr[16] + myArr[17] + myArr[18] + myArr[19] + myArr[20]
}
else {
color = myArr[10] + myArr[11] + myArr[12] + myArr[13] + myArr[14] + myArr[15] + myArr[16] + myArr[17] + myArr[18] + myArr[19] + myArr[20] + myArr[21]
}
// Deleting the image element
const element2 = document.getElementById("del");
element2.remove();
// Logging the values of the "Seeed" and Color in the Console
console.log("seeed: " + hash);
console.log("color: rgba[" + color + "]");
});
// Writing the Identicon to an image
img = document.createElement('img');
img.id = "img"
img.src = String("data:image/svg+xml;base64," + data);
document.body.appendChild(img);
};