Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add color_cmyk and color_cmyka functions
  • Loading branch information
nyalldawson committed Apr 28, 2013
1 parent c038374 commit 6e5221c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
17 changes: 17 additions & 0 deletions resources/function_help/color_cmyk-en_US
@@ -0,0 +1,17 @@

<h3>color_cmyk() function</h3>
Returns a string representation of a color based on its cyan, magenta, yellow and black components

<p><h4>Syntax</h4>
color_cmyk(<i>cyan, magenta, yellow, black</i>)</p>

<p><h4>Arguments</h4>
<!-- List args for functions here-->
<i> cyan</i> &rarr; the cyan component of the color, as a percentage integer value from 0 to 100.<br>
<i> magenta</i> &rarr; the magenta component of the color, as a percentage integer value from 0 to 100.<br>
<i> yellow</i> &rarr; the yellow component of the color, as a percentage integer value from 0 to 100.<br>
<i> black</i> &rarr; the black component of the color, as a percentage integer value from 0 to 100.<br>

<p><h4>Example</h4>
<!-- Show example of function.-->
color_cmyk(100,50,0,10) &rarr; '#0073e6'</p>
18 changes: 18 additions & 0 deletions resources/function_help/color_cmyka-en_US
@@ -0,0 +1,18 @@

<h3>color_cmyka() function</h3>
Returns a string representation of a color based on its cyan, magenta, yellow, black and alpha (transparency) components

<p><h4>Syntax</h4>
color_cmyka(<i>cyan, magenta, yellow, black, alpha</i>)</p>

<p><h4>Arguments</h4>
<!-- List args for functions here-->
<i> cyan</i> &rarr; the cyan component of the color, as a percentage integer value from 0 to 100.<br>
<i> magenta</i> &rarr; the magenta component of the color, as a percentage integer value from 0 to 100.<br>
<i> yellow</i> &rarr; the yellow component of the color, as a percentage integer value from 0 to 100.<br>
<i> black</i> &rarr; the black component of the color, as a percentage 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_cmyka(100,50,0,10,200) &rarr; '0,115,230,200'</p>
47 changes: 47 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -1103,6 +1103,50 @@ static QVariant fncColorHsva( const QVariantList &values, QgsFeature *, QgsExpre
return QgsSymbolLayerV2Utils::encodeColor( color );
}

static QVariant fcnColorCmyk( const QVariantList &values, QgsFeature *, QgsExpression *parent )
{
// Cyan ranges from 0 - 100
double cyan = getIntValue( values.at( 0 ), parent ) / 100.0;
// Magenta ranges from 0 - 100
double magenta = getIntValue( values.at( 1 ), parent ) / 100.0;
// Yellow ranges from 0 - 100
double yellow = getIntValue( values.at( 2 ), parent ) / 100.0;
// Black ranges from 0 - 100
double black = getIntValue( values.at( 3 ), parent ) / 100.0;

QColor color = QColor::fromCmykF( cyan, magenta, yellow, black );

if ( ! color.isValid() )
{
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1:%2:%3:%4' to color" ).arg( cyan ).arg( magenta ).arg( yellow ).arg( black ) );
color = QColor( 0, 0, 0 );
}

return color.name();
}

static QVariant fncColorCmyka( const QVariantList &values, QgsFeature *, QgsExpression *parent )
{
// Cyan ranges from 0 - 100
double cyan = getIntValue( values.at( 0 ), parent ) / 100.0;
// Magenta ranges from 0 - 100
double magenta = getIntValue( values.at( 1 ), parent ) / 100.0;
// Yellow ranges from 0 - 100
double yellow = getIntValue( values.at( 2 ), parent ) / 100.0;
// Black ranges from 0 - 100
double black = getIntValue( values.at( 3 ), parent ) / 100.0;
// Alpha ranges from 0 - 255
double alpha = getIntValue( values.at( 4 ), parent ) / 255.0;

QColor color = QColor::fromCmykF( cyan, magenta, yellow, black, alpha );
if ( ! color.isValid() )
{
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1:%2:%3:%4:%5' to color" ).arg( cyan ).arg( magenta ).arg( yellow ).arg( black ).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 @@ -1159,6 +1203,7 @@ const QStringList &QgsExpression::BuiltinFunctions()
<< "format_number" << "format_date"
<< "color_rgb" << "color_rgba" << "ramp_color"
<< "color_hsl" << "color_hsla" << "color_hsv" << "color_hsva"
<< "color_cymk" << "color_cymka"
<< "xat" << "yat" << "$area"
<< "$length" << "$perimeter" << "$x" << "$y"
<< "$rownum" << "$id" << "$scale" << "_specialcol_";
Expand Down Expand Up @@ -1227,6 +1272,8 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< 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( "color_cmyk", 4, fcnColorCmyk, QObject::tr( "Color" ) )
<< new StaticFunction( "color_cmyka", 5, fncColorCmyka, 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 @@ -323,6 +323,8 @@ class TestQgsExpression: public QObject
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" );
QTest::newRow( "color cmyk" ) << "color_cmyk(100,50,33,10)" << false << QVariant( "#00739a" );
QTest::newRow( "color cmyka" ) << "color_cmyka(50,25,90,60,200)" << false << QVariant( "51,76,10,200" );
}

void evaluation()
Expand Down

0 comments on commit 6e5221c

Please sign in to comment.