Skip to content

Commit 8349cfd

Browse files
author
ersts
committedMar 11, 2009
-Fixed issue with color ramp shader and doubles
-Closes ticket #1497 git-svn-id: http://svn.osgeo.org/qgis/trunk@10271 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 707550a commit 8349cfd

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed
 

‎src/core/raster/qgscolorrampshader.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ originally part of the larger QgsRasterLayer class
1717
* (at your option) any later version. *
1818
* *
1919
***************************************************************************/
20+
#define REALLY_SMALL 0.0000001
2021

2122
#include "qgslogger.h"
2223

2324
#include "qgscolorrampshader.h"
2425

26+
#include <math.h>
27+
2528
QgsColorRampShader::QgsColorRampShader( double theMinimumValue, double theMaximumValue ) : QgsRasterShaderFunction( theMinimumValue, theMaximumValue )
2629
{
2730
QgsDebugMsg( "called." );
@@ -54,17 +57,19 @@ bool QgsColorRampShader::discreteColor( double theValue, int* theReturnRedValue,
5457
return false;
5558
}
5659

60+
double myTinyDiff = 0.0;
5761
QgsColorRampShader::ColorRampItem myColorRampItem;
5862
while ( mCurrentColorRampItemIndex >= 0 && mCurrentColorRampItemIndex < myColorRampItemCount )
5963
{
6064
//Start searching from the last index - assumtion is that neighboring pixels tend to be similar values
6165
myColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex );
66+
myTinyDiff = fabs( theValue - myColorRampItem.value );
6267
//If the previous entry is less, then search closer to the top of the list (assumes mColorRampItemList is sorted)
6368
if ( mCurrentColorRampItemIndex != 0 && theValue <= mColorRampItemList.at( mCurrentColorRampItemIndex - 1 ).value )
6469
{
6570
mCurrentColorRampItemIndex--;
6671
}
67-
else if ( theValue <= myColorRampItem.value )
72+
else if ( theValue <= myColorRampItem.value || myTinyDiff <= REALLY_SMALL )
6873
{
6974
*theReturnRedValue = myColorRampItem.color.red();
7075
*theReturnGreenValue = myColorRampItem.color.green();
@@ -94,12 +99,14 @@ bool QgsColorRampShader::exactColor( double theValue, int* theReturnRedValue, in
9499
return false;
95100
}
96101

102+
double myTinyDiff = 0.0;
97103
QgsColorRampShader::ColorRampItem myColorRampItem;
98104
while ( mCurrentColorRampItemIndex >= 0 && mCurrentColorRampItemIndex < myColorRampItemCount )
99105
{
100106
//Start searching from the last index - assumtion is that neighboring pixels tend to be similar values
101107
myColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex );
102-
if ( theValue == myColorRampItem.value )
108+
myTinyDiff = fabs( theValue - myColorRampItem.value );
109+
if ( theValue == myColorRampItem.value || myTinyDiff <= REALLY_SMALL )
103110
{
104111
*theReturnRedValue = myColorRampItem.color.red();
105112
*theReturnGreenValue = myColorRampItem.color.green();
@@ -133,26 +140,27 @@ bool QgsColorRampShader::exactColor( double theValue, int* theReturnRedValue, in
133140

134141
bool QgsColorRampShader::interpolatedColor( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue )
135142
{
136-
137143
int myColorRampItemCount = mColorRampItemList.count();
138144
if ( myColorRampItemCount <= 0 )
139145
{
140146
return false;
141147
}
142148

149+
double myTinyDiff = 0.0;
143150
double myCurrentRampRange; //difference between two consecutive entry values
144151
double myOffsetInRange; //difference between the previous entry value and value
145152
QgsColorRampShader::ColorRampItem myColorRampItem;
146153
while ( mCurrentColorRampItemIndex >= 0 && mCurrentColorRampItemIndex < myColorRampItemCount )
147154
{
148155
//Start searching from the last index - assumtion is that neighboring pixels tend to be similar values
149156
myColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex );
157+
myTinyDiff = fabs( theValue - myColorRampItem.value );
150158
//If the previous entry is less, then search closer to the top of the list (assumes mColorRampItemList is sorted)
151159
if ( mCurrentColorRampItemIndex != 0 && theValue <= mColorRampItemList.at( mCurrentColorRampItemIndex - 1 ).value )
152160
{
153161
mCurrentColorRampItemIndex--;
154162
}
155-
else if ( mCurrentColorRampItemIndex != 0 && theValue <= myColorRampItem.value )
163+
else if ( mCurrentColorRampItemIndex != 0 && ( theValue <= myColorRampItem.value || myTinyDiff <= REALLY_SMALL ) )
156164
{
157165
QgsColorRampShader::ColorRampItem myPreviousColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex - 1 );
158166
myCurrentRampRange = myColorRampItem.value - myPreviousColorRampItem.value;

0 commit comments

Comments
 (0)
Please sign in to comment.