Skip to content

Commit

Permalink
Merge pull request #11 from studio-YOLO/9-functions-optimization
Browse files Browse the repository at this point in the history
Functions optimization
  • Loading branch information
VinciGit00 authored Mar 5, 2024
2 parents bf00b35 + fb28df9 commit 625b3a5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 43 deletions.
28 changes: 17 additions & 11 deletions src/core/black_and_white.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
/**
* Functiom that given array in the format [R, G, B, alpha,..., R, G, B, alpha] it converts in white and black in the format of a list. Possible values are just 0 and 255
* @param {number[][]} pixelArray: image that has to be encrypt in the format [R, G, B, alpha,..., R, G, B, alpha]
* @returns {number[]} blackWhitePixelArray: black and white image in the format of a list. Possible values are just 0 and 255
* Function to convert an image from color to black and white.
* @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) {
const blackWhitePixelArray = [];
for (let i = 0; i < pixelArray.length / 4; i++) {
for (let i = 0; i < pixelArray.length; i += 4) {
const grayValue =
0.299 * pixelArray[i * 4] +
0.587 * pixelArray[i * 4 + 1] +
0.114 * pixelArray[i * 4 + 2];
if (grayValue >= 128) blackWhitePixelArray.push(255, 255, 255, pixelArray[i * 4 + 3]);
else blackWhitePixelArray.push(0, 0, 0, pixelArray[i * 4 + 3]);
0.299 * pixelArray[i] +
0.587 * pixelArray[i + 1] +
0.114 * pixelArray[i + 2];
if (grayValue >= 128) {
pixelArray[i] = 255;
pixelArray[i + 1] = 255;
pixelArray[i + 2] = 255;
}else {
pixelArray[i] = 0;
pixelArray[i + 1] = 0;
pixelArray[i + 2] = 0;
}
}
return blackWhitePixelArray;
return pixelArray;
}

export default convertToBW;
15 changes: 8 additions & 7 deletions src/core/change_saturation.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import utils from "../utils.js";

/**
* Function to change the saturation of an image
* @param {number[]} pixelArray: Image pixel array in the format [R, G, B, alpha,..., R, G, B, alpha]
* @param {number} factor: factor to adjust the saturation (-100 to 100)
* @returns {number[]} adjustedPixelArray: Adjusted image pixel array in the format [R, G, B, alpha,..., R, G, B, alpha]
* Function to change the saturation of an image.
* @param {number[]} pixelArray: Image pixel array in the format [R, G, B, alpha,..., R, G, B, alpha].
* @param {number} factor: factor to adjust the saturation (-100 to 100).
* @returns {number[]} pixel array of the saturated or desaturated image.
*/
function changeSaturation(pixelArray, factor) {
const adjustedPixelArray = [];
for (let i = 0; i < pixelArray.length; i += 4) {
const hsl = utils.rgbToHsl(pixelArray[i], pixelArray[i + 1], pixelArray[i + 2]);
hsl[1] = Math.max(0, Math.min(100, hsl[1] += factor));
const rgb = utils.hslToRgb(hsl[0], hsl[1], hsl[2]);

adjustedPixelArray.push(rgb[0], rgb[1], rgb[2], pixelArray[i + 3]);
pixelArray[i] = rgb[0];
pixelArray[i + 1] = rgb[1];
pixelArray[i + 2] = rgb[2];
}
return adjustedPixelArray;
return pixelArray;
}

export default changeSaturation;
22 changes: 12 additions & 10 deletions src/core/gray_scale.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
/**
* Functiom that given array in the format [R, G, B, alfa,..., R, G, B, alfa] it converts it gray scale in format [g1,g1,g1,alfa,...,gn,gn,gn,alfa]
* @param {number[]} pixelArray: image that has to be encrypt in the format [R, G, B, alfa,..., R, G, B, alfa]
* @returns {number[][]} grayScaledPixelArray: gray scale image in the format [g1, g1, g1, alfa,..., gn, gn, gn, alfa]
* Function to convert an image to grayscale.
* @param {number[]} pixelArray: Image pixel array in the format [R, G, B, alpha,..., R, G, B, alpha].
* @returns {number[]} array of grayscale pixels of an image.
*/
function convertToGrayScale(pixelArray) {
const grayScaledPixelArray = [];
for (let i = 0; i < pixelArray.length / 4; i++) {
for (let i = 0; i < pixelArray.length; i += 4) {
const grayValue =
0.299 * pixelArray[4 * i] +
0.587 * pixelArray[4 * i + 1] +
0.114 * pixelArray[4 * i + 2];
grayScaledPixelArray.push(grayValue, grayValue, grayValue, pixelArray[4 * i + 3]);
0.299 * pixelArray[i] +
0.587 * pixelArray[i + 1] +
0.114 * pixelArray[i + 2];

pixelArray[i] = grayValue;
pixelArray[i + 1] = grayValue;
pixelArray[i + 2] = grayValue;
}
return grayScaledPixelArray;
return pixelArray;
}

export default convertToGrayScale;
38 changes: 23 additions & 15 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,35 @@ function rgbToHsl(r, g, b) {
r /= 255;
g /= 255;
b /= 255;
const l = Math.max(r, g, b);
const s = l - Math.min(r, g, b);
const h = s
? l === r
? (g - b) / s
: l === g
? 2 + (b - r) / s
: 4 + (r - g) / s
: 0;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;

if (max === min) {
h = s = 0;
} else {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}

return [
Math.round(60 * h < 0 ? 60 * h + 360 : 60 * h),
Math.round(100 * (s ? (l <= 0.5 ? s / (2 * l - s) : s / (2 - (2 * l - s))) : 0)),
Math.round((100 * (2 * l - s)) / 2),
h * 360,
s * 100,
l * 100
];
}

/**
* Function to convert HSL color to RGB
* @param {number} h: Hue value (0 to 1)
* @param {number} s: Saturation value (0 to 1)
* @param {number} l: Lightness value (0 to 1)
* @param {number} h: Hue value (0 to 360)
* @param {number} s: Saturation value (0 to 100)
* @param {number} l: Lightness value (0 to 100)
* @returns {number[]} RGB representation [Red, Green, Blue]
*/
function hslToRgb(h, s, l) {
Expand Down

0 comments on commit 625b3a5

Please sign in to comment.