* @brief Returns the foreground color with optimal contrast to the background. */
| 310 | * @brief Returns the foreground color with optimal contrast to the background. |
| 311 | */ |
| 312 | QColor Titlebar::foregroundColor() const |
| 313 | { |
| 314 | if (m_fgCacheKey == m_backgroundColor && m_fgCache.isValid()) |
| 315 | return m_fgCache; |
| 316 | |
| 317 | m_fgCacheKey = m_backgroundColor; |
| 318 | |
| 319 | const auto& theme = Misc::ThemeManager::instance(); |
| 320 | const QColor toolbarTop = theme.getColor(QStringLiteral("toolbar_top")); |
| 321 | |
| 322 | if (m_backgroundColor == toolbarTop) { |
| 323 | m_fgCache = theme.getColor(QStringLiteral("titlebar_text")); |
| 324 | return m_fgCache; |
| 325 | } |
| 326 | |
| 327 | // clang-format off |
| 328 | constexpr qreal kInv1292 = 1.0 / 12.92; |
| 329 | constexpr qreal kInv1055 = 1.0 / 1.055; |
| 330 | auto linearize = [](qreal c) { return c <= 0.03928 ? c * kInv1292 : qPow((c + 0.055) * kInv1055, 2.4); }; |
| 331 | const qreal luminance = 0.2126 * linearize(m_backgroundColor.redF()) |
| 332 | + 0.7152 * linearize(m_backgroundColor.greenF()) |
| 333 | + 0.0722 * linearize(m_backgroundColor.blueF()); |
| 334 | // clang-format on |
| 335 | |
| 336 | m_fgCache = luminance > 0.179 ? QColor(Qt::black) : QColor(Qt::white); |
| 337 | return m_fgCache; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * @brief Sets the title bar text. |