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 )
148 return qgsDoubleNear( upper, lower ) ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
159 return qgsDoubleNear( upper, lower ) ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
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 )
286 color1.setAlpha( color1.alpha() * opacity );
287 color2.setAlpha( color2.alpha() * opacity );
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();
380 QList<QColor> colors;
383 int safeHueMax = std::max( hueMin, hueMax );
384 int safeHueMin = std::min( hueMin, hueMax );
385 int safeSatMax = std::max( satMin, satMax );
386 int safeSatMin = std::min( satMin, satMax );
387 int safeValMax = std::max( valMin, valMax );
388 int safeValMin = std::min( valMin, valMax );
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 ) );
433 int colorIndex = ( !std::isnan( value ) ?
value : 1 ) * ( mTotalColorCount - 1 );
434 if ( mTotalColorCount >= 1 && mPrecalculatedColors.length() > colorIndex )
437 return mPrecalculatedColors.at( colorIndex );
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 );
450 mPrecalculatedColors.clear();
451 mTotalColorCount = colorCount;
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;
471 mPrecalculatedColors << QColor::fromHsv( h, s, v );
472 currentHue += hueStep;
476 std::random_shuffle( mPrecalculatedColors.begin(), mPrecalculatedColors.end() );
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 )
593 , mVariantName( variantName )
598 if ( doLoadFile && ( variantName != QString() ||
mVariantList.isEmpty() ) )
603 const QString &
variantName,
bool inverted,
bool doLoadFile )
614 if ( doLoadFile && ( variantName != QString() ||
mVariantList.isEmpty() ) )
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();
int satMax() const
Returns the maximum saturation for generated colors.
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_SAT_MAX
static QgsColorRamp * create(const QgsStringMap &properties=QgsStringMap())
Returns a new QgsColorBrewerColorRamp color ramp created using the properties encoded in a string map...
static QgsColorRamp * create(const QgsStringMap &properties=QgsStringMap())
Returns a new QgsPresetSchemeColorRamp color ramp created using the properties encoded in a string ma...
#define DEFAULT_RANDOM_VAL_MAX
#define DEFAULT_RANDOM_HUE_MAX
Represents a color stop within a QgsGradientColorRamp color ramp.
void invert() override
Inverts the ordering of the color ramp.
QgsColorBrewerColorRamp * clone() const override
Creates a clone of the color ramp.
int count() const override
Returns number of defined colors, or -1 if undefined.
QColor color2() const
Returns the gradient end color.
static QString defaultBaseDir()
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
Abstract base class for color ramps.
QString schemeName() const
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
#define DEFAULT_RANDOM_HUE_MIN
QColor color(double value) const override
Returns the color corresponding to a specified value.
void invert() override
Inverts the ordering of the color ramp.
void convertToDiscrete(bool discrete)
Converts a gradient with existing color stops to or from discrete interpolation.
static QgsColorRamp * create(const QgsStringMap &properties=QgsStringMap())
static QList< int > listSchemeVariants(const QString &schemeName)
QString descFileName() const
#define DEFAULT_COLORBREWER_SCHEMENAME
QString copyingFileName() const
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
QList< QColor > colors() const
Returns the list of colors used by the ramp.
QMap< QString, QString > QgsStringMap
QList< QgsGradientStop > QgsGradientStopsList
List of gradient stops.
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
static QString encodeColor(const QColor &color)
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
void copy(const QgsCptCityColorRamp *other)
QgsGradientColorRamp * clone() const override
Creates a clone of the color ramp.
int satMin() const
Returns the minimum saturation for generated colors.
int hueMin() const
Returns the minimum hue for generated colors.
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.
QString type() const override
Returns a string representing the color ramp type.
int colors() const
Returns the number of colors in the ramp.
Constrained random color ramp, which returns random colors based on preset parameters.
void invert() override
Inverts the ordering of the color ramp.
#define DEFAULT_RANDOM_VAL_MIN
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (e.g., authorship notes) ...
double offset
Relative positional offset, between 0 and 1.
QgsPresetSchemeColorRamp * clone() const override
Creates a clone of the color ramp.
QString type() const override
Returns a string representing the color ramp type.
bool stopLessThan(const QgsGradientStop &s1, const QgsGradientStop &s2)
int hueMax() const
Returns the maximum hue for generated colors.
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
int count() const override
Returns number of defined colors, or -1 if undefined.
#define DEFAULT_CPTCITY_SCHEMENAME
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
static QMap< QString, QString > copyingInfo(const QString &fileName)
QgsGradientColorRamp(const QColor &color1=DEFAULT_GRADIENT_COLOR1, const QColor &color2=DEFAULT_GRADIENT_COLOR2, bool discrete=false, const QgsGradientStopsList &stops=QgsGradientStopsList())
Constructor for QgsGradientColorRamp.
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.
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
void setStops(const QgsGradientStopsList &stops)
Sets the list of intermediate gradient stops for the ramp.
bool isDiscrete() const
Returns true if the gradient is using discrete interpolation, rather than smoothly interpolating betw...
#define DEFAULT_GRADIENT_COLOR1
A scheme based color ramp consisting of a list of predefined colors.
virtual void setTotalColorCount(int colorCount)
Sets the desired total number of unique colors for the resultant ramp.
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
Totally random color ramp.
QgsGradientColorRamp * cloneGradientRamp() const
int count() const override
Returns number of defined colors, or -1 if undefined.
QgsLimitedRandomColorRamp * 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.
QgsStringMap copyingInfo() const
static QStringList listSchemes()
QString type() const override
Returns a string representing the color ramp type.
static QList< QColor > listSchemeColors(const QString &schemeName, int colors)
QString schemeName() const
Returns the name of the color brewer color scheme.
static QgsColorRamp * create(const QgsStringMap &properties=QgsStringMap())
Creates a new QgsColorRamp from a map of properties.
#define DEFAULT_RANDOM_COUNT
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
QString type() const override
Returns a string representing the color ramp type.
#define DEFAULT_RANDOM_SAT_MIN
QString type() const override
Returns a string representing the color ramp type.
void invert() override
Inverts the ordering of the color ramp.
void setInfo(const QgsStringMap &info)
Sets additional info to attach to the gradient ramp (e.g., authorship notes)
QgsCptCityColorRamp * 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.
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.
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
Color ramp utilising "Color Brewer" preset color schemes.
int valMin() const
Returns the minimum value for generated colors.
#define DEFAULT_GRADIENT_COLOR2
QgsGradientStopsList mStops
QColor color(double value) const override
Returns the color corresponding to a specified value.
int valMax() const
Returns the maximum value for generated colors.
QColor color(double value) const override
Returns the color corresponding to a specified value.
static QMap< double, QPair< QColor, QColor > > gradientColorMap(const QString &fileName)
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.
QgsColorBrewerColorRamp(const QString &schemeName=DEFAULT_COLORBREWER_SCHEMENAME, int colors=DEFAULT_COLORBREWER_COLORS, bool inverted=false)
Constructor for QgsColorBrewerColorRamp.
QgsRandomColorRamp * clone() const override
Creates a clone of the color ramp.
int ANALYSIS_EXPORT lower(int n, int i)
Lower function.
void loadPalette()
Generates the scheme using the current name and number of colors.
static QgsColorRamp * create(const QgsStringMap &properties=QgsStringMap())
Returns a new QgsLimitedRandomColorRamp color ramp created using the properties encoded in a string m...
void addStopsToGradient(QGradient *gradient, double opacity=1)
Copy color ramp stops to a QGradient.
void updateColors()
Must be called after changing the properties of the color ramp to regenerate the list of random color...
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
QStringList variantList() const
QgsPresetSchemeColorRamp(const QList< QColor > &colors=QList< QColor >())
Constructor for QgsPresetSchemeColorRamp.
#define DEFAULT_CPTCITY_VARIANTNAME
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
QColor color1() const
Returns the gradient start color.
QString variantName() const
static QString findFileName(const QString &target, const QString &startDir, const QString &baseDir)
#define DEFAULT_COLORBREWER_COLORS
static QColor decodeColor(const QString &str)
QColor color(double value) const override
Returns the color corresponding to a specified value.
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.