-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmakecolor.html
More file actions
65 lines (59 loc) · 1.75 KB
/
Copy pathmakecolor.html
File metadata and controls
65 lines (59 loc) · 1.75 KB
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
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width">
<style>
img {
width: 5vw;
}
</style>
<script type="module">
import { CSV } from "https://code4sabae.github.io/js/CSV.js";
const pickColorFromImage = (img) => {
const canvas = document.createElement("canvas");
canvas.width = 1;
canvas.height = 1;
const g = canvas.getContext("2d");
g.drawImage(img, 0, 0);
const c = g.getImageData(0, 0, 1, 1).data;
const hex2 = n => n < 16 ? "0" + n.toString(16) : "" + n.toString(16);
return "#" + hex2(c[0]) + hex2(c[1]) + hex2(c[2]);
};
const waitImageLoad = async (img) => {
return new Promise(resolve => img.onload = resolve);
};
window.onload = async () => {
const baseurl = "https://code4sabae.github.io/SDGs_ja/";
const goals = CSV.toJSON(await CSV.fetch(baseurl + "SDG-goals.csv"));
console.log(goals);
const targets = CSV.toJSON(await CSV.fetch(baseurl + "SDG-targets.csv"));
console.log(targets);
const logos = add(document.body, "div");
const colors = [];
for (const goal of goals) {
const img = new Image();
//img.src = baseurl + goal.logo_ja;
img.src = goal.logo_ja;
//img.src = baseurl + goal.logo;
logos.appendChild(img);
await waitImageLoad(img);
const color = pickColorFromImage(img);
colors.push(color);
}
console.log(colors.join("\n"))
};
const add = (parent, tag, txt, link) => {
const ele = document.createElement(tag);
if (txt) {
if (link) {
const a = add(ele, "a", txt);
a.href = link;
} else {
ele.textContent = txt;
}
}
parent.appendChild(ele);
return ele;
};
</script>
</head>
<body>
</body>
</html>