QGIS API Documentation
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 ) : offset( o ), color( c ) { }
76 
78  double offset;
81 };
82 
85 
86 #define DEFAULT_GRADIENT_COLOR1 QColor(0,0,255)
87 #define DEFAULT_GRADIENT_COLOR2 QColor(0,255,0)
88 
94 //TODO QGIS 3.0 - rename to QgsGradientColorRamp, since this is used by much more than just vectors
96 {
97  public:
98 
107  const QColor& color2 = DEFAULT_GRADIENT_COLOR2,
108  bool discrete = false,
109  const QgsGradientStopsList& stops = QgsGradientStopsList() );
110 
112  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
113 
114  virtual int count() const override { return mStops.count() + 2; }
115  virtual double value( int index ) const override;
116  virtual QColor color( double value ) const override;
117  virtual QString type() const override { return "gradient"; }
118  virtual QgsVectorGradientColorRampV2* clone() const override;
119  virtual QgsStringMap properties() const override;
120 
125  QColor color1() const { return mColor1; }
126 
131  QColor color2() const { return mColor2; }
132 
138  void setColor1( const QColor& color ) { mColor1 = color; }
139 
145  void setColor2( const QColor& color ) { mColor2 = color; }
146 
151  bool isDiscrete() const { return mDiscrete; }
152 
159  void setDiscrete( bool discrete ) { mDiscrete = discrete; }
160 
167  void convertToDiscrete( bool discrete );
168 
173  void setStops( const QgsGradientStopsList& stops ) { mStops = stops; }
174 
178  QgsGradientStopsList stops() const { return mStops; }
179 
183  QgsStringMap info() const { return mInfo; }
184 
189  void setInfo( const QgsStringMap& info ) { mInfo = info; }
190 
197  void addStopsToGradient( QGradient* gradient, double alpha = 1 );
198 
199  protected:
202  bool mDiscrete;
205 };
206 
207 #define DEFAULT_RANDOM_COUNT 10
208 #define DEFAULT_RANDOM_HUE_MIN 0
209 #define DEFAULT_RANDOM_HUE_MAX 359
210 #define DEFAULT_RANDOM_VAL_MIN 200
211 #define DEFAULT_RANDOM_VAL_MAX 240
212 #define DEFAULT_RANDOM_SAT_MIN 100
213 #define DEFAULT_RANDOM_SAT_MAX 240
214 
219 //TODO QGIS 3.0 - rename to QgsRandomColorRamp, since this is used by much more than just vectors
221 {
222  public:
224  int hueMin = DEFAULT_RANDOM_HUE_MIN, int hueMax = DEFAULT_RANDOM_HUE_MAX,
225  int satMin = DEFAULT_RANDOM_SAT_MIN, int satMax = DEFAULT_RANDOM_SAT_MAX,
226  int valMin = DEFAULT_RANDOM_VAL_MIN, int valMax = DEFAULT_RANDOM_VAL_MAX );
227 
228  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
229 
230  virtual double value( int index ) const override;
231 
232  virtual QColor color( double value ) const override;
233 
234  virtual QString type() const override { return "random"; }
235 
236  virtual QgsVectorRandomColorRampV2* clone() const override;
237 
238  virtual QgsStringMap properties() const override;
239 
243  static QList<QColor> randomColors( int count,
244  int hueMax = DEFAULT_RANDOM_HUE_MAX, int hueMin = DEFAULT_RANDOM_HUE_MIN,
245  int satMax = DEFAULT_RANDOM_SAT_MAX, int satMin = DEFAULT_RANDOM_SAT_MIN,
246  int valMax = DEFAULT_RANDOM_VAL_MAX, int valMin = DEFAULT_RANDOM_VAL_MIN );
247 
248  void updateColors();
249 
250  int count() const override { return mCount; }
251  int hueMin() const { return mHueMin; }
252  int hueMax() const { return mHueMax; }
253  int satMin() const { return mSatMin; }
254  int satMax() const { return mSatMax; }
255  int valMin() const { return mValMin; }
256  int valMax() const { return mValMax; }
257 
258  void setCount( int val ) { mCount = val; }
259  void setHueMin( int val ) { mHueMin = val; }
260  void setHueMax( int val ) { mHueMax = val; }
261  void setSatMin( int val ) { mSatMin = val; }
262  void setSatMax( int val ) { mSatMax = val; }
263  void setValMin( int val ) { mValMin = val; }
264  void setValMax( int val ) { mValMax = val; }
265 
266  protected:
267  int mCount;
268  int mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax;
270 };
271 
272 class CORE_EXPORT QgsRandomColorsV2: public QgsVectorColorRampV2
273 {
274  public:
277 
278  int count() const override;
279 
280  double value( int index ) const override;
281 
282  QColor color( double value ) const override;
283 
290  virtual void setTotalColorCount( const int colorCount );
291 
292  QString type() const override;
293 
294  QgsRandomColorsV2* clone() const override;
295 
296  QgsStringMap properties() const override;
297 
298  protected:
299 
302 
303 };
304 
305 
306 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
307 #define DEFAULT_COLORBREWER_COLORS 5
308 
310 {
311  public:
313  int colors = DEFAULT_COLORBREWER_COLORS );
314 
315  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
316 
317  virtual double value( int index ) const override;
318 
319  virtual QColor color( double value ) const override;
320 
321  virtual QString type() const override { return "colorbrewer"; }
322 
323  virtual QgsVectorColorBrewerColorRampV2* clone() const override;
324 
325  virtual QgsStringMap properties() const override;
326 
327  QString schemeName() const { return mSchemeName; }
328  virtual int count() const override { return mColors; }
329  int colors() const { return mColors; }
330 
331  void setSchemeName( const QString& schemeName ) { mSchemeName = schemeName; loadPalette(); }
332  void setColors( int colors ) { mColors = colors; loadPalette(); }
333 
334  static QStringList listSchemeNames();
335  static QList<int> listSchemeVariants( const QString& schemeName );
336 
337  protected:
338 
339  void loadPalette();
340 
342  int mColors;
344 };
345 
346 
347 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
348 #define DEFAULT_CPTCITY_VARIANTNAME "05"
349 
351 {
352  public:
354  const QString& variantName = DEFAULT_CPTCITY_VARIANTNAME,
355  bool doLoadFile = true );
356  QgsCptCityColorRampV2( const QString& schemeName, const QStringList& variantList,
357  const QString& variantName = QString(), bool doLoadFile = true );
358 
359  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
360 
361  virtual QString type() const override { return "cpt-city"; }
362 
363  virtual QgsCptCityColorRampV2* clone() const override;
364  void copy( const QgsCptCityColorRampV2* other );
365  QgsVectorGradientColorRampV2* cloneGradientRamp() const;
366 
367  virtual QgsStringMap properties() const override;
368 
369  QString schemeName() const { return mSchemeName; }
370  QString variantName() const { return mVariantName; }
371  QStringList variantList() const { return mVariantList; }
372 
373  /* lazy loading - have to call loadPalette() explicitly */
374  void setSchemeName( const QString& schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
375  void setVariantName( const QString& variantName ) { mVariantName = variantName; mFileLoaded = false; }
376  void setVariantList( const QStringList& variantList ) { mVariantList = variantList; }
377  void setName( const QString& schemeName, const QString& variantName = "", const QStringList& variantList = QStringList() )
378  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
379 
380  void loadPalette() { loadFile(); }
381  bool hasMultiStops() const { return mMultiStops; }
382 
383  QString fileName() const;
384  bool loadFile();
385  bool fileLoaded() const { return mFileLoaded; }
386 
387  QString copyingFileName() const;
388  QString descFileName() const;
389  QgsStringMap copyingInfo() const;
390 
391  protected:
392 
398 };
399 
400 
401 #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
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:451
#define DEFAULT_RANDOM_SAT_MAX
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
void setStops(const QgsGradientStopsList &stops)
Sets the list of intermediate gradient stops for the ramp.
#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.