20 #define DOUBLE_DIFF_THRESHOLD 0.0000001
30 , mColorRampType( INTERPOLATED )
34 mMaximumColorCacheSize = 1024;
35 mCurrentColorRampItemIndex = 0;
40 switch ( mColorRampType )
43 return QString(
"INTERPOLATED" );
46 return QString(
"DISCRETE" );
49 return QString(
"EXACT" );
52 return QString(
"Unknown" );
55 bool QgsColorRampShader::discreteColor(
double theValue,
int* theReturnRedValue,
int* theReturnGreenValue,
int* theReturnBlueValue,
int* theReturnAlphaValue )
57 int myColorRampItemCount = mColorRampItemList.count();
58 if ( myColorRampItemCount <= 0 )
63 double myTinyDiff = 0.0;
65 while ( mCurrentColorRampItemIndex >= 0 && mCurrentColorRampItemIndex < myColorRampItemCount )
68 myColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex );
69 myTinyDiff = qAbs( theValue - myColorRampItem.
value );
71 if ( mCurrentColorRampItemIndex != 0 &&
72 theValue <= mColorRampItemList.at( mCurrentColorRampItemIndex - 1 ).value )
74 mCurrentColorRampItemIndex--;
78 *theReturnRedValue = myColorRampItem.
color.red();
79 *theReturnGreenValue = myColorRampItem.
color.green();
80 *theReturnBlueValue = myColorRampItem.
color.blue();
81 *theReturnAlphaValue = myColorRampItem.
color.alpha();
83 if ( mMaximumColorCacheSize >= mColorCache.size() )
85 mColorCache.insert( theValue, myColorRampItem.
color );
92 mCurrentColorRampItemIndex++;
99 bool QgsColorRampShader::exactColor(
double theValue,
int* theReturnRedValue,
int* theReturnGreenValue,
int* theReturnBlueValue,
int *theReturnAlphaValue )
101 int myColorRampItemCount = mColorRampItemList.count();
102 if ( myColorRampItemCount <= 0 )
107 double myTinyDiff = 0.0;
109 while ( mCurrentColorRampItemIndex >= 0 && mCurrentColorRampItemIndex < myColorRampItemCount )
112 myColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex );
113 myTinyDiff = qAbs( theValue - myColorRampItem.
value );
116 *theReturnRedValue = myColorRampItem.
color.red();
117 *theReturnGreenValue = myColorRampItem.
color.green();
118 *theReturnBlueValue = myColorRampItem.
color.blue();
119 *theReturnAlphaValue = myColorRampItem.
color.alpha();
121 if ( mMaximumColorCacheSize >= mColorCache.size() )
123 mColorCache.insert( theValue, myColorRampItem.
color );
128 else if ( mCurrentColorRampItemIndex != myColorRampItemCount - 1 &&
129 theValue > myColorRampItem.
value && theValue < mColorRampItemList.at(
130 mCurrentColorRampItemIndex + 1 ).value )
135 else if ( theValue > myColorRampItem.
value )
137 mCurrentColorRampItemIndex++;
142 mCurrentColorRampItemIndex--;
149 bool QgsColorRampShader::interpolatedColor(
double theValue,
int*
150 theReturnRedValue,
int* theReturnGreenValue,
int* theReturnBlueValue,
int* theReturnAlphaValue )
152 int myColorRampItemCount = mColorRampItemList.count();
153 if ( myColorRampItemCount <= 0 )
158 double myTinyDiff = 0.0;
159 double myCurrentRampRange;
160 double myOffsetInRange;
162 while ( mCurrentColorRampItemIndex >= 0 && mCurrentColorRampItemIndex < myColorRampItemCount )
165 myColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex );
166 myTinyDiff = qAbs( theValue - myColorRampItem.
value );
168 if ( mCurrentColorRampItemIndex != 0 && theValue <= mColorRampItemList.at( mCurrentColorRampItemIndex - 1 ).value )
170 mCurrentColorRampItemIndex--;
175 myCurrentRampRange = myColorRampItem.
value - myPreviousColorRampItem.
value;
176 myOffsetInRange = theValue - myPreviousColorRampItem.
value;
177 double scale = myOffsetInRange / myCurrentRampRange;
179 *theReturnRedValue = ( int )((
double ) myPreviousColorRampItem.
color.red() + (( double )( myColorRampItem.
color.red() - myPreviousColorRampItem.
color.red() ) * scale ) );
180 *theReturnGreenValue = ( int )((
double ) myPreviousColorRampItem.
color.green() + (( double )( myColorRampItem.
color.green() - myPreviousColorRampItem.
color.green() ) * scale ) );
181 *theReturnBlueValue = ( int )((
double ) myPreviousColorRampItem.
color.blue() + (( double )( myColorRampItem.
color.blue() - myPreviousColorRampItem.
color.blue() ) * scale ) );
182 *theReturnAlphaValue = ( int )((
double ) myPreviousColorRampItem.
color.alpha() + (( double )( myColorRampItem.
color.alpha() - myPreviousColorRampItem.
color.alpha() ) * scale ) );
183 if ( mMaximumColorCacheSize >= mColorCache.size() )
185 QColor myNewColor( *theReturnRedValue, *theReturnGreenValue, *theReturnBlueValue, *theReturnAlphaValue );
186 mColorCache.insert( theValue, myNewColor );
191 else if (( mCurrentColorRampItemIndex == 0 && ( myTinyDiff <=
DOUBLE_DIFF_THRESHOLD || ( !mClip && theValue <= myColorRampItem.
value ) ) )
192 || ( mCurrentColorRampItemIndex == myColorRampItemCount - 1 && ( myTinyDiff <=
DOUBLE_DIFF_THRESHOLD || ( !mClip && theValue >= myColorRampItem.
value ) ) ) )
198 *theReturnRedValue = myColorRampItem.
color.red();
199 *theReturnGreenValue = myColorRampItem.
color.green();
200 *theReturnBlueValue = myColorRampItem.
color.blue();
201 *theReturnAlphaValue = myColorRampItem.
color.alpha();
202 if ( mMaximumColorCacheSize >= mColorCache.size() )
204 QColor myNewColor( *theReturnRedValue, *theReturnGreenValue, *theReturnBlueValue, *theReturnAlphaValue );
205 mColorCache.insert( theValue, myNewColor );
210 else if ( theValue > myColorRampItem.
value )
212 mCurrentColorRampItemIndex++;
225 mColorRampItemList = theList;
234 mColorRampType = theColorRampType;
241 if ( theType ==
"INTERPOLATED" )
245 else if ( theType ==
"DISCRETE" )
251 mColorRampType =
EXACT;
255 bool QgsColorRampShader::shade(
double theValue,
int* theReturnRedValue,
int* theReturnGreenValue,
int* theReturnBlueValue,
int *theReturnAlphaValue )
259 QColor myColor = mColorCache.value( theValue );
260 if ( myColor.isValid() )
262 *theReturnRedValue = myColor.red();
263 *theReturnGreenValue = myColor.green();
264 *theReturnBlueValue = myColor.blue();
265 *theReturnAlphaValue = myColor.alpha();
272 if ( mCurrentColorRampItemIndex < 0 )
274 mCurrentColorRampItemIndex = 0;
276 else if ( mCurrentColorRampItemIndex >= mColorRampItemList.size() )
278 mCurrentColorRampItemIndex = mColorRampItemList.size() - 1;
283 return exactColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
287 return interpolatedColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
290 return discreteColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
294 double theBlueValue,
double theAlphaValue,
295 int* theReturnRedValue,
int* theReturnGreenValue,
296 int* theReturnBlueValue,
int* theReturnAlphaValue )
298 Q_UNUSED( theRedValue );
299 Q_UNUSED( theGreenValue );
300 Q_UNUSED( theBlueValue );
301 Q_UNUSED( theAlphaValue );
303 *theReturnRedValue = 0;
304 *theReturnGreenValue = 0;
305 *theReturnBlueValue = 0;
306 *theReturnAlphaValue = 0;
313 QList<QgsColorRampShader::ColorRampItem>::const_iterator colorRampIt = mColorRampItemList.constBegin();
314 for ( ; colorRampIt != mColorRampItemList.constEnd(); ++colorRampIt )
316 symbolItems.push_back( qMakePair( colorRampIt->label, colorRampIt->color ) );