1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "KChartPalette.h"
21 
22 #include "KChartMath_p.h"
23 
24 #include <QBrush>
25 #include <QVector>
26 
27 using namespace KChart;
28 
29 namespace {
30 
makeDefaultPalette()31     static Palette makeDefaultPalette() {
32         Palette p;
33 
34         p.addBrush( Qt::red );
35         p.addBrush( Qt::green );
36         p.addBrush( Qt::blue );
37         p.addBrush( Qt::cyan );
38         p.addBrush( Qt::magenta );
39         p.addBrush( Qt::yellow );
40         p.addBrush( Qt::darkRed );
41         p.addBrush( Qt::darkGreen );
42         p.addBrush( Qt::darkBlue );
43         p.addBrush( Qt::darkCyan );
44         p.addBrush( Qt::darkMagenta );
45         p.addBrush( Qt::darkYellow );
46 
47         return p;
48     }
49 
makeSubduedPalette()50     static Palette makeSubduedPalette() {
51         Palette p;
52 
53         p.addBrush( QColor( 0xe0,0x7f,0x70 ) );
54         p.addBrush( QColor( 0xe2,0xa5,0x6f ) );
55         p.addBrush( QColor( 0xe0,0xc9,0x70 ) );
56         p.addBrush( QColor( 0xd1,0xe0,0x70 ) );
57         p.addBrush( QColor( 0xac,0xe0,0x70 ) );
58         p.addBrush( QColor( 0x86,0xe0,0x70 ) );
59         p.addBrush( QColor( 0x70,0xe0,0x7f ) );
60         p.addBrush( QColor( 0x70,0xe0,0xa4 ) );
61         p.addBrush( QColor( 0x70,0xe0,0xc9 ) );
62         p.addBrush( QColor( 0x70,0xd1,0xe0 ) );
63         p.addBrush( QColor( 0x70,0xac,0xe0 ) );
64         p.addBrush( QColor( 0x70,0x86,0xe0 ) );
65         p.addBrush( QColor( 0x7f,0x70,0xe0 ) );
66         p.addBrush( QColor( 0xa4,0x70,0xe0 ) );
67         p.addBrush( QColor( 0xc9,0x70,0xe0 ) );
68         p.addBrush( QColor( 0xe0,0x70,0xd1 ) );
69         p.addBrush( QColor( 0xe0,0x70,0xac ) );
70         p.addBrush( QColor( 0xe0,0x70,0x86 ) );
71 
72         return p;
73     }
74 
makeRainbowPalette()75     static Palette makeRainbowPalette() {
76         Palette p;
77 
78         p.addBrush( QColor(255,  0,196) );
79         p.addBrush( QColor(255,  0, 96) );
80         p.addBrush( QColor(255, 128,64) );
81         p.addBrush( Qt::yellow );
82         p.addBrush( Qt::green );
83         p.addBrush( Qt::cyan );
84         p.addBrush( QColor( 96, 96,255) );
85         p.addBrush( QColor(160,  0,255) );
86         for ( int i = 8 ; i < 16 ; ++i ) {
87             p.addBrush( p.getBrush( i - 8 ).color().lighter(), i );
88         }
89         return p;
90     }
91 
92 }
93 
94 #define d d_func()
95 
96 class Q_DECL_HIDDEN Palette::Private
97 {
98 public:
Private()99     explicit Private() {}
~Private()100     ~Private() {}
101 
102     QVector<QBrush> brushes;
103 };
104 
defaultPalette()105 const Palette& Palette::defaultPalette()
106 {
107     static const Palette palette = makeDefaultPalette();
108     return palette;
109 }
110 
subduedPalette()111 const Palette& Palette::subduedPalette()
112 {
113     static const Palette palette = makeSubduedPalette();
114     return palette;
115 }
116 
rainbowPalette()117 const Palette& Palette::rainbowPalette()
118 {
119     static const Palette palette = makeRainbowPalette();
120     return palette;
121 }
122 
Palette(QObject * parent)123 Palette::Palette( QObject *parent )
124   : QObject( parent ), _d( new Private )
125 {
126 
127 }
128 
~Palette()129 Palette::~Palette()
130 {
131     delete _d; _d = nullptr;
132 }
133 
134 
135 
Palette(const Palette & r)136 Palette::Palette( const Palette& r )
137     : QObject(), _d( new Private( *r.d ) )
138 {
139 }
140 
operator =(const Palette & r)141 Palette& Palette::operator=( const Palette& r )
142 {
143     Palette copy( r );
144     copy.swap( *this );
145 
146     // Q_EMIT changed() ?
147     return *this;
148 }
149 
isValid() const150 bool Palette::isValid() const
151 {
152   return d->brushes.size() >= 1;
153 }
154 
size() const155 int Palette::size() const
156 {
157   return d->brushes.size();
158 }
159 
addBrush(const QBrush & brush,int position)160 void Palette::addBrush( const QBrush& brush, int position )
161 {
162   if ( position < 0 || position >= size() ) {
163     d->brushes.append( brush );
164   } else {
165     d->brushes.insert( position, brush );
166   }
167   Q_EMIT changed();
168 }
169 
getBrush(int position) const170 QBrush Palette::getBrush( int position ) const
171 {
172   if ( !isValid() ) return QBrush();
173   return d->brushes.at( position % size() );
174 }
175 
removeBrush(int position)176 void Palette::removeBrush( int position )
177 {
178   if ( position < 0 || position >= size() ) return;
179   d->brushes.remove( position );
180   Q_EMIT changed();
181 }
182 
183