|
40 | 40 | #include "qgsrasterlayer.h" |
41 | 41 | #include "qgsvectorlayer.h" |
42 | 42 | #include "qgsrasterbandstats.h" |
| 43 | +#include "qgscolorramp.h" |
43 | 44 |
|
44 | 45 | const QString QgsExpressionFunction::helpText() const |
45 | 46 | { |
@@ -2943,15 +2944,26 @@ static QVariant fncColorRgba( const QVariantList &values, const QgsExpressionCon |
2943 | 2944 |
|
2944 | 2945 | QVariant fcnRampColor( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent ) |
2945 | 2946 | { |
2946 | | - QString rampName = QgsExpressionUtils::getStringValue( values.at( 0 ), parent ); |
2947 | | - const QgsColorRamp *mRamp = QgsStyle::defaultStyle()->colorRampRef( rampName ); |
2948 | | - if ( ! mRamp ) |
| 2947 | + QgsGradientColorRamp expRamp; |
| 2948 | + const QgsColorRamp *ramp; |
| 2949 | + if ( values.at( 0 ).canConvert<QgsGradientColorRamp>() ) |
2949 | 2950 | { |
2950 | | - parent->setEvalErrorString( QObject::tr( "\"%1\" is not a valid color ramp" ).arg( rampName ) ); |
2951 | | - return QColor( 0, 0, 0 ).name(); |
| 2951 | + expRamp = QgsExpressionUtils::getRamp( values.at( 0 ), parent ); |
| 2952 | + ramp = &expRamp; |
2952 | 2953 | } |
| 2954 | + else |
| 2955 | + { |
| 2956 | + QString rampName = QgsExpressionUtils::getStringValue( values.at( 0 ), parent ); |
| 2957 | + ramp = QgsStyle::defaultStyle()->colorRampRef( rampName ); |
| 2958 | + if ( ! ramp ) |
| 2959 | + { |
| 2960 | + parent->setEvalErrorString( QObject::tr( "\"%1\" is not a valid color ramp" ).arg( rampName ) ); |
| 2961 | + return QVariant(); |
| 2962 | + } |
| 2963 | + } |
| 2964 | + |
2953 | 2965 | double value = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent ); |
2954 | | - QColor color = mRamp->color( value ); |
| 2966 | + QColor color = ramp->color( value ); |
2955 | 2967 | return QgsSymbolLayerUtils::encodeColor( color ); |
2956 | 2968 | } |
2957 | 2969 |
|
@@ -3122,6 +3134,47 @@ static QVariant fncColorPart( const QVariantList &values, const QgsExpressionCon |
3122 | 3134 | return QVariant(); |
3123 | 3135 | } |
3124 | 3136 |
|
| 3137 | +static QVariant fcnCreateRamp( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent ) |
| 3138 | +{ |
| 3139 | + const QVariantMap map = QgsExpressionUtils::getMapValue( values.at( 0 ), parent ); |
| 3140 | + if ( map.count() < 1 ) |
| 3141 | + { |
| 3142 | + parent->setEvalErrorString( QObject::tr( "A minimum of two colors is required to create a ramp" ) ); |
| 3143 | + return QVariant(); |
| 3144 | + } |
| 3145 | + |
| 3146 | + QList< QColor > colors; |
| 3147 | + QgsGradientStopsList stops; |
| 3148 | + for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it ) |
| 3149 | + { |
| 3150 | + colors << QgsSymbolLayerUtils::decodeColor( it.value().toString() ); |
| 3151 | + if ( !colors.last().isValid() ) |
| 3152 | + { |
| 3153 | + parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to color" ).arg( it.value().toString() ) ); |
| 3154 | + return QVariant(); |
| 3155 | + } |
| 3156 | + |
| 3157 | + double step = it.key().toDouble(); |
| 3158 | + if ( it == map.constBegin() ) |
| 3159 | + { |
| 3160 | + if ( step != 0.0 ) |
| 3161 | + stops << QgsGradientStop( step, colors.last() ); |
| 3162 | + } |
| 3163 | + else if ( it == map.constEnd() ) |
| 3164 | + { |
| 3165 | + if ( step != 1.0 ) |
| 3166 | + stops << QgsGradientStop( step, colors.last() ); |
| 3167 | + } |
| 3168 | + else |
| 3169 | + { |
| 3170 | + stops << QgsGradientStop( step, colors.last() ); |
| 3171 | + } |
| 3172 | + } |
| 3173 | + bool discrete = values.at( 1 ).toBool(); |
| 3174 | + |
| 3175 | + return QVariant::fromValue( QgsGradientColorRamp( colors.first(), colors.last(), discrete, stops ) ); |
| 3176 | +} |
| 3177 | + |
3125 | 3178 | static QVariant fncSetColorPart( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent ) |
3126 | 3179 | { |
3127 | 3180 | QColor color = QgsSymbolLayerUtils::decodeColor( values.at( 0 ).toString() ); |
@@ -3825,6 +3878,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions() |
3825 | 3878 | << new QgsStaticExpressionFunction( QStringLiteral( "color_rgb" ), 3, fcnColorRgb, QStringLiteral( "Color" ) ) |
3826 | 3879 | << new QgsStaticExpressionFunction( QStringLiteral( "color_rgba" ), 4, fncColorRgba, QStringLiteral( "Color" ) ) |
3827 | 3880 | << new QgsStaticExpressionFunction( QStringLiteral( "ramp_color" ), 2, fcnRampColor, QStringLiteral( "Color" ) ) |
| 3881 | + << new QgsStaticExpressionFunction( QStringLiteral( "create_ramp" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "map" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "discrete" ), true, false ), fcnCreateRamp, QStringLiteral( "Color" ) ) |
3828 | 3882 | << new QgsStaticExpressionFunction( QStringLiteral( "color_hsl" ), 3, fcnColorHsl, QStringLiteral( "Color" ) ) |
3829 | 3883 | << new QgsStaticExpressionFunction( QStringLiteral( "color_hsla" ), 4, fncColorHsla, QStringLiteral( "Color" ) ) |
3830 | 3884 | << new QgsStaticExpressionFunction( QStringLiteral( "color_hsv" ), 3, fcnColorHsv, QStringLiteral( "Color" ) ) |
|
0 commit comments