From 73b05364e84d8ab27fa9c3843e64d36f59fc0e1e Mon Sep 17 00:00:00 2001 From: MacDue Date: Mon, 9 May 2022 00:07:24 +0100 Subject: [PATCH] LibGfx: Add Color::contrast_ratio() --- Userland/Libraries/LibGfx/Color.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Userland/Libraries/LibGfx/Color.h b/Userland/Libraries/LibGfx/Color.h index 67a18b9b5a..7e4507107c 100644 --- a/Userland/Libraries/LibGfx/Color.h +++ b/Userland/Libraries/LibGfx/Color.h @@ -221,6 +221,15 @@ public: return (red() * 0.2126f + green() * 0.7152f + blue() * 0.0722f); } + constexpr float contrast_ratio(Color const& other) + { + auto l1 = luminosity(); + auto l2 = other.luminosity(); + auto darkest = min(l1, l2) / 255.; + auto brightest = max(l1, l2) / 255.; + return (brightest + 0.05) / (darkest + 0.05); + } + constexpr Color to_grayscale() const { auto gray = luminosity();