From 3055a12178e9a3042752ca3aff839d4a4f22d6d3 Mon Sep 17 00:00:00 2001 From: Stephanie Anderl <46726333+sanderl@users.noreply.github.com> Date: Tue, 5 May 2020 10:20:05 -0700 Subject: [PATCH] Updated EquationButton contrast fixed dark mode foreground color bugs (#1155) * Fixed contrast between background and foreground colors in the equation button. Fixed the issue where the text in the equation text box is white when the background is white * Adjust the foreground color algorithm * moved the contrast method to utils so that it is resuable * Moved brushes for the GetContrastColor method to the app.xaml resource dictionary * Removed the change for the edit box colors, so it can be in a different PR --- src/CalcViewModel/Common/Utils.cpp | 16 ++++++++++++++++ src/CalcViewModel/Common/Utils.h | 1 + src/Calculator/App.xaml | 4 ++++ src/Calculator/Controls/EquationTextBox.cpp | 2 ++ src/Calculator/Controls/EquationTextBox.h | 1 + .../GraphingCalculator/EquationInputArea.xaml | 14 ++++++++------ .../EquationInputArea.xaml.cpp | 5 +++++ .../GraphingCalculator/EquationInputArea.xaml.h | 2 ++ 8 files changed, 39 insertions(+), 6 deletions(-) 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 + + + +