diff --git a/Software/PC_Application/Traces/Marker/marker.cpp b/Software/PC_Application/Traces/Marker/marker.cpp index a8d679f..1375563 100644 --- a/Software/PC_Application/Traces/Marker/marker.cpp +++ b/Software/PC_Application/Traces/Marker/marker.cpp @@ -619,8 +619,7 @@ void Marker::updateSymbol() p.setPen(traceColor); p.setBrush(traceColor); p.drawConvexPolygon(points, 3); - auto brightness = traceColor.redF() * 0.299 + traceColor.greenF() * 0.587 + traceColor.blueF() * 0.114; - p.setPen((brightness > 0.6) ? Qt::black : Qt::white); + p.setPen(Util::getFontColorFromBackground(traceColor)); p.drawText(QRectF(0,0,width, height*2.0/3.0), Qt::AlignCenter, QString::number(number) + suffix); } else { symbol = QPixmap(1,1); diff --git a/Software/PC_Application/Traces/traceplot.cpp b/Software/PC_Application/Traces/traceplot.cpp index 1d88cab..f0c18f8 100644 --- a/Software/PC_Application/Traces/traceplot.cpp +++ b/Software/PC_Application/Traces/traceplot.cpp @@ -7,6 +7,7 @@ #include #include "unit.h" #include "Marker/markermodel.h" +#include "Util/util.h" std::set TracePlot::plots; @@ -176,7 +177,7 @@ void TracePlot::paintEvent(QPaintEvent *event) path.addRoundedRect(usedLabelArea, borderRadius, borderRadius); p.fillPath(path, t.first->color()); p.drawPath(path); - p.setPen(QColor(Qt::white)); + p.setPen(Util::getFontColorFromBackground(t.first->color())); p.drawText(textArea, 0, label); p.setPen(t.first->color()); x += usedLabelArea.width()+labelMarginRight; @@ -206,7 +207,7 @@ void TracePlot::paintEvent(QPaintEvent *event) p.fillPath(pathM, t.first->color()); p.drawPath(pathM); - p.setPen(QColor(Qt::white)); + p.setPen(Util::getFontColorFromBackground(t.first->color())); p.drawText(textArea, 0, label); p.setPen(t.first->color()); p.drawText(textAreaConsumed.x()+textAreaConsumed.width(), textAreaConsumed.y(), textArea.width(), textArea.height(), 0, description); diff --git a/Software/PC_Application/Util/util.h b/Software/PC_Application/Util/util.h index 7a44343..9bd3044 100644 --- a/Software/PC_Application/Util/util.h +++ b/Software/PC_Application/Util/util.h @@ -5,6 +5,8 @@ #include #include +#include + namespace Util { template T Scale(T value, T from_low, T from_high, T to_low, T to_high) { value -= from_low; @@ -48,6 +50,11 @@ namespace Util { static inline double SparamToQualityFactor(std::complex d) { return abs(d.imag()) / d.real(); } + // attempts to return a font color with good contrast against the given background color + static inline QColor getFontColorFromBackground(QColor q) { + auto brightness = q.redF() * 0.299 + q.greenF() * 0.587 + q.blueF() * 0.114; + return brightness > 0.6 ? Qt::black : Qt::white; + } } #endif // UTILH_H