-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.html
274 lines (258 loc) · 11.4 KB
/
demo.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>miniprogram-pallete</title>
<style>
.container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
box-sizing: border-box;
height: 100%;
background: linear-gradient(0deg, #b7eb8f, 20%, #b5f5ec);
}
.feature-info {
height: 20px;
line-height: 20px;
padding: 14px;
font-size: 14px;
color: #fff;
width: 100%;
text-align: center;
}
.operator-container {
display: flex;
flex-direction: row;
justify-content: center;
}
.input-file-container {
position: relative;
width: 80px;
height: 24px;
line-height: 24px;
border-radius: 4px;
background-color: #95de64;
color: #fff;
font-size: 14px;
box-shadow: 1px 1px 5px #888;
text-align: center;
}
.input-file-button {
position: absolute;
left: 0px;
width: 80px;
opacity: 0;
}
.item_file img {
width: 100%;
height: 100%;
object-fit: cover;
}
.item_colors {
color: #fff;
font-size: 14px;
line-height: 20px;
margin-top: 26px;
margin-left: 26px;
}
.item_file {
padding: 20px;
width: 200px;
margin-left: 26px;
}
.button {
width: 80px;
height: 24px;
line-height: 24px;
border-radius: 4px;
background-color: #95de64;
color: #fff;
font-size: 14px;
box-shadow: 1px 1px 5px #888;
text-align: center;
margin-top: 20px;
}
#image-container {
position: relative;
top: -24px;
left: 120px;
width: 200px;
height: 200px;
border: 4px dashed #FFF;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div class="container">
<div class="feature-info">支持图片主色调获取、暖色调、美颜等</div>
<div class="operator-container">
<div>
<div class="input-file-container">
<span>选择图片</span>
<input type="file" id="file" class="input-file-button" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" />
</div>
<div id="image-container">
<svg viewBox="64 64 896 896" focusable="false" data-icon="picture" width="3em" height="3em" fill="#95de64" aria-hidden="true"><path d="M928 160H96c-17.7 0-32 14.3-32 32v640c0 17.7 14.3 32 32 32h832c17.7 0 32-14.3 32-32V192c0-17.7-14.3-32-32-32zm-40 632H136v-39.9l138.5-164.3 150.1 178L658.1 489 888 761.6V792zm0-129.8L664.2 396.8c-3.2-3.8-9-3.8-12.2 0L424.6 666.4l-144-170.7c-3.2-3.8-9-3.8-12.2 0L136 652.7V232h752v430.2z" fill="#95de64"></path><path d="M424.6 765.8l-150.1-178L136 752.1V792h752v-30.4L658.1 489z" fill="#e6f7ff"></path><path d="M136 652.7l132.4-157c3.2-3.8 9-3.8 12.2 0l144 170.7L652 396.8c3.2-3.8 9-3.8 12.2 0L888 662.2V232H136v420.7zM304 280a88 88 0 110 176 88 88 0 010-176z" fill="#e6f7ff"></path><path d="M276 368a28 28 0 1056 0 28 28 0 10-56 0z" fill="#e6f7ff"></path><path d="M304 456a88 88 0 100-176 88 88 0 000 176zm0-116c15.5 0 28 12.5 28 28s-12.5 28-28 28-28-12.5-28-28 12.5-28 28-28z" fill="#95de64"></path></svg>
</div>
<div id="button-list" style="display: none;">
<div class="button" id="grey-image">灰度化图片</div>
<div class="button" id="warm-image">暖色调</div>
<div class="button" id="gaussian-blur-image">高斯模糊</div>
</div>
</div>
<div class="imgfile">
<div class="item item_file" id="demo"></div>
<div class="item_colors" id="colors"></div>
</div>
</div>
<div class="demos"></div>
</div>
</body>
<script src="./image-main-color.js"></script>
<script type="module">
import { traverse, gaussianBlur } from './lib/utils.js';
import { transformColor, brightness, saturate, channel, grayscale, contrast } from './lib/color-matrix.js';
let imageDataDefaultObj = [];
document.getElementById("file").addEventListener("change", (e) => {
const { files } = e.target;
let file = files ? files[0] : null;
if (!file) return;
getLocationUrl(file, (src) => {
const image = document.getElementById("image");
if (!image) {
document.getElementById("demo").innerHTML = `<img src="${src}" id="image"/>`;
} else {
image.src = src;
}
initImage(src, (imageData) => {
imageDataDefaultObj = imageData;
const res = getMainColor(imageData.data);
if (imageData.data && imageData.data.length) {
document.getElementById("button-list").style.display = "block";
document.getElementById("image-container").style.display = "none";
}
document.getElementById('demo').style.background = res.hex;
document.getElementById('colors').innerHTML = '<div>图片主色调为:</div><div>HEX:' + res.hex + '</div>' +
'<div>HEXA:' + res.hexa + '</div>' +
'<div>RGB:' + res.rgb + '</div>' +
'<div>RGBA:' + res.rgba + '</div>';
});
})
})
document.getElementById("grey-image").addEventListener("click", () => {
const buttonForGaussian = document.getElementById("gaussian-blur-image");
const buttonForWarm = document.getElementById("warm-image");
const buttonForGrey = document.getElementById("grey-image");
buttonForGaussian.style.backgroundColor = "#95de64";
buttonForWarm.style.backgroundColor = "#95de64";
buttonForGrey.style.backgroundColor = "#888888";
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
// const height = document.getElementById("image").clientHeight; // 图片宽
// const width = document.getElementById("image").clientWidth; // 图片高
const x = document.getElementById("image").clientLeft; // 居左坐标
const y = document.getElementById("image").clientTop; // 居右坐标
const tempImageDataObj = context.createImageData(imageDataDefaultObj.width, imageDataDefaultObj.height);
for (let i = 0; i < imageDataDefaultObj.data.length; i++) {
tempImageDataObj.data[i] = imageDataDefaultObj.data[i];
tempImageDataObj.data[i + 1] = imageDataDefaultObj.data[i + 1];
tempImageDataObj.data[i + 2] = imageDataDefaultObj.data[i + 2];
tempImageDataObj.data[i + 3] = imageDataDefaultObj.data[i + 3];
}
greyTheImage(tempImageDataObj)
canvas.width = imageDataDefaultObj.width;
canvas.height = imageDataDefaultObj.height;
// imageDataDefaultObj.data = greyImageData;
context.putImageData(tempImageDataObj, x, y);
var data = canvas.toDataURL("image/png", 1);
var img = document.getElementById("image");
img.src = data;
})
document.getElementById("warm-image").addEventListener("click", () => {
const buttonForGaussian = document.getElementById("gaussian-blur-image");
const buttonForWarm = document.getElementById("warm-image");
const buttonForGrey = document.getElementById("grey-image");
buttonForGaussian.style.backgroundColor = "#95de64";
buttonForWarm.style.backgroundColor = "#888888";
buttonForGrey.style.backgroundColor = "#95de64";
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
// const height = document.getElementById("image").clientHeight; // 图片宽
// const width = document.getElementById("image").clientWidth; // 图片高
const x = document.getElementById("image").clientLeft; // 居左坐标
const y = document.getElementById("image").clientTop; // 居右坐标
// const tempImageData = new ImageData(imageDataDefaultObj);
const tempImageDataObj = context.createImageData(imageDataDefaultObj.width, imageDataDefaultObj.height);
for (let i = 0; i < imageDataDefaultObj.data.length; i++) {
tempImageDataObj.data[i] = imageDataDefaultObj.data[i];
tempImageDataObj.data[i + 1] = imageDataDefaultObj.data[i + 1];
tempImageDataObj.data[i + 2] = imageDataDefaultObj.data[i + 2];
tempImageDataObj.data[i + 3] = imageDataDefaultObj.data[i + 3];
}
traverse(tempImageDataObj, ({ r, g, b, a }) => {
return transformColor(
[r, g, b, a],
channel({ r: 1.2 }),
brightness(1.2),
saturate(1.2),
);
});
canvas.width = imageDataDefaultObj.width;
canvas.height = imageDataDefaultObj.height;
// imageDataDefaultObj.data = tempImageData;
context.putImageData(tempImageDataObj, x, y);
var data = canvas.toDataURL("image/png", 1);
var img = document.getElementById("image");
img.src = data;
})
document.getElementById("gaussian-blur-image").addEventListener("click", () => {
const buttonForGaussian = document.getElementById("gaussian-blur-image");
const buttonForWarm = document.getElementById("warm-image");
const buttonForGrey = document.getElementById("grey-image");
buttonForGaussian.style.backgroundColor = "#888888";
buttonForWarm.style.backgroundColor = "#95de64";
buttonForGrey.style.backgroundColor = "#95de64";
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
// const height = document.getElementById("image").clientHeight; // 图片宽
// const width = document.getElementById("image").clientWidth; // 图片高
const x = document.getElementById("image").clientLeft; // 居左坐标
const y = document.getElementById("image").clientTop; // 居右坐标
// const tempImageData = new ImageData(imageDataDefaultObj);
const tempImageDataObj = context.createImageData(imageDataDefaultObj.width, imageDataDefaultObj.height);
for (let i = 0; i < imageDataDefaultObj.data.length; i++) {
tempImageDataObj.data[i] = imageDataDefaultObj.data[i];
tempImageDataObj.data[i + 1] = imageDataDefaultObj.data[i + 1];
tempImageDataObj.data[i + 2] = imageDataDefaultObj.data[i + 2];
tempImageDataObj.data[i + 3] = imageDataDefaultObj.data[i + 3];
}
gaussianBlur(tempImageDataObj.data, tempImageDataObj.width, tempImageDataObj.height, 4);
traverse(tempImageDataObj, ({r, g, b, a}) => {
return transformColor([r, g, b, a], grayscale(0.5), saturate(1.2), contrast(1.1), brightness(1.2));
});
canvas.width = imageDataDefaultObj.width;
canvas.height = imageDataDefaultObj.height;
// imageDataDefaultObj.data = tempImageData;
context.putImageData(tempImageDataObj, x, y);
var data = canvas.toDataURL("image/png", 1);
var img = document.getElementById("image");
img.src = data;
})
function getLocationUrl(file, callback) {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (e) => {
const { result } = e.target;
callback(result);
}
}
</script>
</html>