Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading