@@ -17,11 +17,14 @@ originally part of the larger QgsRasterLayer class
17
17
* (at your option) any later version. *
18
18
* *
19
19
***************************************************************************/
20
+ #define REALLY_SMALL 0.0000001
20
21
21
22
#include " qgslogger.h"
22
23
23
24
#include " qgscolorrampshader.h"
24
25
26
+ #include < math.h>
27
+
25
28
QgsColorRampShader::QgsColorRampShader ( double theMinimumValue, double theMaximumValue ) : QgsRasterShaderFunction( theMinimumValue, theMaximumValue )
26
29
{
27
30
QgsDebugMsg ( " called." );
@@ -54,17 +57,19 @@ bool QgsColorRampShader::discreteColor( double theValue, int* theReturnRedValue,
54
57
return false ;
55
58
}
56
59
60
+ double myTinyDiff = 0.0 ;
57
61
QgsColorRampShader::ColorRampItem myColorRampItem;
58
62
while ( mCurrentColorRampItemIndex >= 0 && mCurrentColorRampItemIndex < myColorRampItemCount )
59
63
{
60
64
// Start searching from the last index - assumtion is that neighboring pixels tend to be similar values
61
65
myColorRampItem = mColorRampItemList .value ( mCurrentColorRampItemIndex );
66
+ myTinyDiff = fabs ( theValue - myColorRampItem.value );
62
67
// If the previous entry is less, then search closer to the top of the list (assumes mColorRampItemList is sorted)
63
68
if ( mCurrentColorRampItemIndex != 0 && theValue <= mColorRampItemList .at ( mCurrentColorRampItemIndex - 1 ).value )
64
69
{
65
70
mCurrentColorRampItemIndex --;
66
71
}
67
- else if ( theValue <= myColorRampItem.value )
72
+ else if ( theValue <= myColorRampItem.value || myTinyDiff <= REALLY_SMALL )
68
73
{
69
74
*theReturnRedValue = myColorRampItem.color .red ();
70
75
*theReturnGreenValue = myColorRampItem.color .green ();
@@ -94,12 +99,14 @@ bool QgsColorRampShader::exactColor( double theValue, int* theReturnRedValue, in
94
99
return false ;
95
100
}
96
101
102
+ double myTinyDiff = 0.0 ;
97
103
QgsColorRampShader::ColorRampItem myColorRampItem;
98
104
while ( mCurrentColorRampItemIndex >= 0 && mCurrentColorRampItemIndex < myColorRampItemCount )
99
105
{
100
106
// Start searching from the last index - assumtion is that neighboring pixels tend to be similar values
101
107
myColorRampItem = mColorRampItemList .value ( mCurrentColorRampItemIndex );
102
- if ( theValue == myColorRampItem.value )
108
+ myTinyDiff = fabs ( theValue - myColorRampItem.value );
109
+ if ( theValue == myColorRampItem.value || myTinyDiff <= REALLY_SMALL )
103
110
{
104
111
*theReturnRedValue = myColorRampItem.color .red ();
105
112
*theReturnGreenValue = myColorRampItem.color .green ();
@@ -133,26 +140,27 @@ bool QgsColorRampShader::exactColor( double theValue, int* theReturnRedValue, in
133
140
134
141
bool QgsColorRampShader::interpolatedColor ( double theValue, int * theReturnRedValue, int * theReturnGreenValue, int * theReturnBlueValue )
135
142
{
136
-
137
143
int myColorRampItemCount = mColorRampItemList .count ();
138
144
if ( myColorRampItemCount <= 0 )
139
145
{
140
146
return false ;
141
147
}
142
148
149
+ double myTinyDiff = 0.0 ;
143
150
double myCurrentRampRange; // difference between two consecutive entry values
144
151
double myOffsetInRange; // difference between the previous entry value and value
145
152
QgsColorRampShader::ColorRampItem myColorRampItem;
146
153
while ( mCurrentColorRampItemIndex >= 0 && mCurrentColorRampItemIndex < myColorRampItemCount )
147
154
{
148
155
// Start searching from the last index - assumtion is that neighboring pixels tend to be similar values
149
156
myColorRampItem = mColorRampItemList .value ( mCurrentColorRampItemIndex );
157
+ myTinyDiff = fabs ( theValue - myColorRampItem.value );
150
158
// If the previous entry is less, then search closer to the top of the list (assumes mColorRampItemList is sorted)
151
159
if ( mCurrentColorRampItemIndex != 0 && theValue <= mColorRampItemList .at ( mCurrentColorRampItemIndex - 1 ).value )
152
160
{
153
161
mCurrentColorRampItemIndex --;
154
162
}
155
- else if ( mCurrentColorRampItemIndex != 0 && theValue <= myColorRampItem.value )
163
+ else if ( mCurrentColorRampItemIndex != 0 && ( theValue <= myColorRampItem.value || myTinyDiff <= REALLY_SMALL ) )
156
164
{
157
165
QgsColorRampShader::ColorRampItem myPreviousColorRampItem = mColorRampItemList .value ( mCurrentColorRampItemIndex - 1 );
158
166
myCurrentRampRange = myColorRampItem.value - myPreviousColorRampItem.value ;
0 commit comments