From e5a39d134b6a18ea3b98be955f2edcc90eae2b3e Mon Sep 17 00:00:00 2001 From: MacDue Date: Fri, 17 Feb 2023 19:02:03 +0000 Subject: [PATCH] LibGfx: Move Color::mixed_with() inline This seems to give a small speedup to gradient painting and removes Color::mixed_with() (which was 10% of the time) from the profile. --- Userland/Libraries/LibGfx/Color.cpp | 24 ------------------------ Userland/Libraries/LibGfx/Color.h | 24 +++++++++++++++++++++++- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Userland/Libraries/LibGfx/Color.cpp b/Userland/Libraries/LibGfx/Color.cpp index b2c7256f59..b21b3bb4e0 100644 --- a/Userland/Libraries/LibGfx/Color.cpp +++ b/Userland/Libraries/LibGfx/Color.cpp @@ -315,30 +315,6 @@ Optional Color::from_string(StringView string) return Color(r.value(), g.value(), b.value(), a.value()); } -Color 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)), - }; - } - // 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); - auto premultiplied_mix_channel = [&](float channel, float other_channel, float weight) { - return round_to(mix(channel * alpha(), other_channel * other.alpha(), weight) / mixed_alpha); - }; - return Gfx::Color { - premultiplied_mix_channel(red(), other.red(), weight), - premultiplied_mix_channel(green(), other.green(), weight), - premultiplied_mix_channel(blue(), other.blue(), weight), - round_to(mixed_alpha), - }; -} - Vector Color::shades(u32 steps, float max) const { float shade = 1.f; diff --git a/Userland/Libraries/LibGfx/Color.h b/Userland/Libraries/LibGfx/Color.h index a135bb76aa..42626ab49a 100644 --- a/Userland/Libraries/LibGfx/Color.h +++ b/Userland/Libraries/LibGfx/Color.h @@ -237,7 +237,29 @@ public: #endif } - Color mixed_with(Color other, float weight) const; + 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)), + }; + } + // 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); + auto premultiplied_mix_channel = [&](float channel, float other_channel, float weight) { + return round_to(mix(channel * alpha(), other_channel * other.alpha(), weight) / mixed_alpha); + }; + return Gfx::Color { + premultiplied_mix_channel(red(), other.red(), weight), + premultiplied_mix_channel(green(), other.green(), weight), + premultiplied_mix_channel(blue(), other.blue(), weight), + round_to(mixed_alpha), + }; + } Color interpolate(Color other, float weight) const noexcept {