mirror of
https://github.com/lovell/sharp.git
synced 2025-12-19 07:15:08 +01:00
Tests: refactor modulate suite, ~20x faster
This commit is contained in:
@@ -28,115 +28,138 @@ describe('Modulate', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to hue-rotate', function () {
|
||||
const base = 'modulate-hue-120.jpg';
|
||||
const actual = fixtures.path('output.' + base);
|
||||
const expected = fixtures.expected(base);
|
||||
|
||||
return sharp(fixtures.inputJpg)
|
||||
it('should be able to hue-rotate', async () => {
|
||||
const [r, g, b] = await sharp({
|
||||
create: {
|
||||
width: 1,
|
||||
height: 1,
|
||||
channels: 3,
|
||||
background: { r: 153, g: 68, b: 68 }
|
||||
}
|
||||
})
|
||||
.modulate({ hue: 120 })
|
||||
.toFile(actual)
|
||||
.then(function () {
|
||||
fixtures.assertMaxColourDistance(actual, expected, 25);
|
||||
});
|
||||
.raw()
|
||||
.toBuffer();
|
||||
|
||||
assert.deepStrictEqual({ r: 41, g: 107, b: 57 }, { r, g, b });
|
||||
});
|
||||
|
||||
it('should be able to brighten', function () {
|
||||
const base = 'modulate-brightness-2.jpg';
|
||||
const actual = fixtures.path('output.' + base);
|
||||
const expected = fixtures.expected(base);
|
||||
|
||||
return sharp(fixtures.inputJpg)
|
||||
it('should be able to brighten', async () => {
|
||||
const [r, g, b] = await sharp({
|
||||
create: {
|
||||
width: 1,
|
||||
height: 1,
|
||||
channels: 3,
|
||||
background: { r: 153, g: 68, b: 68 }
|
||||
}
|
||||
})
|
||||
.modulate({ brightness: 2 })
|
||||
.toFile(actual)
|
||||
.then(function () {
|
||||
fixtures.assertMaxColourDistance(actual, expected, 25);
|
||||
});
|
||||
.raw()
|
||||
.toBuffer();
|
||||
|
||||
assert.deepStrictEqual({ r: 255, g: 173, b: 168 }, { r, g, b });
|
||||
});
|
||||
|
||||
it('should be able to unbrighten', function () {
|
||||
const base = 'modulate-brightness-0-5.jpg';
|
||||
const actual = fixtures.path('output.' + base);
|
||||
const expected = fixtures.expected(base);
|
||||
|
||||
return sharp(fixtures.inputJpg)
|
||||
it('should be able to darken', async () => {
|
||||
const [r, g, b] = await sharp({
|
||||
create: {
|
||||
width: 1,
|
||||
height: 1,
|
||||
channels: 3,
|
||||
background: { r: 153, g: 68, b: 68 }
|
||||
}
|
||||
})
|
||||
.modulate({ brightness: 0.5 })
|
||||
.toFile(actual)
|
||||
.then(function () {
|
||||
fixtures.assertMaxColourDistance(actual, expected, 25);
|
||||
});
|
||||
.raw()
|
||||
.toBuffer();
|
||||
|
||||
assert.deepStrictEqual({ r: 97, g: 17, b: 25 }, { r, g, b });
|
||||
});
|
||||
|
||||
it('should be able to saturate', function () {
|
||||
const base = 'modulate-saturation-2.jpg';
|
||||
const actual = fixtures.path('output.' + base);
|
||||
const expected = fixtures.expected(base);
|
||||
|
||||
return sharp(fixtures.inputJpg)
|
||||
it('should be able to saturate', async () => {
|
||||
const [r, g, b] = await sharp({
|
||||
create: {
|
||||
width: 1,
|
||||
height: 1,
|
||||
channels: 3,
|
||||
background: { r: 153, g: 68, b: 68 }
|
||||
}
|
||||
})
|
||||
.modulate({ saturation: 2 })
|
||||
.toFile(actual)
|
||||
.then(function () {
|
||||
fixtures.assertMaxColourDistance(actual, expected, 30);
|
||||
});
|
||||
.raw()
|
||||
.toBuffer();
|
||||
|
||||
assert.deepStrictEqual({ r: 198, g: 0, b: 43 }, { r, g, b });
|
||||
});
|
||||
|
||||
it('should be able to desaturate', function () {
|
||||
const base = 'modulate-saturation-0.5.jpg';
|
||||
const actual = fixtures.path('output.' + base);
|
||||
const expected = fixtures.expected(base);
|
||||
|
||||
return sharp(fixtures.inputJpg)
|
||||
it('should be able to desaturate', async () => {
|
||||
const [r, g, b] = await sharp({
|
||||
create: {
|
||||
width: 1,
|
||||
height: 1,
|
||||
channels: 3,
|
||||
background: { r: 153, g: 68, b: 68 }
|
||||
}
|
||||
})
|
||||
.modulate({ saturation: 0.5 })
|
||||
.toFile(actual)
|
||||
.then(function () {
|
||||
fixtures.assertMaxColourDistance(actual, expected, 25);
|
||||
});
|
||||
.raw()
|
||||
.toBuffer();
|
||||
|
||||
assert.deepStrictEqual({ r: 127, g: 83, b: 81 }, { r, g, b });
|
||||
});
|
||||
|
||||
it('should be able to modulate all channels', function () {
|
||||
const base = 'modulate-all.jpg';
|
||||
const actual = fixtures.path('output.' + base);
|
||||
const expected = fixtures.expected(base);
|
||||
|
||||
return sharp(fixtures.inputJpg)
|
||||
it('should be able to modulate all channels', async () => {
|
||||
const [r, g, b] = await sharp({
|
||||
create: {
|
||||
width: 1,
|
||||
height: 1,
|
||||
channels: 3,
|
||||
background: { r: 153, g: 68, b: 68 }
|
||||
}
|
||||
})
|
||||
.modulate({ brightness: 2, saturation: 0.5, hue: 180 })
|
||||
.toFile(actual)
|
||||
.then(function () {
|
||||
fixtures.assertMaxColourDistance(actual, expected, 25);
|
||||
});
|
||||
.raw()
|
||||
.toBuffer();
|
||||
|
||||
assert.deepStrictEqual({ r: 149, g: 209, b: 214 }, { r, g, b });
|
||||
});
|
||||
|
||||
describe('hue-rotate', function (done) {
|
||||
[30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360].forEach(function (angle) {
|
||||
it('should properly hue rotate by ' + angle + 'deg', function () {
|
||||
const base = 'modulate-hue-angle-' + angle + '.png';
|
||||
const actual = fixtures.path('output.' + base);
|
||||
it('should be able to use linear and modulate together', async () => {
|
||||
const contrast = 1.5;
|
||||
const brightness = 0.5;
|
||||
|
||||
const [r, g, b] = await sharp({
|
||||
create: {
|
||||
width: 1,
|
||||
height: 1,
|
||||
channels: 3,
|
||||
background: { r: 153, g: 68, b: 68 }
|
||||
}
|
||||
})
|
||||
.linear(contrast, -(128 * contrast) + 128)
|
||||
.modulate({ brightness })
|
||||
.raw()
|
||||
.toBuffer();
|
||||
|
||||
assert.deepStrictEqual({ r: 81, g: 0, b: 0 }, { r, g, b });
|
||||
});
|
||||
|
||||
describe('hue-rotate', () => {
|
||||
[30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360].forEach(angle => {
|
||||
it(`should hue rotate by ${angle} deg`, async () => {
|
||||
const base = `modulate-hue-angle-${angle}.png`;
|
||||
const actual = fixtures.path(`output.${base}`);
|
||||
const expected = fixtures.expected(base);
|
||||
|
||||
return sharp(fixtures.testPattern)
|
||||
await sharp(fixtures.testPattern)
|
||||
.resize(320)
|
||||
.modulate({ hue: angle })
|
||||
.png({ compressionLevel: 0 })
|
||||
.toFile(actual)
|
||||
.then(function () {
|
||||
fixtures.assertMaxColourDistance(actual, expected, 25);
|
||||
.then(() => {
|
||||
fixtures.assertMaxColourDistance(actual, expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to use linear and modulate together', function () {
|
||||
const base = 'modulate-linear.jpg';
|
||||
const actual = fixtures.path('output.' + base);
|
||||
const expected = fixtures.expected(base);
|
||||
|
||||
const contrast = 1.5;
|
||||
const brightness = 0.5;
|
||||
|
||||
return sharp(fixtures.testPattern)
|
||||
.linear(contrast, -(128 * contrast) + 128)
|
||||
.modulate({ brightness })
|
||||
.toFile(actual)
|
||||
.then(function () {
|
||||
fixtures.assertMaxColourDistance(actual, expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user