Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two new expression functions: rgb_to_hex, to_base #39655

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions resources/function_help/json/rgb_to_hex
@@ -0,0 +1,12 @@
{
"name": "rgb_to_hex",
"type": "function",
"groups": ["Color"],
"description": "Returns a hex string representation of a color based on its red, green, blue, and optional alpha components. The output is either '#RRGGBB' or '#RRGGBBAA' where RR, GG, BB, and AA are the red, green, blue and alpha components.",
"arguments": [ {"arg":"red","description":"red component as an integer value from 0 to 255"},
{"arg":"green","description":"green component as an integer value from 0 to 255"},
{"arg":"blue","description":"blue component as an integer value from 0 to 255"},
{"arg":"alpha","optional":true,"description":"alpha component as an integer value from 0 to 255"}],
"examples": [ { "expression":"rgb_to_hex(255,127,0)", "returns":"'#FF7F00'"} ]
}

15 changes: 15 additions & 0 deletions resources/function_help/json/to_base
@@ -0,0 +1,15 @@

{
"name": "to_base",
"type": "function",
"groups": ["Conversions"],
"description": "Convert an integer value to a string representation in a different base or number system. Binary is base 2, octal base 8, and hexadecimal base 16.",
"arguments": [
{"arg":"value", "description": "A decimal value to encode to a new base"},
{"arg":"base", "description": "The base number system. Values must be between and including 2 through 36"}],
"examples": [
{"expression":"to_base(456, 16)", "returns":"'1C8'" },
{"expression":"to_base(456, 8)", "returns":"'710'" },
{"expression":"to_base(456, 2)", "returns":"'111001000'" }
]
}
45 changes: 45 additions & 0 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -4539,6 +4539,28 @@ static QVariant fcnColorRgb( const QVariantList &values, const QgsExpressionCont
return QStringLiteral( "%1,%2,%3" ).arg( color.red() ).arg( color.green() ).arg( color.blue() );
}

static QVariant fcnRgbToHex(const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction *)
{
int red = QgsExpressionUtils::getNativeIntValue(values.at(0), parent);
int green = QgsExpressionUtils::getNativeIntValue(values.at(1), parent);
int blue = QgsExpressionUtils::getNativeIntValue(values.at(2), parent);
if (red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255) {
parent->setEvalErrorString(QObject::tr("Invalid RGB values %1:%2:%3").arg(red).arg(green).arg(blue));
return QVariant();
}
if (values.at(3).isValid()) {
int alpha = QgsExpressionUtils::getNativeIntValue(values.at(3), parent);
if (alpha < 0 || alpha > 255)
{
parent->setEvalErrorString(QObject::tr("Invalid alpha value %1").arg(alpha));
return QVariant();
}

return QVariant(QLatin1Char('#') + QString::number((qulonglong)(red << 24) | (green << 16) | (blue << 8) | alpha | 0x100000000, 16).right(8));
}
return QVariant(QLatin1Char('#') + QString::number((ulong)(red << 16) | (green << 8) | blue | 0x1000000, 16).right(6).toUpper());
}

static QVariant fcnTry( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsExpressionNode *node = QgsExpressionUtils::getNode( values.at( 0 ), parent );
Expand Down Expand Up @@ -5740,6 +5762,20 @@ static QVariant fcnFromBase64( const QVariantList &values, const QgsExpressionCo
return QVariant( decoded );
}

static QVariant fcnToBase(const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction *)
{
qlonglong value = QgsExpressionUtils::getIntValue(values.at(0), parent);
int base = QgsExpressionUtils::getIntValue(values.at(1), parent);
if (value < 0) {
value = -value;
}
if (base < 2 || base > 36) {
parent->setEvalErrorString(QObject::tr("The base value must be between 2 and 36"));
return QVariant();
}
return QVariant(QString::number(value, base).toUpper());
}

typedef bool ( QgsGeometry::*RelationFunction )( const QgsGeometry &geometry ) const;

static QVariant executeGeomOverlay( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const RelationFunction &relationFunction, bool invert = false, double bboxGrow = 0, bool isNearestFunc = false )
Expand Down Expand Up @@ -6277,6 +6313,11 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< QgsExpressionFunction::Parameter( QStringLiteral( "blue" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "alpha" ) ),
fncColorRgba, QStringLiteral( "Color" ) )
<< new QgsStaticExpressionFunction(QStringLiteral("rgb_to_hex"), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter(QStringLiteral("red"))
<< QgsExpressionFunction::Parameter(QStringLiteral("green"))
<< QgsExpressionFunction::Parameter(QStringLiteral("blue"))
<< QgsExpressionFunction::Parameter(QStringLiteral("alpha"), true, QVariant()),
fcnRgbToHex, QStringLiteral("Color"))
<< new QgsStaticExpressionFunction( QStringLiteral( "ramp_color" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "ramp_name" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ),
fcnRampColor, QStringLiteral( "Color" ) )
Expand Down Expand Up @@ -6355,6 +6396,10 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsStaticExpressionFunction( QStringLiteral( "from_base64" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
fcnFromBase64, QStringLiteral( "Conversions" ) )

// general base conversions
<< new QgsStaticExpressionFunction(QStringLiteral("to_base"), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter(QStringLiteral("value")) << QgsExpressionFunction::Parameter(QStringLiteral("base")),
fcnToBase, QStringLiteral("Conversions"))

// deprecated stuff - hidden from users
<< new QgsStaticExpressionFunction( QStringLiteral( "$scale" ), QgsExpressionFunction::ParameterList(), fcnMapScale, QStringLiteral( "deprecated" ) );

Expand Down