QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
31  // Number of defined colors
32  virtual int count() const = 0;
33 
34  // Relative value (0,1) of color at index
35  virtual double value( int index ) const = 0;
36 
37  virtual QColor color( double value ) const = 0;
38 
39  virtual QString type() const = 0;
40 
41  virtual QgsVectorColorRampV2* clone() const = 0;
42 
43  virtual QgsStringMap properties() const = 0;
44 
45 };
46 
48 {
49  double offset; // relative (0,1)
50  QColor color;
51  QgsGradientStop( double o, const QColor& c ) : offset( o ), color( c ) { }
52 };
53 
54 typedef QList<QgsGradientStop> QgsGradientStopsList;
55 
56 #define DEFAULT_GRADIENT_COLOR1 QColor(0,0,255)
57 #define DEFAULT_GRADIENT_COLOR2 QColor(0,255,0)
58 
60 {
61  public:
63  QColor color2 = DEFAULT_GRADIENT_COLOR2,
64  bool discrete = false,
66 
67  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
68 
69  virtual int count() const override { return mStops.count() + 2; }
70 
71  virtual double value( int index ) const override;
72 
73  virtual QColor color( double value ) const override;
74 
75  virtual QString type() const override { return "gradient"; }
76 
77  virtual QgsVectorColorRampV2* clone() const override;
78 
79  virtual QgsStringMap properties() const override;
80 
81  QColor color1() const { return mColor1; }
82  QColor color2() const { return mColor2; }
83  void setColor1( QColor color ) { mColor1 = color; }
84  void setColor2( QColor color ) { mColor2 = color; }
85 
86  bool isDiscrete() const { return mDiscrete; }
87  void setDiscrete( bool discrete ) { mDiscrete = discrete; }
88  void convertToDiscrete( bool discrete );
89 
90  void setStops( const QgsGradientStopsList& stops ) { mStops = stops; }
91  const QgsGradientStopsList& stops() const { return mStops; }
92 
93  QgsStringMap info() const { return mInfo; }
94  void setInfo( const QgsStringMap& info ) { mInfo = info; }
95 
98  void addStopsToGradient( QGradient* gradient, double alpha = 1 );
99 
100  protected:
101  QColor mColor1, mColor2;
102  bool mDiscrete;
105 };
106 
107 #define DEFAULT_RANDOM_COUNT 10
108 #define DEFAULT_RANDOM_HUE_MIN 0
109 #define DEFAULT_RANDOM_HUE_MAX 359
110 #define DEFAULT_RANDOM_VAL_MIN 200
111 #define DEFAULT_RANDOM_VAL_MAX 240
112 #define DEFAULT_RANDOM_SAT_MIN 100
113 #define DEFAULT_RANDOM_SAT_MAX 240
114 
116 {
117  public:
119  int hueMin = DEFAULT_RANDOM_HUE_MIN, int hueMax = DEFAULT_RANDOM_HUE_MAX,
120  int satMin = DEFAULT_RANDOM_SAT_MIN, int satMax = DEFAULT_RANDOM_SAT_MAX,
121  int valMin = DEFAULT_RANDOM_VAL_MIN, int valMax = DEFAULT_RANDOM_VAL_MAX );
122 
123  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
124 
125  virtual double value( int index ) const override;
126 
127  virtual QColor color( double value ) const override;
128 
129  virtual QString type() const override { return "random"; }
130 
131  virtual QgsVectorColorRampV2* clone() const override;
132 
133  virtual QgsStringMap properties() const override;
134 
137  static QList<QColor> randomColors( int count,
138  int hueMax = DEFAULT_RANDOM_HUE_MAX, int hueMin = DEFAULT_RANDOM_HUE_MIN,
139  int satMax = DEFAULT_RANDOM_SAT_MAX, int satMin = DEFAULT_RANDOM_SAT_MIN,
140  int valMax = DEFAULT_RANDOM_VAL_MAX, int valMin = DEFAULT_RANDOM_VAL_MIN );
141  void updateColors();
142 
143  int count() const override { return mCount; }
144  int hueMin() const { return mHueMin; }
145  int hueMax() const { return mHueMax; }
146  int satMin() const { return mSatMin; }
147  int satMax() const { return mSatMax; }
148  int valMin() const { return mValMin; }
149  int valMax() const { return mValMax; }
150 
151  void setCount( int val ) { mCount = val; }
152  void setHueMin( int val ) { mHueMin = val; }
153  void setHueMax( int val ) { mHueMax = val; }
154  void setSatMin( int val ) { mSatMin = val; }
155  void setSatMax( int val ) { mSatMax = val; }
156  void setValMin( int val ) { mValMin = val; }
157  void setValMax( int val ) { mValMax = val; }
158 
159  protected:
160  int mCount;
161  int mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax;
162  QList<QColor> mColors;
163 };
164 
165 class CORE_EXPORT QgsRandomColorsV2: public QgsVectorColorRampV2
166 {
167  public:
170 
171  int count() const override;
172 
173  double value( int index ) const override;
174 
175  QColor color( double value ) const override;
176 
177  /*Sets the desired total number of unique colors for the resultant ramp. Calling
178  * this method pregenerates a set of visually distinct colors which are returned
179  * by subsequent calls to color().
180  * @param colorCount number of unique colors
181  * @note added in QGIS 2.5
182  */
183  virtual void setTotalColorCount( const int colorCount );
184 
185  QString type() const override;
186 
187  QgsVectorColorRampV2* clone() const override;
188 
189  QgsStringMap properties() const override;
190 
191  protected:
192 
194  QList<QColor> mPrecalculatedColors;
195 
196 };
197 
198 
199 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
200 #define DEFAULT_COLORBREWER_COLORS 5
201 
203 {
204  public:
206  int colors = DEFAULT_COLORBREWER_COLORS );
207 
208  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
209 
210  virtual double value( int index ) const override;
211 
212  virtual QColor color( double value ) const override;
213 
214  virtual QString type() const override { return "colorbrewer"; }
215 
216  virtual QgsVectorColorRampV2* clone() const override;
217 
218  virtual QgsStringMap properties() const override;
219 
220  QString schemeName() const { return mSchemeName; }
221  virtual int count() const override { return mColors; }
222  int colors() const { return mColors; }
223 
224  void setSchemeName( QString schemeName ) { mSchemeName = schemeName; loadPalette(); }
225  void setColors( int colors ) { mColors = colors; loadPalette(); }
226 
227  static QStringList listSchemeNames();
228  static QList<int> listSchemeVariants( QString schemeName );
229 
230  protected:
231 
232  void loadPalette();
233 
234  QString mSchemeName;
235  int mColors;
236  QList<QColor> mPalette;
237 };
238 
239 
240 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
241 #define DEFAULT_CPTCITY_VARIANTNAME "05"
242 
244 {
245  public:
247  QString variantName = DEFAULT_CPTCITY_VARIANTNAME,
248  bool doLoadFile = true );
249  QgsCptCityColorRampV2( QString schemeName, QStringList variantList,
250  QString variantName = QString(), bool doLoadFile = true );
251 
252  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
253 
254  virtual QString type() const override { return "cpt-city"; }
255 
256  virtual QgsVectorColorRampV2* clone() const override;
257  void copy( const QgsCptCityColorRampV2* other );
258  QgsVectorGradientColorRampV2* cloneGradientRamp() const;
259 
260  virtual QgsStringMap properties() const override;
261 
262  QString schemeName() const { return mSchemeName; }
263  QString variantName() const { return mVariantName; }
264  QStringList variantList() const { return mVariantList; }
265 
266  /* lazy loading - have to call loadPalette() explicitly */
267  void setSchemeName( QString schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
268  void setVariantName( QString variantName ) { mVariantName = variantName; mFileLoaded = false; }
269  void setVariantList( QStringList variantList ) { mVariantList = variantList; }
270  void setName( QString schemeName, QString variantName = "", QStringList variantList = QStringList() )
271  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
272 
273  void loadPalette() { loadFile(); }
274  bool hasMultiStops() const { return mMultiStops; }
275 
276  QString fileName() const;
277  bool loadFile();
278  bool fileLoaded() const { return mFileLoaded; }
279 
280  QString copyingFileName() const;
281  QString descFileName() const;
282  QgsStringMap copyingInfo() const;
283 
284  protected:
285 
286  QString mSchemeName;
287  QString mVariantName;
288  QStringList mVariantList;
291 };
292 
293 
294 #endif