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(
';' );
83 bool discrete =
false;
84 if ( props.contains( QStringLiteral(
"discrete" ) ) )
86 if ( props[QStringLiteral(
"discrete" )] == QLatin1String(
"1" ) )
92 for ( QVariantMap::const_iterator it = props.constBegin();
93 it != props.constEnd(); ++it )
95 if ( it.key().startsWith( QLatin1String(
"info_" ) ) )
96 info[ it.key().mid( 5 )] = it.value().toString();
110 else if ( index >=
mStops.size() + 1 )
116 return mStops[index - 1].offset;
130 else if (
mStops.isEmpty() )
139 double lower = 0, upper = 0;
141 for ( QgsGradientStopsList::const_iterator it =
mStops.begin(); it !=
mStops.end(); ++it )
143 if ( it->offset >
value )
179 for (
int k =
mStops.size() - 1; k >= 1; k-- )
190 for (
int k =
mStops.size() - 1; k >= 0; k-- )
214 for ( QgsGradientStopsList::const_iterator it =
mStops.begin(); it !=
mStops.end(); ++it )
218 map[QStringLiteral(
"stops" )] = lst.join( QLatin1Char(
':' ) );
221 map[QStringLiteral(
"discrete" )] =
mDiscrete ?
"1" :
"0";
223 for ( QgsStringMap::const_iterator it =
mInfo.constBegin();
224 it !=
mInfo.constEnd(); ++it )
226 map[
"info_" + it.key()] = it.value();
229 map[QStringLiteral(
"rampType" )] =
type();
243 int numStops =
mStops.count() + 2;
245 for ( QgsGradientStopsList::const_iterator it =
mStops.constBegin();
246 it !=
mStops.constEnd(); ++it )
248 newStops.append(
QgsGradientStop(
static_cast< double >( i ) / numStops, it->color ) );
249 if ( i == numStops - 1 )
259 int numStops =
mStops.count() + 2;
261 for ( QgsGradientStopsList::const_iterator it =
mStops.constBegin();
262 it !=
mStops.constEnd(); ++it )
264 newStops.append(
QgsGradientStop(
static_cast< double >( i ) / ( numStops - 2 ), it->color ) );
265 if ( i == numStops - 3 )
297 gradient->setColorAt( 0,
color1 );
298 gradient->setColorAt( 1,
color2 );
300 for ( QgsGradientStopsList::const_iterator it =
mStops.constBegin();
301 it !=
mStops.constEnd(); ++it )
303 QColor rampColor = it->color;
306 rampColor.setAlpha( rampColor.alpha() * opacity );
308 gradient->setColorAt( it->offset, rampColor );
317 int satMin,
int satMax,
int valMin,
int valMax )
319 , mHueMin( hueMin ), mHueMax( hueMax )
320 , mSatMin( satMin ), mSatMax( satMax )
321 , mValMin( valMin ), mValMax( valMax )
333 if ( props.contains( QStringLiteral(
"count" ) ) )
count = props[QStringLiteral(
"count" )].toInt();
334 if ( props.contains( QStringLiteral(
"hueMin" ) ) )
hueMin = props[QStringLiteral(
"hueMin" )].toInt();
335 if ( props.contains( QStringLiteral(
"hueMax" ) ) )
hueMax = props[QStringLiteral(
"hueMax" )].toInt();
336 if ( props.contains( QStringLiteral(
"satMin" ) ) )
satMin = props[QStringLiteral(
"satMin" )].toInt();
337 if ( props.contains( QStringLiteral(
"satMax" ) ) )
satMax = props[QStringLiteral(
"satMax" )].toInt();
338 if ( props.contains( QStringLiteral(
"valMin" ) ) )
valMin = props[QStringLiteral(
"valMin" )].toInt();
339 if ( props.contains( QStringLiteral(
"valMax" ) ) )
valMax = props[QStringLiteral(
"valMax" )].toInt();
348 return static_cast< double >( index ) / (
mColors.size() - 1 );
353 if ( value < 0 || value > 1 )
356 int colorCnt =
mColors.count();
357 int colorIdx = std::min(
static_cast< int >(
value * colorCnt ), colorCnt - 1 );
359 if ( colorIdx >= 0 && colorIdx < colorCnt )
378 map[QStringLiteral(
"count" )] = QString::number(
mCount );
379 map[QStringLiteral(
"hueMin" )] = QString::number(
mHueMin );
380 map[QStringLiteral(
"hueMax" )] = QString::number(
mHueMax );
381 map[QStringLiteral(
"satMin" )] = QString::number(
mSatMin );
382 map[QStringLiteral(
"satMax" )] = QString::number(
mSatMax );
383 map[QStringLiteral(
"valMin" )] = QString::number(
mValMin );
384 map[QStringLiteral(
"valMax" )] = QString::number(
mValMax );
385 map[QStringLiteral(
"rampType" )] =
type();
390 int hueMax,
int hueMin,
int satMax,
int satMin,
int valMax,
int valMin )
393 QList<QColor> colors;
404 double currentHueAngle = 360.0 *
static_cast< double >( qrand() ) / RAND_MAX;
406 colors.reserve(
count );
407 for (
int i = 0; i <
count; ++i )
412 currentHueAngle += 137.50776;
414 h = qBound( 0.0, std::round( ( std::fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 359.0 );
415 s = qBound( 0, ( qrand() % ( safeSatMax - safeSatMin + 1 ) ) + safeSatMin, 255 );
416 v = qBound( 0, ( qrand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 255 );
417 colors.append( QColor::fromHsv( h, s, v ) );
454 int h =
static_cast< int >( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
456 int v = ( qrand() % ( maxVal - minVal + 1 ) ) + minVal;
457 return QColor::fromHsv( h, s, v );
470 double hueOffset = ( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
475 double hueStep = 359.0 / colorCount;
476 double currentHue = hueOffset;
479 for (
int idx = 0; idx < colorCount; ++ idx )
481 int h =
static_cast< int >( std::round( currentHue ) ) % 360;
485 currentHue += hueStep;
504 return QVariantMap();
510 : mSchemeName( schemeName )
512 , mInverted( inverted )
521 bool inverted =
false;
523 if ( props.contains( QStringLiteral(
"schemeName" ) ) )
524 schemeName = props[QStringLiteral(
"schemeName" )].toString();
525 if ( props.contains( QStringLiteral(
"colors" ) ) )
526 colors = props[QStringLiteral(
"colors" )].toInt();
527 if ( props.contains( QStringLiteral(
"inverted" ) ) )
528 inverted = props[QStringLiteral(
"inverted" )].toInt();
539 QList<QColor> tmpPalette;
541 for (
int k =
mPalette.size() - 1; k >= 0; k-- )
563 return static_cast< double >( index ) / (
mPalette.size() - 1 );
568 if (
mPalette.isEmpty() || value < 0 || value > 1 || std::isnan(
value ) )
571 int paletteEntry =
static_cast< int >(
value *
mPalette.count() );
572 if ( paletteEntry >=
mPalette.count() )
573 paletteEntry =
mPalette.count() - 1;
592 map[QStringLiteral(
"colors" )] = QString::number(
mColors );
593 map[QStringLiteral(
"inverted" )] = QString::number(
mInverted );
594 map[QStringLiteral(
"rampType" )] =
type();
603 bool inverted,
bool doLoadFile )
605 , mSchemeName( schemeName )
606 , mVariantName( variantName )
607 , mInverted( inverted )
616 const QString &variantName,
bool inverted,
bool doLoadFile )
618 , mSchemeName( schemeName )
619 , mVariantName( variantName )
620 , mVariantList( variantList )
621 , mInverted( inverted )
635 bool inverted =
false;
637 if ( props.contains( QStringLiteral(
"schemeName" ) ) )
638 schemeName = props[QStringLiteral(
"schemeName" )].toString();
639 if ( props.contains( QStringLiteral(
"variantName" ) ) )
640 variantName = props[QStringLiteral(
"variantName" )].toString();
641 if ( props.contains( QStringLiteral(
"inverted" ) ) )
642 inverted = props[QStringLiteral(
"inverted" )].toInt();
690 info[QStringLiteral(
"cpt-city-license" )] =
"<cpt-city>" + copyingFilename;
701 map[QStringLiteral(
"inverted" )] = QString::number(
mInverted );
702 map[QStringLiteral(
"rampType" )] =
type();
744 if ( filename.isNull() )
753 QMap< double, QPair<QColor, QColor> > colorMap =
759 QMap<double, QPair<QColor, QColor> >::const_iterator it, prev;
765 it = prev = colorMap.constBegin();
766 while ( it != colorMap.constEnd() )
769 if ( it != colorMap.constBegin() && ( it.value().first != it.value().second ) )
771 if ( it.value().first == prev.value().second )
787 it = prev = colorMap.constBegin();
788 while ( it != colorMap.constEnd() )
800 ( it.key() != 0.0 && it.key() != 1.0 ) )
810 if ( !
mStops.isEmpty() &&
mStops.at( 0 ).offset == 0.0 )
812 if ( !
mStops.isEmpty() &&
mStops.last().offset == 1.0 )
831 const auto constColors =
colors;
832 for (
const QColor &
color : constColors )
837 if ( mColors.isEmpty() )
838 mColors << qMakePair( QColor( 250, 75, 60 ), QStringLiteral(
"#fa4b3c" ) );
845 if ( mColors.isEmpty() )
846 mColors << qMakePair( QColor( 250, 75, 60 ), QStringLiteral(
"#fa4b3c" ) );
854 QString colorString =
properties.value( QStringLiteral(
"preset_color_%1" ).arg( i ), QString() ).toString();
855 QString colorName =
properties.value( QStringLiteral(
"preset_color_name_%1" ).arg( i ), QString() ).toString();
856 while ( !colorString.isEmpty() )
860 colorString =
properties.value( QStringLiteral(
"preset_color_%1" ).arg( i ), QString() ).toString();
861 colorName =
properties.value( QStringLiteral(
"preset_color_name_%1" ).arg( i ), QString() ).toString();
870 l.reserve( mColors.count() );
871 for (
int i = 0; i < mColors.count(); ++i )
873 l << mColors.at( i ).first;
880 if ( mColors.empty() )
882 return static_cast< double >( index ) / ( mColors.size() - 1 );
887 if ( value < 0 || value > 1 )
890 int colorCnt = mColors.count();
891 int colorIdx = std::min(
static_cast< int >(
value * colorCnt ), colorCnt - 1 );
893 if ( colorIdx >= 0 && colorIdx < colorCnt )
894 return mColors.at( colorIdx ).first;
908 for (
int k = mColors.size() - 1; k >= 0; k-- )
910 tmpColors << mColors.at( k );
923 for (
int i = 0; i < mColors.count(); ++i )
926 props.insert( QStringLiteral(
"preset_color_name_%1" ).arg( i ), mColors.at( i ).second );
928 props[QStringLiteral(
"rampType" )] =
type();
934 return mColors.count();
944 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