Skip to content

Commit

Permalink
Fix title color contrasting when titles have a background color
Browse files Browse the repository at this point in the history
If label has its own background color, then don't bother looking at the graph or canvas's color.
  • Loading branch information
Blake-Madden committed Jan 2, 2025
1 parent 53bc806 commit 4cb9472
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 87 deletions.
123 changes: 40 additions & 83 deletions src/base/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ wxDEFINE_EVENT(wxEVT_WISTERIA_CANVAS_DCLICK, wxCommandEvent);

wxIMPLEMENT_DYNAMIC_CLASS(Wisteria::Canvas, wxScrolledWindow)

using namespace Wisteria::GraphItems;
using namespace Wisteria::GraphItems;
using namespace Wisteria::Colors;
using namespace Wisteria::UI;

Expand Down Expand Up @@ -447,8 +447,8 @@ namespace Wisteria
Canvas::Canvas(wxWindow* parent, int itemId, const wxPoint& pos, const wxSize& size,
const long flags)
: wxScrolledWindow(parent, itemId, pos, size,
flags | wxBORDER_NONE | wxVSCROLL | wxHSCROLL |
wxALWAYS_SHOW_SB | wxFULL_REPAINT_ON_RESIZE)
flags | wxBORDER_NONE | wxVSCROLL | wxHSCROLL | wxALWAYS_SHOW_SB |
wxFULL_REPAINT_ON_RESIZE)
{
m_watermarkFont.MakeBold();
SetCanvasMinWidthDIPs(GetDefaultCanvasWidthDIPs());
Expand All @@ -467,14 +467,11 @@ namespace Wisteria

m_resizeTimer.SetOwner(this);

Bind(
wxEVT_MENU, [this]([[maybe_unused]] wxCommandEvent&) { ZoomIn(); }, wxID_ZOOM_IN);
Bind(wxEVT_MENU, [this]([[maybe_unused]] wxCommandEvent&) { ZoomIn(); }, wxID_ZOOM_IN);

Bind(
wxEVT_MENU, [this]([[maybe_unused]] wxCommandEvent&) { ZoomOut(); }, wxID_ZOOM_OUT);
Bind(wxEVT_MENU, [this]([[maybe_unused]] wxCommandEvent&) { ZoomOut(); }, wxID_ZOOM_OUT);

Bind(
wxEVT_MENU, [this]([[maybe_unused]] wxCommandEvent&) { ZoomReset(); }, wxID_ZOOM_FIT);
Bind(wxEVT_MENU, [this]([[maybe_unused]] wxCommandEvent&) { ZoomReset(); }, wxID_ZOOM_FIT);

Bind(wxEVT_TIMER,
[this]([[maybe_unused]] wxTimerEvent&)
Expand All @@ -484,7 +481,7 @@ namespace Wisteria
OnResize(event);
Refresh();
Update();
});
});

Bind(wxEVT_KEY_DOWN, &Canvas::OnKeyDown, this);
Bind(wxEVT_PAINT, &Canvas::OnPaint, this);
Expand Down Expand Up @@ -518,10 +515,38 @@ namespace Wisteria
}

//----------------------------------------------------------------
long Canvas::CalcLeftTitles(wxDC& dc, const long spacingWidth)
void Canvas::ContrastTitleLabel(GraphItems::Label& title)
{
const wxColour contrastingColor{ Wisteria::Colors::ColorContrast::BlackOrWhiteContrast(
GetBackgroundColor()) };
if (title.GetFontBackgroundColor().IsOk() &&
title.GetFontBackgroundColor().GetAlpha() != wxALPHA_TRANSPARENT)
{
if (title.GetFontBackgroundColor() == GetBackgroundColor())
{
title.SetFontBackgroundColor(contrastingColor);
}
}
else if (title.GetFontColor().IsOk() &&
title.GetFontColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetFontColor() == GetBackgroundColor())
{
title.SetFontColor(contrastingColor);
}
if (title.GetHeaderInfo().IsEnabled() && title.GetHeaderInfo().GetFontColor().IsOk() &&
title.GetHeaderInfo().GetFontColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetHeaderInfo().GetFontColor() == GetBackgroundColor() &&
// if a font background color is valid, then don't adjust the font color
!(title.GetFontBackgroundColor().IsOk() &&
title.GetFontBackgroundColor().GetAlpha() != wxALPHA_TRANSPARENT))
{
title.GetHeaderInfo().FontColor(contrastingColor);
}
}

//----------------------------------------------------------------
long Canvas::CalcLeftTitles(wxDC& dc, const long spacingWidth)
{
long leftMarginWidth{ 0 };
// add the left titles
for (auto& title : m_leftTitles)
Expand All @@ -542,24 +567,7 @@ namespace Wisteria
leftMarginWidth += title.GetBoundingBox(dc).GetWidth() + spacingWidth;
// contrast the title if its font color (or background color, if in use)
// is the same as the canvas background
if (title.GetFontBackgroundColor().IsOk() &&
title.GetFontBackgroundColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetFontBackgroundColor() == GetBackgroundColor())
{
title.SetFontBackgroundColor(contrastingColor);
}
else if (title.GetFontColor().IsOk() &&
title.GetFontColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetFontColor() == GetBackgroundColor())
{
title.SetFontColor(contrastingColor);
}
if (title.GetHeaderInfo().IsEnabled() && title.GetHeaderInfo().GetFontColor().IsOk() &&
title.GetHeaderInfo().GetFontColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetHeaderInfo().GetFontColor() == GetBackgroundColor())
{
title.GetHeaderInfo().FontColor(contrastingColor);
}
ContrastTitleLabel(title);
GetTitles().push_back(std::make_unique<GraphItems::Label>(title));
}
return leftMarginWidth;
Expand Down Expand Up @@ -590,24 +598,7 @@ namespace Wisteria
PageVerticalAlignment::BottomAligned);
position -= title.GetBoundingBox(dc).GetWidth() + spacingWidth;
rightMarginWidth += title.GetBoundingBox(dc).GetWidth() + spacingWidth;
if (title.GetFontBackgroundColor().IsOk() &&
title.GetFontBackgroundColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetFontBackgroundColor() == GetBackgroundColor())
{
title.SetFontBackgroundColor(contrastingColor);
}
else if (title.GetFontColor().IsOk() &&
title.GetFontColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetFontColor() == GetBackgroundColor())
{
title.SetFontColor(contrastingColor);
}
if (title.GetHeaderInfo().IsEnabled() && title.GetHeaderInfo().GetFontColor().IsOk() &&
title.GetHeaderInfo().GetFontColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetHeaderInfo().GetFontColor() == GetBackgroundColor())
{
title.GetHeaderInfo().FontColor(contrastingColor);
}
ContrastTitleLabel(title);
GetTitles().push_back(std::make_unique<GraphItems::Label>(title));
}
return rightMarginWidth;
Expand Down Expand Up @@ -635,24 +626,7 @@ namespace Wisteria
PageHorizontalAlignment::RightAligned :
PageHorizontalAlignment::LeftAligned);
topMarginHeight += title.GetBoundingBox(dc).GetHeight() + spacingWidth;
if (title.GetFontBackgroundColor().IsOk() &&
title.GetFontBackgroundColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetFontBackgroundColor() == GetBackgroundColor())
{
title.SetFontBackgroundColor(contrastingColor);
}
else if (title.GetFontColor().IsOk() &&
title.GetFontColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetFontColor() == GetBackgroundColor())
{
title.SetFontColor(contrastingColor);
}
if (title.GetHeaderInfo().IsEnabled() && title.GetHeaderInfo().GetFontColor().IsOk() &&
title.GetHeaderInfo().GetFontColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetHeaderInfo().GetFontColor() == GetBackgroundColor())
{
title.GetHeaderInfo().FontColor(contrastingColor);
}
ContrastTitleLabel(title);
GetTitles().push_back(std::make_unique<GraphItems::Label>(title));
}
return topMarginHeight;
Expand Down Expand Up @@ -682,24 +656,7 @@ namespace Wisteria
PageHorizontalAlignment::LeftAligned);
position -= title.GetBoundingBox(dc).GetHeight() + spacingWidth;
bottomMarginHeight += title.GetBoundingBox(dc).GetHeight() + spacingWidth;
if (title.GetFontBackgroundColor().IsOk() &&
title.GetFontBackgroundColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetFontBackgroundColor() == GetBackgroundColor())
{
title.SetFontBackgroundColor(contrastingColor);
}
else if (title.GetFontColor().IsOk() &&
title.GetFontColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetFontColor() == GetBackgroundColor())
{
title.SetFontColor(contrastingColor);
}
if (title.GetHeaderInfo().IsEnabled() && title.GetHeaderInfo().GetFontColor().IsOk() &&
title.GetHeaderInfo().GetFontColor().GetAlpha() != wxALPHA_TRANSPARENT &&
title.GetHeaderInfo().GetFontColor() == GetBackgroundColor())
{
title.GetHeaderInfo().FontColor(contrastingColor);
}
ContrastTitleLabel(title);
GetTitles().push_back(std::make_unique<GraphItems::Label>(title));
}
return bottomMarginHeight;
Expand Down
4 changes: 4 additions & 0 deletions src/base/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,10 @@ namespace Wisteria
[[nodiscard]]
long CalcBottomTitles(wxDC& dc, const long spacingWidth);

/** @brief Contrasts a title label against the canvas.
@param[in,out] title The title lable to contrast.*/
void ContrastTitleLabel(GraphItems::Label& title);

/// @returns The top-level floating (i.e., not anchored) object on the
/// canvas located at @c pt.
/// @param pt The point to look at.
Expand Down
13 changes: 9 additions & 4 deletions src/graphs/graph2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ namespace Wisteria::Graphs
// contrast a label if its font color (or background color, if in use)
// is the same as the background
if (label.GetFontBackgroundColor().IsOk() &&
label.GetFontBackgroundColor().GetAlpha() != wxALPHA_TRANSPARENT &&
label.GetFontBackgroundColor() == GetPlotOrCanvasColor())
label.GetFontBackgroundColor().GetAlpha() != wxALPHA_TRANSPARENT)
{
label.SetFontBackgroundColor(contrastingColor);
if (label.GetFontBackgroundColor() == GetPlotOrCanvasColor())
{
label.SetFontBackgroundColor(contrastingColor);
}
}
else if (label.GetFontColor().IsOk() &&
label.GetFontColor().GetAlpha() != wxALPHA_TRANSPARENT &&
Expand All @@ -50,7 +52,10 @@ namespace Wisteria::Graphs

if (label.GetHeaderInfo().IsEnabled() && label.GetHeaderInfo().GetFontColor().IsOk() &&
label.GetHeaderInfo().GetFontColor().GetAlpha() != wxALPHA_TRANSPARENT &&
label.GetHeaderInfo().GetFontColor() == GetPlotOrCanvasColor())
label.GetHeaderInfo().GetFontColor() == GetPlotOrCanvasColor() &&
// if a font background color is valid, then don't adjust the font color
!(label.GetFontBackgroundColor().IsOk() &&
label.GetFontBackgroundColor().GetAlpha() != wxALPHA_TRANSPARENT))
{
label.GetHeaderInfo().FontColor(contrastingColor);
}
Expand Down

0 comments on commit 4cb9472

Please sign in to comment.