QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 #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 QVariantMap &properties = QVariantMap() ) 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  QVariantMap 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 QVariantMap &properties = QVariantMap() ) 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  QVariantMap 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  QVariantMap 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 QVariantMap &properties = QVariantMap() ) 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  QVariantMap 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 QVariantMap &properties = QVariantMap() ) 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  QVariantMap 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 
705  static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
706 
712  static QString typeString() { return QStringLiteral( "cpt-city" ); }
713 
714  QString type() const override;
715 
716  void invert() override;
717 
718  QgsCptCityColorRamp *clone() const override SIP_FACTORY;
719  void copy( const QgsCptCityColorRamp *other );
720  QgsGradientColorRamp *cloneGradientRamp() const SIP_FACTORY;
721 
722  QVariantMap properties() const override;
723 
724  QString schemeName() const { return mSchemeName; }
725  QString variantName() const { return mVariantName; }
726  QStringList variantList() const { return mVariantList; }
727 
728  // lazy loading - have to call loadPalette() explicitly
729  void setSchemeName( const QString &schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
730  void setVariantName( const QString &variantName ) { mVariantName = variantName; mFileLoaded = false; }
731  void setVariantList( const QStringList &variantList ) { mVariantList = variantList; }
732  void setName( const QString &schemeName, const QString &variantName = QString(), const QStringList &variantList = QStringList() )
733  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
734 
735  void loadPalette() { loadFile(); }
736  bool hasMultiStops() const { return mMultiStops; }
737 
738  QString fileName() const;
739  bool loadFile();
740  bool fileLoaded() const { return mFileLoaded; }
741 
742  QString copyingFileName() const;
743  QString descFileName() const;
744  QgsStringMap copyingInfo() const;
745 
746  protected:
747  QString mSchemeName;
748  QString mVariantName;
749  QStringList mVariantList;
750  bool mFileLoaded = false;
751  bool mMultiStops = false;
752  bool mInverted;
753 
754 };
755 
756 // clazy:excludeall=qstring-allocations
757 
758 #endif
Color ramp utilising "Color Brewer" preset color schemes.
Definition: qgscolorramp.h:578
QList< QColor > mPalette
Definition: qgscolorramp.h:664
static QString typeString()
Returns the string identifier for QgsColorBrewerColorRamp.
Definition: qgscolorramp.h:607
QString type() const override
Returns a string representing the color ramp type.
Definition: qgscolorramp.h:609
QString schemeName() const
Returns the name of the color brewer color scheme.
Definition: qgscolorramp.h:619
int colors() const
Returns the number of colors in the ramp.
Definition: qgscolorramp.h:625
void setSchemeName(const QString &schemeName)
Sets the name of the color brewer color scheme.
Definition: qgscolorramp.h:633
void setColors(int colors)
Sets the number of colors in the ramp.
Definition: qgscolorramp.h:641
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:732
void setSchemeName(const QString &schemeName)
Definition: qgscolorramp.h:729
void setVariantList(const QStringList &variantList)
Definition: qgscolorramp.h:731
QStringList mVariantList
Definition: qgscolorramp.h:749
QStringList variantList() const
Definition: qgscolorramp.h:726
bool fileLoaded() const
Definition: qgscolorramp.h:740
void setVariantName(const QString &variantName)
Definition: qgscolorramp.h:730
static QString typeString()
Returns the string identifier for QgsCptCityColorRamp.
Definition: qgscolorramp.h:712
bool hasMultiStops() const
Definition: qgscolorramp.h:736
QString variantName() const
Definition: qgscolorramp.h:725
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
Definition: qgscolorramp.h:151
QgsGradientStopsList mStops
Definition: qgscolorramp.h:282
void setColor1(const QColor &color)
Sets the gradient start color.
Definition: qgscolorramp.h:206
void setColor2(const QColor &color)
Sets the gradient end color.
Definition: qgscolorramp.h:214
void setInfo(const QgsStringMap &info)
Sets additional info to attach to the gradient ramp (e.g., authorship notes)
Definition: qgscolorramp.h:267
QgsStringMap mInfo
Definition: qgscolorramp.h:283
bool isDiscrete() const
Returns true if the gradient is using discrete interpolation, rather than smoothly interpolating betw...
Definition: qgscolorramp.h:221
int count() const override
Returns number of defined colors, or -1 if undefined.
Definition: qgscolorramp.h:170
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (e.g., authorship notes)
Definition: qgscolorramp.h:260
static QString typeString()
Returns the string identifier for QgsGradientColorRamp.
Definition: qgscolorramp.h:179
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
Definition: qgscolorramp.h:254
void setDiscrete(bool discrete)
Sets whether the gradient should use discrete interpolation, rather than smoothly interpolating betwe...
Definition: qgscolorramp.h:230
QColor color2() const
Returns the gradient end color.
Definition: qgscolorramp.h:198
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:303
static QString typeString()
Returns the string identifier for QgsLimitedRandomColorRamp.
Definition: qgscolorramp.h:337
void setSatMin(int val)
Sets the minimum saturation for generated colors.
Definition: qgscolorramp.h:416
void setHueMin(int val)
Sets the minimum hue for generated colors.
Definition: qgscolorramp.h:404
void setSatMax(int val)
Sets the maximum saturation for generated colors.
Definition: qgscolorramp.h:422
int valMax() const
Returns the maximum value for generated colors.
Definition: qgscolorramp.h:393
int satMax() const
Returns the maximum saturation for generated colors.
Definition: qgscolorramp.h:381
void setHueMax(int val)
Sets the maximum hue for generated colors.
Definition: qgscolorramp.h:410
void setCount(int val)
Sets the number of colors contained in the ramp.
Definition: qgscolorramp.h:398
int hueMax() const
Returns the maximum hue for generated colors.
Definition: qgscolorramp.h:369
int hueMin() const
Returns the minimum hue for generated colors.
Definition: qgscolorramp.h:363
QList< QColor > mColors
Definition: qgscolorramp.h:444
void setValMax(int val)
Sets the maximum value for generated colors.
Definition: qgscolorramp.h:434
int valMin() const
Returns the minimum value for generated colors.
Definition: qgscolorramp.h:387
void setValMin(int val)
Sets the minimum value for generated colors.
Definition: qgscolorramp.h:428
int satMin() const
Returns the minimum saturation for generated colors.
Definition: qgscolorramp.h:375
A scheme based color ramp consisting of a list of predefined colors.
Definition: qgscolorramp.h:506
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
static QString typeString()
Returns the string identifier for QgsPresetSchemeColorRamp.
Definition: qgscolorramp.h:550
bool isEditable() const override
Returns whether the color scheme is editable.
Definition: qgscolorramp.h:560
Totally random color ramp.
Definition: qgscolorramp.h:455
QgsRandomColorRamp()=default
Constructor for QgsRandomColorRamp.
static QString typeString()
Returns the string identifier for QgsRandomColorRamp.
Definition: qgscolorramp.h:483
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:598
QMap< QString, QString > QgsStringMap
Definition: qgis.h:1041
#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:569
#define DEFAULT_COLORBREWER_SCHEMENAME
Definition: qgscolorramp.h:568
#define DEFAULT_RANDOM_HUE_MAX
Definition: qgscolorramp.h:290
#define DEFAULT_CPTCITY_SCHEMENAME
Definition: qgscolorramp.h:669
#define DEFAULT_RANDOM_HUE_MIN
Definition: qgscolorramp.h:289
#define DEFAULT_RANDOM_COUNT
Definition: qgscolorramp.h:288
#define DEFAULT_RANDOM_SAT_MAX
Definition: qgscolorramp.h:294
#define DEFAULT_RANDOM_SAT_MIN
Definition: qgscolorramp.h:293
#define DEFAULT_CPTCITY_VARIANTNAME
Definition: qgscolorramp.h:670
#define DEFAULT_GRADIENT_COLOR1
Definition: qgscolorramp.h:140
#define DEFAULT_RANDOM_VAL_MIN
Definition: qgscolorramp.h:291
QList< QgsGradientStop > QgsGradientStopsList
List of gradient stops.
Definition: qgscolorramp.h:138
#define DEFAULT_GRADIENT_COLOR2
Definition: qgscolorramp.h:141
#define DEFAULT_RANDOM_VAL_MAX
Definition: qgscolorramp.h:292
Q_DECLARE_METATYPE(QgsMeshTimeSettings)