kwin/clients/oxygen/lib/helper.cpp

135 lines
4.3 KiB
C++
Raw Normal View History

/*
* Copyright 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
* Copyright 2007 Casper Boemann <cbr@boemann.dk>
* Copyright 2007 Fredrik Höglund <fredrik@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "helper.h"
#include <KGlobalSettings>
#include <KColorScheme>
#include <QtGui/QPainter>
// alphaBlendColors Copyright 2003 Sandro Giessl <ceebx@users.sourceforge.net>
// DEPRECATED (use KColorUtils::mix to the extent we still need such a critter)
QColor alphaBlendColors(const QColor &bgColor, const QColor &fgColor, const int a)
{
// normal button...
QRgb rgb = bgColor.rgb();
QRgb rgb_b = fgColor.rgb();
int alpha = a;
if(alpha>255) alpha = 255;
if(alpha<0) alpha = 0;
int inv_alpha = 255 - alpha;
QColor result = QColor( qRgb(qRed(rgb_b)*inv_alpha/255 + qRed(rgb)*alpha/255,
qGreen(rgb_b)*inv_alpha/255 + qGreen(rgb)*alpha/255,
qBlue(rgb_b)*inv_alpha/255 + qBlue(rgb)*alpha/255) );
return result;
}
// NOTE: OxygenStyleHelper needs to use a KConfig from its own KComponentData
// Since the ctor order causes a SEGV if we try to pass in a KConfig here from
// a KComponentData constructed in the OxygenStyleHelper ctor, we'll just keep
// one here, even though the window decoration doesn't really need it.
OxygenHelper::OxygenHelper(const QByteArray &componentName)
: _componentData(componentName, 0, KComponentData::SkipMainComponentRegistration)
{
_config = _componentData.config();
_contrast = KGlobalSettings::contrastF(_config);
m_cache.setMaxCost(64);
}
KSharedConfigPtr OxygenHelper::config() const
{
return _config;
}
QColor OxygenHelper::backgroundRadialColor(const QColor &color) const
{
return KColorScheme::shade(color, KColorScheme::LightShade, _contrast);
}
QColor OxygenHelper::backgroundTopColor(const QColor &color) const
{
return KColorScheme::shade(color, KColorScheme::MidlightShade, _contrast);
}
QColor OxygenHelper::backgroundBottomColor(const QColor &color) const
{
return KColorScheme::shade(color, KColorScheme::MidShade, _contrast - 1.1);
}
QPixmap OxygenHelper::verticalGradient(const QColor &color, int height)
{
quint64 key = (quint64(color.rgba()) << 32) | height | 0x8000;
QPixmap *pixmap = m_cache.object(key);
if (!pixmap)
{
pixmap = new QPixmap(32, height);
QLinearGradient gradient(0, 0, 0, height);
gradient.setColorAt(0.0, backgroundTopColor(color));
gradient.setColorAt(0.5, color);
gradient.setColorAt(1.0, backgroundBottomColor(color));
QPainter p(pixmap);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.fillRect(pixmap->rect(), gradient);
m_cache.insert(key, pixmap);
}
return *pixmap;
}
QPixmap OxygenHelper::radialGradient(const QColor &color, int width)
{
quint64 key = (quint64(color.rgba()) << 32) | width | 0xb000;
QPixmap *pixmap = m_cache.object(key);
if (!pixmap)
{
width /= 2;
pixmap = new QPixmap(width, 64);
pixmap->fill(QColor(0,0,0,0));
QColor radialColor = backgroundRadialColor(color);
radialColor.setAlpha(255);
QRadialGradient gradient(64, 0, 64);
gradient.setColorAt(0, radialColor);
radialColor.setAlpha(101);
gradient.setColorAt(0.5, radialColor);
radialColor.setAlpha(37);
gradient.setColorAt(0.75, radialColor);
radialColor.setAlpha(0);
gradient.setColorAt(1, radialColor);
QPainter p(pixmap);
p.scale(width/128.0,1);
p.fillRect(pixmap->rect(), gradient);
m_cache.insert(key, pixmap);
}
return *pixmap;
}