From 2aa37c2e404825aa6706945859991e921d1c53cb Mon Sep 17 00:00:00 2001 From: Jennifer Taylor Date: Sat, 22 May 2021 23:24:51 +0000 Subject: [PATCH] Fix additive blending on semi-transparent backgrounds. --- bemani/format/afp/blend.py | 12 ++++++------ bemani/format/afp/blendaltimpl.cxx | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bemani/format/afp/blend.py b/bemani/format/afp/blend.py index 6704182..019c198 100644 --- a/bemani/format/afp/blend.py +++ b/bemani/format/afp/blend.py @@ -35,12 +35,12 @@ except ImportError: # Calculate alpha blending. srcpercent = src[3] / 255.0 destpercent = dest[3] / 255.0 - srcremaineder = 1.0 - srcpercent - new_alpha = (srcpercent + destpercent * srcremaineder) + srcremainder = 1.0 - srcpercent + new_alpha = max(min(0.0, srcpercent + destpercent * srcremainder), 1.0) return ( - clamp(((dest[0] * destpercent * srcremaineder) + (src[0] * srcpercent)) / new_alpha), - clamp(((dest[1] * destpercent * srcremaineder) + (src[1] * srcpercent)) / new_alpha), - clamp(((dest[2] * destpercent * srcremaineder) + (src[2] * srcpercent)) / new_alpha), + clamp(((dest[0] * destpercent * srcremainder) + (src[0] * srcpercent)) / new_alpha), + clamp(((dest[1] * destpercent * srcremainder) + (src[1] * srcpercent)) / new_alpha), + clamp(((dest[2] * destpercent * srcremainder) + (src[2] * srcpercent)) / new_alpha), clamp(255 * new_alpha) ) @@ -64,7 +64,7 @@ except ImportError: clamp(dest[0] + (src[0] * srcpercent)), clamp(dest[1] + (src[1] * srcpercent)), clamp(dest[2] + (src[2] * srcpercent)), - dest[3], + clamp(dest[3] + (255 * srcpercent)), ) def blend_subtraction( diff --git a/bemani/format/afp/blendaltimpl.cxx b/bemani/format/afp/blendaltimpl.cxx index a4a6eeb..6533df7 100644 --- a/bemani/format/afp/blendaltimpl.cxx +++ b/bemani/format/afp/blendaltimpl.cxx @@ -92,12 +92,12 @@ extern "C" // Calculate alpha blending. float srcpercent = src.a / 255.0; float destpercent = dest.a / 255.0; - float srcremaineder = 1.0 - srcpercent; - float new_alpha = (srcpercent + destpercent * srcremaineder); + float srcremainder = 1.0 - srcpercent; + float new_alpha = fmin(fmax(0.0, srcpercent + destpercent * srcremainder), 1.0); return (intcolor_t){ - clamp(((dest.r * destpercent * srcremaineder) + (src.r * srcpercent)) / new_alpha), - clamp(((dest.g * destpercent * srcremaineder) + (src.g * srcpercent)) / new_alpha), - clamp(((dest.b * destpercent * srcremaineder) + (src.b * srcpercent)) / new_alpha), + clamp(((dest.r * destpercent * srcremainder) + (src.r * srcpercent)) / new_alpha), + clamp(((dest.g * destpercent * srcremainder) + (src.g * srcpercent)) / new_alpha), + clamp(((dest.b * destpercent * srcremainder) + (src.b * srcpercent)) / new_alpha), clamp(255 * new_alpha) }; } @@ -121,7 +121,7 @@ extern "C" clamp(dest.r + (src.r * srcpercent)), clamp(dest.g + (src.g * srcpercent)), clamp(dest.b + (src.b * srcpercent)), - dest.a, + clamp(dest.a + (255 * srcpercent)), }; }