diff --git a/src/renderer/atlas/AtlasEngine.api.cpp b/src/renderer/atlas/AtlasEngine.api.cpp index f5ddc5a000d..6481d5b9059 100644 --- a/src/renderer/atlas/AtlasEngine.api.cpp +++ b/src/renderer/atlas/AtlasEngine.api.cpp @@ -432,8 +432,8 @@ void AtlasEngine::SetSelectionBackground(const COLORREF color) noexcept { auto misc = _api.s.write()->misc.write(); misc->selectionColor = selectionColor; - // Selection Foreground is based on the default foreground; it is also updated in UpdateDrawingBrushes - misc->selectionForeground = 0xff000000 | ColorFix::GetPerceivableColor(misc->foregroundColor, color, 0.5f * 0.5f); + // Select a black or white foreground based on the perceptual lightness of the background. + misc->selectionForeground = ColorFix::GetLuminosity(selectionColor) < 0.5f ? 0xffffffff : 0xff000000; } } diff --git a/src/renderer/atlas/AtlasEngine.cpp b/src/renderer/atlas/AtlasEngine.cpp index 6aa8bff265a..18e415a5996 100644 --- a/src/renderer/atlas/AtlasEngine.cpp +++ b/src/renderer/atlas/AtlasEngine.cpp @@ -658,10 +658,7 @@ try if (textAttributes.GetForeground().IsDefault() && fg != _api.s->misc->foregroundColor) { - auto misc = _api.s.write()->misc.write(); - misc->foregroundColor = fg; - // Selection Foreground is based on the default foreground; it is also updated in SetSelectionColor - misc->selectionForeground = 0xff000000 | ColorFix::GetPerceivableColor(fg, misc->selectionColor, 0.5f * 0.5f); + _api.s.write()->misc.write()->foregroundColor = fg; } } diff --git a/src/types/ColorFix.cpp b/src/types/ColorFix.cpp index eda47d396a2..7648de22622 100644 --- a/src/types/ColorFix.cpp +++ b/src/types/ColorFix.cpp @@ -209,4 +209,9 @@ COLORREF ColorFix::GetPerceivableColor(COLORREF color, COLORREF reference, float return linearToColorref(oklab::oklab_to_linear_srgb(colorOklab)) | (color & 0xff000000); } +float ColorFix::GetLuminosity(COLORREF color) noexcept +{ + return oklab::linear_srgb_to_oklab(colorrefToLinear(color)).l; +} + TIL_FAST_MATH_END diff --git a/src/types/inc/ColorFix.hpp b/src/types/inc/ColorFix.hpp index 02bdc0631b3..24c4cf79f3f 100644 --- a/src/types/inc/ColorFix.hpp +++ b/src/types/inc/ColorFix.hpp @@ -10,4 +10,5 @@ namespace ColorFix { COLORREF GetPerceivableColor(COLORREF color, COLORREF reference, float minSquaredDistance) noexcept; + float GetLuminosity(COLORREF color) noexcept; }