Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions test/unit/image/pixels.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,154 @@ suite('pixels', function() {
assert.deepEqual(img.get(25, 25.999), img.get(25, 25));
});
});

suite('p5.Image.pixels', function() {
test('should be an array of pixels', function() {
let img = myp5.createImage(10, 10);
img.loadPixels();
assert.typeOf(img.pixels, 'Uint8ClampedArray');
});

test('should store r, g, b, a values for each pixel', function() {
let img = myp5.createImage(10, 10);
myp5.pixelDensity(1);
img.loadPixels();
assert.strictEqual(img.pixels.length, 400);
});

test('should store correct r, g, b, a values for each pixel', function() {
let img = myp5.createImage(10, 10);
myp5.pixelDensity(1);
img.loadPixels();
img.pixels[0] = 255;
img.pixels[1] = 102;
img.pixels[2] = 204;
img.pixels[3] = 255;
img.updatePixels();

assert.strictEqual(img.pixels[0], 255);
assert.strictEqual(img.pixels[1], 102);
assert.strictEqual(img.pixels[2], 204);
assert.strictEqual(img.pixels[3], 255);
});
});

suite('p5.Image.set', function() {
/* Parameter Validation missing */
test('set(x,y) changes color of pixel (x, y)', function() {
let img = myp5.createImage(50, 50);
img.loadPixels();
img.set(0, 0, myp5.color(255, 34, 19));
img.updatePixels();
assert.deepEqual(img.get(0, 0), [255, 34, 19, 255]);
});
});

suite('p5.Image.blend', function() {
test('should copy a region of pixels using the specified blend mode', function() {
let img = myp5.createImage(50, 50);
let img2 = myp5.createImage(50, 50);
img.loadPixels();
for (let i = 0; i < img.width; i++) {
for (let j = 0; j < img.height; j++) {
img.set(i, j, myp5.color(255, 0, 0)); // red
}
}
img.updatePixels();
img2.loadPixels();
for (let i = 0; i < img2.width; i++) {
for (let j = 0; j < img2.height; j++) {
img2.set(i, j, myp5.color(255, 255, 0)); // yellow
}
}
img2.updatePixels();
img.blend(img2, 0, 0, 10, 10, 0, 0, 10, 10, myp5.NORMAL); // blend yellow on red in normal mode
myp5.image(img, 0, 0);

for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
assert.deepEqual(img.get(i, j), [255, 255, 0, 255]); // should be yellow
}
}
});

test('wrong parameter at #8', function() {
assert.validationError(function() {
let img = myp5.createImage(50, 50);
img.blend(0, 0, 10, 10, 10, 0, 10, 10, 'a');
});
});

test('no friendly-err-msg. missing param #0', function() {
assert.doesNotThrow(
function() {
let img = myp5.createImage(50, 50);
img.blend(0, 0, 10, 10, 10, 0, 10, 10, myp5.OVERLAY);
},
Error,
'got unwanted exception'
);
});

test('missing parameter at #3 ', function() {
assert.throw(function() {
let img = myp5.createImage(50, 50);
img.blend(0, 0, 10, 10, 0, 10, 10, myp5.OVERLAY);
});
});

test('missing parameter at #8 ', function() {
assert.throw(function() {
let img = myp5.createImage(50, 50);
img.blend(0, 0, 10, 10, 10, 0, 10, 10);
});
});
});

suite('p5.Image.copy', function() {
test('should copy a region of pixels', function() {
let img = myp5.createImage(50, 50);
let img2 = myp5.createImage(50, 50);
img.loadPixels();
for (let i = 0; i < img.width; i++) {
for (let j = 0; j < img.height; j++) {
img.set(i, j, myp5.color(255, 0, 0)); // red
}
}
img.updatePixels();
img2.loadPixels();
for (let i = 0; i < img2.width; i++) {
for (let j = 0; j < img2.height; j++) {
img2.set(i, j, myp5.color(0, 255, 0)); // green
}
}
img2.updatePixels();
img.copy(img2, 0, 0, 10, 10, 0, 0, 10, 10); // copy green on red
myp5.image(img, 0, 0);

for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
assert.deepEqual(img.get(i, j), [0, 255, 0, 255]); // should be green
}
}
});

test('no friendly-err-msg. missing param #0', function() {
assert.doesNotThrow(
function() {
let img = myp5.createImage(50, 50);
img.copy(0, 0, 10, 10, 10, 0, 10, 10);
},
Error,
'got unwanted exception'
);
});

test('missing parameter at #3 ', function() {
assert.throw(function() {
let img = myp5.createImage(50, 50);
img.copy(0, 0, 10, 10, 0, 10, 10);
}, 'Signature not supported');
});
});
});