Skip to content

Commit

Permalink
FEATURE-RELEASE: ASSETS-23937 - Support setting a color as background…
Browse files Browse the repository at this point in the history
… color

FEATURE-RELEASE: 
* Support setting a color as background color
  • Loading branch information
tmathern authored Jan 16, 2024
1 parent 7711ac1 commit 49d52a1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
if: "!contains(github.event.head_commit.message, '[ci skip]')"
strategy:
matrix:
node-version: [14.16.1, 14.17]
node-version: [14.17]

steps:
- uses: actions/checkout@v2
Expand Down
55 changes: 43 additions & 12 deletions lib/postprocessing/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ const EXTRA_INSTRUCTIONS = new Set([
"interlace",
"dpi",
"convertToDpi",
"watermark"
"watermark",
"pdfbgcolor"
]);

const SRGB_COLOR_PROFILE_FILE = process.env.SRGB_COLOR_PROFILE_FILE;
Expand Down Expand Up @@ -191,7 +192,6 @@ function isTargetFormat(metadata, instructions) {
if (format) {
return metadata.FileType === format;
}

return false;
}

Expand Down Expand Up @@ -473,7 +473,7 @@ async function applyOperations(img, intermediateRendition, rendition, directorie
img = convertSvg(img, instructions);
} else {
// raster
img = handleTransparency(img, instructions.fmt);
img = handleTransparency(img, instructions);
}

// handle exif orientation
Expand Down Expand Up @@ -623,12 +623,10 @@ async function write(img, outfile) {
* @param {Object} directories working directories of the current activation
*/
async function imagePostProcess(intermediateRendition, rendition, directories) {

const img = await read(intermediateRendition.path);
img.setFormat(rendition.instructions.fmt);

await applyOperations(img, intermediateRendition, rendition, directories);

await applyOutputProperties(img, rendition.instructions);

await write(img, rendition.path);
Expand All @@ -639,22 +637,55 @@ function convertSvg(img, instructions) {
// Only using width because ImageMagick will use the smallest value whether it be width or height
const width = instructions.width || SVG_DEFAULT_WIDTH;

img = handleTransparency(img, instructions.fmt);
img = handleTransparency(img, instructions);

// some svgs have no size (only percentage width/height), so we scale them to our target size
img.in("-size", `${width}`); // 2020-11-10 img.rawSize() will not be applied at the correct time and the SVG will be upscaled resulting in a fuzzy final rendition
return img;
}

// gm("img.png").set(attribute, value)
function handleTransparency(img, fmt) {
if (FORMAT_SUPPORTS_TRANSPARENCY[fmt]) {
img.background(DEFAULT_TRANSPARENCCY_BACKGROUND); // Keep transparency
function parseBackgroundColor(instructions){
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 {
img.background(DEFAULT_BACKGROUND_COLOR); // Change tranparency background to default background color
img.flatten(); // forces the image on its background to "commit" the background
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)
const backgroundColor = parseBackgroundColor(instructions);
img.background(backgroundColor);
img.flatten();
} else {
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"fmt": "png",
"pdfbgcolor": "#0EF"
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 49d52a1

Please sign in to comment.