image-js
Version:
Image processing and manipulation in JavaScript
66 lines • 2.35 kB
JavaScript
/**
* Validate an array of channels.
* @param channels - Array of channels.
* @param image - The image being processed.
*/
export function validateChannels(channels, image) {
for (const channel of channels) {
validateChannel(channel, image);
}
}
/**
* Validates that a channel index passed by the user is within range and is an integer.
* @param channel - Channel index to validate.
* @param image - The image being processed.
*/
export function validateChannel(channel, image) {
if (!Number.isInteger(channel) || channel >= image.channels || channel < 0) {
throw new RangeError(`invalid channel: ${channel}. It must be a positive integer smaller than ${image.channels}`);
}
}
/**
* Validates that array of svalues passed by the user are positive and within range.
* @param values - Array of values to validate.
* @param image - Image from which the values come.
*/
export function validateValues(values, image) {
for (const value of values) {
validateValue(value, image);
}
}
/**
* Validates that a value passed by the user is positive and within range.
* @param value - Value to validate.
* @param image - Image from which the value comes.
*/
export function validateValue(value, image) {
if (value > image.maxValue || value < 0) {
throw new RangeError(`invalid value: ${value}. It must be a positive value smaller than ${image.maxValue + 1}`);
}
}
/**
* Validate that two images are compatible for comparison functions.
* @param image - First image.
* @param other - Second image.
*/
export function validateForComparison(image, other) {
if (image.width !== other.width || image.height !== other.height) {
throw new RangeError('both images must have the same size');
}
if (image.alpha !== other.alpha || image.bitDepth !== other.bitDepth) {
throw new RangeError('both images must have the same alpha and bitDepth');
}
if (image.channels !== other.channels) {
throw new RangeError('both images must have the same number of channels');
}
}
/**
* Checks if the given color is valid.
* @param color - Color to check.
* @param image - Image.
*/
export function validateColor(color, image) {
validateChannel(color.length - 1, image);
validateValues(color, image);
}
//# sourceMappingURL=validators.js.map