console
function getImgColor(img) {
this.canvas = document.createElement("canvas")
this.canvas.width = img.width
this.canvas.height = img.height
this.cvs = this.canvas.getContext("2d")
this.cvs.drawImage(img, 0, 0)
this.accuracy = 5
this.progress = ''
}
getImgColor.prototype.getColorXY = function(x, y) {
let obj = this.cvs.getImageData(x, y, 1, 1)
let arr = obj.data.toString().split(",")
let first = parseInt(arr[0]).toString(16)
first = first.length === 2 ? first : first + first
let second = parseInt(arr[1]).toString(16)
second = second.length === 2 ? second : second + second
let third = parseInt(arr[2]).toString(16)
third = third.length === 2 ? third : third + third
let last = parseInt(arr.pop()) / 255
last = last.toFixed(0)
let color = {}
color['rgba'] = 'rgba(' + arr.join(',') + ',' + last + ')'
color['#'] = '#' + first + second + third
return color
}
getImgColor.prototype.getColors = function() {
return (new Promise((resolve, reject) => {
let arr = []
let getY = (i) => {
for(let j = 0; j < this.canvas.height; j++) {
let obj = {}
obj = this.getColorXY(i, j)
obj.index = 1
let is = true
arr.forEach((item) => {
if (item['#'] === obj['#']) {
is = false
item.index += 1
}
let l = []
for (let i = 0; i < obj['#'].length; i++) {
if (item['#'].indexOf(obj['#'][i]) > -1) {
l.push('1')
}
}
let acc = (this.accuracy > 7) ? 7 : this.accuracy
acc = (this.accuracy < 1) ? 2 : this.accuracy
if (l.length > acc) {
is = false
item.index += 1
}
})
if (is) {
arr.push(obj)
}
}
};
let getX = (i) => {
if (i < this.canvas.width) {
getY(i)
this.progress = (i / this.canvas.width * 100).toFixed(2) + '%'
console.log(this.progress)
setTimeout(() => {
getX(++i)
}, 20)
} else {
this.progress = '100%'
console.log( this.progress )
resolve(arr.sort(function(a, b) {
return a.index < b.index ? 1 : (a.index > b.index ? -1 : 0)
}))
}
};
getX(0)
}))
}
let input = document.querySelector("#file")
input.addEventListener("change", (event) => {
let img = document.querySelector("#img")
let file = event.target.files[0]
let fr = new FileReader()
fr.onload = (e) => {
let n_img = new Image()
n_img.src = e.target.result
n_img.onload = (e) => {
n_img.id = 'img'
n_img.width = n_img.width
n_img.height = n_img.height
document.body.replaceChild(n_img, img)
getImg()
}
}
fr.readAsDataURL(file)
})
function getImg() {
let img = document.querySelector("#img")
let a = new getImgColor(img)
console.log(a.getColorXY(0, 0))
a.getColors().then((arr) => {
let ul = document.querySelector("#ul")
let text = document.querySelector("#text")
text.innerText = '共有' + arr.length + '个颜色';
let str = ''
arr.forEach((obj, index) => {
str += `<li style="background-color:${obj['#']}">${obj['#']} - ${obj['index']}次</li>`;
})
ul.innerHTML = str
})
}
<input type="file" id="file">
<img src="" id="img">
<p id="text"></p>
<ul id="ul"></ul>
ul,li{
list-style: none;
margin: 0;
padding: 0;
}
ul{
margin: 20px auto;
font-size: 0px;
}
ul li{
display: inline-block;
min-width: 100px;
height: 50px;
padding: 0 20px;
margin: 1px;
text-align: center;
font-size: 15px;
line-height: 50px;
color: #000;
border-radius: 4px;
transition: all 0.3s linear 0s;
cursor: pointer;
border: 1px solid #e8e8e8;
}
ul li:hover{
opacity: 0.8;
}