-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathindex.html
80 lines (67 loc) · 1.58 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Image Main Color</title>
<style>
* {
box-sizing: border-box;
}
body {
color: #fff;
background: rgb(19, 18, 51);
}
.container {
width: 80%;
margin: 50px auto;
}
.file {
margin: 20px 0;
}
.color-box {
margin-bottom: 20px;
}
.colorful {
width: 100px;
height: 100px;
}
.img-box {
max-width: 500px;
}
.img-box img {
max-width: 100%;
}
</style>
</head>
<body>
<div class="container">
<input class="file" type="file" id="fileInput"/>
<div class="color-box">
图片主色调:<span id="main-color"></span>
<div class="colorful"></div>
</div>
<div class="img-box">
<img src="" id="img">
</div>
</div>
<script src="./index.js"></script>
<script>
(function(){
document.querySelector('#fileInput').addEventListener('change', function(e) {
const reader = new FileReader();
reader.onload = function() {
const img = new Image();
img.onload = function() {
const imgColor = getAverageRGB(img, true, 200);
document.querySelector('#main-color').innerHTML = imgColor;
document.querySelector('.colorful').setAttribute('style', `background-color: ${imgColor}`)
}
img.src = this.result;
document.querySelector('#img').setAttribute('src', this.result)
}
reader.readAsDataURL(e.target.files[0]);
})
})()
</script>
</body>
</html>