Skip to content

Commit 86ec27e

Browse files
committed
Use real values when interpolating color components in gradients
1 parent fd7a4bd commit 86ec27e

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

src/core/symbology-ng/qgsvectorcolorrampv2.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,23 @@
3131
static QColor _interpolate( const QColor& c1, const QColor& c2, const double value )
3232
{
3333
if ( qIsNaN( value ) ) return c2;
34-
int r = static_cast< int >( c1.red() + value * ( c2.red() - c1.red() ) );
35-
int g = static_cast< int >( c1.green() + value * ( c2.green() - c1.green() ) );
36-
int b = static_cast< int >( c1.blue() + value * ( c2.blue() - c1.blue() ) );
37-
int a = static_cast< int >( c1.alpha() + value * ( c2.alpha() - c1.alpha() ) );
3834

39-
return QColor::fromRgb( r, g, b, a );
35+
qreal r = ( c1.redF() + value * ( c2.redF() - c1.redF() ) );
36+
qreal g = ( c1.greenF() + value * ( c2.greenF() - c1.greenF() ) );
37+
qreal b = ( c1.blueF() + value * ( c2.blueF() - c1.blueF() ) );
38+
qreal a = ( c1.alphaF() + value * ( c2.alphaF() - c1.alphaF() ) );
39+
40+
return QColor::fromRgbF( r, g, b, a );
4041
}
4142

4243
//////////////
4344

4445
QgsVectorGradientColorRampV2::QgsVectorGradientColorRampV2( const QColor& color1, const QColor& color2,
4546
bool discrete, const QgsGradientStopsList& stops )
46-
: mColor1( color1 ), mColor2( color2 ), mDiscrete( discrete ), mStops( stops )
47+
: mColor1( color1 )
48+
, mColor2( color2 )
49+
, mDiscrete( discrete )
50+
, mStops( stops )
4751
{
4852
}
4953

tests/src/core/testqgsexpression.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ class TestQgsExpression: public QObject
765765
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 );
766766

767767
// Color functions
768-
QTest::newRow( "ramp color" ) << "ramp_color('Spectral',0.3)" << false << QVariant( "253,190,115,255" );
768+
QTest::newRow( "ramp color" ) << "ramp_color('Spectral',0.3)" << false << QVariant( "254,190,116,255" );
769769
QTest::newRow( "color rgb" ) << "color_rgb(255,127,0)" << false << QVariant( "255,127,0" );
770770
QTest::newRow( "color rgba" ) << "color_rgba(255,127,0,200)" << false << QVariant( "255,127,0,200" );
771771
QTest::newRow( "color hsl" ) << "color_hsl(100,50,70)" << false << QVariant( "166,217,140" );

tests/src/core/testqgsstylev2.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ bool TestStyleV2::imageCheck( QgsMapSettings& ms, const QString& testName )
141141
bool TestStyleV2::testValidColor( QgsVectorColorRampV2 *ramp, double value, const QColor& expected )
142142
{
143143
QColor result = ramp->color( value );
144-
if ( result != expected )
144+
//use int color components when testing (builds some fuzziness into test)
145+
if ( result.red() != expected.red() || result.green() != expected.green() || result.blue() != expected.blue()
146+
|| result.alpha() != expected.alpha() )
145147
{
146148
QWARN( QString( "value = %1 result = %2 expected = %3" ).arg( value ).arg(
147149
result.name(), expected.name() ).toLocal8Bit().data() );
@@ -189,8 +191,8 @@ void TestStyleV2::testLoadColorRamps()
189191

190192
// values for color tests
191193
QMultiMap< QString, QPair< double, QColor> > colorTests;
192-
colorTests.insert( "test_gradient", qMakePair( 0.25, QColor( "#ff7f7f" ) ) );
193-
colorTests.insert( "test_gradient", qMakePair( 0.66, QColor( "#adadff" ) ) );
194+
colorTests.insert( "test_gradient", qMakePair( 0.25, QColor( "#ff8080" ) ) );
195+
colorTests.insert( "test_gradient", qMakePair( 0.66, QColor( "#aeaeff" ) ) );
194196
// cannot test random colors!
195197
colorTests.insert( "test_cb1", qMakePair( 0.25, QColor( "#fdae61" ) ) );
196198
colorTests.insert( "test_cb1", qMakePair( 0.66, QColor( "#abdda4" ) ) );
@@ -205,8 +207,8 @@ void TestStyleV2::testLoadColorRamps()
205207
colorTests.insert( "test_cc2", qMakePair( 0.25, QColor( "#de77ae" ) ) );
206208
colorTests.insert( "test_cc2", qMakePair( 0.66, QColor( "#b8e186" ) ) );
207209
colorRampsTest << "test_cc3";
208-
colorTests.insert( "test_cc3", qMakePair( 0.25, QColor( "#7f7f7f" ) ) );
209-
colorTests.insert( "test_cc3", qMakePair( 0.66, QColor( "#ffad00" ) ) );
210+
colorTests.insert( "test_cc3", qMakePair( 0.25, QColor( "#808080" ) ) );
211+
colorTests.insert( "test_cc3", qMakePair( 0.66, QColor( "#ffae00" ) ) );
210212

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

tests/src/python/test_qgsvectorcolorramp.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@
2525

2626
class PyQgsVectorColorRamp(unittest.TestCase):
2727

28-
def testQgsVectorRandomColorRampV2(self):
28+
def testQgsVectorGradientRampV2(self):
2929
# test gradient with only start/end color
30-
r = QgsVectorGradientColorRampV2(QColor(200, 0, 0), QColor(0, 200, 0))
30+
r = QgsVectorGradientColorRampV2(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200))
3131
self.assertEqual(r.type(), 'gradient')
32-
self.assertEqual(r.color1(), QColor(200, 0, 0))
33-
self.assertEqual(r.color2(), QColor(0, 200, 0))
32+
self.assertEqual(r.color1(), QColor(200, 0, 0, 100))
33+
self.assertEqual(r.color2(), QColor(0, 200, 0, 200))
3434
self.assertEqual(r.isDiscrete(), False)
3535
self.assertEqual(len(r.stops()), 0)
3636
self.assertEqual(r.count(), 2)
3737
self.assertEqual(r.value(0), 0.0)
3838
self.assertEqual(r.value(1), 1.0)
39-
self.assertEqual(r.color(0), QColor(200, 0, 0))
40-
self.assertEqual(r.color(1), QColor(0, 200, 0))
41-
self.assertEqual(r.color(0.5), QColor(100, 100, 0))
39+
self.assertEqual(r.color(0), QColor(200, 0, 0, 100))
40+
self.assertEqual(r.color(1), QColor(0, 200, 0, 200))
41+
self.assertEqual(r.color(0.5), QColor(100, 100, 0, 150))
4242

4343
# test gradient with stops
4444
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):
5757
self.assertEqual(r.color(0.1), QColor(180, 20, 40))
5858
self.assertEqual(r.color(0.5), QColor(110, 40, 70))
5959
self.assertEqual(r.color(0.9), QColor(40, 60, 100))
60-
self.assertEqual(r.color(0.95), QColor(20, 129, 50))
60+
self.assertEqual(r.color(0.95), QColor(20, 130, 50))
6161
self.assertEqual(r.color(1), QColor(0, 200, 0))
6262

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

138-
def testQgsVectorRandomColorRampV2_2(self):
138+
def testQgsVectorRandomColorRampV2(self):
139139
# test random color ramp
140140
r = QgsVectorRandomColorRampV2(5)
141141
self.assertEqual(r.type(), 'random')

0 commit comments

Comments
 (0)