QGIS API Documentation  2.11.0-Master
qgsvectorcolorrampv2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorcolorrampv2.h
3  ---------------------
4  begin : November 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSVECTORCOLORRAMPV2_H
17 #define QGSVECTORCOLORRAMPV2_H
18 
19 #include <QColor>
20 #include <QGradient>
21 
22 #include "qgssymbollayerv2.h" // for QgsStringMap
23 #include "qgslogger.h"
24 
25 class CORE_EXPORT QgsVectorColorRampV2
26 {
27  public:
28 
29  virtual ~QgsVectorColorRampV2() {}
30 
33  virtual int count() const = 0;
34 
37  virtual double value( int index ) const = 0;
38 
39  virtual QColor color( double value ) const = 0;
40 
41  virtual QString type() const = 0;
42 
43  virtual QgsVectorColorRampV2* clone() const = 0;
44 
45  virtual QgsStringMap properties() const = 0;
46 
47 };
48 
50 {
51  double offset; // relative (0,1)
53  QgsGradientStop( double o, const QColor& c ) : offset( o ), color( c ) { }
54 };
55 
57 
58 #define DEFAULT_GRADIENT_COLOR1 QColor(0,0,255)
59 #define DEFAULT_GRADIENT_COLOR2 QColor(0,255,0)
60 
62 {
63  public:
66  bool discrete = false,
68 
69  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
70 
71  virtual int count() const override { return mStops.count() + 2; }
72 
73  virtual double value( int index ) const override;
74 
75  virtual QColor color( double value ) const override;
76 
77  virtual QString type() const override { return "gradient"; }
78 
79  virtual QgsVectorColorRampV2* clone() const override;
80 
81  virtual QgsStringMap properties() const override;
82 
83  QColor color1() const { return mColor1; }
84  QColor color2() const { return mColor2; }
85  void setColor1( QColor color ) { mColor1 = color; }
86  void setColor2( QColor color ) { mColor2 = color; }
87 
88  bool isDiscrete() const { return mDiscrete; }
89  void setDiscrete( bool discrete ) { mDiscrete = discrete; }
90  void convertToDiscrete( bool discrete );
91 
92  void setStops( const QgsGradientStopsList& stops ) { mStops = stops; }
93  const QgsGradientStopsList& stops() const { return mStops; }
94 
95  QgsStringMap info() const { return mInfo; }
96  void setInfo( const QgsStringMap& info ) { mInfo = info; }
97 
100  void addStopsToGradient( QGradient* gradient, double alpha = 1 );
101 
102  protected:
103  QColor mColor1, mColor2;
104  bool mDiscrete;
107 };
108 
109 #define DEFAULT_RANDOM_COUNT 10
110 #define DEFAULT_RANDOM_HUE_MIN 0
111 #define DEFAULT_RANDOM_HUE_MAX 359
112 #define DEFAULT_RANDOM_VAL_MIN 200
113 #define DEFAULT_RANDOM_VAL_MAX 240
114 #define DEFAULT_RANDOM_SAT_MIN 100
115 #define DEFAULT_RANDOM_SAT_MAX 240
116 
118 {
119  public:
121  int hueMin = DEFAULT_RANDOM_HUE_MIN, int hueMax = DEFAULT_RANDOM_HUE_MAX,
122  int satMin = DEFAULT_RANDOM_SAT_MIN, int satMax = DEFAULT_RANDOM_SAT_MAX,
123  int valMin = DEFAULT_RANDOM_VAL_MIN, int valMax = DEFAULT_RANDOM_VAL_MAX );
124 
125  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
126 
127  virtual double value( int index ) const override;
128 
129  virtual QColor color( double value ) const override;
130 
131  virtual QString type() const override { return "random"; }
132 
133  virtual QgsVectorColorRampV2* clone() const override;
134 
135  virtual QgsStringMap properties() const override;
136 
139  static QList<QColor> randomColors( int count,
140  int hueMax = DEFAULT_RANDOM_HUE_MAX, int hueMin = DEFAULT_RANDOM_HUE_MIN,
141  int satMax = DEFAULT_RANDOM_SAT_MAX, int satMin = DEFAULT_RANDOM_SAT_MIN,
142  int valMax = DEFAULT_RANDOM_VAL_MAX, int valMin = DEFAULT_RANDOM_VAL_MIN );
143  void updateColors();
144 
145  int count() const override { return mCount; }
146  int hueMin() const { return mHueMin; }
147  int hueMax() const { return mHueMax; }
148  int satMin() const { return mSatMin; }
149  int satMax() const { return mSatMax; }
150  int valMin() const { return mValMin; }
151  int valMax() const { return mValMax; }
152 
153  void setCount( int val ) { mCount = val; }
154  void setHueMin( int val ) { mHueMin = val; }
155  void setHueMax( int val ) { mHueMax = val; }
156  void setSatMin( int val ) { mSatMin = val; }
157  void setSatMax( int val ) { mSatMax = val; }
158  void setValMin( int val ) { mValMin = val; }
159  void setValMax( int val ) { mValMax = val; }
160 
161  protected:
162  int mCount;
163  int mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax;
165 };
166 
167 class CORE_EXPORT QgsRandomColorsV2: public QgsVectorColorRampV2
168 {
169  public:
172 
173  int count() const override;
174 
175  double value( int index ) const override;
176 
177  QColor color( double value ) const override;
178 
185  virtual void setTotalColorCount( const int colorCount );
186 
187  QString type() const override;
188 
189  QgsVectorColorRampV2* clone() const override;
190 
191  QgsStringMap properties() const override;
192 
193  protected:
194 
197 
198 };
199 
200 
201 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
202 #define DEFAULT_COLORBREWER_COLORS 5
203 
205 {
206  public:
208  int colors = DEFAULT_COLORBREWER_COLORS );
209 
210  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
211 
212  virtual double value( int index ) const override;
213 
214  virtual QColor color( double value ) const override;
215 
216  virtual QString type() const override { return "colorbrewer"; }
217 
218  virtual QgsVectorColorRampV2* clone() const override;
219 
220  virtual QgsStringMap properties() const override;
221 
222  QString schemeName() const { return mSchemeName; }
223  virtual int count() const override { return mColors; }
224  int colors() const { return mColors; }
225 
226  void setSchemeName( QString schemeName ) { mSchemeName = schemeName; loadPalette(); }
227  void setColors( int colors ) { mColors = colors; loadPalette(); }
228 
229  static QStringList listSchemeNames();
230  static QList<int> listSchemeVariants( QString schemeName );
231 
232  protected:
233 
234  void loadPalette();
235 
237  int mColors;
239 };
240 
241 
242 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
243 #define DEFAULT_CPTCITY_VARIANTNAME "05"
244 
246 {
247  public:
249  QString variantName = DEFAULT_CPTCITY_VARIANTNAME,
250  bool doLoadFile = true );
251  QgsCptCityColorRampV2( QString schemeName, QStringList variantList,
252  QString variantName = QString(), bool doLoadFile = true );
253 
254  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
255 
256  virtual QString type() const override { return "cpt-city"; }
257 
258  virtual QgsVectorColorRampV2* clone() const override;
259  void copy( const QgsCptCityColorRampV2* other );
260  QgsVectorGradientColorRampV2* cloneGradientRamp() const;
261 
262  virtual QgsStringMap properties() const override;
263 
264  QString schemeName() const { return mSchemeName; }
265  QString variantName() const { return mVariantName; }
266  QStringList variantList() const { return mVariantList; }
267 
268  /* lazy loading - have to call loadPalette() explicitly */
269  void setSchemeName( QString schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
270  void setVariantName( QString variantName ) { mVariantName = variantName; mFileLoaded = false; }
271  void setVariantList( QStringList variantList ) { mVariantList = variantList; }
272  void setName( QString schemeName, QString variantName = "", QStringList variantList = QStringList() )
273  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
274 
275  void loadPalette() { loadFile(); }
276  bool hasMultiStops() const { return mMultiStops; }
277 
278  QString fileName() const;
279  bool loadFile();
280  bool fileLoaded() const { return mFileLoaded; }
281 
282  QString copyingFileName() const;
283  QString descFileName() const;
284  QgsStringMap copyingInfo() const;
285 
286  protected:
287 
293 };
294 
295 
296 #endif
static unsigned index
int count() const override
Returns number of defined colors, or -1 if undefined.
void setName(QString schemeName, QString variantName="", QStringList variantList=QStringList())
virtual QString type() const =0
#define DEFAULT_CPTCITY_VARIANTNAME
#define DEFAULT_COLORBREWER_SCHEMENAME
#define DEFAULT_RANDOM_HUE_MIN
void setInfo(const QgsStringMap &info)
virtual QgsStringMap properties() const =0
#define DEFAULT_CPTCITY_SCHEMENAME
virtual QString type() const override
QMap< QString, QString > QgsStringMap
Definition: qgis.h:441
#define DEFAULT_RANDOM_SAT_MAX
virtual QgsStringMap properties() const override
virtual int count() const override
Returns number of defined colors, or -1 if undefined.
QString variantName() const
void setSchemeName(QString schemeName)
QStringList variantList() const
#define DEFAULT_GRADIENT_COLOR2
virtual double value(int index) const =0
Returns relative value between [0,1] of color at specified index.
virtual QgsVectorColorRampV2 * clone() const =0
virtual QString type() const override
void setVariantName(QString variantName)
#define DEFAULT_GRADIENT_COLOR1
void setStops(const QgsGradientStopsList &stops)
#define DEFAULT_RANDOM_VAL_MAX
virtual QColor color(double value) const =0
virtual QString type() const override
#define DEFAULT_RANDOM_SAT_MIN
static QgsVectorColorRampV2 * create(const QgsStringMap &properties=QgsStringMap())
virtual int count() const =0
Returns number of defined colors, or -1 if undefined.
QList< QgsGradientStop > QgsGradientStopsList
QgsGradientStop(double o, const QColor &c)
virtual QString type() const override
#define DEFAULT_COLORBREWER_COLORS
const QgsGradientStopsList & stops() const
void setVariantList(QStringList variantList)
QList< QColor > mPrecalculatedColors
#define DEFAULT_RANDOM_HUE_MAX
virtual QgsVectorColorRampV2 * clone() const override
#define DEFAULT_RANDOM_VAL_MIN
void setSchemeName(QString schemeName)
virtual int count() const override
Returns number of defined colors, or -1 if undefined.
#define DEFAULT_RANDOM_COUNT