Commit 614f7572 authored by Allen Winter's avatar Allen Winter
Browse files

Month View: background color computation for dark mode theme

BUG: 413521
FIXED-IN: 5.13.0

Differential Revision: https://phabricator.kde.org/D25262
parent f99e855a
...@@ -31,10 +31,15 @@ ...@@ -31,10 +31,15 @@
#include <QIcon> #include <QIcon>
#include <QPixmap> #include <QPixmap>
QColor EventViews::getTextColor(const QColor &c) bool EventViews::isColorDark(const QColor &c)
{ {
double luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114); double luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114);
return (luminance > 128.0) ? QColor(0, 0, 0) : QColor(255, 255, 255); return (luminance < 128.0) ? true : false;
}
QColor EventViews::getTextColor(const QColor &c)
{
return (!isColorDark(c)) ? QColor(0, 0, 0) : QColor(255, 255, 255);
} }
void EventViews::setResourceColor(const Akonadi::Collection &coll, const QColor &color, const PrefsPtr &preferences) void EventViews::setResourceColor(const Akonadi::Collection &coll, const QColor &color, const PrefsPtr &preferences)
......
...@@ -46,6 +46,15 @@ typedef QSharedPointer<Prefs> PrefsPtr; ...@@ -46,6 +46,15 @@ typedef QSharedPointer<Prefs> PrefsPtr;
*/ */
Q_REQUIRED_RESULT QColor getTextColor(const QColor &c); Q_REQUIRED_RESULT QColor getTextColor(const QColor &c);
/**
* Determines if the @p color is "dark" or "light" by looking at its luminance.
* idea taken from:
* https://stackoverflow.com/questions/9780632/how-do-i-determine-if-a-color-is-closer-to-white-or-black
*
* @return true if the specified color is closer to black than white.
*/
Q_REQUIRED_RESULT bool isColorDark(const QColor &color);
/** /**
This method returns the proper resource / subresource color for the view. This method returns the proper resource / subresource color for the view.
If a value is stored in the preferences, we use it, else we try to find a If a value is stored in the preferences, we use it, else we try to find a
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "monthgraphicsitems.h" #include "monthgraphicsitems.h"
#include "monthitem.h" #include "monthitem.h"
#include "monthview.h" #include "monthview.h"
#include "helper.h"
#include "prefs.h" #include "prefs.h"
#include <CalendarSupport/Utils> #include <CalendarSupport/Utils>
...@@ -257,11 +258,12 @@ void MonthGraphicsView::drawBackground(QPainter *p, const QRectF &rect) ...@@ -257,11 +258,12 @@ void MonthGraphicsView::drawBackground(QPainter *p, const QRectF &rect)
color = palette().color(QPalette::AlternateBase); color = palette().color(QPalette::AlternateBase);
} }
} }
const bool usingDark = EventViews::isColorDark(color);
if (cell == mScene->selectedCell()) { if (cell == mScene->selectedCell()) {
color = color.darker(115); color = (usingDark) ? color.lighter(150) : color.darker(115);
} }
if (cell->date() == QDate::currentDate()) { if (cell->date() == QDate::currentDate()) {
color = color.darker(140); color = (usingDark) ? color.lighter(200) : color.darker(140);
} }
// Draw cell // Draw cell
...@@ -286,7 +288,8 @@ void MonthGraphicsView::drawBackground(QPainter *p, const QRectF &rect) ...@@ -286,7 +288,8 @@ void MonthGraphicsView::drawBackground(QPainter *p, const QRectF &rect)
QLinearGradient bgGradient(QPointF(cellHeaderX, cellHeaderY), QLinearGradient bgGradient(QPointF(cellHeaderX, cellHeaderY),
QPointF(cellHeaderX + cellHeaderWidth, QPointF(cellHeaderX + cellHeaderWidth,
cellHeaderY + cellHeaderHeight)); cellHeaderY + cellHeaderHeight));
if ((color.blue() + color.red() + color.green()) > (256 / 2 * 3)) { // Compute color of grid lines based on dark/lightness
if (!usingDark) {
p->setBrush(color.darker(110)); p->setBrush(color.darker(110));
} else { } else {
p->setBrush(color.lighter(140)); p->setBrush(color.lighter(140));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment