Skip to content

Commit

Permalink
Force selection FG to black or white depending on BG luminance (#17753)
Browse files Browse the repository at this point in the history
  • Loading branch information
DHowett authored Aug 21, 2024
1 parent 516ade5 commit 1cb3445
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/renderer/atlas/AtlasEngine.api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/renderer/atlas/AtlasEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/types/ColorFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/types/inc/ColorFix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
namespace ColorFix
{
COLORREF GetPerceivableColor(COLORREF color, COLORREF reference, float minSquaredDistance) noexcept;
float GetLuminosity(COLORREF color) noexcept;
}

0 comments on commit 1cb3445

Please sign in to comment.