From 1cbab1c7e668413ae7969b47f032d833cc025f39 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Thu, 2 Sep 2021 17:51:56 -0400 Subject: [PATCH] LibGfx/Color: Add `suggested_foreground_color()` method This allows you to ask the color to recommend either white/black as a contrasting text color if you were using the color as the background. This uses the previously added luminosity() method. --- Userland/Libraries/LibGfx/Color.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Userland/Libraries/LibGfx/Color.h b/Userland/Libraries/LibGfx/Color.h index ba9d52feec..d3da99071d 100644 --- a/Userland/Libraries/LibGfx/Color.h +++ b/Userland/Libraries/LibGfx/Color.h @@ -392,6 +392,11 @@ public: return Color(out_r, out_g, out_b); } + constexpr Color suggested_foreground_color() const + { + return luminosity() < 128 ? Color::White : Color::Black; + } + private: constexpr explicit Color(RGBA32 rgba) : m_value(rgba)