mirror of
https://github.com/lovell/sharp.git
synced 2025-12-19 15:25:07 +01:00
Add named 'alpha' channel to extractChannel op #2138
This commit is contained in:
@@ -60,22 +60,19 @@ function ensureAlpha () {
|
||||
* // green.jpg is a greyscale image containing the green channel of the input
|
||||
* });
|
||||
*
|
||||
* @param {number|string} channel - zero-indexed band number to extract, or `red`, `green` or `blue` as alternative to `0`, `1` or `2` respectively.
|
||||
* @param {number|string} channel - zero-indexed channel/band number to extract, or `red`, `green`, `blue` or `alpha`.
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid channel
|
||||
*/
|
||||
function extractChannel (channel) {
|
||||
if (channel === 'red') {
|
||||
channel = 0;
|
||||
} else if (channel === 'green') {
|
||||
channel = 1;
|
||||
} else if (channel === 'blue') {
|
||||
channel = 2;
|
||||
const channelMap = { red: 0, green: 1, blue: 2, alpha: 3 };
|
||||
if (Object.keys(channelMap).includes(channel)) {
|
||||
channel = channelMap[channel];
|
||||
}
|
||||
if (is.integer(channel) && is.inRange(channel, 0, 4)) {
|
||||
this.options.extractChannel = channel;
|
||||
} else {
|
||||
throw is.invalidParameterError('channel', 'integer or one of: red, green, blue', channel);
|
||||
throw is.invalidParameterError('channel', 'integer or one of: red, green, blue, alpha', channel);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user