Skip to content

Commit

Permalink
Merge pull request #25 from studio-YOLO/19-test-demo-change-exposure
Browse files Browse the repository at this point in the history
update: added exposure tests and demo
  • Loading branch information
DPende authored Mar 19, 2024
2 parents 0572e18 + 9f50863 commit ca23b2e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<script type="module" src="scripts/change_opacity_demo.js"></script>
<script type="module" src="scripts/change_shadow_demo.js"></script>-->
<script type="module" src="scripts/change_tint_demo.js"></script>

</body>

</html>
19 changes: 19 additions & 0 deletions demo/scripts/change_exposure_demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import EditPix from "../../src/editpix.js";

const editpix = new EditPix();

const url = "images/img7.jpeg";

var image = new Image();
image.src = url;

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

editpix.changeExposure(image, -30)
.then(resultImage => {
document.body.appendChild(image);
document.body.appendChild(resultImage);
})
.catch(error => { console.log(error) })
}
2 changes: 2 additions & 0 deletions src/core/change_exposure.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ function changeExposure(pixelArray, factor) {
}
return pixelArray;
}

export default changeExposure;
9 changes: 9 additions & 0 deletions src/editpix.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import changeSaturation from "./core/change_saturation.js";
import changeBrightness from "./core/change_brightness.js";
import toSepia from "./core/sepia.js";
import changeOpacity from "./core/change_opacity.js";
import changeShadows from "./core/change_shadows.js";
import changeExposure from "./core/change_exposure.js";
import changeTint from "./core/change_tint.js"

var EditPix = function () { };
Expand Down Expand Up @@ -170,4 +172,11 @@ EditPix.prototype.changeShadows = (image, factor) => {
return imageManager.convertToImage(changeShadows(pixelArray, factor), image.naturalWidth, image.naturalHeight);
}

EditPix.prototype.changeExposure = (image, factor) => {
if (factor < -100 || factor > 100)
throw new Error("Invalid exposure factor: must be a value between -100 and 100");
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(changeExposure(pixelArray, factor), image.naturalWidth, image.naturalHeight);
}

export default EditPix;
27 changes: 26 additions & 1 deletion test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import changeTemperature from "../src/core/change_temperature.js";
import changeOpacity from "../src/core/change_opacity.js";
import changeShadows from "../src/core/change_shadows.js"
import higherColorContrast from "../src/core/higher_contrast.js";
import changeTint from "../src/core/change_tint.js"
import changeExposure from "../src/core/change_exposure.js";
import changeTint from "../src/core/change_tint.js";
import changeSaturation from "../src/core/change_saturation.js"
import changeBrightness from "../src/core/change_brightness.js";
import utils from "../src/utils.js";
Expand Down Expand Up @@ -202,6 +203,30 @@ describe('higherColorContrast', () => {
});
});

describe('changeExposure', () => {
test('should return an unchanged array if the factor is 0', () => {
const color = [67, 141, 23];
changeContrast(color, 0);
expect(color).toEqual([67, 141, 23]);
});
test('should return a brighter array for positive factors', () => {
const luma = (value) => (value[0] + value[1] + value[2])/3
const color = [67, 141, 23];
const luma1 = luma(color);
changeExposure(color, 20);
const luma2 = luma(color);
expect(luma1).toBeLessThan(luma2);
});
test('should return a darker array for positive factors', () => {
const luma = (value) => (value[0] + value[1] + value[2])/3
const color = [67, 141, 23];
const luma1 = luma(color);
changeExposure(color, -20);
const luma2 = luma(color);
expect(luma1).toBeGreaterThan(luma2);
});
});

describe('changeSaturation', () => {
test('should not change anything if factor is 0', () => {
const testColor1 = [173, 114, 255];
Expand Down
19 changes: 19 additions & 0 deletions test/editpix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ describe('EditPix changeShadows method', () => {
});
});

describe('EditPix changeExposure method', () => {
test('should reject lower out-of-range factors', () => {
try {
const editPix = new EditPix();
editPix.changeExposure([0, 234, 87], 150);
} catch (e) {
expect(e).toEqual(new Error("Invalid exposure factor: must be a value between -100 and 100"));
}
});
test('should reject upper out-of-range factors', () => {
try {
const editPix = new EditPix();
editPix.changeExposure([0, 234, 87], -123);
} catch (e) {
expect(e).toEqual(new Error("Invalid exposure factor: must be a value between -100 and 100"));
}
});
});

describe('EditPix changeContrast method', () => {
test('should reject lower out-of-range factors', () => {
try {
Expand Down

0 comments on commit ca23b2e

Please sign in to comment.