From bff2f53996e867ae1059ef8d9107bb7ea5879b31 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Fri, 10 Mar 2023 08:35:18 -0800 Subject: [PATCH] Correct alpha blending in PDF plot We fake the alpha channel by pre-multiplying but we forgot to account for the downscaling of the input channel value in addition to correcting the alpha * white. This led to clipping when the values overflowed Fixes https://gitlab.com/kicad/code/kicad/issues/14238 (cherry picked from commit d405fff1d8472d9ce23e0333f2c32f158287a7a1) --- common/plotters/PDF_plotter.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/common/plotters/PDF_plotter.cpp b/common/plotters/PDF_plotter.cpp index f41e64cfcc..e4349986fd 100644 --- a/common/plotters/PDF_plotter.cpp +++ b/common/plotters/PDF_plotter.cpp @@ -513,10 +513,11 @@ void PDF_PLOTTER::PlotImage( const wxImage& aImage, const VECTOR2I& aPos, double if( alpha < 0xFF ) { - float a = 1.0 - ( (float) alpha / 255.0 ); - r = ( int )( r + ( a * 0xFF ) ) & 0xFF; - g = ( int )( g + ( a * 0xFF ) ) & 0xFF; - b = ( int )( b + ( a * 0xFF ) ) & 0xFF; + float d = alpha / 255.0; + float a = 1.0 - d; + r = std::min( KiROUND( r * d + ( a * 0xFF ) ), 0xFF ); + g = std::min( KiROUND( g * d + ( a * 0xFF ) ), 0xFF ); + b = std::min( KiROUND( b * d + ( a * 0xFF ) ), 0xFF ); } }