Browse Source

LibGfx: Add Color::contrast_ratio()

MacDue 3 years ago
parent
commit
73b05364e8
1 changed files with 9 additions and 0 deletions
  1. 9 0
      Userland/Libraries/LibGfx/Color.h

+ 9 - 0
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();