Skip to content

Commit b57841f

Browse files
committedApr 25, 2013
Merge pull request #531 from ccrook/colour_functions
Colour functions
2 parents 21fd0f2 + fd57ffa commit b57841f

File tree

6 files changed

+119
-3
lines changed

6 files changed

+119
-3
lines changed
 

‎resources/function_help/Color-en_US

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
<h3>Color Group</h3>
3+
This group contains functions for manipulating colors
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
<h3>color_rgb() function</h3>
3+
Returns a string representation of a color based on its red, green, and blue components
4+
5+
<p><h4>Syntax</h4>
6+
color_rgb(<i>red, green, blue</i>)</p>
7+
8+
<p><h4>Arguments</h4>
9+
<!-- List args for functions here-->
10+
<i> red</i> &rarr; the red component as an integer value from 0 to 255.<br>
11+
<i> green</i> &rarr; the green component as an integer value from 0 to 255.<br>
12+
<i> blue</i> &rarr; the blue component as an integer value from 0 to 255.<br>
13+
14+
<p><h4>Example</h4>
15+
<!-- Show example of function.-->
16+
color_rgb(255,127,0) &rarr; '#ff7f00'</p>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
<h3>color_rgba() function</h3>
3+
Returns a string representation of a color based on its red, green, blue, and alpha (transparency) components
4+
5+
<p><h4>Syntax</h4>
6+
color_rgba(<i>red, green, blue, alpha</i>)</p>
7+
8+
<p><h4>Arguments</h4>
9+
<!-- List args for functions here-->
10+
<i> red</i> &rarr; the red component as an integer value from 0 to 255.<br>
11+
<i> green</i> &rarr; the green component as an integer value from 0 to 255.<br>
12+
<i> blue</i> &rarr; the blue component as an integer value from 0 to 255.<br>
13+
<i> alpha</i> &rarr; the alpha component as an integer value from 0 (completely transparent) to 255 (opaque).<br>
14+
15+
<p><h4>Example</h4>
16+
<!-- Show example of function.-->
17+
color_rgba(255,127,0,200) &rarr; '255,127,0,200'</p>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
<h3>ramp_color() function</h3>
3+
Returns a string representing a color from a color ramp.
4+
5+
<p><h4>Syntax</h4>
6+
ramp_color(<i>ramp_name,value</i>)</p>
7+
8+
<p><h4>Arguments</h4>
9+
<!-- List args for functions here-->
10+
<i> ramp_name</i> &rarr; the name of the color ramp as a string, for example 'Spectral'.<br>
11+
<i> value</i> &rarr; the position on the ramp to select the color from as a real number between 0 and 1.<br>
12+
13+
<p><h4>Example</h4>
14+
<!-- Show example of function.-->
15+
ramp_color('Spectral',0.3) &rarr; '#fdbe73'</p>
16+
17+
<p><h4>Note:</h4>
18+
The color ramps available vary between QGIS installations. This function
19+
may not give the expected results if you move your Quantum project.
20+
</p>

‎src/core/qgsexpression.cpp

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <QSettings>
2121
#include <QDate>
2222
#include <QRegExp>
23+
#include <QColor>
2324

2425
#include <math.h>
2526
#include <limits>
@@ -30,6 +31,9 @@
3031
#include "qgslogger.h"
3132
#include "qgsogcutils.h"
3233
#include "qgsvectorlayer.h"
34+
#include "qgssymbollayerv2utils.h"
35+
#include "qgsvectorcolorrampv2.h"
36+
#include "qgsstylev2.h"
3337

3438
// from parser
3539
extern QgsExpression::Node* parseExpression( const QString& str, QString& parserErrorMsg );
@@ -975,6 +979,50 @@ static QVariant fcnFormatDate( const QVariantList& values, QgsFeature*, QgsExpre
975979
return dt.toString( format );
976980
}
977981

982+
static QVariant fcnColorRgb( const QVariantList &values, QgsFeature *, QgsExpression *parent )
983+
{
984+
int red = getIntValue( values.at( 0 ), parent );
985+
int green = getIntValue( values.at( 1 ), parent );
986+
int blue = getIntValue( values.at( 2 ), parent );
987+
QColor color = QColor( red, green, blue );
988+
if ( ! color.isValid() )
989+
{
990+
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1:%2:%3' to color" ).arg( red ).arg( green ).arg( blue ) );
991+
color = QColor( 0, 0, 0 );
992+
}
993+
994+
return color.name();
995+
}
996+
997+
static QVariant fncColorRgba( const QVariantList &values, QgsFeature *, QgsExpression *parent )
998+
{
999+
int red = getIntValue( values.at( 0 ), parent );
1000+
int green = getIntValue( values.at( 1 ), parent );
1001+
int blue = getIntValue( values.at( 2 ), parent );
1002+
int alpha = getIntValue( values.at( 3 ), parent );
1003+
QColor color = QColor( red, green, blue, alpha );
1004+
if ( ! color.isValid() )
1005+
{
1006+
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1:%2:%3:%4' to color" ).arg( red ).arg( green ).arg( blue ).arg( alpha ) );
1007+
color = QColor( 0, 0, 0 );
1008+
}
1009+
return QgsSymbolLayerV2Utils::encodeColor( color );
1010+
}
1011+
1012+
QVariant fcnRampColor( const QVariantList &values, QgsFeature *, QgsExpression *parent )
1013+
{
1014+
QString rampName = getStringValue( values.at( 0 ), parent );
1015+
const QgsVectorColorRampV2 *mRamp = QgsStyleV2::defaultStyle()->colorRampRef( rampName );
1016+
if ( ! mRamp )
1017+
{
1018+
parent->setEvalErrorString( QObject::tr( "\"%1\" is not a valid color ramp" ).arg( rampName ) );
1019+
return QColor( 0, 0, 0 ).name();
1020+
}
1021+
double value = getDoubleValue( values.at( 1 ), parent );
1022+
QColor color = mRamp->color( value );
1023+
return color.name();
1024+
}
1025+
9781026
static QVariant fcnSpecialColumn( const QVariantList& values, QgsFeature* /*f*/, QgsExpression* parent )
9791027
{
9801028
QString varName = getStringValue( values.at( 0 ), parent );
@@ -1008,13 +1056,15 @@ bool QgsExpression::unregisterFunction( QString name )
10081056
return false;
10091057
}
10101058

1059+
1060+
10111061
QStringList QgsExpression::gmBuiltinFunctions;
10121062

10131063
const QStringList &QgsExpression::BuiltinFunctions()
10141064
{
10151065
if ( gmBuiltinFunctions.isEmpty() )
10161066
{
1017-
gmBuiltinFunctions << "sqrt"
1067+
gmBuiltinFunctions
10181068
<< "sqrt" << "cos" << "sin" << "tan"
10191069
<< "asin" << "acos" << "atan" << "atan2"
10201070
<< "exp" << "ln" << "log10" << "log"
@@ -1025,8 +1075,10 @@ const QStringList &QgsExpression::BuiltinFunctions()
10251075
<< "minute" << "second" << "lower" << "upper"
10261076
<< "title" << "length" << "replace" << "regexp_replace"
10271077
<< "substr" << "concat" << "strpos" << "left"
1028-
<< "right" << "rpad" << "lpad" << "format_number"
1029-
<< "format_date" << "xat" << "yat" << "$area"
1078+
<< "right" << "rpad" << "lpad"
1079+
<< "format_number" << "format_date"
1080+
<< "color_rgb" << "color_rgba" << "ramp_color"
1081+
<< "xat" << "yat" << "$area"
10301082
<< "$length" << "$perimeter" << "$x" << "$y"
10311083
<< "$rownum" << "$id" << "$scale" << "_specialcol_";
10321084
}
@@ -1087,6 +1139,9 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
10871139
<< new StaticFunction( "format", -1, fcnFormatString, QObject::tr( "String" ) )
10881140
<< new StaticFunction( "format_number", 2, fcnFormatNumber, QObject::tr( "String" ) )
10891141
<< new StaticFunction( "format_date", 2, fcnFormatDate, QObject::tr( "String" ) )
1142+
<< new StaticFunction( "color_rgb", 3, fcnColorRgb, QObject::tr( "Color" ) )
1143+
<< new StaticFunction( "color_rgba", 4, fncColorRgba, QObject::tr( "Color" ) )
1144+
<< new StaticFunction( "ramp_color", 2, fcnRampColor, QObject::tr( "Color" ) )
10901145
<< new StaticFunction( "xat", 1, fcnXat, QObject::tr( "Geometry" ), "", true )
10911146
<< new StaticFunction( "yat", 1, fcnYat, QObject::tr( "Geometry" ), "", true )
10921147
<< new StaticFunction( "$area", 0, fcnGeomArea, QObject::tr( "Geometry" ), "", true )

‎tests/src/core/testqgsexpression.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ class TestQgsExpression: public QObject
314314
QTest::newRow( "year with interval" ) << "year(tointerval('2 years'))" << false << QVariant( 2.0 );
315315
QTest::newRow( "age" ) << "age('2012-06-30','2012-06-28')" << false << QVariant::fromValue( QgsExpression::Interval( 172800 ) );
316316
QTest::newRow( "negative age" ) << "age('2012-06-28','2012-06-30')" << false << QVariant::fromValue( QgsExpression::Interval( -172800 ) );
317+
318+
// Color functions
319+
QTest::newRow( "ramp color" ) << "ramp_color('Spectral',0.3)" << false << QVariant( "#fdbe73" );
320+
QTest::newRow( "color rgb" ) << "color_rgb(255,127,0)" << false << QVariant( "#ff7f00" );
321+
QTest::newRow( "color rgba" ) << "color_rgba(255,127,0,200)" << false << QVariant( "255,127,0,200" );
317322
}
318323

319324
void evaluation()

0 commit comments

Comments
 (0)
Please sign in to comment.