QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
285 class CORE_EXPORT QgsRandomColorsV2: public QgsVectorColorRampV2
286 {
287  public:
290 
291  int count() const override;
292 
293  double value( int index ) const override;
294 
295  QColor color( double value ) const override;
296 
303  virtual void setTotalColorCount( const int colorCount );
304 
305  QString type() const override;
306 
307  QgsRandomColorsV2* clone() const override;
308 
309  QgsStringMap properties() const override;
310 
311  protected:
312 
315 
316 };
317 
318 
319 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
320 #define DEFAULT_COLORBREWER_COLORS 5
321 
326 {
327  public:
329  int colors = DEFAULT_COLORBREWER_COLORS );
330 
331  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
332 
333  virtual double value( int index ) const override;
334 
335  virtual QColor color( double value ) const override;
336 
337  virtual QString type() const override { return "colorbrewer"; }
338 
339  virtual QgsVectorColorBrewerColorRampV2* clone() const override;
340 
341  virtual QgsStringMap properties() const override;
342 
343  QString schemeName() const { return mSchemeName; }
344  virtual int count() const override { return mColors; }
345  int colors() const { return mColors; }
346 
347  void setSchemeName( const QString& schemeName ) { mSchemeName = schemeName; loadPalette(); }
348  void setColors( int colors ) { mColors = colors; loadPalette(); }
349 
350  static QStringList listSchemeNames();
351  static QList<int> listSchemeVariants( const QString& schemeName );
352 
353  protected:
354 
355  void loadPalette();
356 
358  int mColors;
360 };
361 
362 
363 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
364 #define DEFAULT_CPTCITY_VARIANTNAME "05"
365 
370 {
371  public:
373  const QString& variantName = DEFAULT_CPTCITY_VARIANTNAME,
374  bool doLoadFile = true );
375  QgsCptCityColorRampV2( const QString& schemeName, const QStringList& variantList,
376  const QString& variantName = QString(), bool doLoadFile = true );
377 
378  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
379 
380  virtual QString type() const override { return "cpt-city"; }
381 
382  virtual QgsCptCityColorRampV2* clone() const override;
383  void copy( const QgsCptCityColorRampV2* other );
384  QgsVectorGradientColorRampV2* cloneGradientRamp() const;
385 
386  virtual QgsStringMap properties() const override;
387 
388  QString schemeName() const { return mSchemeName; }
389  QString variantName() const { return mVariantName; }
390  QStringList variantList() const { return mVariantList; }
391 
392  /* lazy loading - have to call loadPalette() explicitly */
393  void setSchemeName( const QString& schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
394  void setVariantName( const QString& variantName ) { mVariantName = variantName; mFileLoaded = false; }
395  void setVariantList( const QStringList& variantList ) { mVariantList = variantList; }
396  void setName( const QString& schemeName, const QString& variantName = "", const QStringList& variantList = QStringList() )
397  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
398 
399  void loadPalette() { loadFile(); }
400  bool hasMultiStops() const { return mMultiStops; }
401 
402  QString fileName() const;
403  bool loadFile();
404  bool fileLoaded() const { return mFileLoaded; }
405 
406  QString copyingFileName() const;
407  QString descFileName() const;
408  QgsStringMap copyingInfo() const;
409 
410  protected:
411 
417 };
418 
419 
420 #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.
#define DEFAULT_COLORBREWER_SCHEMENAME
#define DEFAULT_RANDOM_HUE_MIN
QColor color2() const
Returns the gradient end color.
void setInfo(const QgsStringMap &info)
Sets additional info to attach to the gradient ramp (eg authorship notes)
QColor color1() const
Returns the gradient start color.
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.
bool isDiscrete() const
Returns true if the gradient is using discrete interpolation, rather than smoothly interpolating betw...
#define DEFAULT_CPTCITY_SCHEMENAME
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
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:492
#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:353
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.
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (eg authorship notes)
void setVariantName(const QString &variantName)
#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
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 operator==(const QgsGradientStop &other) const
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 ...
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
QStringList variantList() const
QColor color
Gradient color at stop.