diff --git a/src/core/change-brightness.js b/src/core/change-brightness.js index aff6b21..b4dd007 100644 --- a/src/core/change-brightness.js +++ b/src/core/change-brightness.js @@ -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; } diff --git a/src/core/change-saturation.js b/src/core/change-saturation.js index 4fbb1e9..daa0446 100644 --- a/src/core/change-saturation.js +++ b/src/core/change-saturation.js @@ -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; } diff --git a/src/editpix.js b/src/editpix.js index b0d0205..47fc044 100644 --- a/src/editpix.js +++ b/src/editpix.js @@ -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); } @@ -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); }