Skip to content

Commit

Permalink
Use real values when interpolating color components in gradients
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 6, 2016
1 parent fd7a4bd commit 86ec27e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
16 changes: 10 additions & 6 deletions src/core/symbology-ng/qgsvectorcolorrampv2.cpp
Expand Up @@ -31,19 +31,23 @@
static QColor _interpolate( const QColor& c1, const QColor& c2, const double value )
{
if ( qIsNaN( value ) ) return c2;
int r = static_cast< int >( c1.red() + value * ( c2.red() - c1.red() ) );
int g = static_cast< int >( c1.green() + value * ( c2.green() - c1.green() ) );
int b = static_cast< int >( c1.blue() + value * ( c2.blue() - c1.blue() ) );
int a = static_cast< int >( c1.alpha() + value * ( c2.alpha() - c1.alpha() ) );

return QColor::fromRgb( r, g, b, a );
qreal r = ( c1.redF() + value * ( c2.redF() - c1.redF() ) );
qreal g = ( c1.greenF() + value * ( c2.greenF() - c1.greenF() ) );
qreal b = ( c1.blueF() + value * ( c2.blueF() - c1.blueF() ) );
qreal a = ( c1.alphaF() + value * ( c2.alphaF() - c1.alphaF() ) );

return QColor::fromRgbF( r, g, b, a );
}

//////////////

QgsVectorGradientColorRampV2::QgsVectorGradientColorRampV2( const QColor& color1, const QColor& color2,
bool discrete, const QgsGradientStopsList& stops )
: mColor1( color1 ), mColor2( color2 ), mDiscrete( discrete ), mStops( stops )
: mColor1( color1 )
, mColor2( color2 )
, mDiscrete( discrete )
, mStops( stops )
{
}

Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsexpression.cpp
Expand Up @@ -765,7 +765,7 @@ class TestQgsExpression: public QObject
QTest::newRow( "age datetime" ) << "hour(age(to_datetime('2004-03-22 08:30:22'),to_datetime('2004-03-12 07:30:22')))" << false << QVariant( 241.0 );

// Color functions
QTest::newRow( "ramp color" ) << "ramp_color('Spectral',0.3)" << false << QVariant( "253,190,115,255" );
QTest::newRow( "ramp color" ) << "ramp_color('Spectral',0.3)" << false << QVariant( "254,190,116,255" );
QTest::newRow( "color rgb" ) << "color_rgb(255,127,0)" << false << QVariant( "255,127,0" );
QTest::newRow( "color rgba" ) << "color_rgba(255,127,0,200)" << false << QVariant( "255,127,0,200" );
QTest::newRow( "color hsl" ) << "color_hsl(100,50,70)" << false << QVariant( "166,217,140" );
Expand Down
12 changes: 7 additions & 5 deletions tests/src/core/testqgsstylev2.cpp
Expand Up @@ -141,7 +141,9 @@ bool TestStyleV2::imageCheck( QgsMapSettings& ms, const QString& testName )
bool TestStyleV2::testValidColor( QgsVectorColorRampV2 *ramp, double value, const QColor& expected )
{
QColor result = ramp->color( value );
if ( result != expected )
//use int color components when testing (builds some fuzziness into test)
if ( result.red() != expected.red() || result.green() != expected.green() || result.blue() != expected.blue()
|| result.alpha() != expected.alpha() )
{
QWARN( QString( "value = %1 result = %2 expected = %3" ).arg( value ).arg(
result.name(), expected.name() ).toLocal8Bit().data() );
Expand Down Expand Up @@ -189,8 +191,8 @@ void TestStyleV2::testLoadColorRamps()

// values for color tests
QMultiMap< QString, QPair< double, QColor> > colorTests;
colorTests.insert( "test_gradient", qMakePair( 0.25, QColor( "#ff7f7f" ) ) );
colorTests.insert( "test_gradient", qMakePair( 0.66, QColor( "#adadff" ) ) );
colorTests.insert( "test_gradient", qMakePair( 0.25, QColor( "#ff8080" ) ) );
colorTests.insert( "test_gradient", qMakePair( 0.66, QColor( "#aeaeff" ) ) );
// cannot test random colors!
colorTests.insert( "test_cb1", qMakePair( 0.25, QColor( "#fdae61" ) ) );
colorTests.insert( "test_cb1", qMakePair( 0.66, QColor( "#abdda4" ) ) );
Expand All @@ -205,8 +207,8 @@ void TestStyleV2::testLoadColorRamps()
colorTests.insert( "test_cc2", qMakePair( 0.25, QColor( "#de77ae" ) ) );
colorTests.insert( "test_cc2", qMakePair( 0.66, QColor( "#b8e186" ) ) );
colorRampsTest << "test_cc3";
colorTests.insert( "test_cc3", qMakePair( 0.25, QColor( "#7f7f7f" ) ) );
colorTests.insert( "test_cc3", qMakePair( 0.66, QColor( "#ffad00" ) ) );
colorTests.insert( "test_cc3", qMakePair( 0.25, QColor( "#808080" ) ) );
colorTests.insert( "test_cc3", qMakePair( 0.66, QColor( "#ffae00" ) ) );

QgsDebugMsg( "loaded colorRamps: " + colorRamps.join( " " ) );

Expand Down
18 changes: 9 additions & 9 deletions tests/src/python/test_qgsvectorcolorramp.py
Expand Up @@ -25,20 +25,20 @@

class PyQgsVectorColorRamp(unittest.TestCase):

def testQgsVectorRandomColorRampV2(self):
def testQgsVectorGradientRampV2(self):
# test gradient with only start/end color
r = QgsVectorGradientColorRampV2(QColor(200, 0, 0), QColor(0, 200, 0))
r = QgsVectorGradientColorRampV2(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200))
self.assertEqual(r.type(), 'gradient')
self.assertEqual(r.color1(), QColor(200, 0, 0))
self.assertEqual(r.color2(), QColor(0, 200, 0))
self.assertEqual(r.color1(), QColor(200, 0, 0, 100))
self.assertEqual(r.color2(), QColor(0, 200, 0, 200))
self.assertEqual(r.isDiscrete(), False)
self.assertEqual(len(r.stops()), 0)
self.assertEqual(r.count(), 2)
self.assertEqual(r.value(0), 0.0)
self.assertEqual(r.value(1), 1.0)
self.assertEqual(r.color(0), QColor(200, 0, 0))
self.assertEqual(r.color(1), QColor(0, 200, 0))
self.assertEqual(r.color(0.5), QColor(100, 100, 0))
self.assertEqual(r.color(0), QColor(200, 0, 0, 100))
self.assertEqual(r.color(1), QColor(0, 200, 0, 200))
self.assertEqual(r.color(0.5), QColor(100, 100, 0, 150))

# test gradient with stops
r = QgsVectorGradientColorRampV2(QColor(200, 0, 0), QColor(0, 200, 0), False, [QgsGradientStop(0.1, QColor(180, 20, 40)),
Expand All @@ -57,7 +57,7 @@ def testQgsVectorRandomColorRampV2(self):
self.assertEqual(r.color(0.1), QColor(180, 20, 40))
self.assertEqual(r.color(0.5), QColor(110, 40, 70))
self.assertEqual(r.color(0.9), QColor(40, 60, 100))
self.assertEqual(r.color(0.95), QColor(20, 129, 50))
self.assertEqual(r.color(0.95), QColor(20, 130, 50))
self.assertEqual(r.color(1), QColor(0, 200, 0))

# test setters
Expand Down Expand Up @@ -135,7 +135,7 @@ def testQgsVectorRandomColorRampV2(self):
self.assertEqual(g.stops()[2], (0.9, QColor(40, 60, 100, 127)))
self.assertEqual(g.stops()[3], (1.0, QColor(0, 200, 0, 127)))

def testQgsVectorRandomColorRampV2_2(self):
def testQgsVectorRandomColorRampV2(self):
# test random color ramp
r = QgsVectorRandomColorRampV2(5)
self.assertEqual(r.type(), 'random')
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 86ec27e

Please sign in to comment.