20 #define DOUBLE_DIFF_THRESHOLD 0.0000001 30 , mColorRampType( INTERPOLATED )
34 mMaximumColorCacheSize = 1024;
35 mCurrentColorRampItemIndex = 0;
40 switch ( mColorRampType )
43 return QString(
"INTERPOLATED" );
52 bool QgsColorRampShader::discreteColor(
double theValue,
int* theReturnRedValue,
int* theReturnGreenValue,
int* theReturnBlueValue,
int* theReturnAlphaValue )
54 int myColorRampItemCount = mColorRampItemList.count();
55 if ( myColorRampItemCount <= 0 )
60 double myTinyDiff = 0.0;
62 while ( mCurrentColorRampItemIndex >= 0 && mCurrentColorRampItemIndex < myColorRampItemCount )
65 myColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex );
66 myTinyDiff = qAbs( theValue - myColorRampItem.
value );
68 if ( mCurrentColorRampItemIndex != 0 &&
69 theValue <= mColorRampItemList.at( mCurrentColorRampItemIndex - 1 ).value )
71 mCurrentColorRampItemIndex--;
75 *theReturnRedValue = myColorRampItem.
color.
red();
76 *theReturnGreenValue = myColorRampItem.
color.
green();
77 *theReturnBlueValue = myColorRampItem.
color.
blue();
78 *theReturnAlphaValue = myColorRampItem.
color.
alpha();
80 if ( mMaximumColorCacheSize >= mColorCache.
size() )
82 mColorCache.
insert( theValue, myColorRampItem.
color );
89 mCurrentColorRampItemIndex++;
96 bool QgsColorRampShader::exactColor(
double theValue,
int* theReturnRedValue,
int* theReturnGreenValue,
int* theReturnBlueValue,
int *theReturnAlphaValue )
98 int myColorRampItemCount = mColorRampItemList.count();
99 if ( myColorRampItemCount <= 0 )
104 double myTinyDiff = 0.0;
106 while ( mCurrentColorRampItemIndex >= 0 && mCurrentColorRampItemIndex < myColorRampItemCount )
109 myColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex );
110 myTinyDiff = qAbs( theValue - myColorRampItem.
value );
113 *theReturnRedValue = myColorRampItem.
color.
red();
114 *theReturnGreenValue = myColorRampItem.
color.
green();
115 *theReturnBlueValue = myColorRampItem.
color.
blue();
116 *theReturnAlphaValue = myColorRampItem.
color.
alpha();
118 if ( mMaximumColorCacheSize >= mColorCache.
size() )
120 mColorCache.
insert( theValue, myColorRampItem.
color );
125 else if ( mCurrentColorRampItemIndex != myColorRampItemCount - 1 &&
126 theValue > myColorRampItem.
value && theValue < mColorRampItemList.at(
127 mCurrentColorRampItemIndex + 1 ).value )
132 else if ( theValue > myColorRampItem.
value )
134 mCurrentColorRampItemIndex++;
139 mCurrentColorRampItemIndex--;
146 bool QgsColorRampShader::interpolatedColor(
double theValue,
int*
147 theReturnRedValue,
int* theReturnGreenValue,
int* theReturnBlueValue,
int* theReturnAlphaValue )
149 int myColorRampItemCount = mColorRampItemList.count();
150 if ( myColorRampItemCount <= 0 )
155 double myTinyDiff = 0.0;
156 double myCurrentRampRange;
157 double myOffsetInRange;
159 while ( mCurrentColorRampItemIndex >= 0 && mCurrentColorRampItemIndex < myColorRampItemCount )
162 myColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex );
163 myTinyDiff = qAbs( theValue - myColorRampItem.
value );
165 if ( mCurrentColorRampItemIndex != 0 && theValue <= mColorRampItemList.at( mCurrentColorRampItemIndex - 1 ).value )
167 mCurrentColorRampItemIndex--;
172 myCurrentRampRange = myColorRampItem.
value - myPreviousColorRampItem.
value;
173 myOffsetInRange = theValue - myPreviousColorRampItem.
value;
174 double scale = myOffsetInRange / myCurrentRampRange;
176 *theReturnRedValue =
static_cast< int >(
static_cast< double >( myPreviousColorRampItem.
color.
red() ) + ( static_cast< double >( myColorRampItem.
color.
red() - myPreviousColorRampItem.
color.
red() ) * scale ) );
177 *theReturnGreenValue =
static_cast< int >(
static_cast< double >( myPreviousColorRampItem.
color.
green() ) + ( static_cast< double >( myColorRampItem.
color.
green() - myPreviousColorRampItem.
color.
green() ) * scale ) );
178 *theReturnBlueValue =
static_cast< int >(
static_cast< double >( myPreviousColorRampItem.
color.
blue() ) + ( static_cast< double >( myColorRampItem.
color.
blue() - myPreviousColorRampItem.
color.
blue() ) * scale ) );
179 *theReturnAlphaValue =
static_cast< int >(
static_cast< double >( myPreviousColorRampItem.
color.
alpha() ) + ( static_cast< double >( myColorRampItem.
color.
alpha() - myPreviousColorRampItem.
color.
alpha() ) * scale ) );
180 if ( mMaximumColorCacheSize >= mColorCache.
size() )
182 QColor myNewColor( *theReturnRedValue, *theReturnGreenValue, *theReturnBlueValue, *theReturnAlphaValue );
183 mColorCache.
insert( theValue, myNewColor );
188 else if (( mCurrentColorRampItemIndex == 0 && ( myTinyDiff <=
DOUBLE_DIFF_THRESHOLD || ( !mClip && theValue <= myColorRampItem.
value ) ) )
189 || ( mCurrentColorRampItemIndex == myColorRampItemCount - 1 && ( myTinyDiff <=
DOUBLE_DIFF_THRESHOLD || ( !mClip && theValue >= myColorRampItem.
value ) ) ) )
195 *theReturnRedValue = myColorRampItem.
color.
red();
196 *theReturnGreenValue = myColorRampItem.
color.
green();
197 *theReturnBlueValue = myColorRampItem.
color.
blue();
198 *theReturnAlphaValue = myColorRampItem.
color.
alpha();
199 if ( mMaximumColorCacheSize >= mColorCache.
size() )
201 QColor myNewColor( *theReturnRedValue, *theReturnGreenValue, *theReturnBlueValue, *theReturnAlphaValue );
202 mColorCache.
insert( theValue, myNewColor );
207 else if ( theValue > myColorRampItem.
value )
209 mCurrentColorRampItemIndex++;
222 mColorRampItemList = theList;
231 mColorRampType = theColorRampType;
238 if ( theType ==
"INTERPOLATED" )
242 else if ( theType ==
"DISCRETE" )
248 mColorRampType =
EXACT;
252 bool QgsColorRampShader::shade(
double theValue,
int* theReturnRedValue,
int* theReturnGreenValue,
int* theReturnBlueValue,
int *theReturnAlphaValue )
259 *theReturnRedValue = myColor.
red();
260 *theReturnGreenValue = myColor.
green();
261 *theReturnBlueValue = myColor.
blue();
262 *theReturnAlphaValue = myColor.
alpha();
269 if ( mCurrentColorRampItemIndex < 0 )
271 mCurrentColorRampItemIndex = 0;
273 else if ( mCurrentColorRampItemIndex >= mColorRampItemList.size() )
275 mCurrentColorRampItemIndex = mColorRampItemList.size() - 1;
280 return exactColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
284 return interpolatedColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
287 return discreteColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
291 double theBlueValue,
double theAlphaValue,
292 int* theReturnRedValue,
int* theReturnGreenValue,
293 int* theReturnBlueValue,
int* theReturnAlphaValue )
295 Q_UNUSED( theRedValue );
296 Q_UNUSED( theGreenValue );
297 Q_UNUSED( theBlueValue );
298 Q_UNUSED( theAlphaValue );
300 *theReturnRedValue = 0;
301 *theReturnGreenValue = 0;
302 *theReturnBlueValue = 0;
303 *theReturnAlphaValue = 0;
311 for ( ; colorRampIt != mColorRampItemList.constEnd(); ++colorRampIt )
313 symbolItems.push_back( qMakePair( colorRampIt->label, colorRampIt->color ) );
QgsColorRampShader(double theMinimumValue=0.0, double theMaximumValue=255.0)
void setColorRampItemList(const QList< QgsColorRampShader::ColorRampItem > &theList)
Set custom colormap.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
#define QgsDebugMsgLevel(str, level)
bool shade(double, int *, int *, int *, int *) override
Generates and new RGB value based on one input value.
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
void setColorRampType(QgsColorRampShader::ColorRamp_TYPE theColorRampType)
Set the color ramp type.
QString colorRampTypeAsQString()
Get the color ramp type as a string.
#define DOUBLE_DIFF_THRESHOLD
iterator insert(const Key &key, const T &value)
const T value(const Key &key) const