5
5
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
6
< title > Document</ title >
7
7
< style >
8
+ .root {
9
+ width : calc (100vw - 700px );
10
+ overflow-x : auto;
11
+ }
8
12
div {
9
13
line-height : 25px ;
10
14
}
37
41
display : flex;
38
42
justify-content : center;
39
43
}
44
+ # colors {
45
+ display : flex;
46
+ flex-wrap : wrap;
47
+ }
48
+ .color {
49
+ width : 50px ;
50
+ height : 50px ;
51
+ }
40
52
</ style >
41
53
</ head >
42
54
< body >
@@ -63,43 +75,51 @@ <h3>图片操作选项</h3>
63
75
< input type ='radio ' value ='pickColor2 ' name ='action ' onchange ="changeAction(this.value) " />
64
76
< div id ="color-block2 "> </ div >
65
77
</ label >
66
- < label class ="flex ">
67
- 边界自动吸附(找不到临界点则不移动):
68
- < input type ='radio ' value ='adjust ' name ='action ' onchange ="changeAction(this.value) " /> < button id ="undo " style ="margin-left: 20px; "> 撤销</ button >
69
- </ label >
70
-
71
78
< div class ="flex ">
72
79
色值1/色值2 相似度对比:
73
80
< div id ="color-block3 "> </ div >
74
81
< div id ="color-block4 "> </ div >
75
82
< button id ='compareBtn '> 开始对比</ button >
76
83
</ div >
84
+ < label class ="flex ">
85
+ 边界自动吸附(找不到临界点则不移动):
86
+ < input type ='radio ' value ='adjust ' name ='action ' onchange ="changeAction(this.value) " /> < button id ="undo " style ="margin-left: 20px; "> 撤销</ button >
87
+ </ label >
77
88
78
89
< div class ="flex ">
79
90
更换图片:
80
91
< input type ="file " id ="upload " accept ="image/png,image/jpeg,image/jpg " />
81
92
</ div >
82
93
94
+ < label class ="flex ">
95
+ 色彩列表:
96
+ < div id ="colors "> </ div >
97
+ </ label >
98
+
83
99
< h3 > 参数配置</ h3 >
84
100
< div class ="flex ">
85
101
色彩边界阈值(阈值越高,相似条件越低):
86
- < input type ='range ' id ='boundary ' name ='boundaryValue ' value ="2 " min ="2 " max ="20 " step ="2 "/>
87
- < span id ="boundaryText "> 2</ span >
102
+ < input type ='range ' id ='boundary ' name ='boundaryValue ' value ="10 " min ="10 " max ="100 " step ="2 "/>
103
+ < span id ="boundaryText "> 10</ span >
104
+ </ div >
105
+ < div class ="flex ">
106
+ 颜色提取阈值(阈值越高,相似条件越低):
107
+ < input type ='range ' id ='boundary2 ' name ='boundaryValue2 ' value ="0 " min ="0 " max ="100 " step ="2 "/>
108
+ < span id ="boundaryText2 "> 0</ span >
88
109
</ div >
89
110
< div class ="flex ">
90
111
边界扫描距离(由内向外):
91
112
< input type ='range ' id ='mockMove ' name ='mockMovePx ' value ="30 " min ="10 " max ="200 " step ="10 "/>
92
- < span id ="mockMoveText "> 30 </ span >
113
+ < span id ="mockMoveText "> 30px </ span >
93
114
</ div >
94
115
95
116
</ div >
96
117
< canvas id ='canvas ' width ="375 " height ="812 "> </ canvas >
97
- <!-- <script type="module" src="./build/bundle.js">
98
- </script> -->
99
118
< script >
100
119
var action = 'pickColor1'
101
120
var blockColor1 = [ 255 , 255 , 255 ]
102
121
var blockColor2 = [ 255 , 255 , 255 ]
122
+ var pickColorsValue = [ ]
103
123
104
124
105
125
const canvasDom = document . getElementById ( 'canvas' )
@@ -117,21 +137,28 @@ <h3>参数配置</h3>
117
137
var main = {
118
138
// 初始化
119
139
init ( ) {
120
- const imageUrl = 'https://storage.360buyimg.com/dataset-activity-mini/png-jpg-00002-2.jpg'
140
+ // const imageUrl = 'https://storage.360buyimg.com/dataset-activity-mini/png-jpg-00002-2.jpg'
141
+ // const imageUrl = 'https://img20.360buyimg.com/babel/s1180x940_jfs/t1/147050/6/31926/85670/6357a9afE26ff5c66/3a6823f7820fb72b.jpg'
142
+ const imageUrl = 'https://img10.360buyimg.com/img/jfs/t1/86699/27/29562/39551/62bec631E155c7e41/55d63c89279226f0.jpg'
121
143
const width = 500
122
144
this . isDrawing = false
123
145
this . rects = [ ]
124
- this . boundaryValue = 2
146
+ this . boundaryValue = 10
147
+ this . boundaryValue2 = 20
125
148
this . mockMovePx = 30
126
149
150
+ this . colors = document . getElementById ( 'colors' )
127
151
this . colorBlock1 = document . getElementById ( 'color-block1' )
128
152
this . colorBlock2 = document . getElementById ( 'color-block2' )
129
153
this . colorBlock3 = document . getElementById ( 'color-block3' )
130
154
this . colorBlock4 = document . getElementById ( 'color-block4' )
131
155
this . undo = document . getElementById ( 'undo' )
132
156
133
157
this . boundary = document . getElementById ( 'boundary' )
158
+ this . boundary2 = document . getElementById ( 'boundary2' )
159
+
134
160
this . boundaryText = document . getElementById ( 'boundaryText' )
161
+ this . boundaryText2 = document . getElementById ( 'boundaryText2' )
135
162
136
163
this . mockMove = document . getElementById ( 'mockMove' )
137
164
this . mockMoveText = document . getElementById ( 'mockMoveText' )
@@ -151,6 +178,7 @@ <h3>参数配置</h3>
151
178
152
179
this . initCanvas ( width , height )
153
180
this . initEvent ( )
181
+ this . pickColors ( )
154
182
}
155
183
this . img . src = imageUrl
156
184
@@ -195,6 +223,27 @@ <h3>参数配置</h3>
195
223
this . ctx . strokeRect ( x , y , width , height )
196
224
} ,
197
225
226
+ // 提取色值
227
+ pickColors ( ) {
228
+ const colorUtils = new ImageColorUtils ( {
229
+ origin : this . img ,
230
+ width :this . canvas . width ,
231
+ height : this . canvas . height ,
232
+ boundaryValue : this . boundaryValue2 ,
233
+ onload : ( ) => {
234
+ this . colors . innerHTML = ''
235
+ const res = colorUtils . pickColors ( )
236
+ console . log ( ImageColorUtils . compare ( [ 254 , 255 , 255 ] , [ 255 , 255 , 255 ] , this . boundaryValue2 , 'hsv' ) , 'hsv' )
237
+ for ( const color of res ) {
238
+ const dom = document . createElement ( 'div' )
239
+ dom . className = 'color'
240
+ dom . style . background = `rgb(${ color } )`
241
+ this . colors . appendChild ( dom )
242
+ }
243
+ }
244
+ } )
245
+ } ,
246
+
198
247
// 事件监听初始化
199
248
initEvent ( ) {
200
249
let startX
@@ -205,7 +254,6 @@ <h3>参数配置</h3>
205
254
206
255
function adjust ( leftTopPosition , rightBottomPosition ) {
207
256
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 } )
208
- console . log ( imageColorUtils )
209
257
return imageColorUtils . adjust ( leftTopPosition , rightBottomPosition )
210
258
}
211
259
@@ -222,16 +270,26 @@ <h3>参数配置</h3>
222
270
self . draw ( )
223
271
} )
224
272
273
+
274
+
225
275
this . boundary . addEventListener ( 'change' , ( e ) => {
226
276
const value = parseInt ( e . target . value )
227
277
this . boundaryValue = value
228
278
this . boundaryText . innerHTML = value
279
+ this . pickColors ( )
280
+ } )
281
+
282
+ this . boundary2 . addEventListener ( 'change' , ( e ) => {
283
+ const value = parseInt ( e . target . value )
284
+ this . boundaryValue2 = value
285
+ this . boundaryText2 . innerHTML = value
286
+ this . pickColors ( )
229
287
} )
230
288
231
289
this . mockMove . addEventListener ( 'change' , ( e ) => {
232
290
const value = parseInt ( e . target . value )
233
291
this . mockMovePx = value
234
- this . mockMoveText . innerHTML = value
292
+ this . mockMoveText . innerHTML = value + 'px'
235
293
} )
236
294
237
295
this . upload . addEventListener ( 'change' , ( e ) => {
@@ -245,7 +303,7 @@ <h3>参数配置</h3>
245
303
} )
246
304
247
305
this . compareBtn . addEventListener ( 'click' , ( ) => {
248
- const result = ImageColorUtils . compare ( blockColor1 , blockColor2 , this . boundaryValue )
306
+ const result = ImageColorUtils . compare ( blockColor1 , blockColor2 , this . boundaryValue , 'hsv' )
249
307
this . compareBtn . innerHTML = `(结果:${ result ? '相似' : '不相似' } )点击继续对比`
250
308
} )
251
309
@@ -259,25 +317,20 @@ <h3>参数配置</h3>
259
317
const pickColor = new ImageColorUtils ( {
260
318
origin : this . img ,
261
319
width :this . canvas . width ,
262
- height : this . canvas . height
320
+ height : this . canvas . height ,
263
321
} ) . pickColor ( x , y )
264
322
265
- console . log ( 'pickColor' , pickColor )
266
323
const color = `rgb(${ pickColor [ 0 ] } ,${ pickColor [ 1 ] } ,${ pickColor [ 2 ] } )`
267
324
268
325
if ( action === 'pickColor1' ) {
269
326
this . colorBlock1 . style . backgroundColor = color
270
327
this . colorBlock3 . style . backgroundColor = color
271
-
272
328
blockColor1 = pickColor
273
329
} else if ( action === 'pickColor2' ) {
274
330
this . colorBlock2 . style . backgroundColor = color
275
331
this . colorBlock4 . style . backgroundColor = color
276
-
277
332
blockColor2 = pickColor
278
333
}
279
-
280
-
281
334
} )
282
335
283
336
this . canvas . addEventListener ( 'mousedown' , ( e ) => {
@@ -319,6 +372,7 @@ <h3>参数配置</h3>
319
372
320
373
321
374
main . init ( )
375
+
322
376
323
377
324
378
0 commit comments