From 93dbb3a5fcfb18e4f878d57a480b0238f551ea92 Mon Sep 17 00:00:00 2001
From: Jens Bache-Wiig <jbache@trolltech.com>
Date: Wed, 3 Mar 2010 11:02:13 +0100
Subject: [PATCH] Force custom colors within usable range

We previously allowed fully white and yellow colors as the
base color for our interface. This does not work with our
icons or other interface elements. Instead I now constrain
the selectable colors within a certain range of saturation
and brightness. This still leaves the user in control but
will prevent the common case where the user selects an
overly saturated color from the color picker and ends up
with an unusable or extremely ugly panel.
---
 src/libs/utils/stylehelper.cpp             | 13 ++++++++++++-
 src/libs/utils/stylehelper.h               |  2 ++
 src/plugins/coreplugin/fancytabwidget.cpp  |  2 +-
 src/plugins/coreplugin/generalsettings.cpp |  2 +-
 src/plugins/coreplugin/mainwindow.cpp      |  2 +-
 5 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/libs/utils/stylehelper.cpp b/src/libs/utils/stylehelper.cpp
index 01c9f6869e2..9e946528201 100644
--- a/src/libs/utils/stylehelper.cpp
+++ b/src/libs/utils/stylehelper.cpp
@@ -95,6 +95,7 @@ QColor StyleHelper::panelTextColor(bool lightColored)
 }
 
 QColor StyleHelper::m_baseColor(0x666666);
+QColor StyleHelper::m_requestedBaseColor(0x666666);
 
 QColor StyleHelper::baseColor(bool lightColored)
 {
@@ -136,8 +137,18 @@ QColor StyleHelper::borderColor(bool lightColored)
     return result;
 }
 
-void StyleHelper::setBaseColor(const QColor &color)
+// We try to ensure that the actual color used are within
+// reasonalbe bounds while generating the actual baseColor
+// from the users request.
+void StyleHelper::setBaseColor(const QColor &newcolor)
 {
+    m_requestedBaseColor = newcolor;
+
+    QColor color;
+    color.setHsv(newcolor.hue(),
+                 newcolor.saturation() * 0.7,
+                 64 + newcolor.value() / 3);
+
     if (color.isValid() && color != m_baseColor) {
         m_baseColor = color;
         foreach (QWidget *w, QApplication::topLevelWidgets())
diff --git a/src/libs/utils/stylehelper.h b/src/libs/utils/stylehelper.h
index ee7a61b2a39..9ff2d7b9c8b 100644
--- a/src/libs/utils/stylehelper.h
+++ b/src/libs/utils/stylehelper.h
@@ -53,6 +53,7 @@ public:
     static QPalette sidebarFontPalette(const QPalette &original);
 
     // This is our color table, all colors derive from baseColor
+    static QColor requestedBaseColor() { return m_requestedBaseColor; }
     static QColor baseColor(bool lightColored = false);
     static QColor panelTextColor(bool lightColored = false);
     static QColor highlightColor(bool lightColored = false);
@@ -83,6 +84,7 @@ public:
 
 private:
     static QColor m_baseColor;
+    static QColor m_requestedBaseColor;
 };
 
 } // namespace Utils
diff --git a/src/plugins/coreplugin/fancytabwidget.cpp b/src/plugins/coreplugin/fancytabwidget.cpp
index 5679b91e9fe..3e1d68158cb 100644
--- a/src/plugins/coreplugin/fancytabwidget.cpp
+++ b/src/plugins/coreplugin/fancytabwidget.cpp
@@ -356,7 +356,7 @@ public:
     void mousePressEvent(QMouseEvent *ev)
     {
         if (ev->modifiers() & Qt::ShiftModifier)
-            Utils::StyleHelper::setBaseColor(QColorDialog::getColor(Utils::StyleHelper::baseColor(), m_parent));
+            Utils::StyleHelper::setBaseColor(QColorDialog::getColor(Utils::StyleHelper::requestedBaseColor(), m_parent));
     }
 private:
     QWidget *m_parent;
diff --git a/src/plugins/coreplugin/generalsettings.cpp b/src/plugins/coreplugin/generalsettings.cpp
index 7afec519d82..1e738b5d97d 100644
--- a/src/plugins/coreplugin/generalsettings.cpp
+++ b/src/plugins/coreplugin/generalsettings.cpp
@@ -120,7 +120,7 @@ QWidget *GeneralSettings::createPage(QWidget *parent)
     QSettings* settings = Core::ICore::instance()->settings();
     Q_UNUSED(settings)
     fillLanguageBox();
-    m_page->colorButton->setColor(StyleHelper::baseColor());
+    m_page->colorButton->setColor(StyleHelper::requestedBaseColor());
     m_page->externalEditorEdit->setText(EditorManager::instance()->externalEditor());
     m_page->reloadBehavior->setCurrentIndex(EditorManager::instance()->reloadBehavior());
 #ifdef Q_OS_UNIX
diff --git a/src/plugins/coreplugin/mainwindow.cpp b/src/plugins/coreplugin/mainwindow.cpp
index 8516d158825..7ea753bf713 100644
--- a/src/plugins/coreplugin/mainwindow.cpp
+++ b/src/plugins/coreplugin/mainwindow.cpp
@@ -1131,7 +1131,7 @@ void MainWindow::writeSettings()
     m_settings->beginGroup(QLatin1String(settingsGroup));
 
     if (!(m_overrideColor.isValid() && Utils::StyleHelper::baseColor() == m_overrideColor))
-        m_settings->setValue(QLatin1String(colorKey), Utils::StyleHelper::baseColor());
+        m_settings->setValue(QLatin1String(colorKey), Utils::StyleHelper::requestedBaseColor());
 
     if (windowState() & (Qt::WindowMaximized | Qt::WindowFullScreen)) {
         m_settings->setValue(QLatin1String(maxKey), (bool) (windowState() & Qt::WindowMaximized));
-- 
GitLab