QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgscolorrampshader.h
Go to the documentation of this file.
1 /* **************************************************************************
2  qgscolorrampshader.h - description
3  -------------------
4 begin : Fri Dec 28 2007
5 copyright : (C) 2007 by Peter J. Ersts
6 email : [email protected]
7 
8 This class is based off of code that was originally written by Marco Hugentobler and
9 originally part of the larger QgsRasterLayer class
10 ****************************************************************************/
11 
12 /* **************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  ***************************************************************************/
20 
21 #ifndef QGSCOLORRAMPSHADER_H
22 #define QGSCOLORRAMPSHADER_H
23 
24 #include "qgis_core.h"
25 #include "qgis_sip.h"
26 #include <QColor>
27 #include <QVector>
28 #include <memory>
29 
30 #include "qgscolorramp.h"
31 #include "qgsrasterinterface.h"
33 #include "qgsrectangle.h"
34 
39 class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
40 {
41 
42  public:
43 
45  enum Type
46  {
49  Exact
50  };
51 
54  {
55  Continuous = 1,
56  EqualInterval = 2,
57  Quantile = 3
58  };
59 
69  QgsColorRampShader( double minimumValue = 0.0, double maximumValue = 255.0, QgsColorRamp *colorRamp SIP_TRANSFER = nullptr, Type type = Interpolated, ClassificationMode classificationMode = Continuous );
70 
74  QgsColorRampShader( const QgsColorRampShader &other );
75 
79  QgsColorRampShader &operator=( const QgsColorRampShader &other );
80 
81  //An entry for classification based upon value.
82  //Such a classification is typically used for
83  //single band layers where a pixel value represents
84  //not a color but a quantity, e.g. temperature or elevation
86  {
88  ColorRampItem() = default;
90  ColorRampItem( double val, const QColor &col, const QString &lbl = QString() )
91  : label( lbl )
92  , value( val )
93  , color( col )
94  {}
95 
96  QString label;
97  double value = 0;
98  QColor color;
99 
100  // compare operator for sorting
101  bool operator<( const QgsColorRampShader::ColorRampItem &other ) const { return value < other.value; }
102  };
103 
105  QList<QgsColorRampShader::ColorRampItem> colorRampItemList() const { return mColorRampItemList.toList(); }
106 
108  Type colorRampType() const { return mColorRampType; }
109 
111  QString colorRampTypeAsQString() const;
112 
114  void setColorRampItemList( const QList<QgsColorRampShader::ColorRampItem> &list ); //TODO: sort on set
115 
117  void setColorRampType( QgsColorRampShader::Type colorRampType );
118 
123  bool isEmpty() const;
124 
131  QgsColorRamp *sourceColorRamp() const;
132 
138  void setSourceColorRamp( QgsColorRamp *colorramp SIP_TRANSFER );
139 
141  void setColorRampType( const QString &type );
142 
150  void classifyColorRamp( int classes = 0, int band = -1, const QgsRectangle &extent = QgsRectangle(), QgsRasterInterface *input = nullptr );
151 
158  void classifyColorRamp( int band = -1, const QgsRectangle &extent = QgsRectangle(), QgsRasterInterface *input = nullptr ) SIP_PYNAME( classifyColorRampV2 );
159 
161  bool shade( double value, int *returnRedValue SIP_OUT, int *returnGreenValue SIP_OUT, int *returnBlueValue SIP_OUT, int *returnAlphaValue SIP_OUT ) const override;
162 
164  bool shade( double redValue, double greenValue,
165  double blueValue, double alphaValue,
166  int *returnRedValue SIP_OUT, int *returnGreenValue SIP_OUT,
167  int *returnBlueValue SIP_OUT, int *returnAlphaValue SIP_OUT ) const override;
168 
169  void legendSymbologyItems( QList< QPair< QString, QColor > > &symbolItems SIP_OUT ) const override;
170 
175  QDomElement writeXml( QDomDocument &doc ) const;
176 
181  void readXml( const QDomElement &elem );
182 
184  void setClassificationMode( ClassificationMode classificationMode ) { mClassificationMode = classificationMode; }
185 
187  ClassificationMode classificationMode() const { return mClassificationMode; }
188 
194  void setClip( bool clip ) { mClip = clip; }
195 
200  bool clip() const { return mClip; }
201 
202  protected:
203 
205  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
206 
207  private:
208 
215  QVector<QgsColorRampShader::ColorRampItem> mColorRampItemList;
216 
217  Type mColorRampType;
218  ClassificationMode mClassificationMode;
219 
223  mutable QVector<int> mLUT;
224  mutable double mLUTOffset = 0.0;
225  mutable double mLUTFactor = 1.0;
226  mutable bool mLUTInitialized = false;
227 
229  bool mClip = false;
230 };
231 
232 #endif
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsColorRamp
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
qgsrectangle.h
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsColorRampShader
Definition: qgscolorrampshader.h:39
QgsColorRampShader::Type
Type
Supported methods for color interpolation.
Definition: qgscolorrampshader.h:45
QgsRectangle
Definition: qgsrectangle.h:41
QgsColorRampShader::Discrete
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
Definition: qgscolorrampshader.h:48
QgsColorRampShader::ColorRampItem::color
QColor color
Definition: qgscolorrampshader.h:98
qgsrasterinterface.h
qgsrastershaderfunction.h
QgsColorRampShader::setClip
void setClip(bool clip)
Sets whether the shader should not render values out of range.
Definition: qgscolorrampshader.h:194
QgsColorRampShader::ColorRampItem
Definition: qgscolorrampshader.h:85
qgscolorramp.h
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsColorRampShader::ColorRampItem::ColorRampItem
ColorRampItem(double val, const QColor &col, const QString &lbl=QString())
convenience constructor
Definition: qgscolorrampshader.h:90
QgsColorRampShader::colorRampItemList
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom colormap.
Definition: qgscolorrampshader.h:105
QgsColorRampShader::colorRampType
Type colorRampType() const
Returns the color ramp type.
Definition: qgscolorrampshader.h:108
QgsColorRampShader::ClassificationMode
ClassificationMode
Classification modes used to create the color ramp shader.
Definition: qgscolorrampshader.h:53
QgsRasterInterface
Definition: qgsrasterinterface.h:116
QgsColorRampShader::ColorRampItem::operator<
bool operator<(const QgsColorRampShader::ColorRampItem &other) const
Definition: qgscolorrampshader.h:101
QgsColorRampShader::clip
bool clip() const
Returns whether the shader will clip values which are out of range.
Definition: qgscolorrampshader.h:200
QgsRasterShaderFunction
Definition: qgsrastershaderfunction.h:34
QgsColorRampShader::mSourceColorRamp
std::unique_ptr< QgsColorRamp > mSourceColorRamp
Source color ramp.
Definition: qgscolorrampshader.h:205
QgsColorRampShader::classificationMode
ClassificationMode classificationMode() const
Returns the classification mode.
Definition: qgscolorrampshader.h:187
QgsColorRampShader::ColorRampItem::label
QString label
Definition: qgscolorrampshader.h:96
QgsColorRampShader::Interpolated
@ Interpolated
Interpolates the color between two class breaks linearly.
Definition: qgscolorrampshader.h:47
QgsColorRampShader::ColorRampItem::value
double value
Definition: qgscolorrampshader.h:97