diff --git a/src/CalcViewModel/Common/Utils.cpp b/src/CalcViewModel/Common/Utils.cpp index d87e7892..220c9bf7 100644 --- a/src/CalcViewModel/Common/Utils.cpp +++ b/src/CalcViewModel/Common/Utils.cpp @@ -24,6 +24,7 @@ using namespace Windows::UI; using namespace Windows::UI::Core; using namespace Windows::UI::ViewManagement; using namespace Windows::UI::Xaml; +using namespace Windows::UI::Xaml::Media; using namespace Windows::Foundation; using namespace Windows::Storage; @@ -302,3 +303,18 @@ bool operator!=(const Color& color1, const Color& color2) { return !(color1 == color2); } + +// This method calculates the luminance ratio between White and the given background color. +// The luminance is calculate using the RGB values and does not use the A value. +// White or Black is returned +SolidColorBrush ^ Utils::GetContrastColor(Color backgroundColor) +{ + auto luminance = 0.2126 * backgroundColor.R + 0.7152 * backgroundColor.G + 0.0722 * backgroundColor.B; + + if ((255 + 0.05) / (luminance + 0.05) >= 2.5) + { + return static_cast(Application::Current->Resources->Lookup(L"WhiteBrush")); + } + + return static_cast(Application::Current->Resources->Lookup(L"BlackBrush")); +} diff --git a/src/CalcViewModel/Common/Utils.h b/src/CalcViewModel/Common/Utils.h index 1f24b23e..ed798ad9 100644 --- a/src/CalcViewModel/Common/Utils.h +++ b/src/CalcViewModel/Common/Utils.h @@ -409,6 +409,7 @@ namespace Utils Platform::String ^ EscapeHtmlSpecialCharacters(Platform::String ^ originalString, std::shared_ptr> specialCharacters = nullptr); + Windows::UI::Xaml::Media::SolidColorBrush ^ GetContrastColor(Windows::UI::Color backgroundColor); } // This goes into the header to define the property, in the public: section of the class diff --git a/src/Calculator/App.xaml b/src/Calculator/App.xaml index 46739c13..fcb66b5f 100644 --- a/src/Calculator/App.xaml +++ b/src/Calculator/App.xaml @@ -293,6 +293,10 @@ 14 16 + + + +