QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 QVariantMap 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 // these are the QGIS branding colors, exaggerated a bit to make a default ramp with greater color variation
141 // then the official QGIS color gradient!
142 #define DEFAULT_GRADIENT_COLOR1 QColor(69, 116, 40)
143 #define DEFAULT_GRADIENT_COLOR2 QColor(188, 220, 60)
144 
152 class CORE_EXPORT QgsGradientColorRamp : public QgsColorRamp
153 {
154  public:
155 
164  QgsGradientColorRamp( const QColor &color1 = DEFAULT_GRADIENT_COLOR1,
165  const QColor &color2 = DEFAULT_GRADIENT_COLOR2,
166  bool discrete = false,
167  const QgsGradientStopsList &stops = QgsGradientStopsList() );
168 
170  static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
171 
172  int count() const override { return mStops.count() + 2; }
173  double value( int index ) const override;
174  QColor color( double value ) const override;
175 
181  static QString typeString() { return QStringLiteral( "gradient" ); }
182 
183  QString type() const override;
184  void invert() override;
185  QgsGradientColorRamp *clone() const override SIP_FACTORY;
186  QVariantMap properties() const override;
187 
193  QColor color1() const { return mColor1; }
194 
200  QColor color2() const { return mColor2; }
201 
208  void setColor1( const QColor &color ) { mColor1 = color; }
209 
216  void setColor2( const QColor &color ) { mColor2 = color; }
217 
223  bool isDiscrete() const { return mDiscrete; }
224 
232  void setDiscrete( bool discrete ) { mDiscrete = discrete; }
233 
241  void convertToDiscrete( bool discrete );
242 
250  void setStops( const QgsGradientStopsList &stops );
251 
256  QgsGradientStopsList stops() const { return mStops; }
257 
262  QgsStringMap info() const { return mInfo; }
263 
269  void setInfo( const QgsStringMap &info ) { mInfo = info; }
270 
278  void addStopsToGradient( QGradient *gradient, double opacity = 1 );
279 
280  protected:
281  QColor mColor1;
282  QColor mColor2;
283  bool mDiscrete;
286 };
287 
289 
290 #define DEFAULT_RANDOM_COUNT 10
291 #define DEFAULT_RANDOM_HUE_MIN 0
292 #define DEFAULT_RANDOM_HUE_MAX 359
293 #define DEFAULT_RANDOM_VAL_MIN 200
294 #define DEFAULT_RANDOM_VAL_MAX 240
295 #define DEFAULT_RANDOM_SAT_MIN 100
296 #define DEFAULT_RANDOM_SAT_MAX 240
297 
304 class CORE_EXPORT QgsLimitedRandomColorRamp : public QgsColorRamp
305 {
306  public:
307 
319  int hueMin = DEFAULT_RANDOM_HUE_MIN, int hueMax = DEFAULT_RANDOM_HUE_MAX,
320  int satMin = DEFAULT_RANDOM_SAT_MIN, int satMax = DEFAULT_RANDOM_SAT_MAX,
321  int valMin = DEFAULT_RANDOM_VAL_MIN, int valMax = DEFAULT_RANDOM_VAL_MAX );
322 
329  static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
330 
331  double value( int index ) const override;
332  QColor color( double value ) const override;
333 
339  static QString typeString() { return QStringLiteral( "random" ); }
340 
341  QString type() const override;
342  QgsLimitedRandomColorRamp *clone() const override SIP_FACTORY;
343  QVariantMap properties() const override;
344  int count() const override { return mCount; }
345 
350  static QList<QColor> randomColors( int count,
351  int hueMax = DEFAULT_RANDOM_HUE_MAX, int hueMin = DEFAULT_RANDOM_HUE_MIN,
352  int satMax = DEFAULT_RANDOM_SAT_MAX, int satMin = DEFAULT_RANDOM_SAT_MIN,
353  int valMax = DEFAULT_RANDOM_VAL_MAX, int valMin = DEFAULT_RANDOM_VAL_MIN );
354 
359  void updateColors();
360 
365  int hueMin() const { return mHueMin; }
366 
371  int hueMax() const { return mHueMax; }
372 
377  int satMin() const { return mSatMin; }
378 
383  int satMax() const { return mSatMax; }
384 
389  int valMin() const { return mValMin; }
390 
395  int valMax() const { return mValMax; }
396 
400  void setCount( int val ) { mCount = val; }
401 
406  void setHueMin( int val ) { mHueMin = val; }
407 
412  void setHueMax( int val ) { mHueMax = val; }
413 
418  void setSatMin( int val ) { mSatMin = val; }
419 
424  void setSatMax( int val ) { mSatMax = val; }
425 
430  void setValMin( int val ) { mValMin = val; }
431 
436  void setValMax( int val ) { mValMax = val; }
437 
438  protected:
439  int mCount;
440  int mHueMin;
441  int mHueMax;
442  int mSatMin;
443  int mSatMax;
444  int mValMin;
445  int mValMax;
446  QList<QColor> mColors;
447 };
448 
456 class CORE_EXPORT QgsRandomColorRamp: public QgsColorRamp
457 {
458  public:
459 
463  QgsRandomColorRamp() = default;
464 
465  int count() const override;
466 
467  double value( int index ) const override;
468 
469  QColor color( double value ) const override;
470 
478  virtual void setTotalColorCount( int colorCount );
479 
485  static QString typeString() { return QStringLiteral( "randomcolors" ); }
486 
487  QString type() const override;
488 
489  QgsRandomColorRamp *clone() const override SIP_FACTORY;
490 
491  QVariantMap properties() const override;
492 
493  protected:
494 
495  int mTotalColorCount = 0;
496  QList<QColor> mPrecalculatedColors;
497 
498 };
499 
500 
507 class CORE_EXPORT QgsPresetSchemeColorRamp : public QgsColorRamp, public QgsColorScheme
508 {
509  public:
510 
515  QgsPresetSchemeColorRamp( const QList< QColor > &colors = QList< QColor >() );
516 
522 
529  static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
530 
536  bool setColors( const QgsNamedColorList &colors, const QString & = QString(), const QColor & = QColor() ) override { mColors = colors; return true; }
537 
542  QList< QColor > colors() const;
543 
544  double value( int index ) const override;
545  QColor color( double value ) const override;
546 
552  static QString typeString() { return QStringLiteral( "preset" ); }
553 
554  QString type() const override;
555  void invert() override;
556  QgsPresetSchemeColorRamp *clone() const override SIP_FACTORY;
557  QVariantMap properties() const override;
558  int count() const override;
559 
560  QString schemeName() const override { return QStringLiteral( "preset" ); }
561  QgsNamedColorList fetchColors( const QString &context = QString(), const QColor &baseColor = QColor() ) override;
562  bool isEditable() const override { return true; }
563 
564  private:
565 
566  QgsNamedColorList mColors;
567 };
568 
569 
570 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
571 #define DEFAULT_COLORBREWER_COLORS 5
572 
579 class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
580 {
581  public:
582 
589  QgsColorBrewerColorRamp( const QString &schemeName = DEFAULT_COLORBREWER_SCHEMENAME,
590  int colors = DEFAULT_COLORBREWER_COLORS,
591  bool inverted = false );
592 
599  static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
600 
601  double value( int index ) const override;
602  QColor color( double value ) const override;
603 
609  static QString typeString() { return QStringLiteral( "colorbrewer" ); }
610 
611  QString type() const override { return QgsColorBrewerColorRamp::typeString(); }
612  void invert() override;
613  QgsColorBrewerColorRamp *clone() const override SIP_FACTORY;
614  QVariantMap properties() const override;
615  int count() const override { return mColors; }
616 
621  QString schemeName() const { return mSchemeName; }
622 
627  int colors() const { return mColors; }
628 
635  void setSchemeName( const QString &schemeName ) { mSchemeName = schemeName; loadPalette(); }
636 
643  void setColors( int colors ) { mColors = colors; loadPalette(); }
644 
649  static QStringList listSchemeNames();
650 
657  static QList<int> listSchemeVariants( const QString &schemeName );
658 
659  protected:
660 
662  void loadPalette();
663 
664  QString mSchemeName;
665  int mColors;
666  QList<QColor> mPalette;
667  bool mInverted;
668 };
669 
670 
671 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
672 #define DEFAULT_CPTCITY_VARIANTNAME "05"
673 
678 class CORE_EXPORT QgsCptCityColorRamp : public QgsGradientColorRamp
679 {
680  public:
681 
689  QgsCptCityColorRamp( const QString &schemeName = DEFAULT_CPTCITY_SCHEMENAME,
690  const QString &variantName = DEFAULT_CPTCITY_VARIANTNAME,
691  bool inverted = false,
692  bool doLoadFile = true );
693 
702  QgsCptCityColorRamp( const QString &schemeName, const QStringList &variantList,
703  const QString &variantName = QString(), bool inverted = false,
704  bool doLoadFile = true );
705 
707  static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
708 
714  static QString typeString() { return QStringLiteral( "cpt-city" ); }
715 
716  QString type() const override;
717 
718  void invert() override;
719 
720  QgsCptCityColorRamp *clone() const override SIP_FACTORY;
721  void copy( const QgsCptCityColorRamp *other );
722  QgsGradientColorRamp *cloneGradientRamp() const SIP_FACTORY;
723 
724  QVariantMap properties() const override;
725 
726  QString schemeName() const { return mSchemeName; }
727  QString variantName() const { return mVariantName; }
728  QStringList variantList() const { return mVariantList; }
729 
730  // lazy loading - have to call loadPalette() explicitly
731  void setSchemeName( const QString &schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
732  void setVariantName( const QString &variantName ) { mVariantName = variantName; mFileLoaded = false; }
733  void setVariantList( const QStringList &variantList ) { mVariantList = variantList; }
734  void setName( const QString &schemeName, const QString &variantName = QString(), const QStringList &variantList = QStringList() )
735  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
736 
737  void loadPalette() { loadFile(); }
738  bool hasMultiStops() const { return mMultiStops; }
739 
740  QString fileName() const;
741  bool loadFile();
742  bool fileLoaded() const { return mFileLoaded; }
743 
744  QString copyingFileName() const;
745  QString descFileName() const;
746  QgsStringMap copyingInfo() const;
747 
748  protected:
749  QString mSchemeName;
750  QString mVariantName;
751  QStringList mVariantList;
752  bool mFileLoaded = false;
753  bool mMultiStops = false;
754  bool mInverted;
755 
756 };
757 
758 // clazy:excludeall=qstring-allocations
759 
760 #endif
Color ramp utilising "Color Brewer" preset color schemes.
Definition: qgscolorramp.h:580
QList< QColor > mPalette
Definition: qgscolorramp.h:666
static QString typeString()
Returns the string identifier for QgsColorBrewerColorRamp.
Definition: qgscolorramp.h:609
QString type() const override
Returns a string representing the color ramp type.
Definition: qgscolorramp.h:611
QString schemeName() const
Returns the name of the color brewer color scheme.
Definition: qgscolorramp.h:621
int colors() const
Returns the number of colors in the ramp.
Definition: qgscolorramp.h:627
void setSchemeName(const QString &schemeName)
Sets the name of the color brewer color scheme.
Definition: qgscolorramp.h:635
void setColors(int colors)
Sets the number of colors in the ramp.
Definition: qgscolorramp.h:643
Abstract base class for color ramps.
Definition: qgscolorramp.h:32
virtual QColor color(double value) const =0
Returns the color corresponding to a specified value.
virtual ~QgsColorRamp()=default
virtual int count() const =0
Returns number of defined colors, or -1 if undefined.
virtual double value(int index) const =0
Returns relative value between [0,1] of color at specified index.
virtual QVariantMap properties() const =0
Returns a string map containing all the color ramp's properties.
virtual QString type() const =0
Returns a string representing the color ramp type.
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
virtual void invert()
Inverts the ordering of the color ramp.
Definition: qgscolorramp.h:82
Abstract base class for color schemes.
void setName(const QString &schemeName, const QString &variantName=QString(), const QStringList &variantList=QStringList())
Definition: qgscolorramp.h:734
void setSchemeName(const QString &schemeName)
Definition: qgscolorramp.h:731
void setVariantList(const QStringList &variantList)
Definition: qgscolorramp.h:733
QStringList mVariantList
Definition: qgscolorramp.h:751
QStringList variantList() const
Definition: qgscolorramp.h:728
bool fileLoaded() const
Definition: qgscolorramp.h:742
void setVariantName(const QString &variantName)
Definition: qgscolorramp.h:732
static QString typeString()
Returns the string identifier for QgsCptCityColorRamp.
Definition: qgscolorramp.h:714
bool hasMultiStops() const
Definition: qgscolorramp.h:738
QString variantName() const
Definition: qgscolorramp.h:727
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
Definition: qgscolorramp.h:153
QgsGradientStopsList mStops
Definition: qgscolorramp.h:284
void setColor1(const QColor &color)
Sets the gradient start color.
Definition: qgscolorramp.h:208
void setColor2(const QColor &color)
Sets the gradient end color.
Definition: qgscolorramp.h:216
void setInfo(const QgsStringMap &info)
Sets additional info to attach to the gradient ramp (e.g., authorship notes)
Definition: qgscolorramp.h:269
QgsStringMap mInfo
Definition: qgscolorramp.h:285
bool isDiscrete() const
Returns true if the gradient is using discrete interpolation, rather than smoothly interpolating betw...
Definition: qgscolorramp.h:223
int count() const override
Returns number of defined colors, or -1 if undefined.
Definition: qgscolorramp.h:172
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (e.g., authorship notes)
Definition: qgscolorramp.h:262
static QString typeString()
Returns the string identifier for QgsGradientColorRamp.
Definition: qgscolorramp.h:181
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
Definition: qgscolorramp.h:256
void setDiscrete(bool discrete)
Sets whether the gradient should use discrete interpolation, rather than smoothly interpolating betwe...
Definition: qgscolorramp.h:232
QColor color2() const
Returns the gradient end color.
Definition: qgscolorramp.h:200
Represents a color stop within a QgsGradientColorRamp color ramp.
Definition: qgscolorramp.h:113
bool operator==(const QgsGradientStop &other) const
Definition: qgscolorramp.h:131
double offset
Relative positional offset, between 0 and 1.
Definition: qgscolorramp.h:127
QColor color
Gradient color at stop.
Definition: qgscolorramp.h:129
QgsGradientStop(double offset, const QColor &color)
Constructor for QgsGradientStop.
Definition: qgscolorramp.h:121
Constrained random color ramp, which returns random colors based on preset parameters.
Definition: qgscolorramp.h:305
static QString typeString()
Returns the string identifier for QgsLimitedRandomColorRamp.
Definition: qgscolorramp.h:339
void setSatMin(int val)
Sets the minimum saturation for generated colors.
Definition: qgscolorramp.h:418
void setHueMin(int val)
Sets the minimum hue for generated colors.
Definition: qgscolorramp.h:406
void setSatMax(int val)
Sets the maximum saturation for generated colors.
Definition: qgscolorramp.h:424
int valMax() const
Returns the maximum value for generated colors.
Definition: qgscolorramp.h:395
int satMax() const
Returns the maximum saturation for generated colors.
Definition: qgscolorramp.h:383
void setHueMax(int val)
Sets the maximum hue for generated colors.
Definition: qgscolorramp.h:412
void setCount(int val)
Sets the number of colors contained in the ramp.
Definition: qgscolorramp.h:400
int hueMax() const
Returns the maximum hue for generated colors.
Definition: qgscolorramp.h:371
int hueMin() const
Returns the minimum hue for generated colors.
Definition: qgscolorramp.h:365
QList< QColor > mColors
Definition: qgscolorramp.h:446
void setValMax(int val)
Sets the maximum value for generated colors.
Definition: qgscolorramp.h:436
int valMin() const
Returns the minimum value for generated colors.
Definition: qgscolorramp.h:389
void setValMin(int val)
Sets the minimum value for generated colors.
Definition: qgscolorramp.h:430
int satMin() const
Returns the minimum saturation for generated colors.
Definition: qgscolorramp.h:377
A scheme based color ramp consisting of a list of predefined colors.
Definition: qgscolorramp.h:508
bool setColors(const QgsNamedColorList &colors, const QString &=QString(), const QColor &=QColor()) override
Sets the list of colors used by the ramp.
Definition: qgscolorramp.h:536
static QString typeString()
Returns the string identifier for QgsPresetSchemeColorRamp.
Definition: qgscolorramp.h:552
bool isEditable() const override
Returns whether the color scheme is editable.
Definition: qgscolorramp.h:562
Totally random color ramp.
Definition: qgscolorramp.h:457
QgsRandomColorRamp()=default
Constructor for QgsRandomColorRamp.
static QString typeString()
Returns the string identifier for QgsRandomColorRamp.
Definition: qgscolorramp.h:485
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:1246
QMap< QString, QString > QgsStringMap
Definition: qgis.h:1703
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:194
#define DEFAULT_COLORBREWER_COLORS
Definition: qgscolorramp.h:571
#define DEFAULT_COLORBREWER_SCHEMENAME
Definition: qgscolorramp.h:570
#define DEFAULT_RANDOM_HUE_MAX
Definition: qgscolorramp.h:292
#define DEFAULT_CPTCITY_SCHEMENAME
Definition: qgscolorramp.h:671
#define DEFAULT_RANDOM_HUE_MIN
Definition: qgscolorramp.h:291
#define DEFAULT_RANDOM_COUNT
Definition: qgscolorramp.h:290
#define DEFAULT_RANDOM_SAT_MAX
Definition: qgscolorramp.h:296
#define DEFAULT_RANDOM_SAT_MIN
Definition: qgscolorramp.h:295
#define DEFAULT_CPTCITY_VARIANTNAME
Definition: qgscolorramp.h:672
#define DEFAULT_GRADIENT_COLOR1
Definition: qgscolorramp.h:142
#define DEFAULT_RANDOM_VAL_MIN
Definition: qgscolorramp.h:293
QList< QgsGradientStop > QgsGradientStopsList
List of gradient stops.
Definition: qgscolorramp.h:138
#define DEFAULT_GRADIENT_COLOR2
Definition: qgscolorramp.h:143
#define DEFAULT_RANDOM_VAL_MAX
Definition: qgscolorramp.h:294
Q_DECLARE_METATYPE(QgsMeshTimeSettings)