Skip to content

Commit

Permalink
refactor: code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
DPende committed Apr 24, 2024
1 parent 0c6fb87 commit fc74f38
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/core/change-brightness.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ function changeBrightness(pixelArray, factor) {
return pixelArray;
for (let i = 0; i < pixelArray.length; i += 4) {
const hsl = utils.rgbToHsl(pixelArray[i], pixelArray[i + 1], pixelArray[i + 2]);
hsl[2] = Math.max(0, Math.min(100, hsl[2] += factor));
const rgb = utils.hslToRgb(hsl[0], hsl[1], hsl[2]);
hsl.l = Math.max(0, Math.min(100, hsl.l += factor));
const rgb = utils.hslToRgb(hsl.h, hsl.s, hsl.l);

pixelArray[i] = rgb[0];
pixelArray[i + 1] = rgb[1];
pixelArray[i + 2] = rgb[2];
pixelArray[i] = rgb.r;
pixelArray[i + 1] = rgb.g;
pixelArray[i + 2] = rgb.b;
}
return pixelArray;
}
Expand Down
12 changes: 6 additions & 6 deletions src/core/change-saturation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ function changeSaturation(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]);
hsl[1] = Math.max(0, Math.min(100, hsl[1] += factor));
const rgb = utils.hslToRgb(hsl[0], hsl[1], hsl[2]);
let hsl = utils.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);

pixelArray[i] = rgb[0];
pixelArray[i + 1] = rgb[1];
pixelArray[i + 2] = rgb[2];
pixelArray[i] = rgb.r;
pixelArray[i + 1] = rgb.g;
pixelArray[i + 2] = rgb.b;
}
return pixelArray;
}
Expand Down
12 changes: 12 additions & 0 deletions src/editpix.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ EditPix.prototype.getHigherContrast = (color) => {
}

EditPix.prototype.rgbToHex = (r, g, b) => {
if (r < 0 || r > 255 ||
g < 0 || g > 255 ||
b < 0 || b > 255)
throw new Error("RGB format is invalid.");
return utils.rgbToHex(r, g, b);
}

Expand Down Expand Up @@ -148,10 +152,18 @@ EditPix.prototype.changeBrightness = (image, factor) => {
}

EditPix.prototype.rgbToHsl = (r, g, b) => {
if (r < 0 || r > 255 ||
g < 0 || g > 255 ||
b < 0 || b > 255)
throw new Error("RGB format is invalid.");
return utils.rgbToHsl(r, g, b);
}

EditPix.prototype.hslToRgb = (h, s, l) => {
if (h < 0 || h > 360 ||
s < 0 || s > 100 ||
l < 0 || l > 100)
throw new Error("HSL format is invalid.");
return utils.hslToRgb(h, s, l);
}

Expand Down

0 comments on commit fc74f38

Please sign in to comment.