Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
tmathern committed Jan 6, 2024
1 parent 9d2939f commit 8ef3971
Show file tree
Hide file tree
Showing 2 changed files with 9,736 additions and 6,641 deletions.
95 changes: 35 additions & 60 deletions lib/postprocessing/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,72 +645,47 @@ function convertSvg(img, instructions) {
}

function parseBackgroundColor(instructions){
console.log(" ")
console.log(" ")
console.log(" ")
console.log(" ")
console.log(" ")
console.log(" ")
console.log("Input color value: instructions.pdfbgcolor: ", instructions.pdfbgcolor)
console.log(" ")
console.log(" ")
console.log(" ")
console.log(" ")
console.log(" ")
console.log(" ")
console.log(" ")

let inputBackgroundColor = instructions.pdfbgcolor;
if(!inputBackgroundColor) return null;

if(typeof inputBackgroundColor === 'string') {
if(inputBackgroundColor.match(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)){
// HEX color
console.log(" ")
console.log(" ")
console.log("Matches HEX")
console.log(" ")
console.log(" ")
return inputBackgroundColor;
} else if(inputBackgroundColor.match(/^\d+$/)) {
console.log(" ")
console.log(" ")
console.log("Matches Number")
console.log(" ")
console.log(" ")
// Possibly the numerical value to apply to the R, G and B channels
inputBackgroundColor = Number.parseInt(inputBackgroundColor, 10);
let inputBackgroundColor = instructions.pdfbgcolor;
if(!inputBackgroundColor) return null;

if(typeof inputBackgroundColor === 'string') {
if(inputBackgroundColor.match(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)){
// HEX color
return inputBackgroundColor;
} else if(inputBackgroundColor.match(/^\d+$/)) {
// Possibly the numerical value to apply to the R, G and B channels
inputBackgroundColor = Number.parseInt(inputBackgroundColor, 10);
} else {
throw new GenericError(`Invalid background color set in instructions (value set was: ${instructions.pdfbgcolor})`);
}
}

// If we are here, we have numerical values for the color channels
// Verify the numerical value to apply to the R, G and B channels
if(Number.isInteger(inputBackgroundColor)
&& inputBackgroundColor >= 0
&& inputBackgroundColor <= 255){
return `rgb(${inputBackgroundColor},${inputBackgroundColor},${inputBackgroundColor})`;
} else {
throw new GenericError(`Invalid background color set in instructions (value set was: ${instructions.pdfbgcolor})`);
}
}

// If we are here, we have numerical values for the color channels
// Verify the numerical value to apply to the R, G and B channels
if(Number.isInteger(inputBackgroundColor)
&& inputBackgroundColor >= 0
&& inputBackgroundColor <= 255){
return `rgb(${inputBackgroundColor},${inputBackgroundColor},${inputBackgroundColor})`;
} else {
throw new GenericError(`Invalid background color values set for RGB channels in instructions: ${instructions.pdfbgcolor}`);
}
throw new GenericError(`Invalid background color values set for RGB channels in instructions: ${instructions.pdfbgcolor}`);
}
}

function handleTransparency(img, instructions) {
if(instructions.pdfbgcolor) {
// replacing transparency with a color defined in instructions
// (disregard if the format supports keeping transparency or not)
let backgroundColor = parseBackgroundColor(instructions);
img.background(backgroundColor);
img.flatten();
} else {
if (FORMAT_SUPPORTS_TRANSPARENCY[instructions.fmt]) {
img.background(DEFAULT_TRANSPARENCCY_BACKGROUND); // Keep transparency
if(instructions.pdfbgcolor) {
// replacing transparency with a color defined in instructions
// (disregard if the format supports keeping transparency or not)
const backgroundColor = parseBackgroundColor(instructions);
img.background(backgroundColor);
img.flatten();
} else {
img.background(DEFAULT_BACKGROUND_COLOR); // Change tranparency background to default background color
img.flatten(); // forces the image on its background to "commit" the background
if (FORMAT_SUPPORTS_TRANSPARENCY[instructions.fmt]) {
img.background(DEFAULT_TRANSPARENCCY_BACKGROUND); // Keep transparency
} else {
img.background(DEFAULT_BACKGROUND_COLOR); // Change tranparency background to default background color
img.flatten(); // forces the image on its background to "commit" the background
}
}
}
return img;
}

Expand Down
Loading

0 comments on commit 8ef3971

Please sign in to comment.