QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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( 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 
486 
493  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
494 
500  bool setColors( const QgsNamedColorList &colors, const QString & = QString(), const QColor & = QColor() ) override { mColors = colors; return true; }
501 
506  QList< QColor > colors() const;
507 
508  double value( int index ) const override;
509  QColor color( double value ) const override;
510  QString type() const override { return QStringLiteral( "preset" ); }
511  void invert() override;
512  QgsPresetSchemeColorRamp *clone() const override SIP_FACTORY;
513  QgsStringMap properties() const override;
514  int count() const override;
515 
516  QString schemeName() const override { return QStringLiteral( "preset" ); }
517  QgsNamedColorList fetchColors( const QString &context = QString(), const QColor &baseColor = QColor() ) override;
518  bool isEditable() const override { return true; }
519 
520  private:
521 
522  QgsNamedColorList mColors;
523 };
524 
525 
526 #define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
527 #define DEFAULT_COLORBREWER_COLORS 5
528 
535 class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
536 {
537  public:
538 
545  QgsColorBrewerColorRamp( const QString &schemeName = DEFAULT_COLORBREWER_SCHEMENAME,
546  int colors = DEFAULT_COLORBREWER_COLORS,
547  bool inverted = false );
548 
555  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
556 
557  double value( int index ) const override;
558  QColor color( double value ) const override;
559  QString type() const override { return QStringLiteral( "colorbrewer" ); }
560  void invert() override;
561  QgsColorBrewerColorRamp *clone() const override SIP_FACTORY;
562  QgsStringMap properties() const override;
563  int count() const override { return mColors; }
564 
569  QString schemeName() const { return mSchemeName; }
570 
575  int colors() const { return mColors; }
576 
583  void setSchemeName( const QString &schemeName ) { mSchemeName = schemeName; loadPalette(); }
584 
591  void setColors( int colors ) { mColors = colors; loadPalette(); }
592 
597  static QStringList listSchemeNames();
598 
605  static QList<int> listSchemeVariants( const QString &schemeName );
606 
607  protected:
608 
610  void loadPalette();
611 
612  QString mSchemeName;
613  int mColors;
614  QList<QColor> mPalette;
615  bool mInverted;
616 };
617 
618 
619 #define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
620 #define DEFAULT_CPTCITY_VARIANTNAME "05"
621 
626 class CORE_EXPORT QgsCptCityColorRamp : public QgsGradientColorRamp
627 {
628  public:
629 
637  QgsCptCityColorRamp( const QString &schemeName = DEFAULT_CPTCITY_SCHEMENAME,
638  const QString &variantName = DEFAULT_CPTCITY_VARIANTNAME,
639  bool inverted = false,
640  bool doLoadFile = true );
641 
650  QgsCptCityColorRamp( const QString &schemeName, const QStringList &variantList,
651  const QString &variantName = QString(), bool inverted = false,
652  bool doLoadFile = true );
653 
654  static QgsColorRamp *create( const QgsStringMap &properties = QgsStringMap() ) SIP_FACTORY;
655 
656  QString type() const override { return QStringLiteral( "cpt-city" ); }
657 
658  void invert() override;
659 
660  QgsCptCityColorRamp *clone() const override SIP_FACTORY;
661  void copy( const QgsCptCityColorRamp *other );
662  QgsGradientColorRamp *cloneGradientRamp() const SIP_FACTORY;
663 
664  QgsStringMap properties() const override;
665 
666  QString schemeName() const { return mSchemeName; }
667  QString variantName() const { return mVariantName; }
668  QStringList variantList() const { return mVariantList; }
669 
670  // lazy loading - have to call loadPalette() explicitly
671  void setSchemeName( const QString &schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
672  void setVariantName( const QString &variantName ) { mVariantName = variantName; mFileLoaded = false; }
673  void setVariantList( const QStringList &variantList ) { mVariantList = variantList; }
674  void setName( const QString &schemeName, const QString &variantName = QString(), const QStringList &variantList = QStringList() )
675  { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
676 
677  void loadPalette() { loadFile(); }
678  bool hasMultiStops() const { return mMultiStops; }
679 
680  QString fileName() const;
681  bool loadFile();
682  bool fileLoaded() const { return mFileLoaded; }
683 
684  QString copyingFileName() const;
685  QString descFileName() const;
686  QgsStringMap copyingInfo() const;
687 
688  protected:
689  QString mSchemeName;
690  QString mVariantName;
691  QStringList mVariantList;
692  bool mFileLoaded = false;
693  bool mMultiStops = false;
694  bool mInverted;
695 
696 };
697 
698 // clazy:excludeall=qstring-allocations
699 
700 #endif
int valMin() const
Returns the minimum value for generated colors.
Definition: qgscolorramp.h:360
#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
bool operator==(const QgsGradientStop &other) const
Definition: qgscolorramp.h:120
QStringList mVariantList
Definition: qgscolorramp.h:691
void invert() override
Inverts the ordering of the color ramp.
QString schemeName() const override
Gets the name for the color scheme.
Definition: qgscolorramp.h:516
void setSchemeName(const QString &schemeName)
Definition: qgscolorramp.h:671
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
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:278
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
int colors() const
Returns the number of colors in the ramp.
Definition: qgscolorramp.h:575
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:672
QString schemeName() const
Definition: qgscolorramp.h:666
#define DEFAULT_COLORBREWER_SCHEMENAME
Definition: qgscolorramp.h:526
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:518
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:577
Q_DECLARE_METATYPE(QModelIndex)
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:674
QgsGradientColorRamp * clone() const override
Creates a clone of the color ramp.
QString variantName() const
Definition: qgscolorramp.h:667
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (e.g., authorship notes) ...
Definition: qgscolorramp.h:241
QString type() const override
Returns a string representing the color ramp type.
Definition: qgscolorramp.h:162
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
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:510
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.
int satMax() const
Returns the maximum saturation for generated colors.
Definition: qgscolorramp.h:354
#define DEFAULT_CPTCITY_SCHEMENAME
Definition: qgscolorramp.h:619
int hueMin() const
Returns the minimum hue for generated colors.
Definition: qgscolorramp.h:336
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:182
void setSchemeName(const QString &schemeName)
Sets the name of the color brewer color scheme.
Definition: qgscolorramp.h:583
int count() const override
Returns number of defined colors, or -1 if undefined.
Definition: qgscolorramp.h:563
int hueMax() const
Returns the maximum hue for generated colors.
Definition: qgscolorramp.h:342
#define SIP_FACTORY
Definition: qgis_sip.h:69
bool isDiscrete() const
Returns true if the gradient is using discrete interpolation, rather than smoothly interpolating betw...
Definition: qgscolorramp.h:202
virtual QString type() const =0
Returns a string representing the color ramp type.
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
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:500
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
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:559
#define DEFAULT_RANDOM_SAT_MIN
Definition: qgscolorramp.h:274
QList< QColor > mPalette
Definition: qgscolorramp.h:614
QString type() const override
Returns a string representing the color ramp type.
Definition: qgscolorramp.h:656
void setVariantList(const QStringList &variantList)
Definition: qgscolorramp.h:673
void setInfo(const QgsStringMap &info)
Sets additional info to attach to the gradient ramp (e.g., authorship notes)
Definition: qgscolorramp.h:248
int satMin() const
Returns the minimum saturation for generated colors.
Definition: qgscolorramp.h:348
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
#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:535
#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
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
Definition: qgscolorramp.h:235
QgsStringMap mInfo
Definition: qgscolorramp.h:264
QColor color2() const
Returns the gradient end color.
Definition: qgscolorramp.h:179
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
Definition: qgscolorramp.h:139
#define DEFAULT_CPTCITY_VARIANTNAME
Definition: qgscolorramp.h:620
QColor color1() const
Returns the gradient start color.
Definition: qgscolorramp.h:172
QgsStringMap properties() const override
Returns a string map containing all the color ramp&#39;s properties.
void setColors(int colors)
Sets the number of colors in the ramp.
Definition: qgscolorramp.h:591
QgsGradientStop(double offset, const QColor &color)
Constructor for QgsGradientStop.
Definition: qgscolorramp.h:110
bool fileLoaded() const
Definition: qgscolorramp.h:682
QString schemeName() const
Returns the name of the color brewer color scheme.
Definition: qgscolorramp.h:569
#define DEFAULT_COLORBREWER_COLORS
Definition: qgscolorramp.h:527
QStringList variantList() const
Definition: qgscolorramp.h:668
bool hasMultiStops() const
Definition: qgscolorramp.h:678
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