Force selection FG to black or white depending on BG luminance (#17753)

This commit is contained in:
Dustin L. Howett 2024-08-21 07:50:04 -05:00 committed by GitHub
parent 516ade54cb
commit 1cb3445834
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 9 additions and 6 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -10,4 +10,5 @@
namespace ColorFix
{
COLORREF GetPerceivableColor(COLORREF color, COLORREF reference, float minSquaredDistance) noexcept;
float GetLuminosity(COLORREF color) noexcept;
}