File indexing completed on 2024-05-12 04:20:36

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "KChartPalette.h"
0010 
0011 #include "KChartMath_p.h"
0012 
0013 #include <QBrush>
0014 #include <QVector>
0015 
0016 using namespace KChart;
0017 
0018 namespace {
0019 
0020     static Palette makeDefaultPalette() {
0021         Palette p;
0022 
0023         p.addBrush( Qt::red );
0024         p.addBrush( Qt::green );
0025         p.addBrush( Qt::blue );
0026         p.addBrush( Qt::cyan );
0027         p.addBrush( Qt::magenta );
0028         p.addBrush( Qt::yellow );
0029         p.addBrush( Qt::darkRed );
0030         p.addBrush( Qt::darkGreen );
0031         p.addBrush( Qt::darkBlue );
0032         p.addBrush( Qt::darkCyan );
0033         p.addBrush( Qt::darkMagenta );
0034         p.addBrush( Qt::darkYellow );
0035 
0036         return p;
0037     }
0038 
0039     static Palette makeSubduedPalette() {
0040         Palette p;
0041 
0042         p.addBrush( QColor( 0xe0,0x7f,0x70 ) );
0043         p.addBrush( QColor( 0xe2,0xa5,0x6f ) );
0044         p.addBrush( QColor( 0xe0,0xc9,0x70 ) );
0045         p.addBrush( QColor( 0xd1,0xe0,0x70 ) );
0046         p.addBrush( QColor( 0xac,0xe0,0x70 ) );
0047         p.addBrush( QColor( 0x86,0xe0,0x70 ) );
0048         p.addBrush( QColor( 0x70,0xe0,0x7f ) );
0049         p.addBrush( QColor( 0x70,0xe0,0xa4 ) );
0050         p.addBrush( QColor( 0x70,0xe0,0xc9 ) );
0051         p.addBrush( QColor( 0x70,0xd1,0xe0 ) );
0052         p.addBrush( QColor( 0x70,0xac,0xe0 ) );
0053         p.addBrush( QColor( 0x70,0x86,0xe0 ) );
0054         p.addBrush( QColor( 0x7f,0x70,0xe0 ) );
0055         p.addBrush( QColor( 0xa4,0x70,0xe0 ) );
0056         p.addBrush( QColor( 0xc9,0x70,0xe0 ) );
0057         p.addBrush( QColor( 0xe0,0x70,0xd1 ) );
0058         p.addBrush( QColor( 0xe0,0x70,0xac ) );
0059         p.addBrush( QColor( 0xe0,0x70,0x86 ) );
0060 
0061         return p;
0062     }
0063 
0064     static Palette makeRainbowPalette() {
0065         Palette p;
0066 
0067         p.addBrush( QColor(255,  0,196) );
0068         p.addBrush( QColor(255,  0, 96) );
0069         p.addBrush( QColor(255, 128,64) );
0070         p.addBrush( Qt::yellow );
0071         p.addBrush( Qt::green );
0072         p.addBrush( Qt::cyan );
0073         p.addBrush( QColor( 96, 96,255) );
0074         p.addBrush( QColor(160,  0,255) );
0075         for ( int i = 8 ; i < 16 ; ++i ) {
0076             p.addBrush( p.getBrush( i - 8 ).color().lighter(), i );
0077         }
0078         return p;
0079     }
0080 
0081 }
0082 
0083 #define d d_func()
0084 
0085 class Q_DECL_HIDDEN Palette::Private
0086 {
0087 public:
0088     explicit Private() {}
0089     ~Private() {}
0090 
0091     QVector<QBrush> brushes;
0092 };
0093 
0094 const Palette& Palette::defaultPalette()
0095 {
0096     static const Palette palette = makeDefaultPalette();
0097     return palette;
0098 }
0099 
0100 const Palette& Palette::subduedPalette()
0101 {
0102     static const Palette palette = makeSubduedPalette();
0103     return palette;
0104 }
0105 
0106 const Palette& Palette::rainbowPalette()
0107 {
0108     static const Palette palette = makeRainbowPalette();
0109     return palette;
0110 }
0111 
0112 Palette::Palette( QObject *parent )
0113   : QObject( parent ), _d( new Private )
0114 {
0115 
0116 }
0117 
0118 Palette::~Palette()
0119 {
0120     delete _d; _d = nullptr;
0121 }
0122 
0123 
0124 
0125 Palette::Palette( const Palette& r )
0126     : QObject(), _d( new Private( *r.d ) )
0127 {
0128 }
0129 
0130 Palette& Palette::operator=( const Palette& r )
0131 {
0132     Palette copy( r );
0133     copy.swap( *this );
0134 
0135     // Q_EMIT changed() ?
0136     return *this;
0137 }
0138 
0139 bool Palette::isValid() const
0140 {
0141   return d->brushes.size() >= 1;
0142 }
0143 
0144 int Palette::size() const
0145 {
0146   return d->brushes.size();
0147 }
0148 
0149 void Palette::addBrush( const QBrush& brush, int position )
0150 {
0151   if ( position < 0 || position >= size() ) {
0152     d->brushes.append( brush );
0153   } else {
0154     d->brushes.insert( position, brush );
0155   }
0156   Q_EMIT changed();
0157 }
0158 
0159 QBrush Palette::getBrush( int position ) const
0160 {
0161   if ( !isValid() ) return QBrush();
0162   return d->brushes.at( position % size() );
0163 }
0164 
0165 void Palette::removeBrush( int position )
0166 {
0167   if ( position < 0 || position >= size() ) return;
0168   d->brushes.remove( position );
0169   Q_EMIT changed();
0170 }
0171