diff --git a/src/agenda/agenda.cpp b/src/agenda/agenda.cpp --- a/src/agenda/agenda.cpp +++ b/src/agenda/agenda.cpp @@ -1520,6 +1520,22 @@ drawContents(&p, 0, -y(), d->mGridSpacingX * d->mColumns, d->mGridSpacingY * d->mRows + y()); } +/* compute black or white contrast color +see https://stackoverflow.com/questions/1855884/determine-font-color-based-on-background +-color +*/ +static QColor contrastColor(const QColor &color) +{ + // Counting the perceptive luminance - human eye favors green color... + double luminance = ((0.299 * color.red()) + 0.587 * color.green() + 0.114 * color.blue()) / 255; + + if (luminance > 0.5) { + return Qt::black; + } else { + return Qt::white; + } +} + /* Draw grid in the background of the agenda. */ @@ -1660,20 +1676,32 @@ } } + // Compute the grid line color for both the hour and half-hour QPen hourPen; QPen halfHourPen; if (!d->preferences()->useSystemColor()) { - hourPen = d->preferences()->agendaGridBackgroundColor().darker(150); - halfHourPen = d->preferences()->agendaGridBackgroundColor().darker(125); + // compute a black or white contrasting color against the background color + hourPen = contrastColor(d->preferences()->agendaGridBackgroundColor()); + // now lighten or darken just to soften the look a bit + if (hourPen.color() == Qt::black) { + // dark grey lines + halfHourPen = hourPen.color().lighter(500); + hourPen = hourPen.color().lighter(200); + } else { + // light grey lines + halfHourPen = hourPen.color().darker(200); + hourPen = hourPen.color().darker(150); + } } else { const QColor windowTextColor = palette().color(QPalette::WindowText); + // lighten or darken the window text color if (windowTextColor.red() + windowTextColor.green() + windowTextColor.blue() < (256 / 2 * 3)) { - // dark grey line + // dark grey lines hourPen = windowTextColor.lighter(200); halfHourPen = windowTextColor.lighter(500); } else { - // light grey line + // light grey lines hourPen = windowTextColor.darker(150); halfHourPen = windowTextColor.darker(200); }