Skip to content
Permalink
Browse files

Add color_hsv and color_hsva functions

  • Loading branch information
nyalldawson committed Apr 28, 2013
1 parent d52e007 commit c038374e41a3ea81331010db47a4756076984192
@@ -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.-->
@@ -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>
@@ -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>
@@ -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>
@@ -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 );
@@ -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_";
@@ -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 )
@@ -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()

0 comments on commit c038374

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