Skip to content

Commit a5d6702

Browse files
nirvnnyalldawson
authored andcommitted
[FEATURE] add darker() and lighter() expression functions
1 parent e9ef513 commit a5d6702

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

resources/function_help/json/darker

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "darker",
3+
"type": "function",
4+
"description": "Returns a darker (or lighter) color string",
5+
"arguments": [
6+
{"arg":"color", "description":"a color string"},
7+
{"arg":"factor", "description":"a integer number corresponding to the darkening factor:<ul><li>if the factor is greater than 100, this functions returns a darker color (for e.g., setting factor to 300 returns a color that has one-third the brightness);</li><li>if the factor is less than 100, the return color is lighter, but using the lighter() function for this purpose is recommended;</li><li>if the factor is 0 or negative, the return value is unspecified.</li></ul>"}
8+
],
9+
"examples": [ { "expression":"darker('200,10,30',300)", "returns":"'66,3,10,255'"}]
10+
}

resources/function_help/json/lighter

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "lighter",
3+
"type": "function",
4+
"description": "Returns a lighter (or darker) color string",
5+
"arguments": [
6+
{"arg":"color", "description":"a color string"},
7+
{"arg":"factor", "description":"a integer number corresponding to the lightening factor:<ul><li>if the factor is greater than 100, this functions returns a lighter color (for e.g., setting factor to 150 returns a color that is 50% brighter);</li><li>if the factor is less than 100, the return color is darker, but using the darker() function for this purpose is recommended;</li><li>if the factor is 0 or negative, the return value is unspecified.</li></ul>"}
8+
],
9+
"examples": [ { "expression":"lighter('200,10,30',200)", "returns":"'255,158,168,255'"}]
10+
}

src/core/qgsexpression.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,34 @@ static QVariant fncSetColorPart( const QVariantList &values, const QgsExpression
21352135
return QgsSymbolLayerV2Utils::encodeColor( color );
21362136
}
21372137

2138+
static QVariant fncDarker( const QVariantList &values, const QgsExpressionContext*, QgsExpression *parent )
2139+
{
2140+
QColor color = QgsSymbolLayerV2Utils::decodeColor( values.at( 0 ).toString() );
2141+
if ( ! color.isValid() )
2142+
{
2143+
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to color" ).arg( values.at( 0 ).toString() ) );
2144+
return QVariant();
2145+
}
2146+
2147+
color = color.darker( getIntValue( values.at( 1 ), parent ) );
2148+
2149+
return QgsSymbolLayerV2Utils::encodeColor( color );
2150+
}
2151+
2152+
static QVariant fncLighter( const QVariantList &values, const QgsExpressionContext*, QgsExpression *parent )
2153+
{
2154+
QColor color = QgsSymbolLayerV2Utils::decodeColor( values.at( 0 ).toString() );
2155+
if ( ! color.isValid() )
2156+
{
2157+
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to color" ).arg( values.at( 0 ).toString() ) );
2158+
return QVariant();
2159+
}
2160+
2161+
color = color.lighter( getIntValue( values.at( 1 ), parent ) );
2162+
2163+
return QgsSymbolLayerV2Utils::encodeColor( color );
2164+
}
2165+
21382166
static QVariant fcnSpecialColumn( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
21392167
{
21402168
QString varName = getStringValue( values.at( 0 ), parent );
@@ -2466,6 +2494,8 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
24662494
<< new StaticFunction( "color_cmyk", 4, fcnColorCmyk, "Color" )
24672495
<< new StaticFunction( "color_cmyka", 5, fncColorCmyka, "Color" )
24682496
<< new StaticFunction( "color_part", 2, fncColorPart, "Color" )
2497+
<< new StaticFunction( "darker", 2, fncDarker, "Color" )
2498+
<< new StaticFunction( "lighter", 2, fncLighter, "Color" )
24692499
<< new StaticFunction( "set_color_part", 3, fncSetColorPart, "Color" )
24702500
<< new StaticFunction( "$geometry", 0, fcnGeometry, "GeometryGroup", QString(), true )
24712501
<< new StaticFunction( "$area", 0, fcnGeomArea, "GeometryGroup", QString(), true )

tests/src/core/testqgsexpression.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,11 @@ class TestQgsExpression: public QObject
607607
QTest::newRow( "set color part yellow" ) << "to_int(color_part(set_color_part(color_cmyk(21,0,92,70),'yellow',96),'yellow'))" << false << QVariant( 96 );
608608
QTest::newRow( "set color part black" ) << "to_int(color_part(set_color_part(color_cmyk(21,0,92,70),'black',100),'black'))" << false << QVariant( 100 );
609609

610+
QTest::newRow( "color darker" ) << "darker('200,100,30',150)" << false << QVariant( "133,66,20,255" );
611+
QTest::newRow( "color darker bad color" ) << "darker('notacolor',150)" << true << QVariant();
612+
QTest::newRow( "color lighter" ) << "lighter('200,100,30',150)" << false << QVariant( "255,154,83,255" );
613+
QTest::newRow( "color lighter bad color" ) << "lighter('notacolor',150)" << true << QVariant();
614+
610615
// Precedence and associativity
611616
QTest::newRow( "multiplication first" ) << "1+2*3" << false << QVariant( 7 );
612617
QTest::newRow( "brackets first" ) << "(1+2)*(3+4)" << false << QVariant( 21 );

0 commit comments

Comments
 (0)