QGIS API Documentation  3.0.2-Girona (307d082)
qgscolorramp.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolorramp.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 QGSCOLORRAMP_H
17 #define QGSCOLORRAMP_H
18 
19 #include "qgis_core.h"
20 #include <QColor>
21 #include <QGradient>
22 #include "qgis.h"
23 #include "qgscolorscheme.h"
24 
31 class CORE_EXPORT QgsColorRamp
32 {
33 
34 #ifdef SIP_RUN
36  if ( sipCpp->type() == "gradient" )
37  sipType = sipType_QgsGradientColorRamp;
38  else if ( sipCpp->type() == "random" )
39  sipType = sipType_QgsLimitedRandomColorRamp;
40  else if ( sipCpp->type() == "randomcolors" )
41  sipType = sipType_QgsRandomColorRamp;
42  else if ( sipCpp->type() == "preset" )
43  sipType = sipType_QgsPresetSchemeColorRamp;
44  else if ( sipCpp->type() == "colorbrewer" )
45  sipType = sipType_QgsColorBrewerColorRamp;
46  else if ( sipCpp->type() == "cpt-city" )
47  sipType = sipType_QgsCptCityColorRamp;
48  else
49  sipType = 0;
50  SIP_END
51 #endif
52  public:
53 
54  virtual ~QgsColorRamp() = default;
55 
59  virtual int count() const = 0;
60 
64  virtual double value( int index ) const = 0;
65 
71  virtual QColor color( double value ) const = 0;
72 
76  virtual QString type() const = 0;
77 
78 
82  virtual void invert() {}
83 
87  virtual QgsColorRamp *clone() const = 0 SIP_FACTORY;
88 
92  virtual QgsStringMap properties() const = 0;
93 };
94 
101 class CORE_EXPORT QgsGradientStop
102 {
103  public:
104 
110  QgsGradientStop( double offset, const QColor &color )
111  : offset( offset )
112  , color( color )
113  { }
114 
116  double offset;
118  QColor color;
119 
120  bool operator==( const QgsGradientStop &other ) const
121  {
122  return other.color == color && qgsDoubleNear( other.offset, offset );
123  }
124 };
125 
127 typedef QList<QgsGradientStop> QgsGradientStopsList;
128 
129 #define DEFAULT_GRADIENT_COLOR1 QColor(0,0,255)
130 #define DEFAULT_GRADIENT_COLOR2 QColor(0,255,0)
131 
139 class CORE_EXPORT QgsGradientColorRamp : public QgsColorRamp
140 {
141  public:
142 
151  QgsGradientColorRamp( const QColor &color1 = DEFAULT_GRADIENT_COLOR1,
152  const QColor &color2 = DEFAULT_GRADIENT_COLOR2,
153  bool discrete = false,
154  const QgsGradientStopsList &stops = QgsGradientStopsList() );
155 
157  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
158 
159  int count() const override { return mStops.count() + 2; }
160  double value( int index ) const override;
161  QColor color( double value ) const override;
162  QString type() const override { return QStringLiteral( "gradient" ); }
163  void invert() override;
164  QgsGradientColorRamp *clone() const override SIP_FACTORY;
165  QgsStringMap properties() const override;
166 
172  QColor color1() const { return mColor1; }
173 
179  QColor color2() const { return mColor2; }
180 
187  void setColor1( const QColor &color ) { mColor1 = color; }
188 
195  void setColor2( const QColor &color ) { mColor2 = color; }
196 
202  bool isDiscrete() const { return mDiscrete; }
203 
211  void setDiscrete( bool discrete ) { mDiscrete = discrete; }
212 
220  void convertToDiscrete( bool discrete );
221 
229  void setStops( const QgsGradientStopsList &stops );
230 
235  QgsGradientStopsList stops() const { return mStops; }
236 
241  QgsStringMap info() const { return mInfo; }
242 
248  void setInfo( const QgsStringMap &info ) { mInfo = info; }
249 
257  void addStopsToGradient( QGradient *gradient, double opacity = 1 );
258 
259  protected:
260  QColor mColor1;
261  QColor mColor2;
262  bool mDiscrete;
265 };
266 
268 
269 #define DEFAULT_RANDOM_COUNT 10
270 #define DEFAULT_RANDOM_HUE_MIN 0
271 #define DEFAULT_RANDOM_HUE_MAX 359
272 #define DEFAULT_RANDOM_VAL_MIN 200
273 #define DEFAULT_RANDOM_VAL_MAX 240
274 #define DEFAULT_RANDOM_SAT_MIN 100
275 #define DEFAULT_RANDOM_SAT_MAX 240
276 
283 class CORE_EXPORT QgsLimitedRandomColorRamp : public QgsColorRamp
284 {
285  public:
286 
298  int hueMin = DEFAULT_RANDOM_HUE_MIN, int hueMax = DEFAULT_RANDOM_HUE_MAX,
299  int satMin = DEFAULT_RANDOM_SAT_MIN, int satMax = DEFAULT_RANDOM_SAT_MAX,
300  int valMin = DEFAULT_RANDOM_VAL_MIN, int valMax = DEFAULT_RANDOM_VAL_MAX );
301 
308  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
309 
310  double value( int index ) const override;
311  QColor color( double value ) const override;
312  QString type() const override { return QStringLiteral( "random" ); }
313  QgsLimitedRandomColorRamp *clone() const override SIP_FACTORY;
314  QgsStringMap properties() const override;
315  int count() const override { return mCount; }
316 
321  static QList<QColor> randomColors( int count,
322  int hueMax = DEFAULT_RANDOM_HUE_MAX, int hueMin = DEFAULT_RANDOM_HUE_MIN,
323  int satMax = DEFAULT_RANDOM_SAT_MAX, int satMin = DEFAULT_RANDOM_SAT_MIN,
324  int valMax = DEFAULT_RANDOM_VAL_MAX, int valMin = DEFAULT_RANDOM_VAL_MIN );
325 
330  void updateColors();
331 
336  int hueMin() const { return mHueMin; }
337 
342  int hueMax() const { return mHueMax; }
343 
348  int satMin() const { return mSatMin; }
349 
354  int satMax() const { return mSatMax; }
355 
360  int valMin() const { return mValMin; }
361 
366  int valMax() const { return mValMax; }
367 
371  void setCount( int val ) { mCount = val; }
372 
377  void setHueMin( int val ) { mHueMin = val; }
378 
383  void setHueMax( int val ) { mHueMax = val; }
384 
389  void setSatMin( int val ) { mSatMin = val; }
390 
395  void setSatMax( int val ) { mSatMax = val; }
396 
401  void setValMin( int val ) { mValMin = val; }
402 
407  void setValMax( int val ) { mValMax = val; }
408 
409  protected:
410  int mCount;
411  int mHueMin;
412  int mHueMax;
413  int mSatMin;
414  int mSatMax;
415  int mValMin;
416  int mValMax;
417  QList<QColor> mColors;
418 };
419 
427 class CORE_EXPORT QgsRandomColorRamp: public QgsColorRamp
428 {
429  public:
430 
434  QgsRandomColorRamp() = default;
435 
436  int count() const override;
437 
438  double value( int index ) const override;
439 
440  QColor color( double value ) const override;
441 
449  virtual void setTotalColorCount( const int colorCount );
450 
451  QString type() const override;
452 
453  QgsRandomColorRamp *clone() const override SIP_FACTORY;
454 
455  QgsStringMap properties() const override;
456 
457  protected:
458 
459  int mTotalColorCount = 0;
460  QList<QColor> mPrecalculatedColors;
461 
462 };
463 
464 
471 class CORE_EXPORT QgsPresetSchemeColorRamp : public QgsColorRamp, public QgsColorScheme
472 {
473  public:
474 
479  QgsPresetSchemeColorRamp( const QList< QColor > &colors = QList< QColor >() );
480 
487 
494  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
495 
501  bool setColors( const QgsNamedColorList &colors, const QString & = QString(), const QColor & = QColor() ) override { mColors = colors; return true; }
502 
507  QList< QColor > colors() const;
508 
509  double value( int index ) const override;
510  QColor color( double value ) const override;
511  QString type() const override { return QStringLiteral( "preset" ); }
512  void invert() override;
513  QgsPresetSchemeColorRamp *clone() const override SIP_FACTORY;
514  QgsStringMap properties() const override;
515  int count() const override;
516 
517  QString schemeName() const override { return QStringLiteral( "preset" ); }
518  QgsNamedColorList fetchColors( const QString &context = QString(), const QColor &baseColor = QColor() ) override;
519  bool isEditable() const override { return true; }
520 
521  private:
522 
523  QgsNamedColorList mColors;
524 };
525 
526 
527 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
528 #define DEFAULT_COLORBREWER_COLORS 5
529 
536 class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
537 {
538  public:
539 
546  QgsColorBrewerColorRamp( const QString &schemeName = DEFAULT_COLORBREWER_SCHEMENAME,
547  int colors = DEFAULT_COLORBREWER_COLORS,
548  bool inverted = false );
549 
556  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
557 
558  double value( int index ) const override;
559  QColor color( double value ) const override;
560  QString type() const override { return QStringLiteral( "colorbrewer" ); }
561  void invert() override;
562  QgsColorBrewerColorRamp *clone() const override SIP_FACTORY;
563  QgsStringMap properties() const override;
564  int count() const override { return mColors; }
565 
570  QString schemeName() const { return mSchemeName; }
571 
576  int colors() const { return mColors; }
577 
584  void setSchemeName( const QString &schemeName ) { mSchemeName = schemeName; loadPalette(); }
585 
592  void setColors( int colors ) { mColors = colors; loadPalette(); }
593 
598  static QStringList listSchemeNames();
599 
606  static QList<int> listSchemeVariants( const QString &schemeName );
607 
608  protected:
609 
611  void loadPalette();
612 
613  QString mSchemeName;
614  int mColors;
615  QList<QColor> mPalette;
616  bool mInverted;
617 };
618 
619 
620 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
621 #define DEFAULT_CPTCITY_VARIANTNAME "05"
622 
627 class CORE_EXPORT QgsCptCityColorRamp : public QgsGradientColorRamp
628 {
629  public:
630 
638  QgsCptCityColorRamp( const QString &schemeName = DEFAULT_CPTCITY_SCHEMENAME,
639  const QString &variantName = DEFAULT_CPTCITY_VARIANTNAME,
640  bool inverted = false,
641  bool doLoadFile = true );
642 
651  QgsCptCityColorRamp( const QString &schemeName, const QStringList &variantList,
652  const QString &variantName = QString(), bool inverted = false,
653  bool doLoadFile = true );
654 
655  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
656 
657  QString type() const override { return QStringLiteral( "cpt-city" ); }
658 
659  void invert() override;
660 
661  QgsCptCityColorRamp *clone() const override SIP_FACTORY;
662  void copy( const QgsCptCityColorRamp *other );
663  QgsGradientColorRamp *cloneGradientRamp() const SIP_FACTORY;
664 
665  QgsStringMap properties() const override;
666 
667  QString schemeName() const { return mSchemeName; }
668  QString variantName() const { return mVariantName; }
669  QStringList variantList() const { return mVariantList; }
670 
671  // lazy loading - have to call loadPalette() explicitly
672  void setSchemeName( const QString &schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
673  void setVariantName( const QString &variantName ) { mVariantName = variantName; mFileLoaded = false; }
674  void setVariantList( const QStringList &variantList ) { mVariantList = variantList; }
675  void setName( const QString &schemeName, const QString &variantName = QString(), const QStringList &variantList = QStringList() )
676  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
677 
678  void loadPalette() { loadFile(); }
679  bool hasMultiStops() const { return mMultiStops; }
680 
681  QString fileName() const;
682  bool loadFile();
683  bool fileLoaded() const { return mFileLoaded; }
684 
685  QString copyingFileName() const;
686  QString descFileName() const;
687  QgsStringMap copyingInfo() const;
688 
689  protected:
690  QString mSchemeName;
691  QString mVariantName;
692  QStringList mVariantList;
693  bool mFileLoaded = false;
694  bool mMultiStops = false;
695  bool mInverted;
696 
697 };
698 
699 // clazy:excludeall=qstring-allocations
700 
701 #endif
int satMax() const
Returns the maximum saturation for generated colors.
Definition: qgscolorramp.h:354
#define DEFAULT_RANDOM_SAT_MAX
Definition: qgscolorramp.h:275
void setColor2(const QColor &color)
Sets the gradient end color.
Definition: qgscolorramp.h:195
#define DEFAULT_RANDOM_VAL_MAX
Definition: qgscolorramp.h:273
#define DEFAULT_RANDOM_HUE_MAX
Definition: qgscolorramp.h:271
Represents a color stop within a QgsGradientColorRamp color ramp.
Definition: qgscolorramp.h:101
void setCount(int val)
Sets the number of colors contained in the ramp.
Definition: qgscolorramp.h:371
QStringList mVariantList
Definition: qgscolorramp.h:692
void invert() override
Inverts the ordering of the color ramp.
QString schemeName() const override
Gets the name for the color scheme.
Definition: qgscolorramp.h:517
void setSchemeName(const QString &schemeName)
Definition: qgscolorramp.h:672
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
QColor color2() const
Returns the gradient end color.
Definition: qgscolorramp.h:179
Abstract base class for color schemes.
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
QString schemeName() const
Definition: qgscolorramp.h:667
void setHueMax(int val)
Sets the maximum hue for generated colors.
Definition: qgscolorramp.h:383
#define DEFAULT_RANDOM_HUE_MIN
Definition: qgscolorramp.h:270
void setVariantName(const QString &variantName)
Definition: qgscolorramp.h:673
#define DEFAULT_COLORBREWER_SCHEMENAME
Definition: qgscolorramp.h:527
void setHueMin(int val)
Sets the minimum hue for generated colors.
Definition: qgscolorramp.h:377
bool isEditable() const override
Returns whether the color scheme is editable.
Definition: qgscolorramp.h:519
virtual void invert()
Inverts the ordering of the color ramp.
Definition: qgscolorramp.h:82
virtual QColor color(double value) const =0
Returns the color corresponding to a specified value.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:479
Q_DECLARE_METATYPE(QModelIndex)
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:251
QList< QgsGradientStop > QgsGradientStopsList
List of gradient stops.
Definition: qgscolorramp.h:127
QList< QColor > mColors
Definition: qgscolorramp.h:417
void setName(const QString &schemeName, const QString &variantName=QString(), const QStringList &variantList=QStringList())
Definition: qgscolorramp.h:675
QgsGradientColorRamp * clone() const override
Creates a clone of the color ramp.
int satMin() const
Returns the minimum saturation for generated colors.
Definition: qgscolorramp.h:348
int hueMin() const
Returns the minimum hue for generated colors.
Definition: qgscolorramp.h:336
QString type() const override
Returns a string representing the color ramp type.
Definition: qgscolorramp.h:162
int colors() const
Returns the number of colors in the ramp.
Definition: qgscolorramp.h:576
Constrained random color ramp, which returns random colors based on preset parameters.
Definition: qgscolorramp.h:283
#define DEFAULT_RANDOM_VAL_MIN
Definition: qgscolorramp.h:272
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (e.g., authorship notes) ...
Definition: qgscolorramp.h:241
double offset
Relative positional offset, between 0 and 1.
Definition: qgscolorramp.h:116
QString type() const override
Returns a string representing the color ramp type.
Definition: qgscolorramp.h:511
int hueMax() const
Returns the maximum hue for generated colors.
Definition: qgscolorramp.h:342
int count() const override
Returns number of defined colors, or -1 if undefined.
Definition: qgscolorramp.h:315
virtual double value(int index) const =0
Returns relative value between [0,1] of color at specified index.
#define DEFAULT_CPTCITY_SCHEMENAME
Definition: qgscolorramp.h:620
void setDiscrete(bool discrete)
Sets whether the gradient should use discrete interpolation, rather than smoothly interpolating betwe...
Definition: qgscolorramp.h:211
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
#define SIP_END
Definition: qgis_sip.h:175
void setSchemeName(const QString &schemeName)
Sets the name of the color brewer color scheme.
Definition: qgscolorramp.h:584
int count() const override
Returns number of defined colors, or -1 if undefined.
Definition: qgscolorramp.h:564
#define SIP_FACTORY
Definition: qgis_sip.h:69
virtual QString type() const =0
Returns a string representing the color ramp type.
bool isDiscrete() const
Returns true if the gradient is using discrete interpolation, rather than smoothly interpolating betw...
Definition: qgscolorramp.h:202
virtual int count() const =0
Returns number of defined colors, or -1 if undefined.
#define DEFAULT_GRADIENT_COLOR1
Definition: qgscolorramp.h:129
A scheme based color ramp consisting of a list of predefined colors.
Definition: qgscolorramp.h:471
bool fileLoaded() const
Definition: qgscolorramp.h:683
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
Definition: qgscolorramp.h:235
void setSatMax(int val)
Sets the maximum saturation for generated colors.
Definition: qgscolorramp.h:395
bool setColors(const QgsNamedColorList &colors, const QString &=QString(), const QColor &=QColor()) override
Sets the list of colors used by the ramp.
Definition: qgscolorramp.h:501
Totally random color ramp.
Definition: qgscolorramp.h:427
void setValMin(int val)
Sets the minimum value for generated colors.
Definition: qgscolorramp.h:401
QString type() const override
Returns a string representing the color ramp type.
Definition: qgscolorramp.h:312
QString schemeName() const
Returns the name of the color brewer color scheme.
Definition: qgscolorramp.h:570
static QgsColorRamp * create(const QgsStringMap &properties=QgsStringMap())
Creates a new QgsColorRamp from a map of properties.
#define DEFAULT_RANDOM_COUNT
Definition: qgscolorramp.h:269
QString type() const override
Returns a string representing the color ramp type.
Definition: qgscolorramp.h:560
#define DEFAULT_RANDOM_SAT_MIN
Definition: qgscolorramp.h:274
QList< QColor > mPalette
Definition: qgscolorramp.h:615
QString type() const override
Returns a string representing the color ramp type.
Definition: qgscolorramp.h:657
void setVariantList(const QStringList &variantList)
Definition: qgscolorramp.h:674
void setInfo(const QgsStringMap &info)
Sets additional info to attach to the gradient ramp (e.g., authorship notes)
Definition: qgscolorramp.h:248
bool hasMultiStops() const
Definition: qgscolorramp.h:679
virtual QgsStringMap properties() const =0
Returns a string map containing all the color ramp&#39;s properties.
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor())=0
Gets a list of colors from the scheme.
QList< QColor > mPrecalculatedColors
Definition: qgscolorramp.h:460
bool operator==(const QgsGradientStop &other) const
Definition: qgscolorramp.h:120
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
void setSatMin(int val)
Sets the minimum saturation for generated colors.
Definition: qgscolorramp.h:389
Color ramp utilising "Color Brewer" preset color schemes.
Definition: qgscolorramp.h:536
int valMin() const
Returns the minimum value for generated colors.
Definition: qgscolorramp.h:360
#define DEFAULT_GRADIENT_COLOR2
Definition: qgscolorramp.h:130
QgsGradientStopsList mStops
Definition: qgscolorramp.h:263
int valMax() const
Returns the maximum value for generated colors.
Definition: qgscolorramp.h:366
QgsStringMap mInfo
Definition: qgscolorramp.h:264
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
Definition: qgscolorramp.h:139
QStringList variantList() const
Definition: qgscolorramp.h:669
#define DEFAULT_CPTCITY_VARIANTNAME
Definition: qgscolorramp.h:621
QgsStringMap properties() const override
Returns a string map containing all the color ramp&#39;s properties.
QColor color1() const
Returns the gradient start color.
Definition: qgscolorramp.h:172
void setColors(int colors)
Sets the number of colors in the ramp.
Definition: qgscolorramp.h:592
QgsGradientStop(double offset, const QColor &color)
Constructor for QgsGradientStop.
Definition: qgscolorramp.h:110
QString variantName() const
Definition: qgscolorramp.h:668
#define DEFAULT_COLORBREWER_COLORS
Definition: qgscolorramp.h:528
void setValMax(int val)
Sets the maximum value for generated colors.
Definition: qgscolorramp.h:407
void setColor1(const QColor &color)
Sets the gradient start color.
Definition: qgscolorramp.h:187
QColor color
Gradient color at stop.
Definition: qgscolorramp.h:118
int count() const override
Returns number of defined colors, or -1 if undefined.
Definition: qgscolorramp.h:159