Skip to content
Permalink
Browse files

[color ramp] implement invert() function

  • Loading branch information
nirvn committed Nov 30, 2016
1 parent 57f17e3 commit 7181cf25e854bbdc3ce37da92624ff5b70d88732
Showing with 76 additions and 6 deletions.
  1. +13 −2 python/core/qgscolorramp.sip
  2. +50 −3 src/core/qgscolorramp.cpp
  3. +13 −1 src/core/qgscolorramp.h
@@ -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/;
@@ -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;

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

virtual QString type() const;

virtual void invert();

virtual QgsLimitedRandomColorRamp* clone() const /Factory/;

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

QString type() const;

virtual void invert();

virtual QgsRandomColorRamp* clone() const /Factory/;

QgsStringMap properties() const;
@@ -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;
@@ -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/;

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

virtual QString type() const;

virtual void invert();

virtual QgsColorBrewerColorRamp* clone() const /Factory/;

virtual QgsStringMap properties() const;
@@ -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/;
@@ -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,
@@ -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();
}
@@ -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()
@@ -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;
}

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

@@ -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; }
@@ -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;
@@ -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;
@@ -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.
@@ -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; }
@@ -517,6 +528,7 @@ class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
QString mSchemeName;
int mColors;
QList<QColor> mPalette;
bool mInverted;
};


0 comments on commit 7181cf2

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