Better shadow color calculation. Picks up palette changes on the fly.

svn path=/trunk/KDE/kdebase/workspace/; revision=793031
This commit is contained in:
Louai Al-Khanji 2008-04-02 21:15:04 +00:00
parent d91c540b61
commit 340489c14a
4 changed files with 91 additions and 8 deletions

View file

@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "shadow.h"
#include "shadow_helper.h"
#include <kwinglutils.h>
@ -26,6 +27,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <kdebug.h>
#include <KStandardDirs>
#include <kcolorscheme.h>
#include <KGlobalSettings>
namespace KWin
{
@ -40,11 +42,20 @@ ShadowEffect::ShadowEffect()
shadowOpacity = conf.readEntry( "Opacity", 0.25 );
shadowFuzzyness = conf.readEntry( "Fuzzyness", 10 );
shadowSize = conf.readEntry( "Size", 5 );
shadowColor = conf.readEntry( "Color", KColorScheme::shade( Qt::white, KColorScheme::ShadowShade ) );
intensifyActiveShadow = conf.readEntry( "IntensifyActiveShadow", true );
QString shadowtexture = KGlobal::dirs()->findResource("data", "kwin/shadow-texture.png");
mShadowTexture = new GLTexture(shadowtexture);
updateShadowColor();
connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()),
this, SLOT(updateShadowColor()));
}
void ShadowEffect::updateShadowColor()
{
KConfigGroup conf = effects->effectConfig("Shadow");
shadowColor = conf.readEntry( "Color", schemeShadowColor() );
}
QRect ShadowEffect::shadowRectangle(const QRect& windowRectangle) const
@ -229,3 +240,5 @@ void ShadowEffect::drawShadow( EffectWindow* window, int mask, QRegion region, W
}
} // namespace
#include "shadow.h"

View file

@ -30,8 +30,9 @@ namespace KWin
class GLTexture;
class ShadowEffect
: public Effect
: public QObject, public Effect
{
Q_OBJECT
public:
ShadowEffect();
virtual void prePaintWindow( EffectWindow* w, WindowPrePaintData& data, int time );
@ -39,6 +40,10 @@ class ShadowEffect
virtual void paintScreen( int mask, QRegion region, ScreenPaintData& data );
virtual void windowClosed( EffectWindow* c );
virtual QRect transformWindowDamage( EffectWindow* w, const QRect& r );
private slots:
void updateShadowColor();
private:
void drawShadow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data );
void addQuadVertices(QVector<float>& verts, float x1, float y1, float x2, float y2) const;

View file

@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "shadow_config.h"
#include "shadow_helper.h"
#include <kwineffects.h>
@ -38,12 +39,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
KWIN_EFFECT_CONFIG_FACTORY
#endif
// Save some typing
static QColor schemeShadowColor()
{
return KColorScheme::shade(Qt::white, KColorScheme::ShadowShade);
}
namespace KWin
{

70
effects/shadow_helper.h Normal file
View file

@ -0,0 +1,70 @@
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2008 Louai Al-Khanji <louai.khanji@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
// This contains code shared by the shadow effect and shadow effect configurator
// Mark all functions static so the symbols are not exported!
#ifndef KWIN_SHADOW_HELPER_H
#define KWIN_SHADOW_HELPER_H
#include <KColorScheme>
#include <KColorUtils>
static const int MAX_ITERS = 10; // should be enough
static const qreal LUMA_THRESHOLD = 0.05;
static const qreal MINIMUM_CONTRAST = 3.0;
static bool contrastTooLow(const QColor& one, const QColor& two)
{
return KColorUtils::contrastRatio(one, two) < MINIMUM_CONTRAST;
}
static QColor schemeShadowColor()
{
QPalette palette;
QColor shadowColor;
QPalette::ColorRole shadowRole;
QColor windowColor;
windowColor = palette.color(QPalette::Window);
if (KColorUtils::luma(windowColor) >= LUMA_THRESHOLD)
shadowRole = QPalette::Shadow;
else
shadowRole = QPalette::Light;
shadowColor = palette.color(shadowRole);
// Some styles might set a weird shadow or light color. Make sure we
// dont't end up looping forever or we might lock up the desktop!!
int iters = 0;
while (contrastTooLow(shadowColor, windowColor) && iters < MAX_ITERS)
{
iters++;
if (shadowRole == QPalette::Shadow)
shadowColor = KColorUtils::darken(shadowColor);
else
shadowColor = KColorUtils::lighten(shadowColor);
}
return shadowColor;
}
#endif // KWIN_SHADOW_HELPER_H