QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgscolorrampshader.h
Go to the documentation of this file.
1/* **************************************************************************
2 qgscolorrampshader.h - description
3 -------------------
4begin : Fri Dec 28 2007
5copyright : (C) 2007 by Peter J. Ersts
7
8This class is based off of code that was originally written by Marco Hugentobler and
9originally 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 "qgis.h"
32#include "qgsrectangle.h"
33#include "qgsreadwritecontext.h"
35
36class QgsColorRamp;
38
44{
45
46 public:
47
57
59
61 QgsColorRampShader &operator=( const QgsColorRampShader &other );
62
63 bool operator==( const QgsColorRampShader &other ) const
64 {
65 if ( mColorRampItemList.count() != other.mColorRampItemList.count() ||
66 mClassificationMode != other.mClassificationMode ||
67 mColorRampType != other.mColorRampType )
68 {
69 return false;
70 }
71 for ( int i = 0; i < mColorRampItemList.count(); ++i )
72 {
73 if ( mColorRampItemList.at( i ) != other.mColorRampItemList.at( i ) ) return false;
74 }
75 return true;
76 }
77
78 bool operator!=( const QgsColorRampShader &other ) const
79 {
80 return !( *this == other );
81 }
82
83 //An entry for classification based upon value.
84 //Such a classification is typically used for
85 //single band layers where a pixel value represents
86 //not a color but a quantity, e.g. temperature or elevation
88 {
89
90 ColorRampItem() = default;
92 ColorRampItem( double val, const QColor &col, const QString &lbl = QString() )
93 : label( lbl )
94 , value( val )
95 , color( col )
96 {}
97
98 QString label;
99 double value = 0;
100 QColor color;
101
102 // compare operator for sorting
103 bool operator<( const QgsColorRampShader::ColorRampItem &other ) const { return value < other.value; }
104
106 {
107 return ( color != other.color ) ||
108 ( !std::isnan( value ) && !std::isnan( other.value ) && value != other.value ) ||
109 ( std::isnan( value ) != std::isnan( other.value ) );
110 }
111 };
112
118 QList<QgsColorRampShader::ColorRampItem> colorRampItemList() const { return mColorRampItemList.toList(); }
119
125 Qgis::ShaderInterpolationMethod colorRampType() const { return mColorRampType; }
126
128 QString colorRampTypeAsQString() const;
129
135 void setColorRampItemList( const QList<QgsColorRampShader::ColorRampItem> &list ); //TODO: sort on set
136
142 void setColorRampType( Qgis::ShaderInterpolationMethod colorRampType );
143
148 bool isEmpty() const;
149
154 QgsColorRamp *sourceColorRamp() const;
155
160 QgsColorRamp *createColorRamp() const SIP_FACTORY;
161
166 void setSourceColorRamp( QgsColorRamp *colorramp SIP_TRANSFER );
167
169 void setColorRampType( const QString &type );
170
178 void classifyColorRamp( int classes = 0, int band = -1, const QgsRectangle &extent = QgsRectangle(), QgsRasterInterface *input = nullptr );
179
186 void classifyColorRamp( int band = -1, const QgsRectangle &extent = QgsRectangle(), QgsRasterInterface *input = nullptr ) SIP_PYNAME( classifyColorRampV2 );
187
188 bool shade( double value, int *returnRedValue SIP_OUT, int *returnGreenValue SIP_OUT, int *returnBlueValue SIP_OUT, int *returnAlphaValue SIP_OUT ) const override;
189 bool shade( double redValue, double greenValue,
190 double blueValue, double alphaValue,
191 int *returnRedValue SIP_OUT, int *returnGreenValue SIP_OUT,
192 int *returnBlueValue SIP_OUT, int *returnAlphaValue SIP_OUT ) const override;
193 void legendSymbologyItems( QList< QPair< QString, QColor > > &symbolItems SIP_OUT ) const override;
194
199 QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context = QgsReadWriteContext() ) const;
200
205 void readXml( const QDomElement &elem, const QgsReadWriteContext &context = QgsReadWriteContext() );
206
212 void setClassificationMode( Qgis::ShaderClassificationMethod classificationMode ) { mClassificationMode = classificationMode; }
213
219 Qgis::ShaderClassificationMethod 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
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
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
ShaderInterpolationMethod
Color ramp shader interpolation methods.
Definition qgis.h:1335
@ Linear
Interpolates the color between two class breaks linearly.
ShaderClassificationMethod
Color ramp shader classification methods.
Definition qgis.h:1350
@ Continuous
Uses breaks from color palette.
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
Qgis::ShaderClassificationMethod classificationMode() const
Returns the classification mode.
Qgis::ShaderInterpolationMethod colorRampType() const
Returns the color ramp interpolation method.
bool operator!=(const QgsColorRampShader &other) const
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom color map.
bool operator==(const QgsColorRampShader &other) const
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.
std::unique_ptr< QgsColorRamp > mSourceColorRamp
Source color ramp.
Abstract base class for color ramps.
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.
#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