QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
30 static QColor _interpolate(
const QColor &c1,
const QColor &c2,
const double value )
32 if ( std::isnan( value ) )
return c2;
34 qreal r = ( c1.redF() + value * ( c2.redF() - c1.redF() ) );
35 qreal g = ( c1.greenF() + value * ( c2.greenF() - c1.greenF() ) );
36 qreal b = ( c1.blueF() + value * ( c2.blueF() - c1.blueF() ) );
37 qreal a = ( c1.alphaF() + value * ( c2.alphaF() - c1.alphaF() ) );
39 return QColor::fromRgbF( r, g, b, a );
48 , mDiscrete( discrete )
58 if ( props.contains( QStringLiteral(
"color1" ) ) )
60 if ( props.contains( QStringLiteral(
"color2" ) ) )
65 if ( props.contains( QStringLiteral(
"stops" ) ) )
67 const auto constSplit = props[
"stops"].split(
':' );
68 for (
const QString &stop : constSplit )
70 int i = stop.indexOf(
';' );
80 bool discrete =
false;
81 if ( props.contains( QStringLiteral(
"discrete" ) ) )
83 if ( props[QStringLiteral(
"discrete" )] == QLatin1String(
"1" ) )
89 for ( QgsStringMap::const_iterator it = props.constBegin();
90 it != props.constEnd(); ++it )
92 if ( it.key().startsWith( QLatin1String(
"info_" ) ) )
93 info[ it.key().mid( 5 )] = it.value();
107 else if ( index >=
mStops.size() + 1 )
113 return mStops[index - 1].offset;
127 else if (
mStops.isEmpty() )
136 double lower = 0, upper = 0;
138 for ( QgsGradientStopsList::const_iterator it =
mStops.begin(); it !=
mStops.end(); ++it )
140 if ( it->offset >
value )
176 for (
int k =
mStops.size() - 1; k >= 1; k-- )
187 for (
int k =
mStops.size() - 1; k >= 0; k-- )
211 for ( QgsGradientStopsList::const_iterator it =
mStops.begin(); it !=
mStops.end(); ++it )
215 map[QStringLiteral(
"stops" )] = lst.join( QLatin1Char(
':' ) );
218 map[QStringLiteral(
"discrete" )] =
mDiscrete ?
"1" :
"0";
220 for ( QgsStringMap::const_iterator it =
mInfo.constBegin();
221 it !=
mInfo.constEnd(); ++it )
223 map[
"info_" + it.key()] = it.value();
226 map[QStringLiteral(
"rampType" )] =
type();
240 int numStops =
mStops.count() + 2;
242 for ( QgsGradientStopsList::const_iterator it =
mStops.constBegin();
243 it !=
mStops.constEnd(); ++it )
245 newStops.append(
QgsGradientStop(
static_cast< double >( i ) / numStops, it->color ) );
246 if ( i == numStops - 1 )
256 int numStops =
mStops.count() + 2;
258 for ( QgsGradientStopsList::const_iterator it =
mStops.constBegin();
259 it !=
mStops.constEnd(); ++it )
261 newStops.append(
QgsGradientStop(
static_cast< double >( i ) / ( numStops - 2 ), it->color ) );
262 if ( i == numStops - 3 )
294 gradient->setColorAt( 0,
color1 );
295 gradient->setColorAt( 1,
color2 );
297 for ( QgsGradientStopsList::const_iterator it =
mStops.constBegin();
298 it !=
mStops.constEnd(); ++it )
300 QColor rampColor = it->color;
303 rampColor.setAlpha( rampColor.alpha() * opacity );
305 gradient->setColorAt( it->offset, rampColor );
314 int satMin,
int satMax,
int valMin,
int valMax )
316 , mHueMin( hueMin ), mHueMax( hueMax )
317 , mSatMin( satMin ), mSatMax( satMax )
318 , mValMin( valMin ), mValMax( valMax )
330 if ( props.contains( QStringLiteral(
"count" ) ) )
count = props[QStringLiteral(
"count" )].toInt();
331 if ( props.contains( QStringLiteral(
"hueMin" ) ) )
hueMin = props[QStringLiteral(
"hueMin" )].toInt();
332 if ( props.contains( QStringLiteral(
"hueMax" ) ) )
hueMax = props[QStringLiteral(
"hueMax" )].toInt();
333 if ( props.contains( QStringLiteral(
"satMin" ) ) )
satMin = props[QStringLiteral(
"satMin" )].toInt();
334 if ( props.contains( QStringLiteral(
"satMax" ) ) )
satMax = props[QStringLiteral(
"satMax" )].toInt();
335 if ( props.contains( QStringLiteral(
"valMin" ) ) )
valMin = props[QStringLiteral(
"valMin" )].toInt();
336 if ( props.contains( QStringLiteral(
"valMax" ) ) )
valMax = props[QStringLiteral(
"valMax" )].toInt();
345 return static_cast< double >( index ) / (
mColors.size() - 1 );
350 if ( value < 0 || value > 1 )
353 int colorCnt =
mColors.count();
354 int colorIdx = std::min(
static_cast< int >(
value * colorCnt ), colorCnt - 1 );
356 if ( colorIdx >= 0 && colorIdx < colorCnt )
375 map[QStringLiteral(
"count" )] = QString::number(
mCount );
376 map[QStringLiteral(
"hueMin" )] = QString::number(
mHueMin );
377 map[QStringLiteral(
"hueMax" )] = QString::number(
mHueMax );
378 map[QStringLiteral(
"satMin" )] = QString::number(
mSatMin );
379 map[QStringLiteral(
"satMax" )] = QString::number(
mSatMax );
380 map[QStringLiteral(
"valMin" )] = QString::number(
mValMin );
381 map[QStringLiteral(
"valMax" )] = QString::number(
mValMax );
382 map[QStringLiteral(
"rampType" )] =
type();
387 int hueMax,
int hueMin,
int satMax,
int satMin,
int valMax,
int valMin )
390 QList<QColor> colors;
401 double currentHueAngle = 360.0 *
static_cast< double >( qrand() ) / RAND_MAX;
403 colors.reserve(
count );
404 for (
int i = 0; i <
count; ++i )
409 currentHueAngle += 137.50776;
411 h = qBound( 0.0, std::round( ( std::fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 359.0 );
412 s = qBound( 0, ( qrand() % ( safeSatMax - safeSatMin + 1 ) ) + safeSatMin, 255 );
413 v = qBound( 0, ( qrand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 255 );
414 colors.append( QColor::fromHsv( h, s, v ) );
451 int h =
static_cast< int >( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
453 int v = ( qrand() % ( maxVal - minVal + 1 ) ) + minVal;
454 return QColor::fromHsv( h, s, v );
467 double hueOffset = ( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
472 double hueStep = 359.0 / colorCount;
473 double currentHue = hueOffset;
476 for (
int idx = 0; idx < colorCount; ++ idx )
478 int h =
static_cast< int >( std::round( currentHue ) ) % 360;
482 currentHue += hueStep;
507 : mSchemeName( schemeName )
509 , mInverted( inverted )
518 bool inverted =
false;
520 if ( props.contains( QStringLiteral(
"schemeName" ) ) )
521 schemeName = props[QStringLiteral(
"schemeName" )];
522 if ( props.contains( QStringLiteral(
"colors" ) ) )
523 colors = props[QStringLiteral(
"colors" )].toInt();
524 if ( props.contains( QStringLiteral(
"inverted" ) ) )
525 inverted = props[QStringLiteral(
"inverted" )].toInt();
536 QList<QColor> tmpPalette;
538 for (
int k =
mPalette.size() - 1; k >= 0; k-- )
560 return static_cast< double >( index ) / (
mPalette.size() - 1 );
565 if (
mPalette.isEmpty() || value < 0 || value > 1 || std::isnan(
value ) )
568 int paletteEntry =
static_cast< int >(
value *
mPalette.count() );
569 if ( paletteEntry >=
mPalette.count() )
570 paletteEntry =
mPalette.count() - 1;
589 map[QStringLiteral(
"colors" )] = QString::number(
mColors );
590 map[QStringLiteral(
"inverted" )] = QString::number(
mInverted );
591 map[QStringLiteral(
"rampType" )] =
type();
600 bool inverted,
bool doLoadFile )
602 , mSchemeName( schemeName )
603 , mVariantName( variantName )
604 , mInverted( inverted )
613 const QString &variantName,
bool inverted,
bool doLoadFile )
615 , mSchemeName( schemeName )
616 , mVariantName( variantName )
617 , mVariantList( variantList )
618 , mInverted( inverted )
632 bool inverted =
false;
634 if ( props.contains( QStringLiteral(
"schemeName" ) ) )
635 schemeName = props[QStringLiteral(
"schemeName" )];
636 if ( props.contains( QStringLiteral(
"variantName" ) ) )
637 variantName = props[QStringLiteral(
"variantName" )];
638 if ( props.contains( QStringLiteral(
"inverted" ) ) )
639 inverted = props[QStringLiteral(
"inverted" )].toInt();
687 info[QStringLiteral(
"cpt-city-license" )] =
"<cpt-city>" + copyingFilename;
698 map[QStringLiteral(
"inverted" )] = QString::number(
mInverted );
699 map[QStringLiteral(
"rampType" )] =
type();
741 if ( filename.isNull() )
750 QMap< double, QPair<QColor, QColor> > colorMap =
756 QMap<double, QPair<QColor, QColor> >::const_iterator it, prev;
762 it = prev = colorMap.constBegin();
763 while ( it != colorMap.constEnd() )
766 if ( it != colorMap.constBegin() && ( it.value().first != it.value().second ) )
768 if ( it.value().first == prev.value().second )
784 it = prev = colorMap.constBegin();
785 while ( it != colorMap.constEnd() )
797 ( it.key() != 0.0 && it.key() != 1.0 ) )
807 if ( !
mStops.isEmpty() &&
mStops.at( 0 ).offset == 0.0 )
809 if ( !
mStops.isEmpty() &&
mStops.last().offset == 1.0 )
828 const auto constColors =
colors;
829 for (
const QColor &
color : constColors )
834 if ( mColors.isEmpty() )
835 mColors << qMakePair( QColor( 250, 75, 60 ), QStringLiteral(
"#fa4b3c" ) );
842 if ( mColors.isEmpty() )
843 mColors << qMakePair( QColor( 250, 75, 60 ), QStringLiteral(
"#fa4b3c" ) );
851 QString colorString =
properties.value( QStringLiteral(
"preset_color_%1" ).arg( i ), QString() );
852 QString colorName =
properties.value( QStringLiteral(
"preset_color_name_%1" ).arg( i ), QString() );
853 while ( !colorString.isEmpty() )
857 colorString =
properties.value( QStringLiteral(
"preset_color_%1" ).arg( i ), QString() );
858 colorName =
properties.value( QStringLiteral(
"preset_color_name_%1" ).arg( i ), QString() );
867 l.reserve( mColors.count() );
868 for (
int i = 0; i < mColors.count(); ++i )
870 l << mColors.at( i ).first;
877 if ( mColors.empty() )
879 return static_cast< double >( index ) / ( mColors.size() - 1 );
884 if ( value < 0 || value > 1 )
887 int colorCnt = mColors.count();
888 int colorIdx = std::min(
static_cast< int >(
value * colorCnt ), colorCnt - 1 );
890 if ( colorIdx >= 0 && colorIdx < colorCnt )
891 return mColors.at( colorIdx ).first;
905 for (
int k = mColors.size() - 1; k >= 0; k-- )
907 tmpColors << mColors.at( k );
920 for (
int i = 0; i < mColors.count(); ++i )
923 props.insert( QStringLiteral(
"preset_color_name_%1" ).arg( i ), mColors.at( i ).second );
925 props[QStringLiteral(
"rampType" )] =
type();
931 return mColors.count();
941 return QList<QPair<QString, QString> >
static QList< int > listSchemeVariants(const QString &schemeName)
Returns a list of the valid variants (numbers of colors) for a specified color brewer scheme name.
#define DEFAULT_RANDOM_VAL_MAX
#define DEFAULT_RANDOM_HUE_MAX
Abstract base class for color ramps.
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
static QgsColorRamp * create(const QgsStringMap &properties=QgsStringMap())
Returns a new QgsColorBrewerColorRamp color ramp created using the properties encoded in a string map...
QList< QColor > mPrecalculatedColors
QgsPresetSchemeColorRamp * clone() const override
Creates a clone of the color ramp.
QColor color1() const
Returns the gradient start color.
static QList< int > listSchemeVariants(const QString &schemeName)
static QString encodeColor(const QColor &color)
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
QgsColorBrewerColorRamp * clone() const override
Creates a clone of the color ramp.
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
QgsCptCityColorRamp(const QString &schemeName=DEFAULT_CPTCITY_SCHEMENAME, const QString &variantName=DEFAULT_CPTCITY_VARIANTNAME, bool inverted=false, bool doLoadFile=true)
Constructor for QgsCptCityColorRamp.
int hueMin() const
Returns the minimum hue for generated colors.
static QgsColorRamp * create(const QgsStringMap &properties=QgsStringMap())
#define DEFAULT_CPTCITY_VARIANTNAME
static QList< QPair< QString, QString > > rampTypes()
Returns a list of available ramp types, where the first value in each item is the QgsColorRamp::type(...
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
QString copyingFileName() const
static QString findFileName(const QString &target, const QString &startDir, const QString &baseDir)
QColor color(double value) const override
Returns the color corresponding to a specified value.
int count() const override
Returns number of defined colors, or -1 if undefined.
#define DEFAULT_COLORBREWER_COLORS
void setInfo(const QgsStringMap &info)
Sets additional info to attach to the gradient ramp (e.g., authorship notes)
QString type() const override
Returns a string representing the color ramp type.
A scheme based color ramp consisting of a list of predefined colors.
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
#define DEFAULT_COLORBREWER_SCHEMENAME
int count() const override
Returns number of defined colors, or -1 if undefined.
QgsGradientColorRamp * clone() const override
Creates a clone of the color ramp.
static QString defaultBaseDir()
void setStops(const QgsGradientStopsList &stops)
Sets the list of intermediate gradient stops for the ramp.
bool stopLessThan(const QgsGradientStop &s1, const QgsGradientStop &s2)
QString variantName() const
QStringList variantList() const
int ANALYSIS_EXPORT lower(int n, int i)
Lower function.
#define DEFAULT_RANDOM_HUE_MIN
void invert() override
Inverts the ordering of the color ramp.
static QColor decodeColor(const QString &str)
QString type() const override
Returns a string representing the color ramp type.
static QString typeString()
Returns the string identifier for QgsColorBrewerColorRamp.
void invert() override
Inverts the ordering of the color ramp.
Constrained random color ramp, which returns random colors based on preset parameters.
int satMax() const
Returns the maximum saturation for generated colors.
Color ramp utilising "Color Brewer" preset color schemes.
QgsGradientColorRamp * cloneGradientRamp() const
QColor color(double value) const override
Returns the color corresponding to a specified value.
void convertToDiscrete(bool discrete)
Converts a gradient with existing color stops to or from discrete interpolation.
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
Totally random color ramp.
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
static QString typeString()
Returns the string identifier for QgsCptCityColorRamp.
static QgsColorRamp * create(const QgsStringMap &properties=QgsStringMap())
Returns a new QgsLimitedRandomColorRamp color ramp created using the properties encoded in a string m...
QString type() const override
Returns a string representing the color ramp type.
static QString typeString()
Returns the string identifier for QgsGradientColorRamp.
QList< QgsGradientStop > QgsGradientStopsList
List of gradient stops.
static QString typeString()
Returns the string identifier for QgsPresetSchemeColorRamp.
void updateColors()
Must be called after changing the properties of the color ramp to regenerate the list of random color...
void invert() override
Inverts the ordering of the color ramp.
QgsLimitedRandomColorRamp * clone() const override
Creates a clone of the color ramp.
QgsStringMap copyingInfo() const
#define DEFAULT_GRADIENT_COLOR1
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
int valMax() const
Returns the maximum value for generated colors.
QColor color2() const
Returns the gradient end color.
int satMin() const
Returns the minimum saturation for generated colors.
static QString typeString()
Returns the string identifier for QgsRandomColorRamp.
int colors() const
Returns the number of colors in the ramp.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
virtual void setTotalColorCount(int colorCount)
Sets the desired total number of unique colors for the resultant ramp.
QString schemeName() const
QgsCptCityColorRamp * clone() const override
Creates a clone of the color ramp.
#define DEFAULT_RANDOM_VAL_MIN
QString schemeName() const
Returns the name of the color brewer color scheme.
QString type() const override
Returns a string representing the color ramp type.
#define DEFAULT_RANDOM_SAT_MIN
QgsGradientColorRamp(const QColor &color1=DEFAULT_GRADIENT_COLOR1, const QColor &color2=DEFAULT_GRADIENT_COLOR2, bool discrete=false, const QgsGradientStopsList &stops=QgsGradientStopsList())
Constructor for QgsGradientColorRamp.
#define DEFAULT_CPTCITY_SCHEMENAME
QgsRandomColorRamp * clone() const override
Creates a clone of the color ramp.
QString type() const override
Returns a string representing the color ramp type.
QString descFileName() const
static QMap< QString, QString > copyingInfo(const QString &fileName)
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.
void invert() override
Inverts the ordering of the color ramp.
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
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.
QgsRandomColorRamp()=default
Constructor for QgsRandomColorRamp.
QMap< QString, QString > QgsStringMap
void copy(const QgsCptCityColorRamp *other)
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
static QList< QColor > listSchemeColors(const QString &schemeName, int colors)
int hueMax() const
Returns the maximum hue for generated colors.
#define DEFAULT_RANDOM_COUNT
QColor color(double value) const override
Returns the color corresponding to a specified value.
static QgsColorRamp * create(const QgsStringMap &properties=QgsStringMap())
Creates a new QgsColorRamp from a map of properties.
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
QgsColorBrewerColorRamp(const QString &schemeName=DEFAULT_COLORBREWER_SCHEMENAME, int colors=DEFAULT_COLORBREWER_COLORS, bool inverted=false)
Constructor for QgsColorBrewerColorRamp.
QgsGradientStopsList mStops
void addStopsToGradient(QGradient *gradient, double opacity=1)
Copy color ramp stops to a QGradient.
static QStringList listSchemes()
#define DEFAULT_GRADIENT_COLOR2
static QMap< double, QPair< QColor, QColor > > gradientColorMap(const QString &fileName)
QgsPresetSchemeColorRamp(const QList< QColor > &colors=QList< QColor >())
Constructor for QgsPresetSchemeColorRamp.
QList< QColor > colors() const
Returns the list of colors used by the ramp.
void loadPalette()
Generates the scheme using the current name and number of colors.
static QString typeString()
Returns the string identifier for QgsLimitedRandomColorRamp.
Represents a color stop within a QgsGradientColorRamp color ramp.
bool isDiscrete() const
Returns true if the gradient is using discrete interpolation, rather than smoothly interpolating betw...
int valMin() const
Returns the minimum value for generated colors.
double offset
Relative positional offset, between 0 and 1.
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (e.g., authorship notes)
QColor color(double value) const override
Returns the color corresponding to a specified value.
static QgsColorRamp * create(const QgsStringMap &properties=QgsStringMap())
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.
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.
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.
#define DEFAULT_RANDOM_SAT_MAX