0
In a project I’m working on, the need arose to check if it’s an image upload is actually valid or a totally random image. The idea would be to verify what would be the predominant color and what would be the percentage of this starting color. An example would be to rescue the RGB and return for example that it exists in about 90% of the image. I was taking a look at this "lib" Color Thief in Javascript, but unable to verify the percentage of color in predominance.
What would be the best way to do that?
const $input = document.getElementById('input');
const $canvas = document.getElementById('canvas');
const $resultado = document.getElementById('resultado');
const ctx = $canvas.getContext("2d");
const reader = new FileReader();
const image = new Image();
const size = 200;
$input.addEventListener('change', (event) => {
const input = event.target;
if (input.files && input.files[0]) {
reader.readAsDataURL(input.files[0]);
}
});
reader.onload = (event) => {
image.src = event.target.result;
};
image.onload = () => {
$canvas.width = size;
$canvas.height = $canvas.width * (image.height / image.width);
const oc = document.createElement('canvas');
const octx = oc.getContext('2d');
oc.width = $canvas.width;
oc.height = $canvas.height;
octx.drawImage(image, 0, 0, oc.width, oc.height);
ctx.drawImage(oc, 0, 0, oc.width, oc.height, 0, 0, oc.width, oc.height);
// hardcode value
$resultado.innerHTML = 'resultado: 60% cor: #0B70A4' ;
};
<main class="centerer">
<input type="file" id="input"/><br/>
<canvas id="canvas"></canvas><br/>
<span id="resultado"></span>
</main>