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

Blend square background color with focus/selection colors. #203

Merged
merged 1 commit into from
Sep 28, 2023
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
2 changes: 1 addition & 1 deletion src/XGridCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ XGridCtrl::DrawSquare(wxDC & dc, const puz::Square & square, const wxColour & co
if (color == wxNullColour || &color == &EraseColor)
m_drawer.DrawSquare(dc, square);
else
m_drawer.DrawSquare(dc, square, color, GetPenColor());
m_drawer.DrawSquare(dc, square, color, GetPenColor(), /* blendBackground= */ true);

// Reset the drawing mode
if (drawOutline)
Expand Down
29 changes: 26 additions & 3 deletions src/XGridDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,23 @@ XGridDrawer::DrawSquare(wxDC & dc, const puz::Square & square)
DrawSquare(dc, square, GetSquareColor(square), GetPenColor());
}

// Blend the foreground color and the background color.
wxColour
blendColors(const wxColour& foreground, const wxColour& background) {
// We average the background and foreground colors, but we darken the background
// color somewhat to improve the contrast with other selected squares.
return wxColour(
(std::max(background.Red() - 50, 0) + foreground.Red()) / 2,
(std::max(background.Green() - 50, 0) + foreground.Green()) / 2,
(std::max(background.Blue() - 50, 0) + foreground.Blue()) / 2);
}

void
XGridDrawer::DrawSquare(wxDC & adc,
const puz::Square & square,
const wxColour & bgColor,
const wxColour & textColor_)
const puz::Square & square,
const wxColour & bgColor_,
const wxColour & textColor_,
const bool blendBackground)
{
if (square.IsMissing())
return;
Expand All @@ -433,6 +445,17 @@ XGridDrawer::DrawSquare(wxDC & adc,
// the default square background, and then draw the outline using the
// bgColor.

// If the caller wants us to blend the square's inherent color with the given color (e.g. this
// is a selected/highlighted square), do so as long as the given color is non-white. This keeps
// the standard highlight color consistent with the user selection most of the time, while
// providing a visible contrast for selected cells with different backgrounds against other
// selected cells.
wxColour bgColor = bgColor_;
wxColour squareColor = GetSquareColor(square);
if (blendBackground && squareColor != GetWhiteSquareColor()) {
bgColor = blendColors(bgColor, squareColor);
}

// Check the square background to see if this text color works
wxColour textColor = textColor_;
// If the user chose these colors, respect them
Expand Down
3 changes: 2 additions & 1 deletion src/XGridDrawer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ class XGridDrawer

void DrawSquare(wxDC & dc, const puz::Square & square,
const wxColour & bgColor,
const wxColour & textColor);
const wxColour & textColor,
const bool blendBackground = false);

void DrawSquare(wxDC & dc, const puz::Square & square);

Expand Down