Skip to content

Commit

Permalink
[color ramp] implement invert() function
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Nov 30, 2016
1 parent 57f17e3 commit 7181cf2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
15 changes: 13 additions & 2 deletions python/core/qgscolorramp.sip
Expand Up @@ -48,6 +48,8 @@ class QgsColorRamp
*/
virtual QString type() const = 0;

virtual void invert() = 0;

/** Creates a clone of the color ramp.
*/
virtual QgsColorRamp* clone() const = 0 /Factory/;
Expand Down Expand Up @@ -118,6 +120,7 @@ class QgsGradientColorRamp : QgsColorRamp
virtual double value( int index ) const;
virtual QColor color( double value ) const;
virtual QString type() const;
virtual void invert();
virtual QgsGradientColorRamp* clone() const /Factory/;
virtual QgsStringMap properties() const;

Expand Down Expand Up @@ -223,6 +226,8 @@ class QgsLimitedRandomColorRamp : QgsColorRamp

virtual QString type() const;

virtual void invert();

virtual QgsLimitedRandomColorRamp* clone() const /Factory/;

virtual QgsStringMap properties() const;
Expand Down Expand Up @@ -282,6 +287,8 @@ class QgsRandomColorRamp : QgsColorRamp

QString type() const;

virtual void invert();

virtual QgsRandomColorRamp* clone() const /Factory/;

QgsStringMap properties() const;
Expand Down Expand Up @@ -333,6 +340,7 @@ class QgsPresetSchemeColorRamp : QgsColorRamp, QgsColorScheme
virtual double value( int index ) const;
virtual QColor color( double value ) const;
virtual QString type() const;
virtual void invert();
virtual QgsPresetSchemeColorRamp* clone() const /Factory/;
virtual QgsStringMap properties() const;
int count() const;
Expand All @@ -355,7 +363,8 @@ class QgsColorBrewerColorRamp : QgsColorRamp
%End
public:
QgsColorBrewerColorRamp( const QString& schemeName = DEFAULT_COLORBREWER_SCHEMENAME,
int colors = DEFAULT_COLORBREWER_COLORS );
int colors = DEFAULT_COLORBREWER_COLORS,
bool inverted = false );

static QgsColorRamp* create( const QgsStringMap& properties = QgsStringMap() ) /Factory/;

Expand All @@ -365,6 +374,8 @@ class QgsColorBrewerColorRamp : QgsColorRamp

virtual QString type() const;

virtual void invert();

virtual QgsColorBrewerColorRamp* clone() const /Factory/;

virtual QgsStringMap properties() const;
Expand Down Expand Up @@ -403,7 +414,7 @@ class QgsCptCityColorRamp : QgsGradientColorRamp
static QgsColorRamp* create( const QgsStringMap& properties = QgsStringMap() ) /Factory/;

virtual QString type() const;

virtual void invert();
virtual QgsCptCityColorRamp* clone() const /Factory/;
void copy( const QgsCptCityColorRamp* other );
QgsGradientColorRamp* cloneGradientRamp() const /Factory/;
Expand Down
53 changes: 50 additions & 3 deletions src/core/qgscolorramp.cpp
Expand Up @@ -160,6 +160,20 @@ 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-- )
{
newStops << QgsGradientStop( 1 - mStops.at( k ).offset, mStops.at( k ).color );
}
mStops = newStops;
}

QgsGradientColorRamp* QgsGradientColorRamp::clone() const
{
QgsGradientColorRamp* r = new QgsGradientColorRamp( mColor1, mColor2,
Expand Down Expand Up @@ -473,9 +487,10 @@ QgsStringMap QgsRandomColorRamp::properties() const

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

QgsColorBrewerColorRamp::QgsColorBrewerColorRamp( const QString& schemeName, int colors )
QgsColorBrewerColorRamp::QgsColorBrewerColorRamp( const QString& schemeName, int colors, bool inverted )
: mSchemeName( schemeName )
, mColors( colors )
, mInverted( inverted )
{
loadPalette();
}
Expand All @@ -484,18 +499,32 @@ QgsColorRamp* QgsColorBrewerColorRamp::create( const QgsStringMap& props )
{
QString schemeName = DEFAULT_COLORBREWER_SCHEMENAME;
int colors = DEFAULT_COLORBREWER_COLORS;
bool inverted = false;

if ( props.contains( QStringLiteral( "schemeName" ) ) )
schemeName = props[QStringLiteral( "schemeName" )];
if ( props.contains( QStringLiteral( "colors" ) ) )
colors = props[QStringLiteral( "colors" )].toInt();
if ( props.contains( QStringLiteral( "inverted" ) ) )
inverted = props[QStringLiteral( "inverted" )].toInt();

return new QgsColorBrewerColorRamp( schemeName, colors );
return new QgsColorBrewerColorRamp( schemeName, colors, inverted );
}

void QgsColorBrewerColorRamp::loadPalette()
{
mPalette = QgsColorBrewerPalette::listSchemeColors( mSchemeName, mColors );

if ( mInverted )
{
QList<QColor> tmpPalette;

for ( int k = mPalette.size() - 1; k >= 0; k-- )
{
tmpPalette << mPalette.at( k );
}
mPalette = tmpPalette;
}
}

QStringList QgsColorBrewerColorRamp::listSchemeNames()
Expand Down Expand Up @@ -525,16 +554,23 @@ QColor QgsColorBrewerColorRamp::color( double value ) const
return mPalette.at( paletteEntry );
}

void QgsColorBrewerColorRamp::invert()
{
mInverted = !mInverted;
loadPalette();
}

QgsColorBrewerColorRamp* QgsColorBrewerColorRamp::clone() const
{
return new QgsColorBrewerColorRamp( mSchemeName, mColors );
return new QgsColorBrewerColorRamp( mSchemeName, mColors, mInverted );
}

QgsStringMap QgsColorBrewerColorRamp::properties() const
{
QgsStringMap map;
map[QStringLiteral( "schemeName" )] = mSchemeName;
map[QStringLiteral( "colors" )] = QString::number( mColors );
map[QStringLiteral( "inverted" )] = QString::number( mInverted );
return map;
}

Expand Down Expand Up @@ -813,6 +849,17 @@ QColor QgsPresetSchemeColorRamp::color( double value ) const
return QColor();
}

void QgsPresetSchemeColorRamp::invert()
{
QgsNamedColorList tmpColors;

for ( int k = mColors.size() - 1; k >= 0; k-- )
{
tmpColors << mColors.at( k );
}
mColors = tmpColors;
}

QgsPresetSchemeColorRamp* QgsPresetSchemeColorRamp::clone() const
{
return new QgsPresetSchemeColorRamp( *this );
Expand Down
14 changes: 13 additions & 1 deletion src/core/qgscolorramp.h
Expand Up @@ -50,6 +50,9 @@ class CORE_EXPORT QgsColorRamp
*/
virtual QString type() const = 0;


virtual void invert() = 0;

/** Creates a clone of the color ramp.
*/
virtual QgsColorRamp* clone() const = 0;
Expand Down Expand Up @@ -123,6 +126,7 @@ class CORE_EXPORT QgsGradientColorRamp : public QgsColorRamp
virtual double value( int index ) const override;
virtual QColor color( double value ) const override;
virtual QString type() const override { return QStringLiteral( "gradient" ); }
virtual void invert() override;
virtual QgsGradientColorRamp* clone() const override;
virtual QgsStringMap properties() const override;

Expand Down Expand Up @@ -255,6 +259,7 @@ class CORE_EXPORT QgsLimitedRandomColorRamp : public QgsColorRamp
virtual double value( int index ) const override;
virtual QColor color( double value ) const override;
virtual QString type() const override { return QStringLiteral( "random" ); }
virtual void invert() override { return; }
virtual QgsLimitedRandomColorRamp* clone() const override;
virtual QgsStringMap properties() const override;
int count() const override { return mCount; }
Expand Down Expand Up @@ -370,6 +375,8 @@ class CORE_EXPORT QgsRandomColorRamp: public QgsColorRamp

QString type() const override;

virtual void invert() override { return; }

QgsRandomColorRamp* clone() const override;

QgsStringMap properties() const override;
Expand Down Expand Up @@ -424,6 +431,7 @@ class CORE_EXPORT QgsPresetSchemeColorRamp : public QgsColorRamp, public QgsColo
virtual double value( int index ) const override;
virtual QColor color( double value ) const override;
virtual QString type() const override { return QStringLiteral( "preset" ); }
virtual void invert() override;
virtual QgsPresetSchemeColorRamp* clone() const override;
virtual QgsStringMap properties() const override;
int count() const override;
Expand Down Expand Up @@ -455,9 +463,11 @@ class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
/** Constructor for QgsColorBrewerColorRamp
* @param schemeName color brewer scheme name
* @param colors number of colors in ramp
* @param inverted invert ramp ordering
*/
QgsColorBrewerColorRamp( const QString& schemeName = DEFAULT_COLORBREWER_SCHEMENAME,
int colors = DEFAULT_COLORBREWER_COLORS );
int colors = DEFAULT_COLORBREWER_COLORS,
bool inverted = false );

/** Returns a new QgsColorBrewerColorRamp color ramp created using the properties encoded in a string
* map.
Expand All @@ -469,6 +479,7 @@ class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
virtual double value( int index ) const override;
virtual QColor color( double value ) const override;
virtual QString type() const override { return QStringLiteral( "colorbrewer" ); }
virtual void invert() override;
virtual QgsColorBrewerColorRamp* clone() const override;
virtual QgsStringMap properties() const override;
virtual int count() const override { return mColors; }
Expand Down Expand Up @@ -517,6 +528,7 @@ class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
QString mSchemeName;
int mColors;
QList<QColor> mPalette;
bool mInverted;
};


Expand Down

0 comments on commit 7181cf2

Please sign in to comment.