QGIS API Documentation  2.15.0-Master (972fc9f)
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 
29 //TODO QGIS 3.0 - rename to QgsColorRamp, since this is used by much more than just vectors
30 class CORE_EXPORT QgsVectorColorRampV2
31 {
32  public:
33 
34  virtual ~QgsVectorColorRampV2() {}
35 
38  virtual int count() const = 0;
39 
42  virtual double value( int index ) const = 0;
43 
48  virtual QColor color( double value ) const = 0;
49 
52  virtual QString type() const = 0;
53 
56  virtual QgsVectorColorRampV2* clone() const = 0;
57 
60  virtual QgsStringMap properties() const = 0;
61 };
62 
67 class CORE_EXPORT QgsGradientStop
68 {
69  public:
70 
75  QgsGradientStop( double o, const QColor& c )
76  : offset( o )
77  , color( c )
78  { }
79 
81  double offset;
84 
85  bool operator==( const QgsGradientStop& other ) const
86  {
87  return other.color == color && qgsDoubleNear( other.offset, offset );
88  }
89 };
90 
93 
94 #define DEFAULT_GRADIENT_COLOR1 QColor(0,0,255)
95 #define DEFAULT_GRADIENT_COLOR2 QColor(0,255,0)
96 
102 //TODO QGIS 3.0 - rename to QgsGradientColorRamp, since this is used by much more than just vectors
104 {
105  public:
106 
115  const QColor& color2 = DEFAULT_GRADIENT_COLOR2,
116  bool discrete = false,
117  const QgsGradientStopsList& stops = QgsGradientStopsList() );
118 
120  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
121 
122  virtual int count() const override { return mStops.count() + 2; }
123  virtual double value( int index ) const override;
124  virtual QColor color( double value ) const override;
125  virtual QString type() const override { return "gradient"; }
126  virtual QgsVectorGradientColorRampV2* clone() const override;
127  virtual QgsStringMap properties() const override;
128 
133  QColor color1() const { return mColor1; }
134 
139  QColor color2() const { return mColor2; }
140 
146  void setColor1( const QColor& color ) { mColor1 = color; }
147 
153  void setColor2( const QColor& color ) { mColor2 = color; }
154 
159  bool isDiscrete() const { return mDiscrete; }
160 
167  void setDiscrete( bool discrete ) { mDiscrete = discrete; }
168 
175  void convertToDiscrete( bool discrete );
176 
183  void setStops( const QgsGradientStopsList& stops );
184 
188  QgsGradientStopsList stops() const { return mStops; }
189 
193  QgsStringMap info() const { return mInfo; }
194 
199  void setInfo( const QgsStringMap& info ) { mInfo = info; }
200 
207  void addStopsToGradient( QGradient* gradient, double alpha = 1 );
208 
209  protected:
212  bool mDiscrete;
215 };
216 
217 #define DEFAULT_RANDOM_COUNT 10
218 #define DEFAULT_RANDOM_HUE_MIN 0
219 #define DEFAULT_RANDOM_HUE_MAX 359
220 #define DEFAULT_RANDOM_VAL_MIN 200
221 #define DEFAULT_RANDOM_VAL_MAX 240
222 #define DEFAULT_RANDOM_SAT_MIN 100
223 #define DEFAULT_RANDOM_SAT_MAX 240
224 
229 //TODO QGIS 3.0 - rename to QgsRandomColorRamp, since this is used by much more than just vectors
231 {
232  public:
234  int hueMin = DEFAULT_RANDOM_HUE_MIN, int hueMax = DEFAULT_RANDOM_HUE_MAX,
235  int satMin = DEFAULT_RANDOM_SAT_MIN, int satMax = DEFAULT_RANDOM_SAT_MAX,
236  int valMin = DEFAULT_RANDOM_VAL_MIN, int valMax = DEFAULT_RANDOM_VAL_MAX );
237 
238  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
239 
240  virtual double value( int index ) const override;
241 
242  virtual QColor color( double value ) const override;
243 
244  virtual QString type() const override { return "random"; }
245 
246  virtual QgsVectorRandomColorRampV2* clone() const override;
247 
248  virtual QgsStringMap properties() const override;
249 
253  static QList<QColor> randomColors( int count,
254  int hueMax = DEFAULT_RANDOM_HUE_MAX, int hueMin = DEFAULT_RANDOM_HUE_MIN,
255  int satMax = DEFAULT_RANDOM_SAT_MAX, int satMin = DEFAULT_RANDOM_SAT_MIN,
256  int valMax = DEFAULT_RANDOM_VAL_MAX, int valMin = DEFAULT_RANDOM_VAL_MIN );
257 
258  void updateColors();
259 
260  int count() const override { return mCount; }
261  int hueMin() const { return mHueMin; }
262  int hueMax() const { return mHueMax; }
263  int satMin() const { return mSatMin; }
264  int satMax() const { return mSatMax; }
265  int valMin() const { return mValMin; }
266  int valMax() const { return mValMax; }
267 
268  void setCount( int val ) { mCount = val; }
269  void setHueMin( int val ) { mHueMin = val; }
270  void setHueMax( int val ) { mHueMax = val; }
271  void setSatMin( int val ) { mSatMin = val; }
272  void setSatMax( int val ) { mSatMax = val; }
273  void setValMin( int val ) { mValMin = val; }
274  void setValMax( int val ) { mValMax = val; }
275 
276  protected:
277  int mCount;
278  int mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax;
280 };
281 
282 class CORE_EXPORT QgsRandomColorsV2: public QgsVectorColorRampV2
283 {
284  public:
287 
288  int count() const override;
289 
290  double value( int index ) const override;
291 
292  QColor color( double value ) const override;
293 
300  virtual void setTotalColorCount( const int colorCount );
301 
302  QString type() const override;
303 
304  QgsRandomColorsV2* clone() const override;
305 
306  QgsStringMap properties() const override;
307 
308  protected:
309 
312 
313 };
314 
315 
316 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
317 #define DEFAULT_COLORBREWER_COLORS 5
318 
320 {
321  public:
323  int colors = DEFAULT_COLORBREWER_COLORS );
324 
325  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
326 
327  virtual double value( int index ) const override;
328 
329  virtual QColor color( double value ) const override;
330 
331  virtual QString type() const override { return "colorbrewer"; }
332 
333  virtual QgsVectorColorBrewerColorRampV2* clone() const override;
334 
335  virtual QgsStringMap properties() const override;
336 
337  QString schemeName() const { return mSchemeName; }
338  virtual int count() const override { return mColors; }
339  int colors() const { return mColors; }
340 
341  void setSchemeName( const QString& schemeName ) { mSchemeName = schemeName; loadPalette(); }
342  void setColors( int colors ) { mColors = colors; loadPalette(); }
343 
344  static QStringList listSchemeNames();
345  static QList<int> listSchemeVariants( const QString& schemeName );
346 
347  protected:
348 
349  void loadPalette();
350 
352  int mColors;
354 };
355 
356 
357 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
358 #define DEFAULT_CPTCITY_VARIANTNAME "05"
359 
361 {
362  public:
364  const QString& variantName = DEFAULT_CPTCITY_VARIANTNAME,
365  bool doLoadFile = true );
366  QgsCptCityColorRampV2( const QString& schemeName, const QStringList& variantList,
367  const QString& variantName = QString(), bool doLoadFile = true );
368 
369  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
370 
371  virtual QString type() const override { return "cpt-city"; }
372 
373  virtual QgsCptCityColorRampV2* clone() const override;
374  void copy( const QgsCptCityColorRampV2* other );
375  QgsVectorGradientColorRampV2* cloneGradientRamp() const;
376 
377  virtual QgsStringMap properties() const override;
378 
379  QString schemeName() const { return mSchemeName; }
380  QString variantName() const { return mVariantName; }
381  QStringList variantList() const { return mVariantList; }
382 
383  /* lazy loading - have to call loadPalette() explicitly */
384  void setSchemeName( const QString& schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
385  void setVariantName( const QString& variantName ) { mVariantName = variantName; mFileLoaded = false; }
386  void setVariantList( const QStringList& variantList ) { mVariantList = variantList; }
387  void setName( const QString& schemeName, const QString& variantName = "", const QStringList& variantList = QStringList() )
388  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
389 
390  void loadPalette() { loadFile(); }
391  bool hasMultiStops() const { return mMultiStops; }
392 
393  QString fileName() const;
394  bool loadFile();
395  bool fileLoaded() const { return mFileLoaded; }
396 
397  QString copyingFileName() const;
398  QString descFileName() const;
399  QgsStringMap copyingInfo() const;
400 
401  protected:
402 
408 };
409 
410 
411 #endif
static unsigned index
int count() const override
Returns number of defined colors, or -1 if undefined.
virtual QString type() const =0
Returns a string representing the color ramp type.
void setName(const QString &schemeName, const QString &variantName="", const QStringList &variantList=QStringList())
#define DEFAULT_CPTCITY_VARIANTNAME
Represents a color stop within a gradient color ramp.
bool operator==(const QgsGradientStop &other) const
#define DEFAULT_COLORBREWER_SCHEMENAME
#define DEFAULT_RANDOM_HUE_MIN
void setInfo(const QgsStringMap &info)
Sets additional info to attach to the gradient ramp (eg authorship notes)
virtual QgsVectorGradientColorRampV2 * clone() const override
Creates a clone of the color ramp.
virtual QgsStringMap properties() const =0
Returns a string map containing all the color ramp&#39;s properties.
#define DEFAULT_CPTCITY_SCHEMENAME
void setColor2(const QColor &color)
Sets the gradient end color.
virtual QString type() const override
Returns a string representing the color ramp type.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:491
#define DEFAULT_RANDOM_SAT_MAX
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:352
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (eg authorship notes)
void setColor1(const QColor &color)
Sets the gradient start color.
virtual QgsStringMap properties() const override
Returns a string map containing all the color ramp&#39;s properties.
virtual int count() const override
Returns number of defined colors, or -1 if undefined.
QString variantName() const
void setVariantName(const QString &variantName)
QStringList variantList() const
#define DEFAULT_GRADIENT_COLOR2
void setSchemeName(const QString &schemeName)
virtual double value(int index) const =0
Returns relative value between [0,1] of color at specified index.
virtual QgsVectorColorRampV2 * clone() const =0
Creates a clone of the color ramp.
double offset
Relative positional offset, between 0 and 1.
virtual QString type() const override
Returns a string representing the color ramp type.
#define DEFAULT_GRADIENT_COLOR1
#define DEFAULT_RANDOM_VAL_MAX
virtual QColor color(double value) const =0
Returns the color corresponding to a specified value.
QgsGradientStop(double o, const QColor &c)
Constructor for QgsGradientStop.
virtual QString type() const override
Returns a string representing the color ramp type.
#define DEFAULT_RANDOM_SAT_MIN
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
static QgsVectorColorRampV2 * create(const QgsStringMap &properties=QgsStringMap())
Creates a new QgsVectorColorRampV2 from a map of properties.
Random color ramp, which returns random colors based on preset parameters.
virtual int count() const =0
Returns number of defined colors, or -1 if undefined.
QList< QgsGradientStop > QgsGradientStopsList
List of gradient stops.
void setSchemeName(const QString &schemeName)
virtual QString type() const override
Returns a string representing the color ramp type.
#define DEFAULT_COLORBREWER_COLORS
bool isDiscrete() const
Returns true if the gradient is using discrete interpolation, rather than smoothly interpolating betw...
QList< QColor > mPrecalculatedColors
#define DEFAULT_RANDOM_HUE_MAX
#define DEFAULT_RANDOM_VAL_MIN
Abstract base class for color ramps.
void setVariantList(const QStringList &variantList)
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
QColor color2() const
Returns the gradient end color.
QColor color1() const
Returns the gradient start color.
virtual int count() const override
Returns number of defined colors, or -1 if undefined.
void setDiscrete(bool discrete)
Sets whether the gradient should use discrete interpolation, rather than smoothly interpolating betwe...
#define DEFAULT_RANDOM_COUNT
QColor color
Gradient color at stop.