Skip to content

Commit 1cb3445

Browse files
authored
Force selection FG to black or white depending on BG luminance (#17753)
1 parent 516ade5 commit 1cb3445

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

src/renderer/atlas/AtlasEngine.api.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,8 @@ void AtlasEngine::SetSelectionBackground(const COLORREF color) noexcept
432432
{
433433
auto misc = _api.s.write()->misc.write();
434434
misc->selectionColor = selectionColor;
435-
// Selection Foreground is based on the default foreground; it is also updated in UpdateDrawingBrushes
436-
misc->selectionForeground = 0xff000000 | ColorFix::GetPerceivableColor(misc->foregroundColor, color, 0.5f * 0.5f);
435+
// Select a black or white foreground based on the perceptual lightness of the background.
436+
misc->selectionForeground = ColorFix::GetLuminosity(selectionColor) < 0.5f ? 0xffffffff : 0xff000000;
437437
}
438438
}
439439

src/renderer/atlas/AtlasEngine.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,7 @@ try
658658

659659
if (textAttributes.GetForeground().IsDefault() && fg != _api.s->misc->foregroundColor)
660660
{
661-
auto misc = _api.s.write()->misc.write();
662-
misc->foregroundColor = fg;
663-
// Selection Foreground is based on the default foreground; it is also updated in SetSelectionColor
664-
misc->selectionForeground = 0xff000000 | ColorFix::GetPerceivableColor(fg, misc->selectionColor, 0.5f * 0.5f);
661+
_api.s.write()->misc.write()->foregroundColor = fg;
665662
}
666663
}
667664

src/types/ColorFix.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,9 @@ COLORREF ColorFix::GetPerceivableColor(COLORREF color, COLORREF reference, float
209209
return linearToColorref(oklab::oklab_to_linear_srgb(colorOklab)) | (color & 0xff000000);
210210
}
211211

212+
float ColorFix::GetLuminosity(COLORREF color) noexcept
213+
{
214+
return oklab::linear_srgb_to_oklab(colorrefToLinear(color)).l;
215+
}
216+
212217
TIL_FAST_MATH_END

src/types/inc/ColorFix.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
namespace ColorFix
1111
{
1212
COLORREF GetPerceivableColor(COLORREF color, COLORREF reference, float minSquaredDistance) noexcept;
13+
float GetLuminosity(COLORREF color) noexcept;
1314
}

0 commit comments

Comments
 (0)