Skip to content

Commit

Permalink
Add color_hsv and color_hsva functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 28, 2013
1 parent d52e007 commit c038374
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 5 deletions.
4 changes: 2 additions & 2 deletions resources/function_help/color_hsl-en_US
Expand Up @@ -8,8 +8,8 @@ Returns a string representation of a color based on its hue, saturation, and lig
<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> saturation</i> &rarr; the saturation percentage of the color as an integer value from 0 to 100.<br>
<i> lightness</i> &rarr; the lightness percentage of the color as an integer value from 0 to 100.<br>

<p><h4>Example</h4>
<!-- Show example of function.-->
Expand Down
4 changes: 2 additions & 2 deletions resources/function_help/color_hsla-en_US
Expand Up @@ -8,8 +8,8 @@ Returns a string representation of a color based on its hue, saturation, lightne
<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> saturation</i> &rarr; the saturation percentage of the color as an integer value from 0 to 100.<br>
<i> lightness</i> &rarr; the lightness percentage 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>
Expand Down
16 changes: 16 additions & 0 deletions resources/function_help/color_hsv-en_US
@@ -0,0 +1,16 @@

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

<p><h4>Syntax</h4>
color_hsv(<i>hue, saturation, value</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 percentage of the color as an integer value from 0 to 100.<br>
<i> value</i> &rarr; the value percentage of the color as an integer from 0 to 100.<br>

<p><h4>Example</h4>
<!-- Show example of function.-->
color_hsv(40,100,100) &rarr; '#ffaa00'</p>
17 changes: 17 additions & 0 deletions resources/function_help/color_hsva-en_US
@@ -0,0 +1,17 @@

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

<p><h4>Syntax</h4>
color_hsva(<i>hue, saturation, value, 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 percentage of the color as an integer value from 0 to 100.<br>
<i> value</i> &rarr; the value percentage of the color as an integer 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_hsva(40,100,100,200) &rarr; '255,170,0,200'</p>
44 changes: 43 additions & 1 deletion src/core/qgsexpression.cpp
Expand Up @@ -1063,6 +1063,46 @@ static QVariant fncColorHsla( const QVariantList &values, QgsFeature *, QgsExpre
return QgsSymbolLayerV2Utils::encodeColor( color );
}

static QVariant fcnColorHsv( 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;
// Value ranges from 0 - 100
double value = getIntValue( values.at( 2 ), parent ) / 100.0;

QColor color = QColor::fromHsvF( hue, saturation, value );

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

return color.name();
}

static QVariant fncColorHsva( 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;
// Value ranges from 0 - 100
double value = getIntValue( values.at( 2 ), parent ) / 100.0;
// Alpha ranges from 0 - 255
double alpha = getIntValue( values.at( 3 ), parent ) / 255.0;

QColor color = QColor::fromHsvF( hue, saturation, value, alpha );
if ( ! color.isValid() )
{
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1:%2:%3:%4' to color" ).arg( hue ).arg( saturation ).arg( value ).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 );
Expand Down Expand Up @@ -1118,7 +1158,7 @@ const QStringList &QgsExpression::BuiltinFunctions()
<< "right" << "rpad" << "lpad"
<< "format_number" << "format_date"
<< "color_rgb" << "color_rgba" << "ramp_color"
<< "color_hsl" << "color_hsla"
<< "color_hsl" << "color_hsla" << "color_hsv" << "color_hsva"
<< "xat" << "yat" << "$area"
<< "$length" << "$perimeter" << "$x" << "$y"
<< "$rownum" << "$id" << "$scale" << "_specialcol_";
Expand Down Expand Up @@ -1185,6 +1225,8 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< 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( "color_hsv", 3, fcnColorHsv, QObject::tr( "Color" ) )
<< new StaticFunction( "color_hsva", 4, fncColorHsva, 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 )
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -321,6 +321,8 @@ class TestQgsExpression: public QObject
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" );
QTest::newRow( "color hsv" ) << "color_hsv(40,100,100)" << false << QVariant( "#ffaa00" );
QTest::newRow( "color hsva" ) << "color_hsva(40,100,100,200)" << false << QVariant( "255,170,0,200" );
}

void evaluation()
Expand Down

0 comments on commit c038374

Please sign in to comment.