QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
72  QgsColorRampShader( double minimumValue = 0.0, double maximumValue = 255.0, QgsColorRamp *colorRamp SIP_TRANSFER = nullptr, Type type = Interpolated, ClassificationMode classificationMode = Continuous );
73 
74  ~QgsColorRampShader() override;
75 
79  QgsColorRampShader( const QgsColorRampShader &other );
80 
84  QgsColorRampShader &operator=( const QgsColorRampShader &other );
85 
86  bool operator==( const QgsColorRampShader &other ) const
87  {
88  if ( mColorRampItemList.count() != other.mColorRampItemList.count() ||
89  mClassificationMode != other.mClassificationMode ||
90  mColorRampType != other.mColorRampType )
91  {
92  return false;
93  }
94  for ( int i = 0; i < mColorRampItemList.count(); ++i )
95  {
96  if ( mColorRampItemList.at( i ) != other.mColorRampItemList.at( i ) ) return false;
97  }
98  return true;
99  }
100 
101  //An entry for classification based upon value.
102  //Such a classification is typically used for
103  //single band layers where a pixel value represents
104  //not a color but a quantity, e.g. temperature or elevation
106  {
108  ColorRampItem() = default;
110  ColorRampItem( double val, const QColor &col, const QString &lbl = QString() )
111  : label( lbl )
112  , value( val )
113  , color( col )
114  {}
115 
116  QString label;
117  double value = 0;
118  QColor color;
119 
120  // compare operator for sorting
121  bool operator<( const QgsColorRampShader::ColorRampItem &other ) const { return value < other.value; }
122 
124  {
125  return ( color != other.color ) ||
126  ( !std::isnan( value ) && !std::isnan( other.value ) && value != other.value ) ||
127  ( std::isnan( value ) != std::isnan( other.value ) );
128  }
129  };
130 
132  QList<QgsColorRampShader::ColorRampItem> colorRampItemList() const { return mColorRampItemList.toList(); }
133 
135  Type colorRampType() const { return mColorRampType; }
136 
138  QString colorRampTypeAsQString() const;
139 
141  void setColorRampItemList( const QList<QgsColorRampShader::ColorRampItem> &list ); //TODO: sort on set
142 
144  void setColorRampType( QgsColorRampShader::Type colorRampType );
145 
150  bool isEmpty() const;
151 
157  QgsColorRamp *sourceColorRamp() const;
158 
163  QgsColorRamp *createColorRamp() const SIP_FACTORY;
164 
170  void setSourceColorRamp( QgsColorRamp *colorramp SIP_TRANSFER );
171 
173  void setColorRampType( const QString &type );
174 
182  void classifyColorRamp( int classes = 0, int band = -1, const QgsRectangle &extent = QgsRectangle(), QgsRasterInterface *input = nullptr );
183 
190  void classifyColorRamp( int band = -1, const QgsRectangle &extent = QgsRectangle(), QgsRasterInterface *input = nullptr ) SIP_PYNAME( classifyColorRampV2 );
191 
193  bool shade( double value, int *returnRedValue SIP_OUT, int *returnGreenValue SIP_OUT, int *returnBlueValue SIP_OUT, int *returnAlphaValue SIP_OUT ) const override;
194 
196  bool shade( double redValue, double greenValue,
197  double blueValue, double alphaValue,
198  int *returnRedValue SIP_OUT, int *returnGreenValue SIP_OUT,
199  int *returnBlueValue SIP_OUT, int *returnAlphaValue SIP_OUT ) const override;
200 
201  void legendSymbologyItems( QList< QPair< QString, QColor > > &symbolItems SIP_OUT ) const override;
202 
207  QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context = QgsReadWriteContext() ) const;
208 
213  void readXml( const QDomElement &elem, const QgsReadWriteContext &context = QgsReadWriteContext() );
214 
216  void setClassificationMode( ClassificationMode classificationMode ) { mClassificationMode = classificationMode; }
217 
219  ClassificationMode classificationMode() const { return mClassificationMode; }
220 
226  void setClip( bool clip ) { mClip = clip; }
227 
232  bool clip() const { return mClip; }
233 
240  const QgsColorRampLegendNodeSettings *legendSettings() const;
241 
250  void setLegendSettings( QgsColorRampLegendNodeSettings *settings SIP_TRANSFER );
251 
252  protected:
253 
255  std::unique_ptr<QgsColorRamp> mSourceColorRamp;
256 
257  private:
258 
266  QVector<QgsColorRampShader::ColorRampItem> mColorRampItemList;
267 
268  Type mColorRampType;
269  ClassificationMode mClassificationMode;
270 
275  mutable std::vector<int> mLUT;
276  mutable double mLUTOffset = 0.0;
277  mutable double mLUTFactor = 1.0;
278  mutable bool mLUTInitialized = false;
279 
281  bool mClip = false;
282 
283  std::unique_ptr< QgsColorRampLegendNodeSettings > mLegendSettings;
284 
285 };
286 
287 #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:32
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