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 )
76  : offset( o )
77  , color( c )
78  { }
79 
81  double offset;
84 };
85 
88 
89 #define DEFAULT_GRADIENT_COLOR1 QColor(0,0,255)
90 #define DEFAULT_GRADIENT_COLOR2 QColor(0,255,0)
91 
97 //TODO QGIS 3.0 - rename to QgsGradientColorRamp, since this is used by much more than just vectors
99 {
100  public:
101 
110  const QColor& color2 = DEFAULT_GRADIENT_COLOR2,
111  bool discrete = false,
112  const QgsGradientStopsList& stops = QgsGradientStopsList() );
113 
115  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
116 
117  virtual int count() const override { return mStops.count() + 2; }
118  virtual double value( int index ) const override;
119  virtual QColor color( double value ) const override;
120  virtual QString type() const override { return "gradient"; }
121  virtual QgsVectorGradientColorRampV2* clone() const override;
122  virtual QgsStringMap properties() const override;
123 
128  QColor color1() const { return mColor1; }
129 
134  QColor color2() const { return mColor2; }
135 
141  void setColor1( const QColor& color ) { mColor1 = color; }
142 
148  void setColor2( const QColor& color ) { mColor2 = color; }
149 
154  bool isDiscrete() const { return mDiscrete; }
155 
162  void setDiscrete( bool discrete ) { mDiscrete = discrete; }
163 
170  void convertToDiscrete( bool discrete );
171 
176  void setStops( const QgsGradientStopsList& stops ) { mStops = stops; }
177 
181  QgsGradientStopsList stops() const { return mStops; }
182 
186  QgsStringMap info() const { return mInfo; }
187 
192  void setInfo( const QgsStringMap& info ) { mInfo = info; }
193 
200  void addStopsToGradient( QGradient* gradient, double alpha = 1 );
201 
202  protected:
205  bool mDiscrete;
208 };
209 
210 #define DEFAULT_RANDOM_COUNT 10
211 #define DEFAULT_RANDOM_HUE_MIN 0
212 #define DEFAULT_RANDOM_HUE_MAX 359
213 #define DEFAULT_RANDOM_VAL_MIN 200
214 #define DEFAULT_RANDOM_VAL_MAX 240
215 #define DEFAULT_RANDOM_SAT_MIN 100
216 #define DEFAULT_RANDOM_SAT_MAX 240
217 
222 //TODO QGIS 3.0 - rename to QgsRandomColorRamp, since this is used by much more than just vectors
224 {
225  public:
227  int hueMin = DEFAULT_RANDOM_HUE_MIN, int hueMax = DEFAULT_RANDOM_HUE_MAX,
228  int satMin = DEFAULT_RANDOM_SAT_MIN, int satMax = DEFAULT_RANDOM_SAT_MAX,
229  int valMin = DEFAULT_RANDOM_VAL_MIN, int valMax = DEFAULT_RANDOM_VAL_MAX );
230 
231  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
232 
233  virtual double value( int index ) const override;
234 
235  virtual QColor color( double value ) const override;
236 
237  virtual QString type() const override { return "random"; }
238 
239  virtual QgsVectorRandomColorRampV2* clone() const override;
240 
241  virtual QgsStringMap properties() const override;
242 
246  static QList<QColor> randomColors( int count,
247  int hueMax = DEFAULT_RANDOM_HUE_MAX, int hueMin = DEFAULT_RANDOM_HUE_MIN,
248  int satMax = DEFAULT_RANDOM_SAT_MAX, int satMin = DEFAULT_RANDOM_SAT_MIN,
249  int valMax = DEFAULT_RANDOM_VAL_MAX, int valMin = DEFAULT_RANDOM_VAL_MIN );
250 
251  void updateColors();
252 
253  int count() const override { return mCount; }
254  int hueMin() const { return mHueMin; }
255  int hueMax() const { return mHueMax; }
256  int satMin() const { return mSatMin; }
257  int satMax() const { return mSatMax; }
258  int valMin() const { return mValMin; }
259  int valMax() const { return mValMax; }
260 
261  void setCount( int val ) { mCount = val; }
262  void setHueMin( int val ) { mHueMin = val; }
263  void setHueMax( int val ) { mHueMax = val; }
264  void setSatMin( int val ) { mSatMin = val; }
265  void setSatMax( int val ) { mSatMax = val; }
266  void setValMin( int val ) { mValMin = val; }
267  void setValMax( int val ) { mValMax = val; }
268 
269  protected:
270  int mCount;
271  int mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax;
273 };
274 
275 class CORE_EXPORT QgsRandomColorsV2: public QgsVectorColorRampV2
276 {
277  public:
280 
281  int count() const override;
282 
283  double value( int index ) const override;
284 
285  QColor color( double value ) const override;
286 
293  virtual void setTotalColorCount( const int colorCount );
294 
295  QString type() const override;
296 
297  QgsRandomColorsV2* clone() const override;
298 
299  QgsStringMap properties() const override;
300 
301  protected:
302 
305 
306 };
307 
308 
309 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
310 #define DEFAULT_COLORBREWER_COLORS 5
311 
313 {
314  public:
316  int colors = DEFAULT_COLORBREWER_COLORS );
317 
318  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
319 
320  virtual double value( int index ) const override;
321 
322  virtual QColor color( double value ) const override;
323 
324  virtual QString type() const override { return "colorbrewer"; }
325 
326  virtual QgsVectorColorBrewerColorRampV2* clone() const override;
327 
328  virtual QgsStringMap properties() const override;
329 
330  QString schemeName() const { return mSchemeName; }
331  virtual int count() const override { return mColors; }
332  int colors() const { return mColors; }
333 
334  void setSchemeName( const QString& schemeName ) { mSchemeName = schemeName; loadPalette(); }
335  void setColors( int colors ) { mColors = colors; loadPalette(); }
336 
337  static QStringList listSchemeNames();
338  static QList<int> listSchemeVariants( const QString& schemeName );
339 
340  protected:
341 
342  void loadPalette();
343 
345  int mColors;
347 };
348 
349 
350 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
351 #define DEFAULT_CPTCITY_VARIANTNAME "05"
352 
354 {
355  public:
357  const QString& variantName = DEFAULT_CPTCITY_VARIANTNAME,
358  bool doLoadFile = true );
359  QgsCptCityColorRampV2( const QString& schemeName, const QStringList& variantList,
360  const QString& variantName = QString(), bool doLoadFile = true );
361 
362  static QgsVectorColorRampV2* create( const QgsStringMap& properties = QgsStringMap() );
363 
364  virtual QString type() const override { return "cpt-city"; }
365 
366  virtual QgsCptCityColorRampV2* clone() const override;
367  void copy( const QgsCptCityColorRampV2* other );
368  QgsVectorGradientColorRampV2* cloneGradientRamp() const;
369 
370  virtual QgsStringMap properties() const override;
371 
372  QString schemeName() const { return mSchemeName; }
373  QString variantName() const { return mVariantName; }
374  QStringList variantList() const { return mVariantList; }
375 
376  /* lazy loading - have to call loadPalette() explicitly */
377  void setSchemeName( const QString& schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
378  void setVariantName( const QString& variantName ) { mVariantName = variantName; mFileLoaded = false; }
379  void setVariantList( const QStringList& variantList ) { mVariantList = variantList; }
380  void setName( const QString& schemeName, const QString& variantName = "", const QStringList& variantList = QStringList() )
381  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
382 
383  void loadPalette() { loadFile(); }
384  bool hasMultiStops() const { return mMultiStops; }
385 
386  QString fileName() const;
387  bool loadFile();
388  bool fileLoaded() const { return mFileLoaded; }
389 
390  QString copyingFileName() const;
391  QString descFileName() const;
392  QgsStringMap copyingInfo() const;
393 
394  protected:
395 
401 };
402 
403 
404 #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.