@@ -25,10 +25,22 @@ originally part of the larger QgsRasterLayer class
25
25
QgsColorRampShader::QgsColorRampShader ( double theMinimumValue, double theMaximumValue ) : QgsRasterShaderFunction( theMinimumValue, theMaximumValue )
26
26
{
27
27
QgsDebugMsg ( " called." );
28
+ mMaximumColorCacheSize = 256 ; // good starting value
28
29
}
29
30
30
31
bool QgsColorRampShader::generateShadedValue ( double theValue, int * theReturnRedValue, int * theReturnGreenValue, int * theReturnBlueValue )
31
32
{
33
+ // Get the shaded from the cache if it exists already
34
+ QColor myColor = mColorCache .value (theValue);
35
+ if (myColor.isValid ())
36
+ {
37
+ *theReturnRedValue = myColor.red ();
38
+ *theReturnGreenValue = myColor.green ();
39
+ *theReturnBlueValue = myColor.blue ();
40
+ return true ;
41
+ }
42
+
43
+ // Else we have to generate the shaded value
32
44
if ( QgsColorRampShader::INTERPOLATED == mColorRampType )
33
45
{
34
46
return getInterpolatedColor ( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue );
@@ -81,15 +93,16 @@ bool QgsColorRampShader::getDiscreteColor( double theValue, int* theReturnRedVal
81
93
myCurrentRampValue = it->value ;
82
94
if ( theValue <= myCurrentRampValue )
83
95
{
84
- if ( last_it != mColorRampItemList .end () )
96
+ *theReturnRedValue = it->color .red ();
97
+ *theReturnGreenValue = it->color .green ();
98
+ *theReturnBlueValue = it->color .blue ();
99
+ // Cache the shaded value
100
+ if (mMaximumColorCacheSize <= mColorCache .size ())
85
101
{
86
- *theReturnRedValue = last_it->color .red ();
87
- *theReturnGreenValue = last_it->color .green ();
88
- *theReturnBlueValue = last_it->color .blue ();
89
- return true ;
102
+ mColorCache .insert (theValue, it->color );
90
103
}
104
+ return true ;
91
105
}
92
- last_it = it;
93
106
}
94
107
95
108
return false ; // value not found
@@ -109,6 +122,11 @@ bool QgsColorRampShader::getExactColor( double theValue, int* theReturnRedValue,
109
122
*theReturnRedValue = it->color .red ();
110
123
*theReturnGreenValue = it->color .green ();
111
124
*theReturnBlueValue = it->color .blue ();
125
+ // Cache the shaded value
126
+ if (mMaximumColorCacheSize <= mColorCache .size ())
127
+ {
128
+ mColorCache .insert (theValue, it->color );
129
+ }
112
130
return true ;
113
131
}
114
132
}
@@ -143,6 +161,11 @@ bool QgsColorRampShader::getInterpolatedColor( double theValue, int* theReturnRe
143
161
*theReturnRedValue = ( int )(( it->color .red () * myDiffTheValueLastRampValue + last_it->color .red () * myDiffCurrentRampValueTheValue ) / myCurrentRampRange );
144
162
*theReturnGreenValue = ( int )(( it->color .green () * myDiffTheValueLastRampValue + last_it->color .green () * myDiffCurrentRampValueTheValue ) / myCurrentRampRange );
145
163
*theReturnBlueValue = ( int )(( it->color .blue () * myDiffTheValueLastRampValue + last_it->color .blue () * myDiffCurrentRampValueTheValue ) / myCurrentRampRange );
164
+ // Cache the shaded value
165
+ if (mMaximumColorCacheSize <= mColorCache .size ())
166
+ {
167
+ mColorCache .insert (theValue, it->color );
168
+ }
146
169
return true ;
147
170
}
148
171
}
0 commit comments