QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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.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
ClassificationMode classificationMode() const
Returns the classification mode.
A rectangle specified with double values.
Definition: qgsrectangle.h:40
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
void setClip(bool clip)
Sets whether the shader should not render values out of range.
Type
Supported methods for color interpolation.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom colormap.
bool clip() const
Returns whether the shader will clip values which are out of range.
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
Base class for processing filters like renderers, reprojector, resampler etc.
Type colorRampType() const
Returns the color ramp type.
bool operator<(const QgsColorRampShader::ColorRampItem &other) const
std::unique_ptr< QgsColorRamp > mSourceColorRamp
Source color ramp.
#define SIP_OUT
Definition: qgis_sip.h:51
void setClassificationMode(ClassificationMode classificationMode)
Sets classification mode.
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.
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74