33 static QColor _interpolate( 
const QColor &c1, 
const QColor &c2, 
const double value )
 
   35   if ( std::isnan( value ) ) 
return c2;
 
   37   qreal r = ( c1.redF() + value * ( c2.redF() - c1.redF() ) );
 
   38   qreal g = ( c1.greenF() + value * ( c2.greenF() - c1.greenF() ) );
 
   39   qreal b = ( c1.blueF() + value * ( c2.blueF() - c1.blueF() ) );
 
   40   qreal a = ( c1.alphaF() + value * ( c2.alphaF() - c1.alphaF() ) );
 
   42   return QColor::fromRgbF( r, g, b, a );
 
   51   , mDiscrete( discrete )
 
   61   if ( props.contains( QStringLiteral( 
"color1" ) ) )
 
   63   if ( props.contains( QStringLiteral( 
"color2" ) ) )
 
   68   if ( props.contains( QStringLiteral( 
"stops" ) ) )
 
   70     const auto constSplit = props[
"stops"].toString().split( 
':' );
 
   71     for ( 
const QString &stop : constSplit )
 
   73       int i = stop.indexOf( 
';' );
 
   78 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 2) 
   87   bool discrete = 
false;
 
   88   if ( props.contains( QStringLiteral( 
"discrete" ) ) )
 
   90     if ( props[QStringLiteral( 
"discrete" )] == QLatin1String( 
"1" ) )
 
   96   for ( QVariantMap::const_iterator it = props.constBegin();
 
   97         it != props.constEnd(); ++it )
 
   99     if ( it.key().startsWith( QLatin1String( 
"info_" ) ) )
 
  100       info[ it.key().mid( 5 )] = it.value().toString();
 
  114   else if ( index >= 
mStops.size() + 1 )
 
  120     return mStops[index - 1].offset;
 
  134   else if ( 
mStops.isEmpty() )
 
  143     double lower = 0, upper = 0;
 
  145     for ( QgsGradientStopsList::const_iterator it = 
mStops.begin(); it != 
mStops.end(); ++it )
 
  147       if ( it->offset > 
value )
 
  183     for ( 
int k = 
mStops.size() - 1; k >= 1; k-- )
 
  194     for ( 
int k = 
mStops.size() - 1; k >= 0; k-- )
 
  218     for ( QgsGradientStopsList::const_iterator it = 
mStops.begin(); it != 
mStops.end(); ++it )
 
  222     map[QStringLiteral( 
"stops" )] = lst.join( QLatin1Char( 
':' ) );
 
  225   map[QStringLiteral( 
"discrete" )] = 
mDiscrete ? 
"1" : 
"0";
 
  227   for ( QgsStringMap::const_iterator it = 
mInfo.constBegin();
 
  228         it != 
mInfo.constEnd(); ++it )
 
  230     map[
"info_" + it.key()] = it.value();
 
  233   map[QStringLiteral( 
"rampType" )] = 
type();
 
  247     int numStops = 
mStops.count() + 2;
 
  249     for ( QgsGradientStopsList::const_iterator it = 
mStops.constBegin();
 
  250           it != 
mStops.constEnd(); ++it )
 
  252       newStops.append( 
QgsGradientStop( 
static_cast< double >( i ) / numStops, it->color ) );
 
  253       if ( i == numStops - 1 )
 
  263     int numStops = 
mStops.count() + 2;
 
  265     for ( QgsGradientStopsList::const_iterator it = 
mStops.constBegin();
 
  266           it != 
mStops.constEnd(); ++it )
 
  268       newStops.append( 
QgsGradientStop( 
static_cast< double >( i ) / ( numStops - 2 ), it->color ) );
 
  269       if ( i == numStops - 3 )
 
  301   gradient->setColorAt( 0, 
color1 );
 
  302   gradient->setColorAt( 1, 
color2 );
 
  304   for ( QgsGradientStopsList::const_iterator it = 
mStops.constBegin();
 
  305         it != 
mStops.constEnd(); ++it )
 
  307     QColor rampColor = it->color;
 
  310       rampColor.setAlpha( rampColor.alpha() * opacity );
 
  312     gradient->setColorAt( it->offset, rampColor );
 
  321     int satMin, 
int satMax, 
int valMin, 
int valMax )
 
  323   , mHueMin( hueMin ), mHueMax( hueMax )
 
  324   , mSatMin( satMin ), mSatMax( satMax )
 
  325   , mValMin( valMin ), mValMax( valMax )
 
  337   if ( props.contains( QStringLiteral( 
"count" ) ) ) 
count = props[QStringLiteral( 
"count" )].toInt();
 
  338   if ( props.contains( QStringLiteral( 
"hueMin" ) ) ) 
hueMin = props[QStringLiteral( 
"hueMin" )].toInt();
 
  339   if ( props.contains( QStringLiteral( 
"hueMax" ) ) ) 
hueMax = props[QStringLiteral( 
"hueMax" )].toInt();
 
  340   if ( props.contains( QStringLiteral( 
"satMin" ) ) ) 
satMin = props[QStringLiteral( 
"satMin" )].toInt();
 
  341   if ( props.contains( QStringLiteral( 
"satMax" ) ) ) 
satMax = props[QStringLiteral( 
"satMax" )].toInt();
 
  342   if ( props.contains( QStringLiteral( 
"valMin" ) ) ) 
valMin = props[QStringLiteral( 
"valMin" )].toInt();
 
  343   if ( props.contains( QStringLiteral( 
"valMax" ) ) ) 
valMax = props[QStringLiteral( 
"valMax" )].toInt();
 
  352   return static_cast< double >( index ) / ( 
mColors.size() - 1 );
 
  357   if ( value < 0 || value > 1 )
 
  360   int colorCnt = 
mColors.count();
 
  361   int colorIdx = std::min( 
static_cast< int >( 
value * colorCnt ), colorCnt - 1 );
 
  363   if ( colorIdx >= 0 && colorIdx < colorCnt )
 
  382   map[QStringLiteral( 
"count" )] = QString::number( 
mCount );
 
  383   map[QStringLiteral( 
"hueMin" )] = QString::number( 
mHueMin );
 
  384   map[QStringLiteral( 
"hueMax" )] = QString::number( 
mHueMax );
 
  385   map[QStringLiteral( 
"satMin" )] = QString::number( 
mSatMin );
 
  386   map[QStringLiteral( 
"satMax" )] = QString::number( 
mSatMax );
 
  387   map[QStringLiteral( 
"valMin" )] = QString::number( 
mValMin );
 
  388   map[QStringLiteral( 
"valMax" )] = QString::number( 
mValMax );
 
  389   map[QStringLiteral( 
"rampType" )] = 
type();
 
  394     int hueMax, 
int hueMin, 
int satMax, 
int satMin, 
int valMax, 
int valMin )
 
  397   QList<QColor> colors;
 
  408   double currentHueAngle = 360.0 * 
static_cast< double >( std::rand() ) / RAND_MAX;
 
  410   colors.reserve( 
count );
 
  411   for ( 
int i = 0; i < 
count; ++i )
 
  416     currentHueAngle += 137.50776;
 
  418     h = std::clamp( std::round( ( std::fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 0.0, 359.0 );
 
  419     s = std::clamp( ( 
static_cast<int>( std::rand() ) % ( safeSatMax - safeSatMin + 1 ) ) + safeSatMin, 0, 255 );
 
  420     v = std::clamp( ( 
static_cast<int>( std::rand() ) % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 0, 255 );
 
  421     colors.append( QColor::fromHsv( h, s, v ) );
 
  450   if ( std::isnan( 
value ) )
 
  464   int h = 
static_cast< int >( 360.0 * std::rand() / ( RAND_MAX + 1.0 ) );
 
  466   int v = ( std::rand() % ( maxVal - minVal + 1 ) ) + minVal;
 
  467   return QColor::fromHsv( h, s, v );
 
  480   double hueOffset = ( 360.0 * std::rand() / ( RAND_MAX + 1.0 ) );
 
  485   double hueStep = 359.0 / colorCount;
 
  486   double currentHue = hueOffset;
 
  489   for ( 
int idx = 0; idx < colorCount; ++ idx )
 
  491     int h = 
static_cast< int >( std::round( currentHue ) ) % 360;
 
  495     currentHue += hueStep;
 
  499   std::random_device rd;
 
  500   std::mt19937 g( rd() );
 
  516   return QVariantMap();
 
  522   : mSchemeName( schemeName )
 
  524   , mInverted( inverted )
 
  533   bool inverted = 
false;
 
  535   if ( props.contains( QStringLiteral( 
"schemeName" ) ) )
 
  536     schemeName = props[QStringLiteral( 
"schemeName" )].toString();
 
  537   if ( props.contains( QStringLiteral( 
"colors" ) ) )
 
  538     colors = props[QStringLiteral( 
"colors" )].toInt();
 
  539   if ( props.contains( QStringLiteral( 
"inverted" ) ) )
 
  540     inverted = props[QStringLiteral( 
"inverted" )].toInt();
 
  551     QList<QColor> tmpPalette;
 
  553     for ( 
int k = 
mPalette.size() - 1; k >= 0; k-- )
 
  575   return static_cast< double >( index ) / ( 
mPalette.size() - 1 );
 
  580   if ( 
mPalette.isEmpty() || value < 0 || value > 1 || std::isnan( 
value ) )
 
  583   int paletteEntry = 
static_cast< int >( 
value * 
mPalette.count() );
 
  584   if ( paletteEntry >= 
mPalette.count() )
 
  585     paletteEntry = 
mPalette.count() - 1;
 
  604   map[QStringLiteral( 
"colors" )] = QString::number( 
mColors );
 
  605   map[QStringLiteral( 
"inverted" )] = QString::number( 
mInverted );
 
  606   map[QStringLiteral( 
"rampType" )] = 
type();
 
  615     bool inverted, 
bool doLoadFile )
 
  617   , mSchemeName( schemeName )
 
  618   , mVariantName( variantName )
 
  619   , mInverted( inverted )
 
  628     const QString &variantName, 
bool inverted, 
bool doLoadFile )
 
  630   , mSchemeName( schemeName )
 
  631   , mVariantName( variantName )
 
  632   , mVariantList( variantList )
 
  633   , mInverted( inverted )
 
  647   bool inverted = 
false;
 
  649   if ( props.contains( QStringLiteral( 
"schemeName" ) ) )
 
  650     schemeName = props[QStringLiteral( 
"schemeName" )].toString();
 
  651   if ( props.contains( QStringLiteral( 
"variantName" ) ) )
 
  652     variantName = props[QStringLiteral( 
"variantName" )].toString();
 
  653   if ( props.contains( QStringLiteral( 
"inverted" ) ) )
 
  654     inverted = props[QStringLiteral( 
"inverted" )].toInt();
 
  702   info[QStringLiteral( 
"cpt-city-license" )] = 
"<cpt-city>" + copyingFilename;
 
  713   map[QStringLiteral( 
"inverted" )] = QString::number( 
mInverted );
 
  714   map[QStringLiteral( 
"rampType" )] = 
type();
 
  756   if ( filename.isNull() )
 
  765   QMap< double, QPair<QColor, QColor> > colorMap =
 
  771   QMap<double, QPair<QColor, QColor> >::const_iterator it, prev;
 
  777   it = prev = colorMap.constBegin();
 
  778   while ( it != colorMap.constEnd() )
 
  781     if ( it != colorMap.constBegin() && ( it.value().first != it.value().second ) )
 
  783       if ( it.value().first == prev.value().second )
 
  799   it = prev = colorMap.constBegin();
 
  800   while ( it != colorMap.constEnd() )
 
  812            ( it.key() != 0.0 && it.key() != 1.0 ) )
 
  822   if ( ! 
mStops.isEmpty() && 
mStops.at( 0 ).offset == 0.0 )
 
  824   if ( ! 
mStops.isEmpty() && 
mStops.last().offset == 1.0 )
 
  843   const auto constColors = 
colors;
 
  844   for ( 
const QColor &
color : constColors )
 
  849   if ( mColors.isEmpty() )
 
  850     mColors << qMakePair( QColor( 250, 75, 60 ), QStringLiteral( 
"#fa4b3c" ) );
 
  857   if ( mColors.isEmpty() )
 
  858     mColors << qMakePair( QColor( 250, 75, 60 ), QStringLiteral( 
"#fa4b3c" ) );
 
  866   QString colorString = 
properties.value( QStringLiteral( 
"preset_color_%1" ).arg( i ), QString() ).toString();
 
  867   QString colorName = 
properties.value( QStringLiteral( 
"preset_color_name_%1" ).arg( i ), QString() ).toString();
 
  868   while ( !colorString.isEmpty() )
 
  872     colorString = 
properties.value( QStringLiteral( 
"preset_color_%1" ).arg( i ), QString() ).toString();
 
  873     colorName = 
properties.value( QStringLiteral( 
"preset_color_name_%1" ).arg( i ), QString() ).toString();
 
  882   l.reserve( mColors.count() );
 
  883   for ( 
int i = 0; i < mColors.count(); ++i )
 
  885     l << mColors.at( i ).first;
 
  892   if ( mColors.empty() )
 
  894   return static_cast< double >( index ) / ( mColors.size() - 1 );
 
  899   if ( value < 0 || value > 1 )
 
  902   int colorCnt = mColors.count();
 
  903   int colorIdx = std::min( 
static_cast< int >( 
value * colorCnt ), colorCnt - 1 );
 
  905   if ( colorIdx >= 0 && colorIdx < colorCnt )
 
  906     return mColors.at( colorIdx ).first;
 
  920   for ( 
int k = mColors.size() - 1; k >= 0; k-- )
 
  922     tmpColors << mColors.at( k );
 
  935   for ( 
int i = 0; i < mColors.count(); ++i )
 
  938     props.insert( QStringLiteral( 
"preset_color_name_%1" ).arg( i ), mColors.at( i ).second );
 
  940   props[QStringLiteral( 
"rampType" )] = 
type();
 
  946   return mColors.count();
 
  956   return QList<QPair<QString, QString> >
 
Color ramp utilising "Color Brewer" preset color schemes.
void invert() override
Inverts the ordering of the color ramp.
static QList< int > listSchemeVariants(const QString &schemeName)
Returns a list of the valid variants (numbers of colors) for a specified color brewer scheme name.
QColor color(double value) const override
Returns the color corresponding to a specified value.
QgsColorBrewerColorRamp * clone() const override
Creates a clone of the color ramp.
static QString typeString()
Returns the string identifier for QgsColorBrewerColorRamp.
static QgsColorRamp * create(const QVariantMap &properties=QVariantMap())
Returns a new QgsColorBrewerColorRamp color ramp created using the properties encoded in a string map...
static QStringList listSchemeNames()
Returns a list of all valid color brewer scheme names.
QString type() const override
Returns a string representing the color ramp type.
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
QString schemeName() const
Returns the name of the color brewer color scheme.
int colors() const
Returns the number of colors in the ramp.
QVariantMap properties() const override
Returns a string map containing all the color ramp's properties.
QgsColorBrewerColorRamp(const QString &schemeName=DEFAULT_COLORBREWER_SCHEMENAME, int colors=DEFAULT_COLORBREWER_COLORS, bool inverted=false)
Constructor for QgsColorBrewerColorRamp.
void loadPalette()
Generates the scheme using the current name and number of colors.
static QStringList listSchemes()
static QList< QColor > listSchemeColors(const QString &schemeName, int colors)
static QList< int > listSchemeVariants(const QString &schemeName)
Abstract base class for color ramps.
static QList< QPair< QString, QString > > rampTypes()
Returns a list of available ramp types, where the first value in each item is the QgsColorRamp::type(...
static QString defaultBaseDir()
static QMap< QString, QString > copyingInfo(const QString &fileName)
static QString findFileName(const QString &target, const QString &startDir, const QString &baseDir)
static QMap< double, QPair< QColor, QColor > > gradientColorMap(const QString &fileName)
QgsCptCityColorRamp * clone() const override
Creates a clone of the color ramp.
QgsStringMap copyingInfo() const
QVariantMap properties() const override
Returns a string map containing all the color ramp's properties.
QgsCptCityColorRamp(const QString &schemeName=DEFAULT_CPTCITY_SCHEMENAME, const QString &variantName=DEFAULT_CPTCITY_VARIANTNAME, bool inverted=false, bool doLoadFile=true)
Constructor for QgsCptCityColorRamp.
static QgsColorRamp * create(const QVariantMap &properties=QVariantMap())
Creates the symbol layer.
QStringList variantList() const
void copy(const QgsCptCityColorRamp *other)
static QString typeString()
Returns the string identifier for QgsCptCityColorRamp.
QString descFileName() const
QgsGradientColorRamp * cloneGradientRamp() const
QString copyingFileName() const
void invert() override
Inverts the ordering of the color ramp.
QString type() const override
Returns a string representing the color ramp type.
QString schemeName() const
QString variantName() const
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
QgsGradientStopsList mStops
void setInfo(const QgsStringMap &info)
Sets additional info to attach to the gradient ramp (e.g., authorship notes)
bool isDiscrete() const
Returns true if the gradient is using discrete interpolation, rather than smoothly interpolating betw...
QVariantMap properties() const override
Returns a string map containing all the color ramp's properties.
static QgsColorRamp * create(const QVariantMap &properties=QVariantMap())
Creates a new QgsColorRamp from a map of properties.
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (e.g., authorship notes)
void convertToDiscrete(bool discrete)
Converts a gradient with existing color stops to or from discrete interpolation.
QColor color(double value) const override
Returns the color corresponding to a specified value.
static QString typeString()
Returns the string identifier for QgsGradientColorRamp.
void setStops(const QgsGradientStopsList &stops)
Sets the list of intermediate gradient stops for the ramp.
QString type() const override
Returns a string representing the color ramp type.
QgsGradientColorRamp(const QColor &color1=DEFAULT_GRADIENT_COLOR1, const QColor &color2=DEFAULT_GRADIENT_COLOR2, bool discrete=false, const QgsGradientStopsList &stops=QgsGradientStopsList())
Constructor for QgsGradientColorRamp.
QColor color1() const
Returns the gradient start color.
void invert() override
Inverts the ordering of the color ramp.
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
void addStopsToGradient(QGradient *gradient, double opacity=1)
Copy color ramp stops to a QGradient.
QgsGradientColorRamp * clone() const override
Creates a clone of the color ramp.
QColor color2() const
Returns the gradient end color.
Represents a color stop within a QgsGradientColorRamp color ramp.
double offset
Relative positional offset, between 0 and 1.
Constrained random color ramp, which returns random colors based on preset parameters.
static QString typeString()
Returns the string identifier for QgsLimitedRandomColorRamp.
void updateColors()
Must be called after changing the properties of the color ramp to regenerate the list of random color...
static QList< QColor > randomColors(int count, int hueMax=DEFAULT_RANDOM_HUE_MAX, int hueMin=DEFAULT_RANDOM_HUE_MIN, int satMax=DEFAULT_RANDOM_SAT_MAX, int satMin=DEFAULT_RANDOM_SAT_MIN, int valMax=DEFAULT_RANDOM_VAL_MAX, int valMin=DEFAULT_RANDOM_VAL_MIN)
Gets a list of random colors.
int count() const override
Returns number of defined colors, or -1 if undefined.
QColor color(double value) const override
Returns the color corresponding to a specified value.
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
QVariantMap properties() const override
Returns a string map containing all the color ramp's properties.
int valMax() const
Returns the maximum value for generated colors.
QString type() const override
Returns a string representing the color ramp type.
int satMax() const
Returns the maximum saturation for generated colors.
static QgsColorRamp * create(const QVariantMap &properties=QVariantMap())
Returns a new QgsLimitedRandomColorRamp color ramp created using the properties encoded in a string m...
QgsLimitedRandomColorRamp * clone() const override
Creates a clone of the color ramp.
int hueMax() const
Returns the maximum hue for generated colors.
int hueMin() const
Returns the minimum hue for generated colors.
int valMin() const
Returns the minimum value for generated colors.
QgsLimitedRandomColorRamp(int count=DEFAULT_RANDOM_COUNT, int hueMin=DEFAULT_RANDOM_HUE_MIN, int hueMax=DEFAULT_RANDOM_HUE_MAX, int satMin=DEFAULT_RANDOM_SAT_MIN, int satMax=DEFAULT_RANDOM_SAT_MAX, int valMin=DEFAULT_RANDOM_VAL_MIN, int valMax=DEFAULT_RANDOM_VAL_MAX)
Constructor for QgsLimitedRandomColorRamp.
int satMin() const
Returns the minimum saturation for generated colors.
A scheme based color ramp consisting of a list of predefined colors.
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
QColor color(double value) const override
Returns the color corresponding to a specified value.
QString type() const override
Returns a string representing the color ramp type.
QList< QColor > colors() const
Returns the list of colors used by the ramp.
static QString typeString()
Returns the string identifier for QgsPresetSchemeColorRamp.
void invert() override
Inverts the ordering of the color ramp.
QVariantMap properties() const override
Returns a string map containing all the color ramp's properties.
static QgsColorRamp * create(const QVariantMap &properties=QVariantMap())
Returns a new QgsPresetSchemeColorRamp color ramp created using the properties encoded in a string ma...
int count() const override
Returns number of defined colors, or -1 if undefined.
QgsPresetSchemeColorRamp(const QList< QColor > &colors=QList< QColor >())
Constructor for QgsPresetSchemeColorRamp.
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
QgsPresetSchemeColorRamp * clone() const override
Creates a clone of the color ramp.
Totally random color ramp.
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
QList< QColor > mPrecalculatedColors
QgsRandomColorRamp * clone() const override
Creates a clone of the color ramp.
QgsRandomColorRamp()=default
Constructor for QgsRandomColorRamp.
static QString typeString()
Returns the string identifier for QgsRandomColorRamp.
int count() const override
Returns number of defined colors, or -1 if undefined.
QString type() const override
Returns a string representing the color ramp type.
virtual void setTotalColorCount(int colorCount)
Sets the desired total number of unique colors for the resultant ramp.
QVariantMap properties() const override
Returns a string map containing all the color ramp's properties.
QColor color(double value) const override
Returns the color corresponding to a specified value.
static QColor decodeColor(const QString &str)
static QString encodeColor(const QColor &color)
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
int ANALYSIS_EXPORT lower(int n, int i)
Lower function.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QMap< QString, QString > QgsStringMap
bool stopLessThan(const QgsGradientStop &s1, const QgsGradientStop &s2)
#define DEFAULT_COLORBREWER_COLORS
#define DEFAULT_COLORBREWER_SCHEMENAME
#define DEFAULT_RANDOM_HUE_MAX
#define DEFAULT_CPTCITY_SCHEMENAME
#define DEFAULT_RANDOM_HUE_MIN
#define DEFAULT_RANDOM_COUNT
#define DEFAULT_RANDOM_SAT_MAX
#define DEFAULT_RANDOM_SAT_MIN
#define DEFAULT_CPTCITY_VARIANTNAME
#define DEFAULT_GRADIENT_COLOR1
#define DEFAULT_RANDOM_VAL_MIN
QList< QgsGradientStop > QgsGradientStopsList
List of gradient stops.
#define DEFAULT_GRADIENT_COLOR2
#define DEFAULT_RANDOM_VAL_MAX