QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
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 
31 #include "qgsrectangle.h"
32 #include "qgsreadwritecontext.h"
34 
35 class QgsColorRamp;
36 class QgsRasterInterface;
37 
42 class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
43 {
44 
45  public:
46 
48  enum Type
49  {
52  Exact
53  };
54 
57  {
58  Continuous = 1,
59  EqualInterval = 2,
60  Quantile = 3
61  };
62 
71  QgsColorRampShader( double minimumValue = 0.0, double maximumValue = 255.0, QgsColorRamp *colorRamp SIP_TRANSFER = nullptr, Type type = Interpolated, ClassificationMode classificationMode = Continuous );
72 
73  ~QgsColorRampShader() override;
74 
78  QgsColorRampShader( const QgsColorRampShader &other );
79 
83  QgsColorRampShader &operator=( const QgsColorRampShader &other );
84 
85  bool operator==( const QgsColorRampShader &other ) const
86  {
87  if ( mColorRampItemList.count() != other.mColorRampItemList.count() ||
88  mClassificationMode != other.mClassificationMode ||
89  mColorRampType != other.mColorRampType )
90  {
91  return false;
92  }
93  for ( int i = 0; i < mColorRampItemList.count(); ++i )
94  {
95  if ( mColorRampItemList.at( i ) != other.mColorRampItemList.at( i ) ) return false;
96  }
97  return true;
98  }
99 
100  //An entry for classification based upon value.
101  //Such a classification is typically used for
102  //single band layers where a pixel value represents
103  //not a color but a quantity, e.g. temperature or elevation
105  {
107  ColorRampItem() = default;
109  ColorRampItem( double val, const QColor &col, const QString &lbl = QString() )
110  : label( lbl )
111  , value( val )
112  , color( col )
113  {}
114 
115  QString label;
116  double value = 0;
117  QColor color;
118 
119  // compare operator for sorting
120  bool operator<( const QgsColorRampShader::ColorRampItem &other ) const { return value < other.value; }
121 
123  {
124  return ( color != other.color ) ||
125  ( !std::isnan( value ) && !std::isnan( other.value ) && value != other.value ) ||
126  ( std::isnan( value ) != std::isnan( other.value ) );
127  }
128  };
129 
131  QList<QgsColorRampShader::ColorRampItem> colorRampItemList() const { return mColorRampItemList.toList(); }
132 
134  Type colorRampType() const { return mColorRampType; }
135 
137  QString colorRampTypeAsQString() const;
138 
140  void setColorRampItemList( const QList<QgsColorRampShader::ColorRampItem> &list ); //TODO: sort on set
141 
143  void setColorRampType( QgsColorRampShader::Type colorRampType );
144 
149  bool isEmpty() const;
150 
155  QgsColorRamp *sourceColorRamp() const;
156 
161  QgsColorRamp *createColorRamp() const SIP_FACTORY;
162 
167  void setSourceColorRamp( QgsColorRamp *colorramp SIP_TRANSFER );
168 
170  void setColorRampType( const QString &type );
171 
179  void classifyColorRamp( int classes = 0, int band = -1, const QgsRectangle &extent = QgsRectangle(), QgsRasterInterface *input = nullptr );
180 
187  void classifyColorRamp( int band = -1, const QgsRectangle &extent = QgsRectangle(), QgsRasterInterface *input = nullptr ) SIP_PYNAME( classifyColorRampV2 );
188 
190  bool shade( double value, int *returnRedValue SIP_OUT, int *returnGreenValue SIP_OUT, int *returnBlueValue SIP_OUT, int *returnAlphaValue SIP_OUT ) const override;
191 
193  bool shade( double redValue, double greenValue,
194  double blueValue, double alphaValue,
195  int *returnRedValue SIP_OUT, int *returnGreenValue SIP_OUT,
196  int *returnBlueValue SIP_OUT, int *returnAlphaValue SIP_OUT ) const override;
197 
198  void legendSymbologyItems( QList< QPair< QString, QColor > > &symbolItems SIP_OUT ) const override;
199 
204  QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context = QgsReadWriteContext() ) const;
205 
210  void readXml( const QDomElement &elem, const QgsReadWriteContext &context = QgsReadWriteContext() );
211 
213  void setClassificationMode( ClassificationMode classificationMode ) { mClassificationMode = classificationMode; }
214 
216  ClassificationMode classificationMode() const { return mClassificationMode; }
217 
223  void setClip( bool clip ) { mClip = clip; }
224 
229  bool clip() const { return mClip; }
230 
237  const QgsColorRampLegendNodeSettings *legendSettings() const;
238 
247  void setLegendSettings( QgsColorRampLegendNodeSettings *settings SIP_TRANSFER );
248 
249  protected:
250 
252  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
253 
254  private:
255 
263  QVector<QgsColorRampShader::ColorRampItem> mColorRampItemList;
264 
265  Type mColorRampType;
266  ClassificationMode mClassificationMode;
267 
272  mutable std::vector<int> mLUT;
273  mutable double mLUTOffset = 0.0;
274  mutable double mLUTFactor = 1.0;
275  mutable bool mLUTInitialized = false;
276 
278  bool mClip = false;
279 
280  std::unique_ptr< QgsColorRampLegendNodeSettings > mLegendSettings;
281 
282 };
283 
284 #endif
Settings for a color ramp legend node.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
~QgsColorRampShader() override
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom colormap.
ClassificationMode classificationMode() const
Returns the classification mode.
Type colorRampType() const
Returns the color ramp type.
bool operator==(const QgsColorRampShader &other) const
ClassificationMode
Classification modes used to create the color ramp shader.
void setClip(bool clip)
Sets whether the shader should not render values out of range.
bool clip() const
Returns whether the shader will clip values which are out of range.
Type
Supported methods for color interpolation.
@ Interpolated
Interpolates the color between two class breaks linearly.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
std::unique_ptr< QgsColorRamp > mSourceColorRamp
Source color ramp.
Abstract base class for color ramps.
Definition: qgscolorramp.h:29
Base class for processing filters like renderers, reprojector, resampler etc.
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_FACTORY
Definition: qgis_sip.h:76
ColorRampItem(double val, const QColor &col, const QString &lbl=QString())
convenience constructor
bool operator!=(const QgsColorRampShader::ColorRampItem &other) const
bool operator<(const QgsColorRampShader::ColorRampItem &other) const
ColorRampItem()=default
default constructor