in src/videofx/VideoFxProcessor.ts [419:478]
private async validateReplacementConfig(config: VideoFxConfig): Promise<void> {
if (
config.backgroundReplacement.backgroundImageURL &&
config.backgroundReplacement.defaultColor
) {
throw new Error(
`Invalid VideoFx configuration: Background Replacement can not have both an ` +
`image URL and default color`
);
}
if (
!config.backgroundReplacement.backgroundImageURL &&
!config.backgroundReplacement.defaultColor
) {
throw new Error(
`Invalid VideoFx configuration: Background Replacement image URL and default ` +
`can not both be null/undefined`
);
}
// Confirm that the image properly loads
try {
if (config.backgroundReplacement.backgroundImageURL) {
await this.canvasOpsManager.loadImage(config.backgroundReplacement.backgroundImageURL);
}
} catch (error) {
this.logger.error(error.toString());
throw new Error(`Invalid VideoFx configuration: backgroundImageURL failed to load`);
}
const defaultColor = config.backgroundReplacement.defaultColor;
// Confirm that defaultColor is a valid color for fillStyle. We support black by default.
if (
defaultColor &&
defaultColor !== 'black' &&
defaultColor !== '#000000' &&
defaultColor !== '#000'
) {
// First validate hexadecimal color code
if (defaultColor.includes('#')) {
const hexRegex = new RegExp(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/);
if (!hexRegex.test(defaultColor)) {
throw new Error(
`Invalid hexadecimal color code for default replacement ` +
`background: ${defaultColor}`
);
}
}
const testColorCanvas = document.createElement('canvas');
const testColorCtx = testColorCanvas.getContext('2d');
// fillStyle is #000000 black by default
const prevFillStyle = testColorCtx.fillStyle;
testColorCtx.fillStyle = defaultColor;
// fillStyle will not change after assignment if invalid value is used
/* istanbul ignore next */
if (testColorCtx.fillStyle === prevFillStyle) {
throw new Error(`Invalid color for default replacement background: ${defaultColor}`);
}
}
}