QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
130  QgsColorRamp *sourceColorRamp() const SIP_FACTORY;
131 
137  void setSourceColorRamp( QgsColorRamp *colorramp SIP_TRANSFER );
138 
140  void setColorRampType( const QString &type );
141 
149  void classifyColorRamp( int classes = 0, int band = -1, const QgsRectangle &extent = QgsRectangle(), QgsRasterInterface *input = nullptr );
150 
157  void classifyColorRamp( int band = -1, const QgsRectangle &extent = QgsRectangle(), QgsRasterInterface *input = nullptr ) SIP_PYNAME( classifyColorRampV2 );
158 
160  bool shade( double value, int *returnRedValue SIP_OUT, int *returnGreenValue SIP_OUT, int *returnBlueValue SIP_OUT, int *returnAlphaValue SIP_OUT ) const override;
161 
163  bool shade( double redValue, double greenValue,
164  double blueValue, double alphaValue,
165  int *returnRedValue SIP_OUT, int *returnGreenValue SIP_OUT,
166  int *returnBlueValue SIP_OUT, int *returnAlphaValue SIP_OUT ) const override;
167 
168  void legendSymbologyItems( QList< QPair< QString, QColor > > &symbolItems SIP_OUT ) const override;
169 
174  QDomElement writeXml( QDomDocument &doc ) const;
175 
180  void readXml( const QDomElement &elem );
181 
183  void setClassificationMode( ClassificationMode classificationMode ) { mClassificationMode = classificationMode; }
184 
186  ClassificationMode classificationMode() const { return mClassificationMode; }
187 
193  void setClip( bool clip ) { mClip = clip; }
194 
199  bool clip() const { return mClip; }
200 
201  protected:
202 
204  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
205 
206  private:
207 
214  QVector<QgsColorRampShader::ColorRampItem> mColorRampItemList;
215 
216  Type mColorRampType;
217  ClassificationMode mClassificationMode;
218 
222  mutable QVector<int> mLUT;
223  mutable double mLUTOffset = 0.0;
224  mutable double mLUTFactor = 1.0;
225  mutable bool mLUTInitialized = false;
226 
228  bool mClip = false;
229 };
230 
231 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:41
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom colormap.
void setClip(bool clip)
Sets whether the shader should not render values out of range.
Type
Supported methods for color interpolation.
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
ColorRampItem(double val, const QColor &col, const QString &lbl=QString())
convenience constructor
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:69
Base class for processing filters like renderers, reprojector, resampler etc.
Type colorRampType() const
Returns the color ramp type.
std::unique_ptr< QgsColorRamp > mSourceColorRamp
Source color ramp.
#define SIP_OUT
Definition: qgis_sip.h:51
void setClassificationMode(ClassificationMode classificationMode)
Sets classification mode.
bool clip() const
Returns whether the shader will clip values which are out of range.
Interpolates the color between two class breaks linearly.
ClassificationMode
Classification modes used to create the color ramp shader.
Assigns the color of the higher class for every pixel between two class breaks.
ClassificationMode classificationMode() const
Returns the classification mode.
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74
bool operator<(const QgsColorRampShader::ColorRampItem &other) const