Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Remove similar pixels #2

Merged
merged 2 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions assets/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ function RunOnLoad() {
var ctx = canvas.getContext("2d");

function handleImage(e) {
console.log("handleImage");
var reader = new FileReader();
reader.onload = function(event) {
var img = new Image();
Expand All @@ -16,14 +15,23 @@ function RunOnLoad() {
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);

//invertColors(ctx, imgData)
//invertColors(ctx, imgData);

let mapOfPopulariesResult = mapOfPopularies(imgData);
let mostPopularRank = [];

var isChecked = document.getElementById('removeSimilar').checked;

let rankTotal = 5;
for (let index = 0; index < rankTotal; index++) {
let theMostPopular = getTheMostPopular(mapOfPopulariesResult);
let theMostPopular;
if (isChecked) {
theMostPopular = getTheMostPopular(mapOfPopulariesResult, mostPopularRank);
}
else {
theMostPopular = getTheMostPopular(mapOfPopulariesResult);
}

mostPopularRank.push(theMostPopular);
mapOfPopulariesResult.delete(theMostPopular[0]);
}
Expand All @@ -39,14 +47,32 @@ function RunOnLoad() {
};
reader.readAsDataURL(e.target.files[0]);

function getTheMostPopular(mapOfPopulariesResult) {
function getTheMostPopular(mapOfPopulariesResult, filterSimilarPixel) {
let mostPopular = [...mapOfPopulariesResult.entries()].reduce((a, e) =>
e[1] > a[1] ? e : a
(e[1] > a[1]) && IsSimilarPixel(a[0], filterSimilarPixel) ? e : a
);

return mostPopular;
}

function IsSimilarPixel(pixelToCompareRgb, pixels) {
if (!pixels) {
return true;
}

pixelToCompareRgb = pixelToCompareRgb.split(", ");
for (const pixel of pixels) {
for (const pixelToCompare of pixelToCompareRgb) {
let constains = pixel[0].split(", ").filter(value => pixelToCompare == value);
if (constains.length > 0) {
return true;
}
}
}

return false;
}

function setBackgroundColor(elementId, colorRGB) {
var element = document.getElementById(elementId);
element.style.backgroundColor = `rgb(${colorRGB})`;
Expand Down
8 changes: 6 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Mostpopularpx: The Most popular pixels in an image (Draft)</title>
<title>(Draft) Mostpopularpx: The Most popular pixels in an image</title>

<link rel="stylesheet" href="assets/app.css" />
</head>
<body>
<h1>(Draft) Mostpopularpx: The Most popular pixels in an image</h1>
<label for="removeSimilar">Remove similar:</label><br />
<input type="checkbox" id="removeSimilar" name="removeSimilar" /><br />
<label for="imageLoader">Image File:</label><br />
<input type="file" id="imageLoader" name="imageLoader" />
<input type="file" id="imageLoader" name="imageLoader" /><br />

<canvas id="imageCanvas" width="200" height="200"></canvas>
<div>
<div class="top" id="top-0"></div>
Expand Down