QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
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 )
171 for (
int k =
mStops.size() - 1; k >= 1; k-- )
182 for (
int k =
mStops.size() - 1; k >= 0; k-- )
206 for ( QgsGradientStopsList::const_iterator it =
mStops.begin(); it !=
mStops.end(); ++it )
210 map[QStringLiteral(
"stops" )] = lst.join( QStringLiteral(
":" ) );
213 map[QStringLiteral(
"discrete" )] =
mDiscrete ?
"1" :
"0";
215 for ( QgsStringMap::const_iterator it =
mInfo.constBegin();
216 it !=
mInfo.constEnd(); ++it )
218 map[
"info_" + it.key()] = it.value();
221 map[QStringLiteral(
"rampType" )] =
type();
235 int numStops =
mStops.count() + 2;
237 for ( QgsGradientStopsList::const_iterator it =
mStops.constBegin();
238 it !=
mStops.constEnd(); ++it )
240 newStops.append(
QgsGradientStop(
static_cast< double >( i ) / numStops, it->color ) );
241 if ( i == numStops - 1 )
251 int numStops =
mStops.count() + 2;
253 for ( QgsGradientStopsList::const_iterator it =
mStops.constBegin();
254 it !=
mStops.constEnd(); ++it )
256 newStops.append(
QgsGradientStop(
static_cast< double >( i ) / ( numStops - 2 ), it->color ) );
257 if ( i == numStops - 3 )
289 gradient->setColorAt( 0,
color1 );
290 gradient->setColorAt( 1,
color2 );
292 for ( QgsGradientStopsList::const_iterator it =
mStops.constBegin();
293 it !=
mStops.constEnd(); ++it )
295 QColor rampColor = it->color;
298 rampColor.setAlpha( rampColor.alpha() * opacity );
300 gradient->setColorAt( it->offset, rampColor );
309 int satMin,
int satMax,
int valMin,
int valMax )
311 , mHueMin( hueMin ), mHueMax( hueMax )
312 , mSatMin( satMin ), mSatMax( satMax )
313 , mValMin( valMin ), mValMax( valMax )
325 if ( props.contains( QStringLiteral(
"count" ) ) )
count = props[QStringLiteral(
"count" )].toInt();
326 if ( props.contains( QStringLiteral(
"hueMin" ) ) )
hueMin = props[QStringLiteral(
"hueMin" )].toInt();
327 if ( props.contains( QStringLiteral(
"hueMax" ) ) )
hueMax = props[QStringLiteral(
"hueMax" )].toInt();
328 if ( props.contains( QStringLiteral(
"satMin" ) ) )
satMin = props[QStringLiteral(
"satMin" )].toInt();
329 if ( props.contains( QStringLiteral(
"satMax" ) ) )
satMax = props[QStringLiteral(
"satMax" )].toInt();
330 if ( props.contains( QStringLiteral(
"valMin" ) ) )
valMin = props[QStringLiteral(
"valMin" )].toInt();
331 if ( props.contains( QStringLiteral(
"valMax" ) ) )
valMax = props[QStringLiteral(
"valMax" )].toInt();
340 return static_cast< double >( index ) / (
mColors.size() - 1 );
345 if ( value < 0 || value > 1 )
348 int colorCnt =
mColors.count();
349 int colorIdx = std::min(
static_cast< int >(
value * colorCnt ), colorCnt - 1 );
351 if ( colorIdx >= 0 && colorIdx < colorCnt )
365 map[QStringLiteral(
"count" )] = QString::number(
mCount );
366 map[QStringLiteral(
"hueMin" )] = QString::number(
mHueMin );
367 map[QStringLiteral(
"hueMax" )] = QString::number(
mHueMax );
368 map[QStringLiteral(
"satMin" )] = QString::number(
mSatMin );
369 map[QStringLiteral(
"satMax" )] = QString::number(
mSatMax );
370 map[QStringLiteral(
"valMin" )] = QString::number(
mValMin );
371 map[QStringLiteral(
"valMax" )] = QString::number(
mValMax );
372 map[QStringLiteral(
"rampType" )] =
type();
377 int hueMax,
int hueMin,
int satMax,
int satMin,
int valMax,
int valMin )
380 QList<QColor> colors;
391 double currentHueAngle = 360.0 *
static_cast< double >( qrand() ) / RAND_MAX;
393 colors.reserve(
count );
394 for (
int i = 0; i <
count; ++i )
399 currentHueAngle += 137.50776;
401 h = qBound( 0.0, std::round( ( std::fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 359.0 );
402 s = qBound( 0, ( qrand() % ( safeSatMax - safeSatMin + 1 ) ) + safeSatMin, 255 );
403 v = qBound( 0, ( qrand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 255 );
404 colors.append( QColor::fromHsv( h, s, v ) );
441 int h =
static_cast< int >( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
443 int v = ( qrand() % ( maxVal - minVal + 1 ) ) + minVal;
444 return QColor::fromHsv( h, s, v );
457 double hueOffset = ( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
462 double hueStep = 359.0 / colorCount;
463 double currentHue = hueOffset;
466 for (
int idx = 0; idx < colorCount; ++ idx )
468 int h =
static_cast< int >( std::round( currentHue ) ) % 360;
472 currentHue += hueStep;
481 return QStringLiteral(
"randomcolors" );
497 : mSchemeName( schemeName )
499 , mInverted( inverted )
508 bool inverted =
false;
510 if ( props.contains( QStringLiteral(
"schemeName" ) ) )
511 schemeName = props[QStringLiteral(
"schemeName" )];
512 if ( props.contains( QStringLiteral(
"colors" ) ) )
513 colors = props[QStringLiteral(
"colors" )].toInt();
514 if ( props.contains( QStringLiteral(
"inverted" ) ) )
515 inverted = props[QStringLiteral(
"inverted" )].toInt();
526 QList<QColor> tmpPalette;
528 for (
int k =
mPalette.size() - 1; k >= 0; k-- )
550 return static_cast< double >( index ) / (
mPalette.size() - 1 );
555 if (
mPalette.isEmpty() || value < 0 || value > 1 || std::isnan(
value ) )
558 int paletteEntry =
static_cast< int >(
value *
mPalette.count() );
559 if ( paletteEntry >=
mPalette.count() )
560 paletteEntry =
mPalette.count() - 1;
579 map[QStringLiteral(
"colors" )] = QString::number(
mColors );
580 map[QStringLiteral(
"inverted" )] = QString::number(
mInverted );
581 map[QStringLiteral(
"rampType" )] =
type();
590 bool inverted,
bool doLoadFile )
592 , mSchemeName( schemeName )
593 , mVariantName( variantName )
594 , mInverted( inverted )
603 const QString &variantName,
bool inverted,
bool doLoadFile )
605 , mSchemeName( schemeName )
606 , mVariantName( variantName )
607 , mVariantList( variantList )
608 , mInverted( inverted )
622 bool inverted =
false;
624 if ( props.contains( QStringLiteral(
"schemeName" ) ) )
625 schemeName = props[QStringLiteral(
"schemeName" )];
626 if ( props.contains( QStringLiteral(
"variantName" ) ) )
627 variantName = props[QStringLiteral(
"variantName" )];
628 if ( props.contains( QStringLiteral(
"inverted" ) ) )
629 inverted = props[QStringLiteral(
"inverted" )].toInt();
672 info[QStringLiteral(
"cpt-city-license" )] =
"<cpt-city>" + copyingFilename;
683 map[QStringLiteral(
"inverted" )] = QString::number(
mInverted );
684 map[QStringLiteral(
"rampType" )] =
type();
726 if ( filename.isNull() )
735 QMap< double, QPair<QColor, QColor> > colorMap =
741 QMap<double, QPair<QColor, QColor> >::const_iterator it, prev;
747 it = prev = colorMap.constBegin();
748 while ( it != colorMap.constEnd() )
751 if ( it != colorMap.constBegin() && ( it.value().first != it.value().second ) )
753 if ( it.value().first == prev.value().second )
769 it = prev = colorMap.constBegin();
770 while ( it != colorMap.constEnd() )
782 ( it.key() != 0.0 && it.key() != 1.0 ) )
792 if ( !
mStops.isEmpty() &&
mStops.at( 0 ).offset == 0.0 )
794 if ( !
mStops.isEmpty() &&
mStops.last().offset == 1.0 )
813 const auto constColors =
colors;
814 for (
const QColor &
color : constColors )
819 if ( mColors.isEmpty() )
820 mColors << qMakePair( QColor( 250, 75, 60 ), QStringLiteral(
"#fa4b3c" ) );
827 if ( mColors.isEmpty() )
828 mColors << qMakePair( QColor( 250, 75, 60 ), QStringLiteral(
"#fa4b3c" ) );
836 QString colorString =
properties.value( QStringLiteral(
"preset_color_%1" ).arg( i ), QString() );
837 QString colorName =
properties.value( QStringLiteral(
"preset_color_name_%1" ).arg( i ), QString() );
838 while ( !colorString.isEmpty() )
842 colorString =
properties.value( QStringLiteral(
"preset_color_%1" ).arg( i ), QString() );
843 colorName =
properties.value( QStringLiteral(
"preset_color_name_%1" ).arg( i ), QString() );
852 l.reserve( mColors.count() );
853 for (
int i = 0; i < mColors.count(); ++i )
855 l << mColors.at( i ).first;
862 if ( mColors.empty() )
864 return static_cast< double >( index ) / ( mColors.size() - 1 );
869 if ( value < 0 || value > 1 )
872 int colorCnt = mColors.count();
873 int colorIdx = std::min(
static_cast< int >(
value * colorCnt ), colorCnt - 1 );
875 if ( colorIdx >= 0 && colorIdx < colorCnt )
876 return mColors.at( colorIdx ).first;
885 for (
int k = mColors.size() - 1; k >= 0; k-- )
887 tmpColors << mColors.at( k );
900 for (
int i = 0; i < mColors.count(); ++i )
903 props.insert( QStringLiteral(
"preset_color_name_%1" ).arg( i ), mColors.at( i ).second );
905 props[QStringLiteral(
"rampType" )] =
type();
911 return mColors.count();
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
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
#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.
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. Returns colors generated at random, but constrained to some hardcoded satu...
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
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.
QList< QgsGradientStop > QgsGradientStopsList
List of gradient stops.
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.
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.
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