Skip to content

Commit

Permalink
update: now most functions return javascript Promises
Browse files Browse the repository at this point in the history
  • Loading branch information
DPende committed Feb 22, 2024
1 parent 343bbed commit 33be13f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 26 deletions.
32 changes: 21 additions & 11 deletions src/editpix.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,32 @@ import higherColorContrast from "./core/higher_contrast.js";
var EditPix = function () { };

EditPix.prototype.getColorPalette = (image, colorNumber = 5, quality = 1) => {
utils.validate(quality, colorNumber);
const pixelArray = utils.removeAlpha(imageManager.getPixelArray(image));
return kMeans(pixelArray, 10);
return new Promise((resolve, reject) => {
utils.validate(quality, colorNumber);
imageManager.resizeByQuality(image, quality)
.then(resizedImage => {
const pixelArray = utils.removeAlpha(imageManager.getPixelArray(resizedImage));
resolve(kMeans(pixelArray, colorNumber));
})
.catch(error => { reject(error) })
})
}

EditPix.prototype.getDominantColor = (image, quality = 1) => {
utils.validate(quality, 1);
image = imageManager.resizeByQuality(image, quality);
const pixelArray = utils.removeAlpha(imageManager.getPixelArray(image));
return kMeans(pixelArray, 1);
EditPix.prototype.getDominantColor = function(image, quality = 1) {
return this.getColorPalette(image, 1, quality);
}

EditPix.prototype.getImageFromUrl = (url) => {
const image = new Image();
image.url = url;
return image;
return new Promise((resolve, reject) => {
const image = new Image();
image.onload = () => {
resolve(image);
}
image.onerror = () => {
reject(image);
}
image.src = url;
})
}

EditPix.prototype.toGrayScale = (image) => {
Expand Down
56 changes: 42 additions & 14 deletions src/image_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ function getPixelArray(image) {

function resizeByQuality(image, quality) {
const { canvas, context } = createCanvas();
const newWidth = image.naturalWidth * ((quality - 1) * 0.10);
const newHeight = image.naturalHeight * ((quality - 1) * 0.10);
const newWidth = image.naturalWidth * (quality * 0.10);
const newHeight = image.naturalHeight * (quality * 0.10);
canvas.width = newWidth;
canvas.height = newHeight;
context.drawImage(image, 0, 0, newWidth, newHeight);
let resizedImage = new Image();
resizedImage.src = canvas.toDataURL();
return resizedImage;
return new Promise((resolve, reject) => {
let resizedImage = new Image();
resizedImage.onload = () => {
resolve(resizedImage);
}
resizedImage.onerror = (error) => {
reject(error);
}
resizedImage.src = canvas.toDataURL();
})
}

function resizeByWidth(image, newWidth) {
Expand All @@ -24,9 +31,16 @@ function resizeByWidth(image, newWidth) {
canvas.width = newWidth;
canvas.height = newHeight;
context.drawImage(image, 0, 0, newWidth, newHeight);
let resizedImage = new Image();
resizedImage.src = canvas.toDataURL();
return resizedImage;
return new Promise((resolve, reject) => {
let resizedImage = new Image();
resizedImage.onload = () => {
resolve(resizedImage);
}
resizedImage.onerror = (error) => {
reject(error);
}
resizedImage.src = canvas.toDataURL();
})
}

function resizeByHeight(image, newHeight) {
Expand All @@ -35,9 +49,16 @@ function resizeByHeight(image, newHeight) {
canvas.width = newWidth;
canvas.height = newHeight;
context.drawImage(image, 0, 0, newWidth, newHeight);
let resizedImage = new Image();
resizedImage.src = canvas.toDataURL();
return resizedImage;
return new Promise((resolve, reject) => {
let resizedImage = new Image();
resizedImage.onload = () => {
resolve(resizedImage);
}
resizedImage.onerror = (error) => {
reject(error);
}
resizedImage.src = canvas.toDataURL();
})
}

function convertToImage(pixelArray, width, height) {
Expand All @@ -47,9 +68,16 @@ function convertToImage(pixelArray, width, height) {
let imageData = context.createImageData(width, height);
imageData.data.set(new Uint8ClampedArray(pixelArray));
context.putImageData(imageData, 0, 0);
let image = new Image();
image.src = canvas.toDataURL();
return image;
return new Promise((resolve, reject) => {
let image = new Image();
image.onload = () => {
resolve(image);
}
image.onerror = (error) => {
reject(error);
}
image.src = canvas.toDataURL();
})
}

function createCanvas() {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function validate(quality, colorNumber) {
if (quality < 1 || quality > 10) {
throw new Error("The quality parameter is invalid: it must be a number between 1 and 10")
}
if (colorNumber < 1 || colorNumber > 10) {
if (colorNumber < 1 || colorNumber > 15) {
throw new Error("Color number is invalid.")
}
}
Expand Down

0 comments on commit 33be13f

Please sign in to comment.