2
* Copyright 2008 Long Huynh Huu <long.upcase@googlemail.com>
3
* Copyright 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
4
* Copyright 2007 Casper Boemann <cbr@boemann.dk>
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License version 2 as published by the Free Software Foundation.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; see the file COPYING.LIB. If not, write to
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#include "oxygendecohelper.h"
23
#include <QtGui/QPainter>
24
#include <KColorUtils>
32
//______________________________________________________________________________
33
DecoHelper::DecoHelper(const QByteArray &componentName):
34
Helper(componentName),
35
_debugArea( KDebug::registerArea( "Oxygen (decoration)" ) )
39
//______________________________________________________________________________
40
void DecoHelper::invalidateCaches( void )
43
Helper::invalidateCaches();
46
_titleBarTextColorCache.clear();
47
_buttonTextColorCache.clear();
51
//______________________________________________________________________________
52
QPixmap DecoHelper::windecoButton(const QColor &color, bool pressed, int size)
54
const quint64 key( (quint64(color.rgba()) << 32) | (size << 1) | (int)pressed );
55
QPixmap *pixmap( windecoButtonCache().object(key) );
59
pixmap = new QPixmap(size, size);
60
pixmap->fill(Qt::transparent);
62
const QColor light( calcLightColor(color) );
63
const QColor dark( calcDarkColor(color) );
66
p.setRenderHints(QPainter::Antialiasing);
69
p.translate( 0.5*u, (0.5-0.668)*u );
73
QLinearGradient lg( 0, u*1.665, 0, u*(12.33+1.665) );
76
lg.setColorAt( 1, light );
77
lg.setColorAt( 0, dark );
79
lg.setColorAt( 0, light );
80
lg.setColorAt( 1, dark );
83
const QRectF r( u*0.5*(17-12.33), u*1.665, u*12.33, u*12.33 );
90
const qreal penWidth( 0.7 );
91
QLinearGradient lg( 0, u*1.665, 0, u*(2.0*12.33+1.665) );
92
lg.setColorAt( 0, light );
93
lg.setColorAt( 1, dark );
94
const QRectF r( u*0.5*(17-12.33+penWidth), u*(1.665+penWidth), u*(12.33-penWidth), u*(12.33-penWidth) );
95
p.setPen( QPen( lg, penWidth*u ) );
96
p.setBrush( Qt::NoBrush );
101
windecoButtonCache().insert( key, pixmap );
107
//_______________________________________________________________________
108
QPixmap DecoHelper::windecoButtonGlow(const QColor &color, int size)
110
const quint64 key( (quint64(color.rgba()) << 32) | size );
111
QPixmap *pixmap( windecoButtonGlowCache().object(key) );
115
pixmap = new QPixmap(size, size);
116
pixmap->fill(Qt::transparent);
118
// right now the same color is used for the two shadows
119
const QColor& light( color );
120
const QColor& dark( color );
123
p.setRenderHints(QPainter::Antialiasing);
126
p.translate( 0.5*u, (0.5-0.668)*u );
131
QRadialGradient rg( u*8.5, u*8.5, u*8.5 );
133
static const int nPoints(5);
134
const qreal x[5] = { 0.61, 0.72, 0.81, 0.9, 1};
135
const qreal values[5] = { 255-172, 255-178, 255-210, 255-250, 0 };
137
for( int i = 0; i<nPoints; i++ )
138
{ c.setAlpha( values[i] ); rg.setColorAt( x[i], c ); }
140
QRectF r( pixmap->rect() );
147
QRadialGradient rg( u*8.5, u*8.5, u*8.5 );
149
static const int nPoints(6);
150
const qreal x[6] = { 0.61, 0.67, 0.7, 0.74, 0.78, 1 };
151
const qreal values[6] = { 255-92, 255-100, 255-135, 255-205, 255-250, 0 };
153
for( int i = 0; i<nPoints; i++ )
154
{ c.setAlpha( values[i] ); rg.setColorAt( x[i], c ); }
156
QRectF r( pixmap->rect() );
161
windecoButtonGlowCache().insert( key, pixmap );
168
//_______________________________________________________________________
169
QRegion DecoHelper::decoRoundedMask( const QRect& r, int left, int right, int top, int bottom ) const
173
r.getRect(&x, &y, &w, &h);
175
QRegion mask(x + 3*left, y + 0*top, w-3*(left+right), h-0*(top+bottom));
176
mask += QRegion(x + 0*left, y + 3*top, w-0*(left+right), h-3*(top+bottom));
177
mask += QRegion(x + 1*left, y + 1*top, w-1*(left+right), h-1*(top+bottom));
182
//______________________________________________________________________________
183
const QColor& DecoHelper::inactiveTitleBarTextColor( const QPalette& palette )
186
const quint32 key( palette.color(QPalette::Active, QPalette::Window).rgba() );
187
QColor* out( _titleBarTextColorCache.object( key ) );
191
// todo: reimplement cache
192
const QColor ab = palette.color(QPalette::Active, QPalette::Window);
193
const QColor af = palette.color(QPalette::Active, QPalette::WindowText);
194
const QColor nb = palette.color(QPalette::Inactive, QPalette::Window);
195
const QColor nf = palette.color(QPalette::Inactive, QPalette::WindowText);
196
out = new QColor( reduceContrast(nb, nf, qMax(qreal(2.5), KColorUtils::contrastRatio(ab, KColorUtils::mix(ab, af, 0.4)))) );
197
_titleBarTextColorCache.insert( key, out );
204
//______________________________________________________________________________
205
const QColor& DecoHelper::inactiveButtonTextColor( const QPalette& palette )
208
const quint32 key( palette.color(QPalette::Active, QPalette::Window).rgba() );
209
QColor* out( _buttonTextColorCache.object( key ) );
213
// todo: reimplement cache
214
const QColor ab = palette.color(QPalette::Active, QPalette::Button);
215
const QColor af = palette.color(QPalette::Active, QPalette::ButtonText);
216
const QColor nb = palette.color(QPalette::Inactive, QPalette::Button);
217
const QColor nf = palette.color(QPalette::Inactive, QPalette::ButtonText);
218
out = new QColor( reduceContrast(nb, nf, qMax(qreal(2.5), KColorUtils::contrastRatio(ab, KColorUtils::mix(ab, af, 0.4)))) );
219
_buttonTextColorCache.insert( key, out );
225
//_________________________________________________________
226
QColor DecoHelper::reduceContrast(const QColor &c0, const QColor &c1, double t) const
228
const double s( KColorUtils::contrastRatio(c0, c1) );
229
if( s < t ) return c1;
236
for (int maxiter = 16; maxiter; --maxiter)
240
r = KColorUtils::mix(c0, c1, a);
241
x = KColorUtils::contrastRatio(c0, r);
243
if ( std::abs(x - t) < 0.01) break;