Skip to content

Commit

Permalink
Merge pull request #203 from jpd236/bgcolor-highlight
Browse files Browse the repository at this point in the history
Blend square background color with focus/selection colors.
  • Loading branch information
mrichards42 authored Sep 28, 2023
2 parents 319fe9b + 95ed965 commit ce16297
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
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

0 comments on commit ce16297

Please sign in to comment.