Skip to content

Commit 597c6ea

Browse files
author
dengwen
committed
feat: 发布1.2.3
1 parent cfa23d7 commit 597c6ea

File tree

5 files changed

+32
-25
lines changed

5 files changed

+32
-25
lines changed

build/index.es.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function rgb2lab(rgb) {
9898

9999
class ImageColorUtils {
100100
constructor(params) {
101-
const { origin, mockMovePx = 30, boundaryValue = 10, ParticleSize = 4, width, height, onload, } = params || {};
101+
const { origin, mockMovePx = 30, boundaryValue = 10, ParticleSize = 8, width, height, onload, } = params || {};
102102
if (!origin) {
103103
throw new Error('Origin is necessary');
104104
}
@@ -402,6 +402,7 @@ class ImageColorUtils {
402402
const similarColorsMap = {};
403403
const res = [];
404404
const boundaryValue = 25;
405+
const type = 'lab';
405406
let lastColor;
406407
for (let x = 1; x < this.canvas.width; x += ImageColorUtils.ParticleSize) {
407408
for (let y = 1; y < this.canvas.height; y += ImageColorUtils.ParticleSize) {
@@ -416,11 +417,11 @@ class ImageColorUtils {
416417
}
417418
else if (similarValues.length &&
418419
lastColor &&
419-
ImageColorUtils.compare(rgba, lastColor, ImageColorUtils.boundaryValue)) {
420+
ImageColorUtils.compare(rgba, lastColor, ImageColorUtils.boundaryValue, type)) {
420421
let insert = false;
421422
for (const similarValue of similarValues) {
422-
if (ImageColorUtils.compare(rgba, similarValue[0], boundaryValue, 'lab') ||
423-
ImageColorUtils.compare(rgba, similarValue[similarValue.length - 1], boundaryValue, 'lab')) {
423+
if (ImageColorUtils.compare(rgba, similarValue[similarValue.length - 1], boundaryValue, type) &&
424+
ImageColorUtils.compare(rgba, similarValue[Math.floor(similarValue.length / 2)], boundaryValue, type)) {
424425
similarValue.push(rgba);
425426
insert = true;
426427
}
@@ -434,9 +435,10 @@ class ImageColorUtils {
434435
const values = Object.values(similarColorsMap);
435436
values
436437
.sort((x, y) => (x.length < y.length ? 1 : -1))
438+
.filter((item) => item.length > 5)
437439
.forEach((item) => {
438-
if (!res.some((value) => ImageColorUtils.compare(value, ImageColorUtils.getMost(item), boundaryValue, 'lab'))) {
439-
res.push(ImageColorUtils.getMost(item));
440+
if (!res.some((value) => ImageColorUtils.compare(value, ImageColorUtils.getMedian(item), boundaryValue, type))) {
441+
res.push(ImageColorUtils.getMedian(item));
440442
}
441443
});
442444
return {

build/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function rgb2lab(rgb) {
102102

103103
class ImageColorUtils {
104104
constructor(params) {
105-
const { origin, mockMovePx = 30, boundaryValue = 10, ParticleSize = 4, width, height, onload, } = params || {};
105+
const { origin, mockMovePx = 30, boundaryValue = 10, ParticleSize = 8, width, height, onload, } = params || {};
106106
if (!origin) {
107107
throw new Error('Origin is necessary');
108108
}
@@ -406,6 +406,7 @@ class ImageColorUtils {
406406
const similarColorsMap = {};
407407
const res = [];
408408
const boundaryValue = 25;
409+
const type = 'lab';
409410
let lastColor;
410411
for (let x = 1; x < this.canvas.width; x += ImageColorUtils.ParticleSize) {
411412
for (let y = 1; y < this.canvas.height; y += ImageColorUtils.ParticleSize) {
@@ -420,11 +421,11 @@ class ImageColorUtils {
420421
}
421422
else if (similarValues.length &&
422423
lastColor &&
423-
ImageColorUtils.compare(rgba, lastColor, ImageColorUtils.boundaryValue)) {
424+
ImageColorUtils.compare(rgba, lastColor, ImageColorUtils.boundaryValue, type)) {
424425
let insert = false;
425426
for (const similarValue of similarValues) {
426-
if (ImageColorUtils.compare(rgba, similarValue[0], boundaryValue, 'lab') ||
427-
ImageColorUtils.compare(rgba, similarValue[similarValue.length - 1], boundaryValue, 'lab')) {
427+
if (ImageColorUtils.compare(rgba, similarValue[similarValue.length - 1], boundaryValue, type) &&
428+
ImageColorUtils.compare(rgba, similarValue[Math.floor(similarValue.length / 2)], boundaryValue, type)) {
428429
similarValue.push(rgba);
429430
insert = true;
430431
}
@@ -438,9 +439,10 @@ class ImageColorUtils {
438439
const values = Object.values(similarColorsMap);
439440
values
440441
.sort((x, y) => (x.length < y.length ? 1 : -1))
442+
.filter((item) => item.length > 5)
441443
.forEach((item) => {
442-
if (!res.some((value) => ImageColorUtils.compare(value, ImageColorUtils.getMost(item), boundaryValue, 'lab'))) {
443-
res.push(ImageColorUtils.getMost(item));
444+
if (!res.some((value) => ImageColorUtils.compare(value, ImageColorUtils.getMedian(item), boundaryValue, type))) {
445+
res.push(ImageColorUtils.getMedian(item));
444446
}
445447
});
446448
return {

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ <h3>参数配置</h3>
141141
// 初始化
142142
init(){
143143
// const imageUrl = 'https://storage.360buyimg.com/dataset-activity-mini/png-jpg-00002-2.jpg'
144-
// const imageUrl = 'https://img20.360buyimg.com/babel/s1180x940_jfs/t1/147050/6/31926/85670/6357a9afE26ff5c66/3a6823f7820fb72b.jpg'
145-
const imageUrl='https://img10.360buyimg.com/img/jfs/t1/86699/27/29562/39551/62bec631E155c7e41/55d63c89279226f0.jpg'
144+
const imageUrl = 'https://img20.360buyimg.com/babel/s1180x940_jfs/t1/147050/6/31926/85670/6357a9afE26ff5c66/3a6823f7820fb72b.jpg'
145+
// const imageUrl='https://img10.360buyimg.com/img/jfs/t1/86699/27/29562/39551/62bec631E155c7e41/55d63c89279226f0.jpg'
146146
const width = 500
147147
this.isDrawing = false
148148
this.rects = []

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "image-color-utils",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "Operate image pixels, such as pickup color, color contrast, etc",
55
"main": "./build/index.js",
66
"module": "./build/index.es.js",

src/imageColorUtils.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class ImageColorUtils {
8181
origin,
8282
mockMovePx = 30,
8383
boundaryValue = 10,
84-
ParticleSize = 4,
84+
ParticleSize = 8,
8585
width,
8686
height,
8787
onload,
@@ -537,6 +537,7 @@ export class ImageColorUtils {
537537

538538
const res: number[][] = []
539539
const boundaryValue = 25
540+
const type = 'lab'
540541

541542
let lastColor
542543

@@ -565,7 +566,8 @@ export class ImageColorUtils {
565566
ImageColorUtils.compare(
566567
rgba,
567568
lastColor,
568-
ImageColorUtils.boundaryValue
569+
ImageColorUtils.boundaryValue,
570+
type
569571
)
570572
) {
571573
// 是否已经被插入
@@ -574,15 +576,15 @@ export class ImageColorUtils {
574576
if (
575577
ImageColorUtils.compare(
576578
rgba,
577-
similarValue[0],
579+
similarValue[similarValue.length - 1],
578580
boundaryValue,
579-
'lab'
580-
) ||
581+
type
582+
) &&
581583
ImageColorUtils.compare(
582584
rgba,
583-
similarValue[similarValue.length - 1],
585+
similarValue[Math.floor(similarValue.length / 2)],
584586
boundaryValue,
585-
'lab'
587+
type
586588
)
587589
) {
588590
similarValue.push(rgba)
@@ -599,18 +601,19 @@ export class ImageColorUtils {
599601
const values = Object.values(similarColorsMap)
600602
values
601603
.sort((x, y) => (x.length < y.length ? 1 : -1))
604+
.filter((item) => item.length > 5)
602605
.forEach((item) => {
603606
if (
604607
!res.some((value) =>
605608
ImageColorUtils.compare(
606609
value,
607-
ImageColorUtils.getMost(item),
610+
ImageColorUtils.getMedian(item),
608611
boundaryValue,
609-
'lab'
612+
type
610613
)
611614
)
612615
) {
613-
res.push(ImageColorUtils.getMost(item))
616+
res.push(ImageColorUtils.getMedian(item))
614617
}
615618
})
616619

0 commit comments

Comments
 (0)