-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
398 lines (339 loc) · 12 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.root{
width: calc(100vw - 700px);
overflow-x: auto;
}
div{
line-height: 25px;
}
#canvas {
margin-left: 50px;
display: block;
}
#color-block1,#color-block2,#color-block3,#color-block4{
width: 20px;
height: 20px;
display: inline-block;
margin: 0 5px;
background: transparent;
border: 1px solid #ccc;
}
.flex{
display: flex;
align-items: center;
text-align: center;
line-height: 30px;
cursor: pointer;
color: #888;
transition: color 0.3s;
flex-wrap: wrap;
}
.flex:hover{
color: #000;
}
body{
display: flex;
justify-content: center;
}
#colors{
display: flex;
flex-wrap: wrap;
}
.color{
width: 160px;
height: 50px;
color: #ccc;
line-height: 120px;
margin: 20px 0;
}
</style>
</head>
<body>
<div class="root">
<iframe
style="margin-left: 2px; margin-bottom:-5px;"
frameborder="0" scrolling="0" width="100px" height="20px"
src="https://ghbtns.com/github-btn.html?user=o2team&repo=image-color-utils&type=star&count=true" >
</iframe>
<iframe
style="margin-left: 2px; margin-bottom:-5px;"
frameborder="0" scrolling="0" width="100px" height="20px"
src="https://ghbtns.com/github-btn.html?user=o2team&repo=image-color-utils&type=fork&count=true" >
</iframe>
<h3>图片操作选项</h3>
<label class="flex">
提取色值1:
<input type='radio' value='pickColor1' name='action' checked onchange="changeAction(this.value)" />
<div id="color-block1"></div>
</label>
<label class="flex">
色值1深浅判断:
<button id='deepLightBtn'>开始判断</button>
</label>
<label class="flex">
提取色值2:
<input type='radio' value='pickColor2' name='action' onchange="changeAction(this.value)" />
<div id="color-block2"></div>
</label>
<div class="flex">
色值1/色值2 相似度对比:
<div id="color-block3"></div>
<div id="color-block4"></div>
<button id='compareBtn'>开始对比</button>
</div>
<label class="flex">
边界自动吸附(找不到临界点则不移动):
<input type='radio' value='adjust' name='action' onchange="changeAction(this.value)" /> <button id="undo" style="margin-left: 20px;">撤销</button>
</label>
<div class="flex">
更换图片:
<input type="file" id="upload" accept="image/png,image/jpeg,image/jpg" />
</div>
<label class="flex">
色彩列表:
<div id="colors"></div>
</label>
<h3>参数配置</h3>
<div class="flex">
色彩边界阈值(阈值越高,相似条件越低):
<input type='range' id='boundary' name='boundaryValue' value="10" min="10" max="100" step="2"/>
<span id="boundaryText">10</span>
</div>
<div class="flex" style="display: none">
颜色提取阈值(阈值越高,相似条件越低):
<input type='range' id='boundary2' name='boundaryValue2' value="0" min="0" max="100" step="2"/>
<span id="boundaryText2">0</span>
</div>
<div class="flex">
边界扫描距离(由内向外):
<input type='range' id='mockMove' name='mockMovePx' value="30" min="10" max="200" step="10"/>
<span id="mockMoveText">30px</span>
</div>
</div>
<canvas id='canvas' width="375" height="812"> </canvas>
<script>
var action = 'pickColor1'
var blockColor1 = [255,255,255]
var blockColor2 = [255,255,255]
var pickColorsValue = []
const canvasDom = document.getElementById('canvas')
function changeAction(value){
action = value
canvasDom.style.cursor = action === 'adjust' ? 'crosshair' : `default`
}
</script>
<script type="module">
import { ImageColorUtils, rgb2hsv, isDeepColorByHsv } from './build/index.es.js'
var main = {
// 初始化
init(){
const imageUrl = 'https://storage.360buyimg.com/dataset-activity-mini/png-jpg-00002-2.jpg'
// const imageUrl = 'https://img20.360buyimg.com/babel/s1180x940_jfs/t1/147050/6/31926/85670/6357a9afE26ff5c66/3a6823f7820fb72b.jpg'
// const imageUrl='https://img10.360buyimg.com/img/jfs/t1/86699/27/29562/39551/62bec631E155c7e41/55d63c89279226f0.jpg'
const width = 500
this.isDrawing = false
this.rects = []
this.boundaryValue = 10
this.boundaryValue2 = 20
this.mockMovePx = 30
this.colors = document.getElementById('colors')
this.colorBlock1 = document.getElementById('color-block1')
this.colorBlock2 = document.getElementById('color-block2')
this.colorBlock3 = document.getElementById('color-block3')
this.colorBlock4 = document.getElementById('color-block4')
this.undo = document.getElementById('undo')
this.boundary = document.getElementById('boundary')
this.boundary2 = document.getElementById('boundary2')
this.boundaryText = document.getElementById('boundaryText')
this.boundaryText2 = document.getElementById('boundaryText2')
this.mockMove = document.getElementById('mockMove')
this.mockMoveText = document.getElementById('mockMoveText')
this.compareBtn = document.getElementById('compareBtn')
this.upload = document.getElementById('upload')
this.canvas = document.getElementById('canvas')
this.ctx = canvas.getContext('2d')
this.deepLightBtn = document.getElementById('deepLightBtn')
this.img = new Image()
this.img.crossOrigin = 'Anonymous'
this.img.onload = () => {
const height = width * this.img.height / this.img.width
// 计算出画布的宽高
this.initCanvas(width, height)
this.initEvent()
this.pickColors()
}
this.img.src = imageUrl
},
initCanvas(width,height) {
const lineColor = '#12f430'
this.canvas.width = width
this.canvas.height = height
this.canvas.style.width = width + 'px'
this.canvas.style.height = height + 'px'
this.ctx.lineWidth = 3
this.ctx.strokeStyle = lineColor
this.clear()
this.drawImage()
this.imageData = this.ctx.getImageData(0,0,this.canvas.width, this.canvas.height)
},
// 绘制
draw(x, y, width, height) {
this.clear()
this.drawImage()
this.drawRect(x, y, width, height)
this.rects.forEach(rect => {
this.drawRect(rect.x, rect.y, rect.width, rect.height)
})
},
// 清除画布
clear() {
this.ctx.clearRect(0,0,this.canvas.width, this.canvas.height)
},
// 绘制图片
drawImage() {
this.ctx.drawImage(this.img,0,0,this.canvas.width, this.canvas.height)
},
// 绘制标注框
drawRect(x, y, width, height){
this.ctx.strokeRect(x,y,width,height)
},
// 提取色值
pickColors(){
const colorUtils = new ImageColorUtils({
origin: this.img,
width:this.canvas.width,
height: this.canvas.height,
onload: ()=>{
this.colors.innerHTML=''
const res = colorUtils.pickColors()
for(const color of res.rgb){
const dom = document.createElement('div')
dom.className = 'color'
dom.style.background = color
dom.innerHTML = color
this.colors.appendChild(dom)
}
}
})
},
// 事件监听初始化
initEvent(){
let startX
let startY
let endX
let endY
const self = this
function adjust(leftTopPosition, rightBottomPosition) {
const imageColorUtils = new ImageColorUtils({ origin: self.img,width: Math.floor(self.canvas.width), height: Math.floor(self.canvas.height), boundaryValue: self.boundaryValue, mockMovePx: self.mockMovePx })
return imageColorUtils.adjust(leftTopPosition, rightBottomPosition)
}
function end() {
self.rects.push(adjust([startX, startY], [endX, endY] ))
startX = startY = endX = endY = null
self.isDrawing = false
self.draw()
}
this.undo.addEventListener('click',()=>{
self.rects.splice(self.rects.length - 1 )
self.draw()
})
this.boundary.addEventListener('change',(e)=>{
const value = parseInt(e.target.value)
this.boundaryValue = value
this.boundaryText.innerHTML = value
this.pickColors()
})
this.boundary2.addEventListener('change',(e)=>{
const value = parseInt(e.target.value)
this.boundaryValue2 = value
this.boundaryText2.innerHTML = value
this.pickColors()
})
this.mockMove.addEventListener('change',(e)=>{
const value = parseInt(e.target.value)
this.mockMovePx = value
this.mockMoveText.innerHTML = value + 'px'
})
this.upload.addEventListener('change',(e)=>{
const reader = new FileReader();
reader.addEventListener('load', () => {
this.img.src = reader.result;
this.rects = []
// this.initCanvas()
}, false);
reader.readAsDataURL(e.target.files[0]);
})
this.compareBtn.addEventListener('click', () => {
const result = ImageColorUtils.compare(blockColor1, blockColor2, this.boundaryValue, 'lab')
this.compareBtn.innerHTML = `(结果:${result ? '相似': '不相似'})点击继续对比`
})
this.deepLightBtn.addEventListener('click', () => {
const rgb = blockColor1.slice(0,3)
const result = rgb2hsv(rgb)
const isDeep = isDeepColorByHsv(result)
console.log(isDeep)
// const result = ImageColorUtils.compare(blockColor1, blockColor2, this.boundaryValue, 'lab')
this.deepLightBtn.innerHTML = `(结果:${isDeep ? '深色': '浅色'})点击继续判断`
})
this.canvas.addEventListener('click', (e) => {
if(!action.match('pickColor') && action !== 'compare' ){
return
}
const x = e.offsetX
const y = e.offsetY
const pickColor = new ImageColorUtils({
origin: this.img,
width:this.canvas.width,
height: this.canvas.height,
}).pickColor(x, y)
const color = `rgba(${pickColor.join(',')})`
if(action === 'pickColor1'){
this.colorBlock1.style.backgroundColor = color
this.colorBlock3.style.backgroundColor = color
blockColor1 = pickColor
} else if(action === 'pickColor2'){
this.colorBlock2.style.backgroundColor = color
this.colorBlock4.style.backgroundColor = color
blockColor2 = pickColor
}
})
this.canvas.addEventListener('mousedown', (e) => {
if(action !== 'adjust'){
return
}
this.isDrawing = true
startX = e.offsetX
startY = e.offsetY
this.canvas.addEventListener('mousemove', (e) => {
if(!this.isDrawing){
return
}
endX = e.offsetX
endY= e.offsetY
this.draw(startX, startY, endX - startX, endY - startY )
})
this.canvas.addEventListener('mouseup', (e) => {
if(!this.isDrawing){
return
}
end()
})
this.canvas.addEventListener('mouseout', () => {
if(!this.isDrawing){
return
}
end()
})
})
}
}
main.init()
</script>
</body>
</html>