Several customers have asked for the ability to create PNG previews while preserving the transparency of the background. By default, Mako treats empty background of the document as white.

For example, the sample on the left, as displayed in Acrobat with the transparency grid switched on, renders to an output (shown right) with a solid white background.

Original
Mako's default rendering

Sample code

Using Mako sample code, located at the Mako site on GitHub, it's possible to produce the desired result, as Mako's PNG encoder will include an alpha channel if it's present.

The sample achieves this by rendering the content twice, once with no background (effectively white) and a second time against black. It then creates a new bitmap with an alpha channel. The value for this is inferred from the relative lightness of the two results, derived by converting RGB to HSL. The formula for conversion came from this source: https://www.rapidtables.com/convert/color/rgb-to-hsl.html

Example of default Mako output:

Output from MakoAlphaChan (as seen in Photoshop)


Caveats

This sample works only with RGB. Examples with more complex transparency have not been tested, but as this code is leveraging Mako's RIP which should be able to resolve such cases, it is reasonable to expect a good result.

We encourage Mako developers to adapt it as they see fit. If you can improve the code, please comment on the GitHub page.

An obvious disadvantage of the approach the sample code takes is that it requires two rendering passes, as well as post-processing to produce the result. An obvious improvement would be to add this as a core capability to Mako's RIP. With this in mind a development task has been added to Mako's backlog.