widgets/colorbutton.cpp

changeset 257
afdab56e8210
parent 256
c6f5de03dc0f
child 264
76a025db4948
--- a/widgets/colorbutton.cpp	Wed Jun 22 22:59:56 2022 +0300
+++ b/widgets/colorbutton.cpp	Wed Jun 22 23:32:34 2022 +0300
@@ -1,4 +1,5 @@
 #include <QColorDialog>
+#include <QStyleFactory>
 #include "colorbutton.h"
 
 ColorButton::ColorButton(const QColor& color, QWidget* parent) :
@@ -28,9 +29,13 @@
 {
 	if (this->storedSelectedColor != color) {
 		this->storedSelectedColor = color;
-		this->setStyleSheet(QString{"background-color: %1"}.arg(color.name()));
 		this->setText(color.name());
+		QPalette palette{color};
+		const qreal lightness = color.lightnessF();
+		const QColor foreground = lightness < 0.4 ? Qt::white : Qt::black;
+		palette.setColor(QPalette::ButtonText, foreground);
+		this->setPalette(palette);
+		this->setStyle(QStyleFactory::create("Fusion"));
 		Q_EMIT this->colorChanged(this->storedSelectedColor);
 	}
 }
-

mercurial