Skip to content
Permalink
Browse files

Use real values when interpolating color components in gradients

  • Loading branch information
nyalldawson committed Apr 6, 2016
1 parent fd7a4bd commit 86ec27e08eeee63911a93c2b46d4418d8e5b62fb
@@ -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 )
{
}

@@ -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" );
@@ -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() );
@@ -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" ) ) );
@@ -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( " " ) );

@@ -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)),
@@ -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
@@ -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')
Binary file not shown.

0 comments on commit 86ec27e

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