mirror of
https://github.com/lovell/sharp.git
synced 2025-07-13 20:30:14 +02:00
Enable PNG palette when at least one of quality, colours, colors or dither is set (#2226)
This commit is contained in:
parent
c76ae06fd1
commit
a7b1185602
@ -293,6 +293,9 @@ function png (options) {
|
||||
}
|
||||
if (is.defined(options.palette)) {
|
||||
this._setBooleanOption('pngPalette', options.palette);
|
||||
} else if (is.defined(options.quality) || is.defined(options.colours || options.colors) || is.defined(options.dither)) {
|
||||
this._setBooleanOption('pngPalette', true);
|
||||
}
|
||||
if (this.options.pngPalette) {
|
||||
if (is.defined(options.quality)) {
|
||||
if (is.integer(options.quality) && is.inRange(options.quality, 0, 100)) {
|
||||
@ -318,7 +321,6 @@ function png (options) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return this._updateFormatOut('png', options);
|
||||
}
|
||||
|
||||
|
@ -66,7 +66,8 @@
|
||||
"Paul Neave <paul.neave@gmail.com>",
|
||||
"Brendan Kennedy <brenwken@gmail.com>",
|
||||
"Brychan Bennett-Odlum <git@brychan.io>",
|
||||
"Edward Silverton <e.silverton@gmail.com>"
|
||||
"Edward Silverton <e.silverton@gmail.com>",
|
||||
"Roman Malieiev <aromaleev@gmail.com>"
|
||||
],
|
||||
"scripts": {
|
||||
"install": "(node install/libvips && node install/dll-copy && prebuild-install --runtime=napi) || (node-gyp rebuild && node install/dll-copy)",
|
||||
|
@ -127,8 +127,8 @@ describe('PNG', function () {
|
||||
it('Valid PNG libimagequant quality value produces image of same size or smaller', function () {
|
||||
const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
|
||||
return Promise.all([
|
||||
sharp(inputPngBuffer).resize(10).png({ palette: true, quality: 80 }).toBuffer(),
|
||||
sharp(inputPngBuffer).resize(10).png({ palette: true, quality: 100 }).toBuffer()
|
||||
sharp(inputPngBuffer).resize(10).png({ quality: 80 }).toBuffer(),
|
||||
sharp(inputPngBuffer).resize(10).png({ quality: 100 }).toBuffer()
|
||||
]).then(function (data) {
|
||||
assert.strictEqual(true, data[0].length <= data[1].length);
|
||||
});
|
||||
@ -136,15 +136,15 @@ describe('PNG', function () {
|
||||
|
||||
it('Invalid PNG libimagequant quality value throws error', function () {
|
||||
assert.throws(function () {
|
||||
sharp().png({ palette: true, quality: 101 });
|
||||
sharp().png({ quality: 101 });
|
||||
});
|
||||
});
|
||||
|
||||
it('Valid PNG libimagequant colours value produces image of same size or smaller', function () {
|
||||
const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
|
||||
return Promise.all([
|
||||
sharp(inputPngBuffer).resize(10).png({ palette: true, colours: 100 }).toBuffer(),
|
||||
sharp(inputPngBuffer).resize(10).png({ palette: true, colours: 200 }).toBuffer()
|
||||
sharp(inputPngBuffer).resize(10).png({ colours: 100 }).toBuffer(),
|
||||
sharp(inputPngBuffer).resize(10).png({ colours: 200 }).toBuffer()
|
||||
]).then(function (data) {
|
||||
assert.strictEqual(true, data[0].length <= data[1].length);
|
||||
});
|
||||
@ -152,21 +152,21 @@ describe('PNG', function () {
|
||||
|
||||
it('Invalid PNG libimagequant colours value throws error', function () {
|
||||
assert.throws(function () {
|
||||
sharp().png({ palette: true, colours: -1 });
|
||||
sharp().png({ colours: -1 });
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid PNG libimagequant colors value throws error', function () {
|
||||
assert.throws(function () {
|
||||
sharp().png({ palette: true, colors: 0.1 });
|
||||
sharp().png({ colors: 0.1 });
|
||||
});
|
||||
});
|
||||
|
||||
it('Valid PNG libimagequant dither value produces image of same size or smaller', function () {
|
||||
const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
|
||||
return Promise.all([
|
||||
sharp(inputPngBuffer).resize(10).png({ palette: true, dither: 0.1 }).toBuffer(),
|
||||
sharp(inputPngBuffer).resize(10).png({ palette: true, dither: 0.9 }).toBuffer()
|
||||
sharp(inputPngBuffer).resize(10).png({ dither: 0.1 }).toBuffer(),
|
||||
sharp(inputPngBuffer).resize(10).png({ dither: 0.9 }).toBuffer()
|
||||
]).then(function (data) {
|
||||
assert.strictEqual(true, data[0].length <= data[1].length);
|
||||
});
|
||||
@ -174,7 +174,7 @@ describe('PNG', function () {
|
||||
|
||||
it('Invalid PNG libimagequant dither value throws error', function () {
|
||||
assert.throws(function () {
|
||||
sharp().png({ palette: true, dither: 'fail' });
|
||||
sharp().png({ dither: 'fail' });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user