KiCad PCB EDA Suite
Loading...
Searching...
No Matches
render_utils.h File Reference
#include <tl/expected.hpp>
#include <wx/colour.h>
#include <wx/image.h>

Go to the source code of this file.

Functions

tl::expected< wxImage, std::string > CreateAlphaImageFromTwoRenders (const wxImage &aOnWhite, const wxImage &aOnBlack)
 Combine two opaque renders of the same content into a single image with an alpha channel.
 
void ConvertColourToAlphaInPlace (wxImage &aImage, const wxColour &aColour)
 Apply the "Color to Alpha" algorithm in place, converting aColour to transparent.
 

Function Documentation

◆ ConvertColourToAlphaInPlace()

void ConvertColourToAlphaInPlace ( wxImage & aImage,
const wxColour & aColour )

Apply the "Color to Alpha" algorithm in place, converting aColour to transparent.

Pixels matching aColour exactly become fully transparent; pixels further from it keep proportionally more opacity. An alpha channel is added if the image does not have one.

This is a port of the GEGL color-to-alpha operation used by GIMP; see the implementation file for the full attribution.

Parameters
aImagethe image to modify in place.
aColourthe colour to make transparent.

Definition at line 103 of file render_utils.cpp.

References EPSILON, and KiROUND().

Referenced by BOOST_AUTO_TEST_CASE(), and BITMAP_BASE::ConvertColourToAlpha().

◆ CreateAlphaImageFromTwoRenders()

tl::expected< wxImage, std::string > CreateAlphaImageFromTwoRenders ( const wxImage & aOnWhite,
const wxImage & aOnBlack )

Combine two opaque renders of the same content into a single image with an alpha channel.

Given two renders of the same content, one composited over a pure white background and one over a pure black background, the per-pixel alpha can be recovered from the difference between the renders, and the straight (un-premultiplied) colour from the black-background render:

B = ( c * alpha ) / 255
W = B + ( 255 - alpha )

where c is the straight colour and alpha the per-pixel alpha, both in the interval 0..255. Solving gives:

alpha = 255 - ( W - B )
c     = ( B * 255 ) / alpha

The returned image reproduces the same appearance over any background, which is useful where transparent output is needed from an opaque renderer (for example clipboard images).

Parameters
aOnWhiteis a render of the content over an opaque pure-white background.
aOnBlackis a render of the same content over an opaque pure-black background.
Returns
the combined image with an alpha channel, or an unexpected value containing an error message when the inputs are not valid images of the same size.

Definition at line 27 of file render_utils.cpp.

References result.

Referenced by BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), and BOOST_AUTO_TEST_CASE().