Skip to content
Permalink
Browse files

Merge pull request #531 from ccrook/colour_functions

Colour functions
  • Loading branch information
NathanW2 committed Apr 25, 2013
2 parents 21fd0f2 + fd57ffa commit b57841fb5c8282e8c71522f99a0155549de1a85b
@@ -0,0 +1,3 @@

<h3>Color Group</h3>
This group contains functions for manipulating colors
@@ -0,0 +1,16 @@

<h3>color_rgb() function</h3>
Returns a string representation of a color based on its red, green, and blue components

<p><h4>Syntax</h4>
color_rgb(<i>red, green, blue</i>)</p>

<p><h4>Arguments</h4>
<!-- List args for functions here-->
<i> red</i> &rarr; the red component as an integer value from 0 to 255.<br>
<i> green</i> &rarr; the green component as an integer value from 0 to 255.<br>
<i> blue</i> &rarr; the blue component as an integer value from 0 to 255.<br>

<p><h4>Example</h4>
<!-- Show example of function.-->
color_rgb(255,127,0) &rarr; '#ff7f00'</p>
@@ -0,0 +1,17 @@

<h3>color_rgba() function</h3>
Returns a string representation of a color based on its red, green, blue, and alpha (transparency) components

<p><h4>Syntax</h4>
color_rgba(<i>red, green, blue, alpha</i>)</p>

<p><h4>Arguments</h4>
<!-- List args for functions here-->
<i> red</i> &rarr; the red component as an integer value from 0 to 255.<br>
<i> green</i> &rarr; the green component as an integer value from 0 to 255.<br>
<i> blue</i> &rarr; the blue component as an integer value from 0 to 255.<br>
<i> alpha</i> &rarr; the alpha component as an integer value from 0 (completely transparent) to 255 (opaque).<br>

<p><h4>Example</h4>
<!-- Show example of function.-->
color_rgba(255,127,0,200) &rarr; '255,127,0,200'</p>
@@ -0,0 +1,20 @@

<h3>ramp_color() function</h3>
Returns a string representing a color from a color ramp.

<p><h4>Syntax</h4>
ramp_color(<i>ramp_name,value</i>)</p>

<p><h4>Arguments</h4>
<!-- List args for functions here-->
<i> ramp_name</i> &rarr; the name of the color ramp as a string, for example 'Spectral'.<br>
<i> value</i> &rarr; the position on the ramp to select the color from as a real number between 0 and 1.<br>

<p><h4>Example</h4>
<!-- Show example of function.-->
ramp_color('Spectral',0.3) &rarr; '#fdbe73'</p>

<p><h4>Note:</h4>
The color ramps available vary between QGIS installations. This function
may not give the expected results if you move your Quantum project.
</p>
@@ -20,6 +20,7 @@
#include <QSettings>
#include <QDate>
#include <QRegExp>
#include <QColor>

#include <math.h>
#include <limits>
@@ -30,6 +31,9 @@
#include "qgslogger.h"
#include "qgsogcutils.h"
#include "qgsvectorlayer.h"
#include "qgssymbollayerv2utils.h"
#include "qgsvectorcolorrampv2.h"
#include "qgsstylev2.h"

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

static QVariant fcnColorRgb( const QVariantList &values, QgsFeature *, QgsExpression *parent )
{
int red = getIntValue( values.at( 0 ), parent );
int green = getIntValue( values.at( 1 ), parent );
int blue = getIntValue( values.at( 2 ), parent );
QColor color = QColor( red, green, blue );
if ( ! color.isValid() )
{
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1:%2:%3' to color" ).arg( red ).arg( green ).arg( blue ) );
color = QColor( 0, 0, 0 );
}

return color.name();
}

static QVariant fncColorRgba( const QVariantList &values, QgsFeature *, QgsExpression *parent )
{
int red = getIntValue( values.at( 0 ), parent );
int green = getIntValue( values.at( 1 ), parent );
int blue = getIntValue( values.at( 2 ), parent );
int alpha = getIntValue( values.at( 3 ), parent );
QColor color = QColor( red, green, blue, alpha );
if ( ! color.isValid() )
{
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1:%2:%3:%4' to color" ).arg( red ).arg( green ).arg( blue ).arg( alpha ) );
color = QColor( 0, 0, 0 );
}
return QgsSymbolLayerV2Utils::encodeColor( color );
}

QVariant fcnRampColor( const QVariantList &values, QgsFeature *, QgsExpression *parent )
{
QString rampName = getStringValue( values.at( 0 ), parent );
const QgsVectorColorRampV2 *mRamp = QgsStyleV2::defaultStyle()->colorRampRef( rampName );
if ( ! mRamp )
{
parent->setEvalErrorString( QObject::tr( "\"%1\" is not a valid color ramp" ).arg( rampName ) );
return QColor( 0, 0, 0 ).name();
}
double value = getDoubleValue( values.at( 1 ), parent );
QColor color = mRamp->color( value );
return color.name();
}

static QVariant fcnSpecialColumn( const QVariantList& values, QgsFeature* /*f*/, QgsExpression* parent )
{
QString varName = getStringValue( values.at( 0 ), parent );
@@ -1008,13 +1056,15 @@ bool QgsExpression::unregisterFunction( QString name )
return false;
}



QStringList QgsExpression::gmBuiltinFunctions;

const QStringList &QgsExpression::BuiltinFunctions()
{
if ( gmBuiltinFunctions.isEmpty() )
{
gmBuiltinFunctions << "sqrt"
gmBuiltinFunctions
<< "sqrt" << "cos" << "sin" << "tan"
<< "asin" << "acos" << "atan" << "atan2"
<< "exp" << "ln" << "log10" << "log"
@@ -1025,8 +1075,10 @@ const QStringList &QgsExpression::BuiltinFunctions()
<< "minute" << "second" << "lower" << "upper"
<< "title" << "length" << "replace" << "regexp_replace"
<< "substr" << "concat" << "strpos" << "left"
<< "right" << "rpad" << "lpad" << "format_number"
<< "format_date" << "xat" << "yat" << "$area"
<< "right" << "rpad" << "lpad"
<< "format_number" << "format_date"
<< "color_rgb" << "color_rgba" << "ramp_color"
<< "xat" << "yat" << "$area"
<< "$length" << "$perimeter" << "$x" << "$y"
<< "$rownum" << "$id" << "$scale" << "_specialcol_";
}
@@ -1087,6 +1139,9 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "format", -1, fcnFormatString, QObject::tr( "String" ) )
<< new StaticFunction( "format_number", 2, fcnFormatNumber, QObject::tr( "String" ) )
<< new StaticFunction( "format_date", 2, fcnFormatDate, QObject::tr( "String" ) )
<< new StaticFunction( "color_rgb", 3, fcnColorRgb, QObject::tr( "Color" ) )
<< new StaticFunction( "color_rgba", 4, fncColorRgba, QObject::tr( "Color" ) )
<< new StaticFunction( "ramp_color", 2, fcnRampColor, QObject::tr( "Color" ) )
<< new StaticFunction( "xat", 1, fcnXat, QObject::tr( "Geometry" ), "", true )
<< new StaticFunction( "yat", 1, fcnYat, QObject::tr( "Geometry" ), "", true )
<< new StaticFunction( "$area", 0, fcnGeomArea, QObject::tr( "Geometry" ), "", true )
@@ -314,6 +314,11 @@ class TestQgsExpression: public QObject
QTest::newRow( "year with interval" ) << "year(tointerval('2 years'))" << false << QVariant( 2.0 );
QTest::newRow( "age" ) << "age('2012-06-30','2012-06-28')" << false << QVariant::fromValue( QgsExpression::Interval( 172800 ) );
QTest::newRow( "negative age" ) << "age('2012-06-28','2012-06-30')" << false << QVariant::fromValue( QgsExpression::Interval( -172800 ) );

// Color functions
QTest::newRow( "ramp color" ) << "ramp_color('Spectral',0.3)" << false << QVariant( "#fdbe73" );
QTest::newRow( "color rgb" ) << "color_rgb(255,127,0)" << false << QVariant( "#ff7f00" );
QTest::newRow( "color rgba" ) << "color_rgba(255,127,0,200)" << false << QVariant( "255,127,0,200" );
}

void evaluation()

0 comments on commit b57841f

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