Skip to content
Permalink
Browse files

Add color_hsl and color_hsla functions

  • Loading branch information
nyalldawson committed Apr 28, 2013
1 parent 1a19947 commit d52e007de8633d2c0f0b6d723c865acc4952f27e
@@ -0,0 +1,16 @@

<h3>color_hsl() function</h3>
Returns a string representation of a color based on its hue, saturation, and lightness attributes

<p><h4>Syntax</h4>
color_hsl(<i>hue, saturation, lightness</i>)</p>

<p><h4>Arguments</h4>
<!-- List args for functions here-->
<i> hue</i> &rarr; the hue of the color, as an integer value from 0 to 360.<br>
<i> saturation</i> &rarr; the saturation of the color as an integer value from 0 to 100.<br>
<i> lightness</i> &rarr; the lightness of the color as an integer value from 0 to 100.<br>

<p><h4>Example</h4>
<!-- Show example of function.-->
color_hsl(100,50,70) &rarr; '#a6d98c'</p>
@@ -0,0 +1,17 @@

<h3>color_hsla() function</h3>
Returns a string representation of a color based on its hue, saturation, lightness and alpha (transparency) attributes

<p><h4>Syntax</h4>
color_hsla(<i>hue, saturation, lightness, alpha</i>)</p>

<p><h4>Arguments</h4>
<!-- List args for functions here-->
<i> hue</i> &rarr; the hue of the color, as an integer value from 0 to 360.<br>
<i> saturation</i> &rarr; the saturation of the color as an integer value from 0 to 100.<br>
<i> lightness</i> &rarr; the lightness of the color as an integer value from 0 to 100.<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_hsla(100,50,70,200) &rarr; '166,217,140,200'</p>
@@ -1023,6 +1023,46 @@ QVariant fcnRampColor( const QVariantList &values, QgsFeature *, QgsExpression *
return color.name();
}

static QVariant fcnColorHsl( const QVariantList &values, QgsFeature *, QgsExpression *parent )
{
// Hue ranges from 0 - 360
double hue = getIntValue( values.at( 0 ), parent ) / 360.0;
// Saturation ranges from 0 - 100
double saturation = getIntValue( values.at( 1 ), parent ) / 100.0;
// Lightness ranges from 0 - 100
double lightness = getIntValue( values.at( 2 ), parent ) / 100.0;

QColor color = QColor::fromHslF( hue, saturation, lightness );

if ( ! color.isValid() )
{
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1:%2:%3' to color" ).arg( hue ).arg( saturation ).arg( lightness ) );
color = QColor( 0, 0, 0 );
}

return color.name();
}

static QVariant fncColorHsla( const QVariantList &values, QgsFeature *, QgsExpression *parent )
{
// Hue ranges from 0 - 360
double hue = getIntValue( values.at( 0 ), parent ) / 360.0;
// Saturation ranges from 0 - 100
double saturation = getIntValue( values.at( 1 ), parent ) / 100.0;
// Lightness ranges from 0 - 100
double lightness = getIntValue( values.at( 2 ), parent ) / 100.0;
// Alpha ranges from 0 - 255
double alpha = getIntValue( values.at( 3 ), parent ) / 255.0;

QColor color = QColor::fromHslF( hue, saturation, lightness, alpha );
if ( ! color.isValid() )
{
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1:%2:%3:%4' to color" ).arg( hue ).arg( saturation ).arg( lightness ).arg( alpha ) );
color = QColor( 0, 0, 0 );
}
return QgsSymbolLayerV2Utils::encodeColor( color );
}

static QVariant fcnSpecialColumn( const QVariantList& values, QgsFeature* /*f*/, QgsExpression* parent )
{
QString varName = getStringValue( values.at( 0 ), parent );
@@ -1078,6 +1118,7 @@ const QStringList &QgsExpression::BuiltinFunctions()
<< "right" << "rpad" << "lpad"
<< "format_number" << "format_date"
<< "color_rgb" << "color_rgba" << "ramp_color"
<< "color_hsl" << "color_hsla"
<< "xat" << "yat" << "$area"
<< "$length" << "$perimeter" << "$x" << "$y"
<< "$rownum" << "$id" << "$scale" << "_specialcol_";
@@ -1142,6 +1183,8 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< 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( "color_hsl", 3, fcnColorHsl, QObject::tr( "Color" ) )
<< new StaticFunction( "color_hsla", 4, fncColorHsla, 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 )
@@ -319,6 +319,8 @@ class TestQgsExpression: public QObject
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" );
QTest::newRow( "color hsl" ) << "color_hsl(100,50,70)" << false << QVariant( "#a6d98c" );
QTest::newRow( "color hsla" ) << "color_hsla(100,50,70,200)" << false << QVariant( "166,217,140,200" );
}

void evaluation()

0 comments on commit d52e007

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