Skip to content
Permalink
Browse files

[color ramp] improve invert() for discrete gradient ramps (#3833)

  • Loading branch information
nirvn committed Dec 4, 2016
1 parent e135e79 commit e94a352e083a7aa771dca7608712f2340668c10f
Showing with 31 additions and 7 deletions.
  1. +19 −6 src/core/qgscolorramp.cpp
  2. +12 −1 tests/src/python/test_qgsvectorcolorramp.py
@@ -162,14 +162,27 @@ QColor QgsGradientColorRamp::color( double value ) const

void QgsGradientColorRamp::invert()
{
QColor tmpColor = mColor1;
mColor1 = mColor2;
mColor2 = tmpColor;

QgsGradientStopsList newStops;
for ( int k = mStops.size() - 1; k >= 0; k-- )

if ( mDiscrete )
{
newStops << QgsGradientStop( 1 - mStops.at( k ).offset, mStops.at( k ).color );
mColor2 = mColor1;
mColor1 = mStops.at( mStops.size() - 1 ).color;
for ( int k = mStops.size() - 1; k >= 1; k-- )
{
newStops << QgsGradientStop( 1 - mStops.at( k ).offset, mStops.at( k - 1 ).color );
}
newStops << QgsGradientStop( 1 - mStops.at( 0 ).offset, mColor2 );
}
else
{
QColor tmpColor = mColor2;
mColor2 = mColor1;
mColor1 = tmpColor;
for ( int k = mStops.size() - 1; k >= 0; k-- )
{
newStops << QgsGradientStop( 1 - mStops.at( k ).offset, mStops.at( k ).color );
}
}
mStops = newStops;
}
@@ -161,12 +161,23 @@ def testQgsVectorGradientRampV2(self):
self.assertEqual(s[3].offset, 0.8)
self.assertEqual(s[3].color, QColor(50, 20, 10))

# test invert function
# test continous invert function
r.invert()
self.assertEqual(r.color(0), QColor(0, 200, 0))
self.assertEqual(r.color(1), QColor(200, 0, 0))
self.assertEqual(r.color(0.2), QColor(50, 20, 10))

# test discrete invert function
r = QgsGradientColorRamp(QColor(255, 255, 255), QColor(0, 0, 0), True, [QgsGradientStop(0.33, QColor(128, 128, 128)),
QgsGradientStop(0.66, QColor(0, 0, 0))])
self.assertEqual(r.color(0.2), QColor(255, 255, 255))
self.assertEqual(r.color(0.5), QColor(128, 128, 128))
self.assertEqual(r.color(0.8), QColor(0, 0, 0))
r.invert()
self.assertEqual(r.color(0.2), QColor(0, 0, 0))
self.assertEqual(r.color(0.5), QColor(128, 128, 128))
self.assertEqual(r.color(0.8), QColor(255, 255, 255))

def testQgsLimitedRandomColorRampV2(self):
# test random color ramp
r = QgsLimitedRandomColorRamp(5)

0 comments on commit e94a352

Please sign in to comment.
You can’t perform that action at this time.