QGIS API Documentation  3.17.0-Master (a035f434f4)
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() == QgsGradientColorRamp::typeString() )
37  sipType = sipType_QgsGradientColorRamp;
38  else if ( sipCpp->type() == QgsLimitedRandomColorRamp::typeString() )
39  sipType = sipType_QgsLimitedRandomColorRamp;
40  else if ( sipCpp->type() == QgsRandomColorRamp::typeString() )
41  sipType = sipType_QgsRandomColorRamp;
42  else if ( sipCpp->type() == QgsPresetSchemeColorRamp::typeString() )
43  sipType = sipType_QgsPresetSchemeColorRamp;
44  else if ( sipCpp->type() == QgsColorBrewerColorRamp::typeString() )
45  sipType = sipType_QgsColorBrewerColorRamp;
46  else if ( sipCpp->type() == QgsCptCityColorRamp::typeString() )
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 
103  static QList< QPair< QString, QString > > rampTypes();
104 };
105 
112 class CORE_EXPORT QgsGradientStop
113 {
114  public:
115 
121  QgsGradientStop( double offset, const QColor &color )
122  : offset( offset )
123  , color( color )
124  { }
125 
127  double offset;
129  QColor color;
130 
131  bool operator==( const QgsGradientStop &other ) const
132  {
133  return other.color == color && qgsDoubleNear( other.offset, offset );
134  }
135 };
136 
138 typedef QList<QgsGradientStop> QgsGradientStopsList;
139 
140 #define DEFAULT_GRADIENT_COLOR1 QColor(0,0,255)
141 #define DEFAULT_GRADIENT_COLOR2 QColor(0,255,0)
142 
150 class CORE_EXPORT QgsGradientColorRamp : public QgsColorRamp
151 {
152  public:
153 
162  QgsGradientColorRamp( const QColor &color1 = DEFAULT_GRADIENT_COLOR1,
163  const QColor &color2 = DEFAULT_GRADIENT_COLOR2,
164  bool discrete = false,
165  const QgsGradientStopsList &stops = QgsGradientStopsList() );
166 
168  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
169 
170  int count() const override { return mStops.count() + 2; }
171  double value( int index ) const override;
172  QColor color( double value ) const override;
173 
179  static QString typeString() { return QStringLiteral( "gradient" ); }
180 
181  QString type() const override;
182  void invert() override;
183  QgsGradientColorRamp *clone() const override SIP_FACTORY;
184  QgsStringMap properties() const override;
185 
191  QColor color1() const { return mColor1; }
192 
198  QColor color2() const { return mColor2; }
199 
206  void setColor1( const QColor &color ) { mColor1 = color; }
207 
214  void setColor2( const QColor &color ) { mColor2 = color; }
215 
221  bool isDiscrete() const { return mDiscrete; }
222 
230  void setDiscrete( bool discrete ) { mDiscrete = discrete; }
231 
239  void convertToDiscrete( bool discrete );
240 
248  void setStops( const QgsGradientStopsList &stops );
249 
254  QgsGradientStopsList stops() const { return mStops; }
255 
260  QgsStringMap info() const { return mInfo; }
261 
267  void setInfo( const QgsStringMap &info ) { mInfo = info; }
268 
276  void addStopsToGradient( QGradient *gradient, double opacity = 1 );
277 
278  protected:
279  QColor mColor1;
280  QColor mColor2;
281  bool mDiscrete;
284 };
285 
287 
288 #define DEFAULT_RANDOM_COUNT 10
289 #define DEFAULT_RANDOM_HUE_MIN 0
290 #define DEFAULT_RANDOM_HUE_MAX 359
291 #define DEFAULT_RANDOM_VAL_MIN 200
292 #define DEFAULT_RANDOM_VAL_MAX 240
293 #define DEFAULT_RANDOM_SAT_MIN 100
294 #define DEFAULT_RANDOM_SAT_MAX 240
295 
302 class CORE_EXPORT QgsLimitedRandomColorRamp : public QgsColorRamp
303 {
304  public:
305 
317  int hueMin = DEFAULT_RANDOM_HUE_MIN, int hueMax = DEFAULT_RANDOM_HUE_MAX,
318  int satMin = DEFAULT_RANDOM_SAT_MIN, int satMax = DEFAULT_RANDOM_SAT_MAX,
319  int valMin = DEFAULT_RANDOM_VAL_MIN, int valMax = DEFAULT_RANDOM_VAL_MAX );
320 
327  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
328 
329  double value( int index ) const override;
330  QColor color( double value ) const override;
331 
337  static QString typeString() { return QStringLiteral( "random" ); }
338 
339  QString type() const override;
340  QgsLimitedRandomColorRamp *clone() const override SIP_FACTORY;
341  QgsStringMap properties() const override;
342  int count() const override { return mCount; }
343 
348  static QList<QColor> randomColors( int count,
349  int hueMax = DEFAULT_RANDOM_HUE_MAX, int hueMin = DEFAULT_RANDOM_HUE_MIN,
350  int satMax = DEFAULT_RANDOM_SAT_MAX, int satMin = DEFAULT_RANDOM_SAT_MIN,
351  int valMax = DEFAULT_RANDOM_VAL_MAX, int valMin = DEFAULT_RANDOM_VAL_MIN );
352 
357  void updateColors();
358 
363  int hueMin() const { return mHueMin; }
364 
369  int hueMax() const { return mHueMax; }
370 
375  int satMin() const { return mSatMin; }
376 
381  int satMax() const { return mSatMax; }
382 
387  int valMin() const { return mValMin; }
388 
393  int valMax() const { return mValMax; }
394 
398  void setCount( int val ) { mCount = val; }
399 
404  void setHueMin( int val ) { mHueMin = val; }
405 
410  void setHueMax( int val ) { mHueMax = val; }
411 
416  void setSatMin( int val ) { mSatMin = val; }
417 
422  void setSatMax( int val ) { mSatMax = val; }
423 
428  void setValMin( int val ) { mValMin = val; }
429 
434  void setValMax( int val ) { mValMax = val; }
435 
436  protected:
437  int mCount;
438  int mHueMin;
439  int mHueMax;
440  int mSatMin;
441  int mSatMax;
442  int mValMin;
443  int mValMax;
444  QList<QColor> mColors;
445 };
446 
454 class CORE_EXPORT QgsRandomColorRamp: public QgsColorRamp
455 {
456  public:
457 
461  QgsRandomColorRamp() = default;
462 
463  int count() const override;
464 
465  double value( int index ) const override;
466 
467  QColor color( double value ) const override;
468 
476  virtual void setTotalColorCount( int colorCount );
477 
483  static QString typeString() { return QStringLiteral( "randomcolors" ); }
484 
485  QString type() const override;
486 
487  QgsRandomColorRamp *clone() const override SIP_FACTORY;
488 
489  QgsStringMap properties() const override;
490 
491  protected:
492 
493  int mTotalColorCount = 0;
494  QList<QColor> mPrecalculatedColors;
495 
496 };
497 
498 
505 class CORE_EXPORT QgsPresetSchemeColorRamp : public QgsColorRamp, public QgsColorScheme
506 {
507  public:
508 
513  QgsPresetSchemeColorRamp( const QList< QColor > &colors = QList< QColor >() );
514 
520 
527  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
528 
534  bool setColors( const QgsNamedColorList &colors, const QString & = QString(), const QColor & = QColor() ) override { mColors = colors; return true; }
535 
540  QList< QColor > colors() const;
541 
542  double value( int index ) const override;
543  QColor color( double value ) const override;
544 
550  static QString typeString() { return QStringLiteral( "preset" ); }
551 
552  QString type() const override;
553  void invert() override;
554  QgsPresetSchemeColorRamp *clone() const override SIP_FACTORY;
555  QgsStringMap properties() const override;
556  int count() const override;
557 
558  QString schemeName() const override { return QStringLiteral( "preset" ); }
559  QgsNamedColorList fetchColors( const QString &context = QString(), const QColor &baseColor = QColor() ) override;
560  bool isEditable() const override { return true; }
561 
562  private:
563 
564  QgsNamedColorList mColors;
565 };
566 
567 
568 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
569 #define DEFAULT_COLORBREWER_COLORS 5
570 
577 class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
578 {
579  public:
580 
587  QgsColorBrewerColorRamp( const QString &schemeName = DEFAULT_COLORBREWER_SCHEMENAME,
588  int colors = DEFAULT_COLORBREWER_COLORS,
589  bool inverted = false );
590 
597  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
598 
599  double value( int index ) const override;
600  QColor color( double value ) const override;
601 
607  static QString typeString() { return QStringLiteral( "colorbrewer" ); }
608 
609  QString type() const override { return QgsColorBrewerColorRamp::typeString(); }
610  void invert() override;
611  QgsColorBrewerColorRamp *clone() const override SIP_FACTORY;
612  QgsStringMap properties() const override;
613  int count() const override { return mColors; }
614 
619  QString schemeName() const { return mSchemeName; }
620 
625  int colors() const { return mColors; }
626 
633  void setSchemeName( const QString &schemeName ) { mSchemeName = schemeName; loadPalette(); }
634 
641  void setColors( int colors ) { mColors = colors; loadPalette(); }
642 
647  static QStringList listSchemeNames();
648 
655  static QList<int> listSchemeVariants( const QString &schemeName );
656 
657  protected:
658 
660  void loadPalette();
661 
662  QString mSchemeName;
663  int mColors;
664  QList<QColor> mPalette;
665  bool mInverted;
666 };
667 
668 
669 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
670 #define DEFAULT_CPTCITY_VARIANTNAME "05"
671 
676 class CORE_EXPORT QgsCptCityColorRamp : public QgsGradientColorRamp
677 {
678  public:
679 
687  QgsCptCityColorRamp( const QString &schemeName = DEFAULT_CPTCITY_SCHEMENAME,
688  const QString &variantName = DEFAULT_CPTCITY_VARIANTNAME,
689  bool inverted = false,
690  bool doLoadFile = true );
691 
700  QgsCptCityColorRamp( const QString &schemeName, const QStringList &variantList,
701  const QString &variantName = QString(), bool inverted = false,
702  bool doLoadFile = true );
703 
704  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
705 
711  static QString typeString() { return QStringLiteral( "cpt-city" ); }
712 
713  QString type() const override;
714 
715  void invert() override;
716 
717  QgsCptCityColorRamp *clone() const override SIP_FACTORY;
718  void copy( const QgsCptCityColorRamp *other );
719  QgsGradientColorRamp *cloneGradientRamp() const SIP_FACTORY;
720 
721  QgsStringMap properties() const override;
722 
723  QString schemeName() const { return mSchemeName; }
724  QString variantName() const { return mVariantName; }
725  QStringList variantList() const { return mVariantList; }
726 
727  // lazy loading - have to call loadPalette() explicitly
728  void setSchemeName( const QString &schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
729  void setVariantName( const QString &variantName ) { mVariantName = variantName; mFileLoaded = false; }
730  void setVariantList( const QStringList &variantList ) { mVariantList = variantList; }
731  void setName( const QString &schemeName, const QString &variantName = QString(), const QStringList &variantList = QStringList() )
732  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
733 
734  void loadPalette() { loadFile(); }
735  bool hasMultiStops() const { return mMultiStops; }
736 
737  QString fileName() const;
738  bool loadFile();
739  bool fileLoaded() const { return mFileLoaded; }
740 
741  QString copyingFileName() const;
742  QString descFileName() const;
743  QgsStringMap copyingInfo() const;
744 
745  protected:
746  QString mSchemeName;
747  QString mVariantName;
748  QStringList mVariantList;
749  bool mFileLoaded = false;
750  bool mMultiStops = false;
751  bool mInverted;
752 
753 };
754 
755 // clazy:excludeall=qstring-allocations
756 
757 #endif
int satMax() const
Returns the maximum saturation for generated colors.
Definition: qgscolorramp.h:381
static QString typeString()
Returns the string identifier for QgsLimitedRandomColorRamp.
Definition: qgscolorramp.h:337
#define DEFAULT_RANDOM_SAT_MAX
Definition: qgscolorramp.h:294
void setColor2(const QColor &color)
Sets the gradient end color.
Definition: qgscolorramp.h:214
#define DEFAULT_RANDOM_VAL_MAX
Definition: qgscolorramp.h:292
#define DEFAULT_RANDOM_HUE_MAX
Definition: qgscolorramp.h:290
Represents a color stop within a QgsGradientColorRamp color ramp.
Definition: qgscolorramp.h:112
void setCount(int val)
Sets the number of colors contained in the ramp.
Definition: qgscolorramp.h:398
QStringList mVariantList
Definition: qgscolorramp.h:748
void invert() override
Inverts the ordering of the color ramp.
QString schemeName() const override
Gets the name for the color scheme.
Definition: qgscolorramp.h:558
void setSchemeName(const QString &schemeName)
Definition: qgscolorramp.h:728
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
QColor color2() const
Returns the gradient end color.
Definition: qgscolorramp.h:198
Abstract base class for color schemes.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:316
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
QString schemeName() const
Definition: qgscolorramp.h:723
static QString typeString()
Returns the string identifier for QgsCptCityColorRamp.
Definition: qgscolorramp.h:711
void setHueMax(int val)
Sets the maximum hue for generated colors.
Definition: qgscolorramp.h:410
#define DEFAULT_RANDOM_HUE_MIN
Definition: qgscolorramp.h:289
void setVariantName(const QString &variantName)
Definition: qgscolorramp.h:729
#define DEFAULT_COLORBREWER_SCHEMENAME
Definition: qgscolorramp.h:568
void setHueMin(int val)
Sets the minimum hue for generated colors.
Definition: qgscolorramp.h:404
bool isEditable() const override
Returns whether the color scheme is editable.
Definition: qgscolorramp.h:560
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:759
QList< QgsGradientStop > QgsGradientStopsList
List of gradient stops.
Definition: qgscolorramp.h:138
QList< QColor > mColors
Definition: qgscolorramp.h:444
void setName(const QString &schemeName, const QString &variantName=QString(), const QStringList &variantList=QStringList())
Definition: qgscolorramp.h:731
QgsGradientColorRamp * clone() const override
Creates a clone of the color ramp.
int satMin() const
Returns the minimum saturation for generated colors.
Definition: qgscolorramp.h:375
int hueMin() const
Returns the minimum hue for generated colors.
Definition: qgscolorramp.h:363
QString type() const override
Returns a string representing the color ramp type.
int colors() const
Returns the number of colors in the ramp.
Definition: qgscolorramp.h:625
Constrained random color ramp, which returns random colors based on preset parameters.
Definition: qgscolorramp.h:302
#define DEFAULT_RANDOM_VAL_MIN
Definition: qgscolorramp.h:291
static QString typeString()
Returns the string identifier for QgsColorBrewerColorRamp.
Definition: qgscolorramp.h:607
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (e.g., authorship notes) ...
Definition: qgscolorramp.h:260
double offset
Relative positional offset, between 0 and 1.
Definition: qgscolorramp.h:127
int hueMax() const
Returns the maximum hue for generated colors.
Definition: qgscolorramp.h:369
static QString typeString()
Returns the string identifier for QgsPresetSchemeColorRamp.
Definition: qgscolorramp.h:550
int count() const override
Returns number of defined colors, or -1 if undefined.
Definition: qgscolorramp.h:342
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:669
void setDiscrete(bool discrete)
Sets whether the gradient should use discrete interpolation, rather than smoothly interpolating betwe...
Definition: qgscolorramp.h:230
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
#define SIP_END
Definition: qgis_sip.h:194
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
void setSchemeName(const QString &schemeName)
Sets the name of the color brewer color scheme.
Definition: qgscolorramp.h:633
int count() const override
Returns number of defined colors, or -1 if undefined.
Definition: qgscolorramp.h:613
#define SIP_FACTORY
Definition: qgis_sip.h:76
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:221
virtual int count() const =0
Returns number of defined colors, or -1 if undefined.
#define DEFAULT_GRADIENT_COLOR1
Definition: qgscolorramp.h:140
A scheme based color ramp consisting of a list of predefined colors.
Definition: qgscolorramp.h:505
bool fileLoaded() const
Definition: qgscolorramp.h:739
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
Definition: qgscolorramp.h:254
void setSatMax(int val)
Sets the maximum saturation for generated colors.
Definition: qgscolorramp.h:422
bool setColors(const QgsNamedColorList &colors, const QString &=QString(), const QColor &=QColor()) override
Sets the list of colors used by the ramp.
Definition: qgscolorramp.h:534
Totally random color ramp.
Definition: qgscolorramp.h:454
void setValMin(int val)
Sets the minimum value for generated colors.
Definition: qgscolorramp.h:428
QString schemeName() const
Returns the name of the color brewer color scheme.
Definition: qgscolorramp.h:619
static QgsColorRamp * create(const QgsStringMap &properties=QgsStringMap())
Creates a new QgsColorRamp from a map of properties.
#define DEFAULT_RANDOM_COUNT
Definition: qgscolorramp.h:288
QString type() const override
Returns a string representing the color ramp type.
Definition: qgscolorramp.h:609
#define DEFAULT_RANDOM_SAT_MIN
Definition: qgscolorramp.h:293
QList< QColor > mPalette
Definition: qgscolorramp.h:664
void setVariantList(const QStringList &variantList)
Definition: qgscolorramp.h:730
void setInfo(const QgsStringMap &info)
Sets additional info to attach to the gradient ramp (e.g., authorship notes)
Definition: qgscolorramp.h:267
bool hasMultiStops() const
Definition: qgscolorramp.h:735
static QString typeString()
Returns the string identifier for QgsGradientColorRamp.
Definition: qgscolorramp.h:179
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:494
bool operator==(const QgsGradientStop &other) const
Definition: qgscolorramp.h:131
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
void setSatMin(int val)
Sets the minimum saturation for generated colors.
Definition: qgscolorramp.h:416
Color ramp utilising "Color Brewer" preset color schemes.
Definition: qgscolorramp.h:577
int valMin() const
Returns the minimum value for generated colors.
Definition: qgscolorramp.h:387
#define DEFAULT_GRADIENT_COLOR2
Definition: qgscolorramp.h:141
static QString typeString()
Returns the string identifier for QgsRandomColorRamp.
Definition: qgscolorramp.h:483
QgsGradientStopsList mStops
Definition: qgscolorramp.h:282
int valMax() const
Returns the maximum value for generated colors.
Definition: qgscolorramp.h:393
QgsStringMap mInfo
Definition: qgscolorramp.h:283
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
Definition: qgscolorramp.h:150
QStringList variantList() const
Definition: qgscolorramp.h:725
#define DEFAULT_CPTCITY_VARIANTNAME
Definition: qgscolorramp.h:670
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:191
void setColors(int colors)
Sets the number of colors in the ramp.
Definition: qgscolorramp.h:641
QgsGradientStop(double offset, const QColor &color)
Constructor for QgsGradientStop.
Definition: qgscolorramp.h:121
QString variantName() const
Definition: qgscolorramp.h:724
#define DEFAULT_COLORBREWER_COLORS
Definition: qgscolorramp.h:569
void setValMax(int val)
Sets the maximum value for generated colors.
Definition: qgscolorramp.h:434
void setColor1(const QColor &color)
Sets the gradient start color.
Definition: qgscolorramp.h:206
QColor color
Gradient color at stop.
Definition: qgscolorramp.h:129
int count() const override
Returns number of defined colors, or -1 if undefined.
Definition: qgscolorramp.h:170