From 3cc074c1b0a9fd7af5911eb9387699c15cf71ce7 Mon Sep 17 00:00:00 2001 From: MacDue Date: Fri, 17 Feb 2023 19:03:40 +0000 Subject: [PATCH] LibGfx: De-duplicate color interpolation code --- Userland/Libraries/LibGfx/Color.h | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/Userland/Libraries/LibGfx/Color.h b/Userland/Libraries/LibGfx/Color.h index 42626ab49a..afa6a4873a 100644 --- a/Userland/Libraries/LibGfx/Color.h +++ b/Userland/Libraries/LibGfx/Color.h @@ -239,14 +239,8 @@ public: Color mixed_with(Color other, float weight) const { - if (alpha() == other.alpha() || with_alpha(0) == other.with_alpha(0)) { - return Gfx::Color { - round_to(mix(red(), other.red(), weight)), - round_to(mix(green(), other.green(), weight)), - round_to(mix(blue(), other.blue(), weight)), - round_to(mix(alpha(), other.alpha(), weight)), - }; - } + if (alpha() == other.alpha() || with_alpha(0) == other.with_alpha(0)) + return interpolate(other, weight); // Fallback to slower, but more visually pleasing premultiplied alpha mix. // This is needed for linear-gradient()s in LibWeb. auto mixed_alpha = mix(alpha(), other.alpha(), weight); @@ -263,11 +257,12 @@ public: Color interpolate(Color other, float weight) const noexcept { - u8 r = red() + round_to(static_cast(other.red() - red()) * weight); - u8 g = green() + round_to(static_cast(other.green() - green()) * weight); - u8 b = blue() + round_to(static_cast(other.blue() - blue()) * weight); - u8 a = alpha() + round_to(static_cast(other.alpha() - alpha()) * weight); - return Color(r, g, b, a); + return Gfx::Color { + round_to(mix(red(), other.red(), weight)), + round_to(mix(green(), other.green(), weight)), + round_to(mix(blue(), other.blue(), weight)), + round_to(mix(alpha(), other.alpha(), weight)), + }; } constexpr Color multiply(Color other) const