From f2db544be035003598105ade7ffccb2da7678ce8 Mon Sep 17 00:00:00 2001 From: Mentalflow <312902918@qq.com> Date: Sun, 16 Apr 2023 01:18:17 +0800 Subject: [PATCH] Add automatic dark color mode switching function. --- src/FluTheme.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/FluTheme.cpp b/src/FluTheme.cpp index ad8415a3..e1f08ddc 100644 --- a/src/FluTheme.cpp +++ b/src/FluTheme.cpp @@ -1,6 +1,8 @@ #include "FluTheme.h" #include "FluColors.h" +#include +#include FluTheme* FluTheme::m_instance = nullptr; @@ -19,5 +21,13 @@ FluTheme::FluTheme(QObject *parent) textSize(13); nativeText(true); frameless(true); - dark(false); + std::function isDark = [](){ + QPalette palette = (qobject_cast(QCoreApplication::instance()))->palette(); + QColor color = palette.color(QPalette::Window).rgb(); + return !(color.red() * 0.2126 + color.green() * 0.7152 + color.blue() * 0.0722 > 255 / 2); + }; + dark(isDark()); + connect(qobject_cast(QCoreApplication::instance()), &QGuiApplication::paletteChanged, this, [=] (const QPalette &) { + dark(isDark()); + }); }