Skip to content

Commit b42c893

Browse files
authoredMar 7, 2018
[FEATURE] color_grayscale_average() and color_mix_rgb() expression functions (#6554)
1 parent 0a63919 commit b42c893

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed
 
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "color_grayscale_average",
3+
"type": "function",
4+
"description": "Applies a grayscale filter and returns a string representation from a provided color.",
5+
"arguments": [
6+
{"arg":"color", "description":"a color string"}
7+
],
8+
"examples": [ { "expression":"color_grayscale_average('255,100,50')", "returns":"127,127,127,255"}]
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "color_mix_rgb",
3+
"type": "function",
4+
"description": "Returns a string representing a color mixing the reg, green, blue, and alpha values of two provided colors based on a given ratio.",
5+
"arguments": [
6+
{"arg":"color1", "description":"a color string"},
7+
{"arg":"color2", "description":"a color string"},
8+
{"arg":"ratio", "description":"a ratio"}
9+
],
10+
"examples": [ { "expression":"color_mix_rgb('0,0,0','255,255,255',0.5)", "returns":"127,127,127,255"}]
11+
}

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,6 +2998,41 @@ static QVariant fcnFormatDate( const QVariantList &values, const QgsExpressionCo
29982998
return dt.toString( format );
29992999
}
30003000

3001+
static QVariant fcnColorGrayscaleAverage( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * )
3002+
{
3003+
QColor color = QgsSymbolLayerUtils::decodeColor( values.at( 0 ).toString() );
3004+
int avg = ( color.red() + color.green() + color.blue() ) / 3;
3005+
int alpha = color.alpha();
3006+
3007+
color.setRgb( avg, avg, avg, alpha );
3008+
3009+
return QgsSymbolLayerUtils::encodeColor( color );
3010+
}
3011+
3012+
static QVariant fcnColorMixRgb( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
3013+
{
3014+
QColor color1 = QgsSymbolLayerUtils::decodeColor( values.at( 0 ).toString() );
3015+
QColor color2 = QgsSymbolLayerUtils::decodeColor( values.at( 1 ).toString() );
3016+
double ratio = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
3017+
if ( ratio > 1 )
3018+
{
3019+
ratio = 1;
3020+
}
3021+
else if ( ratio < 0 )
3022+
{
3023+
ratio = 0;
3024+
}
3025+
3026+
int red = color1.red() * ( 1 - ratio ) + color2.red() * ratio;
3027+
int green = color1.green() * ( 1 - ratio ) + color2.green() * ratio;
3028+
int blue = color1.blue() * ( 1 - ratio ) + color2.blue() * ratio;
3029+
int alpha = color1.alpha() * ( 1 - ratio ) + color2.alpha() * ratio;
3030+
3031+
QColor newColor( red, green, blue, alpha );
3032+
3033+
return QgsSymbolLayerUtils::encodeColor( newColor );
3034+
}
3035+
30013036
static QVariant fcnColorRgb( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
30023037
{
30033038
int red = QgsExpressionUtils::getIntValue( values.at( 0 ), parent );
@@ -4098,6 +4133,8 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
40984133
<< new QgsStaticExpressionFunction( QStringLiteral( "format" ), -1, fcnFormatString, QStringLiteral( "String" ) )
40994134
<< new QgsStaticExpressionFunction( QStringLiteral( "format_number" ), 2, fcnFormatNumber, QStringLiteral( "String" ) )
41004135
<< new QgsStaticExpressionFunction( QStringLiteral( "format_date" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "date" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "format" ) ), fcnFormatDate, QStringList() << QStringLiteral( "String" ) << QStringLiteral( "Date and Time" ) )
4136+
<< new QgsStaticExpressionFunction( QStringLiteral( "color_grayscale_average" ), 1, fcnColorGrayscaleAverage, QStringLiteral( "Color" ) )
4137+
<< new QgsStaticExpressionFunction( QStringLiteral( "color_mix_rgb" ), 3, fcnColorMixRgb, QStringLiteral( "Color" ) )
41014138
<< new QgsStaticExpressionFunction( QStringLiteral( "color_rgb" ), 3, fcnColorRgb, QStringLiteral( "Color" ) )
41024139
<< new QgsStaticExpressionFunction( QStringLiteral( "color_rgba" ), 4, fncColorRgba, QStringLiteral( "Color" ) )
41034140
<< new QgsStaticExpressionFunction( QStringLiteral( "ramp_color" ), 2, fcnRampColor, QStringLiteral( "Color" ) )

‎tests/src/core/testqgsexpression.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,8 @@ class TestQgsExpression: public QObject
11091109
QTest::newRow( "color hsva" ) << "color_hsva(40,100,100,200)" << false << QVariant( "255,170,0,200" );
11101110
QTest::newRow( "color cmyk" ) << "color_cmyk(100,50,33,10)" << false << QVariant( "0,115,154" );
11111111
QTest::newRow( "color cmyka" ) << "color_cmyka(50,25,90,60,200)" << false << QVariant( "51,76,10,200" );
1112+
QTest::newRow( "color grayscale average" ) << "color_grayscale_average('255,100,50')" << false << QVariant( "135,135,135,255" );
1113+
QTest::newRow( "color mix rgb" ) << "color_mix_rgb('0,0,0,100','255,255,255',0.5)" << false << QVariant( "127,127,127,177" );
11121114

11131115
QTest::newRow( "color part bad color" ) << "color_part('notacolor','red')" << true << QVariant();
11141116
QTest::newRow( "color part bad part" ) << "color_part(color_rgb(255,127,0),'bad')" << true << QVariant();

0 commit comments

Comments
 (0)
Please sign in to comment.