Skip to content

Commit

Permalink
Merge pull request #52 from studio-YOLO/code-refactoring
Browse files Browse the repository at this point in the history
Code refactoring
  • Loading branch information
DPende authored May 4, 2024
2 parents e87671a + e1df560 commit 9979c82
Show file tree
Hide file tree
Showing 47 changed files with 980 additions and 1,209 deletions.
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
demo
test
lib
coverage
.vscode
12 changes: 6 additions & 6 deletions demo/scripts/black-white-filter-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const url = "images/img1.jpg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {

// convert image to black and white
document.body.appendChild(image);
// edit the image
editpix.toBackWhite(image)
.then(blackWhiteImage => {
document.body.appendChild(image);
document.body.appendChild(blackWhiteImage);
.then(resultImage => {
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
};
7 changes: 4 additions & 3 deletions demo/scripts/change-brightness-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const url = "images/img7.jpeg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {

document.body.appendChild(image);
// edit the image
editpix.changeBrightness(image, -90)
.then(resultImage => {
document.body.appendChild(image);
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
Expand Down
11 changes: 6 additions & 5 deletions demo/scripts/change-contrast-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ const url = "images/img3.jpeg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {

document.body.appendChild(image);
// edit the image
editpix.changeContrast(image, 2)
.then(highContrastImage => {
document.body.appendChild(image);
document.body.appendChild(highContrastImage);
.then(resultImage => {
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
}
7 changes: 4 additions & 3 deletions demo/scripts/change-exposure-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const url = "images/img7.jpeg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {

document.body.appendChild(image);
// edit the image
editpix.changeExposure(image, -30)
.then(resultImage => {
document.body.appendChild(image);
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
Expand Down
7 changes: 4 additions & 3 deletions demo/scripts/change-highlights-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const url = "images/img7.jpeg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {

document.body.appendChild(image);
// edit the image
editpix.changeHighlights(image, 100)
.then(resultImage => {
document.body.appendChild(image);
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
Expand Down
12 changes: 6 additions & 6 deletions demo/scripts/change-opacity-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const url = "images/img1.jpg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {

// convert image to black and white
document.body.appendChild(image);
// edit the image
editpix.changeOpacity(image, 128)
.then(newImage => {
document.body.appendChild(image);
document.body.appendChild(newImage);
.then(resultImage => {
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
};
7 changes: 4 additions & 3 deletions demo/scripts/change-saturation-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const url = "images/img7.jpeg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {

document.body.appendChild(image);
// edit the image
editpix.changeSaturation(image, 20)
.then(resultImage => {
document.body.appendChild(image);
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
Expand Down
7 changes: 4 additions & 3 deletions demo/scripts/change-shadow-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const url = "images/img7.jpeg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {

document.body.appendChild(image);
// edit the image
editpix.changeShadows(image, -100)
.then(resultImage => {
document.body.appendChild(image);
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
Expand Down
7 changes: 4 additions & 3 deletions demo/scripts/change-sharpness-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const url = "images/img7.jpeg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {

document.body.appendChild(image);
// edit the image
editpix.changeSharpness(image, 70)
.then(resultImage => {
document.body.appendChild(image);
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
Expand Down
27 changes: 5 additions & 22 deletions demo/scripts/change-temperature-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,14 @@ const url = "images/img8.jpeg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {
document.body.appendChild(image);

// edit the image
editpix.changeTemperature(image, 20)
.then(highContrastImage => {
document.body.appendChild(highContrastImage);
})
.catch(error => { console.log(error) })

editpix.changeTemperature(image, 50)
.then(highContrastImage => {
document.body.appendChild(highContrastImage);
})
.catch(error => { console.log(error) })

editpix.changeTemperature(image, -20)
.then(highContrastImage => {
document.body.appendChild(highContrastImage);
})
.catch(error => { console.log(error) })

editpix.changeTemperature(image, -50)
.then(highContrastImage => {
document.body.appendChild(highContrastImage);
.then(resultImage => {
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
}
7 changes: 4 additions & 3 deletions demo/scripts/change-tint-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const url = "images/img7.jpeg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {

document.body.appendChild(image);
// edit the image
editpix.changeTint(image, -80)
.then(resultImage => {
document.body.appendChild(image);
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
Expand Down
12 changes: 6 additions & 6 deletions demo/scripts/gray-scale-filter-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const url = "images/img1.jpg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {

// convert image to gray scale
document.body.appendChild(image);
// edit the image
editpix.toGrayScale(image)
.then(grayScaledImage => {
document.body.appendChild(image);
document.body.appendChild(grayScaledImage);
.then(resultImage => {
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
};
13 changes: 0 additions & 13 deletions demo/scripts/image-from-url-demo.js

This file was deleted.

10 changes: 6 additions & 4 deletions demo/scripts/optimize-contrast-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ const url = "images/img2.jpg";
var image = new Image();
image.src = url;

// waiting image load
image.onload = () => {

document.body.appendChild(image);
// edit the image
editpix.toOptimizedContrast(image)
.then(highContrastImage => {
document.body.appendChild(image);
document.body.appendChild(highContrastImage);
.then(resultImage => {
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
}
2 changes: 1 addition & 1 deletion demo/scripts/resize-image-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ container.classList.add("container")



//waiting image load
// waiting image load
image.onload = () => {
// image dimension before resizing
console.log("Dimension before resizing: " + image.naturalWidth + "x" + image.naturalHeight);
Expand Down
12 changes: 6 additions & 6 deletions demo/scripts/sepia-filter-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const url = "images/img1.jpg";
var image = new Image();
image.src = url;

//waiting image load
// waiting image load
image.onload = () => {

// convert image to black and white
document.body.appendChild(image);
// edit the image
editpix.toSepia(image)
.then(sepiaImage => {
document.body.appendChild(image);
document.body.appendChild(sepiaImage);
.then(resultImage => {
// render modified image
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
};
4 changes: 2 additions & 2 deletions src/core/black-white-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param {number[]} pixelArray: Image pixel array in the format [R, G, B, alpha,..., R, G, B, alpha].
* @returns {number[]} array of black and white image pixels.
*/
function convertToBW(pixelArray) {
function toBlackWhite(pixelArray) {
for (let i = 0; i < pixelArray.length; i += 4) {
const grayValue =
0.299 * pixelArray[i] +
Expand All @@ -22,4 +22,4 @@ function convertToBW(pixelArray) {
return pixelArray;
}

export default convertToBW;
export default toBlackWhite;
6 changes: 3 additions & 3 deletions src/core/change-brightness.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import utils from "../utils.js";
import {rgbToHsl, hslToRgb} from "../core/colorspace-conversion.js";

/**
* Function to change the brightness of an image
Expand All @@ -10,9 +10,9 @@ function changeBrightness(pixelArray, factor) {
if (factor == 0)
return pixelArray;
for (let i = 0; i < pixelArray.length; i += 4) {
const hsl = utils.rgbToHsl(pixelArray[i], pixelArray[i + 1], pixelArray[i + 2]);
const hsl = rgbToHsl(pixelArray[i], pixelArray[i + 1], pixelArray[i + 2]);
hsl.l = Math.max(0, Math.min(100, hsl.l += factor));
const rgb = utils.hslToRgb(hsl.h, hsl.s, hsl.l);
const rgb = hslToRgb(hsl.h, hsl.s, hsl.l);

pixelArray[i] = rgb.r;
pixelArray[i + 1] = rgb.g;
Expand Down
6 changes: 3 additions & 3 deletions src/core/change-saturation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import utils from "../utils.js";
import {rgbToHsl, hslToRgb} from "../core/colorspace-conversion.js";

/**
* Function to change the saturation of an image.
Expand All @@ -10,9 +10,9 @@ function changeSaturation(pixelArray, factor) {
if (factor == 0)
return pixelArray;
for (let i = 0; i < pixelArray.length; i += 4) {
let hsl = utils.rgbToHsl(pixelArray[i], pixelArray[i + 1], pixelArray[i + 2]);
let hsl = rgbToHsl(pixelArray[i], pixelArray[i + 1], pixelArray[i + 2]);
hsl.s = Math.max(0, Math.min(100, hsl.s += factor));
const rgb = utils.hslToRgb(hsl.h, hsl.s, hsl.l);
const rgb = hslToRgb(hsl.h, hsl.s, hsl.l);

pixelArray[i] = rgb.r;
pixelArray[i + 1] = rgb.g;
Expand Down
29 changes: 0 additions & 29 deletions src/core/color-selection.js

This file was deleted.

Loading

0 comments on commit 9979c82

Please sign in to comment.