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 Q_FOREACH (
const QString &stop, props[
"stops"].split(
':' ) )
69 int i = stop.indexOf(
';' );
79 bool discrete =
false;
80 if ( props.contains( QStringLiteral(
"discrete" ) ) )
82 if ( props[QStringLiteral(
"discrete" )] == QLatin1String(
"1" ) )
88 for ( QgsStringMap::const_iterator it = props.constBegin();
89 it != props.constEnd(); ++it )
91 if ( it.key().startsWith( QLatin1String(
"info_" ) ) )
92 info[ it.key().mid( 5 )] = it.value();
106 else if ( index >=
mStops.size() + 1 )
112 return mStops[index - 1].offset;
126 else if (
mStops.isEmpty() )
135 double lower = 0, upper = 0;
137 for ( QgsGradientStopsList::const_iterator it =
mStops.begin(); it !=
mStops.end(); ++it )
139 if ( it->offset > value )
147 return qgsDoubleNear( upper, lower ) ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
158 return qgsDoubleNear( upper, lower ) ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
170 for (
int k =
mStops.size() - 1; k >= 1; k-- )
181 for (
int k =
mStops.size() - 1; k >= 0; k-- )
205 for ( QgsGradientStopsList::const_iterator it =
mStops.begin(); it !=
mStops.end(); ++it )
209 map[QStringLiteral(
"stops" )] = lst.join( QStringLiteral(
":" ) );
212 map[QStringLiteral(
"discrete" )] =
mDiscrete ?
"1" :
"0";
214 for ( QgsStringMap::const_iterator it =
mInfo.constBegin();
215 it !=
mInfo.constEnd(); ++it )
217 map[
"info_" + it.key()] = it.value();
220 map[QStringLiteral(
"rampType" )] =
type();
234 int numStops =
mStops.count() + 2;
236 for ( QgsGradientStopsList::const_iterator it =
mStops.constBegin();
237 it !=
mStops.constEnd(); ++it )
239 newStops.append(
QgsGradientStop( static_cast< double >( i ) / numStops, it->color ) );
240 if ( i == numStops - 1 )
250 int numStops =
mStops.count() + 2;
252 for ( QgsGradientStopsList::const_iterator it =
mStops.constBegin();
253 it !=
mStops.constEnd(); ++it )
255 newStops.append(
QgsGradientStop( static_cast< double >( i ) / ( numStops - 2 ), it->color ) );
256 if ( i == numStops - 3 )
285 color1.setAlpha( color1.alpha() * opacity );
286 color2.setAlpha( color2.alpha() * opacity );
288 gradient->setColorAt( 0, color1 );
289 gradient->setColorAt( 1, color2 );
291 for ( QgsGradientStopsList::const_iterator it =
mStops.constBegin();
292 it !=
mStops.constEnd(); ++it )
294 QColor rampColor = it->color;
297 rampColor.setAlpha( rampColor.alpha() * opacity );
299 gradient->setColorAt( it->offset, rampColor );
308 int satMin,
int satMax,
int valMin,
int valMax )
310 , mHueMin( hueMin ), mHueMax( hueMax )
311 , mSatMin( satMin ), mSatMax( satMax )
312 , mValMin( valMin ), mValMax( valMax )
324 if ( props.contains( QStringLiteral(
"count" ) ) ) count = props[QStringLiteral(
"count" )].toInt();
325 if ( props.contains( QStringLiteral(
"hueMin" ) ) ) hueMin = props[QStringLiteral(
"hueMin" )].toInt();
326 if ( props.contains( QStringLiteral(
"hueMax" ) ) ) hueMax = props[QStringLiteral(
"hueMax" )].toInt();
327 if ( props.contains( QStringLiteral(
"satMin" ) ) ) satMin = props[QStringLiteral(
"satMin" )].toInt();
328 if ( props.contains( QStringLiteral(
"satMax" ) ) ) satMax = props[QStringLiteral(
"satMax" )].toInt();
329 if ( props.contains( QStringLiteral(
"valMin" ) ) ) valMin = props[QStringLiteral(
"valMin" )].toInt();
330 if ( props.contains( QStringLiteral(
"valMax" ) ) ) valMax = props[QStringLiteral(
"valMax" )].toInt();
339 return static_cast< double >( index ) / (
mColors.size() - 1 );
344 if ( value < 0 || value > 1 )
347 int colorCnt =
mColors.count();
348 int colorIdx = std::min( static_cast< int >( value * colorCnt ), colorCnt - 1 );
350 if ( colorIdx >= 0 && colorIdx < colorCnt )
364 map[QStringLiteral(
"count" )] = QString::number(
mCount );
365 map[QStringLiteral(
"hueMin" )] = QString::number(
mHueMin );
366 map[QStringLiteral(
"hueMax" )] = QString::number(
mHueMax );
367 map[QStringLiteral(
"satMin" )] = QString::number(
mSatMin );
368 map[QStringLiteral(
"satMax" )] = QString::number(
mSatMax );
369 map[QStringLiteral(
"valMin" )] = QString::number(
mValMin );
370 map[QStringLiteral(
"valMax" )] = QString::number(
mValMax );
371 map[QStringLiteral(
"rampType" )] =
type();
379 QList<QColor> colors;
382 int safeHueMax = std::max( hueMin, hueMax );
383 int safeHueMin = std::min( hueMin, hueMax );
384 int safeSatMax = std::max( satMin, satMax );
385 int safeSatMin = std::min( satMin, satMax );
386 int safeValMax = std::max( valMin, valMax );
387 int safeValMin = std::min( valMin, valMax );
390 double currentHueAngle = 360.0 *
static_cast< double >( qrand() ) / RAND_MAX;
392 colors.reserve( count );
393 for (
int i = 0; i <
count; ++i )
398 currentHueAngle += 137.50776;
400 h = qBound( 0.0, std::round( ( std::fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 359.0 );
401 s = qBound( 0, ( qrand() % ( safeSatMax - safeSatMin + 1 ) ) + safeSatMin, 255 );
402 v = qBound( 0, ( qrand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 255 );
403 colors.append( QColor::fromHsv( h, s, v ) );
432 int colorIndex = ( !std::isnan( value ) ? value : 1 ) * ( mTotalColorCount - 1 );
433 if ( mTotalColorCount >= 1 && mPrecalculatedColors.length() > colorIndex )
436 return mPrecalculatedColors.at( colorIndex );
440 int h =
static_cast< int >( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
442 int v = ( qrand() % ( maxVal - minVal + 1 ) ) + minVal;
443 return QColor::fromHsv( h, s, v );
449 mPrecalculatedColors.clear();
450 mTotalColorCount = colorCount;
456 double hueOffset = ( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
461 double hueStep = 359.0 / colorCount;
462 double currentHue = hueOffset;
465 for (
int idx = 0; idx < colorCount; ++ idx )
467 int h =
static_cast< int >( std::round( currentHue ) ) % 360;
470 mPrecalculatedColors << QColor::fromHsv( h, s, v );
471 currentHue += hueStep;
475 std::random_shuffle( mPrecalculatedColors.begin(), mPrecalculatedColors.end() );
480 return QStringLiteral(
"randomcolors" );
496 : mSchemeName( schemeName )
498 , mInverted( inverted )
507 bool inverted =
false;
509 if ( props.contains( QStringLiteral(
"schemeName" ) ) )
510 schemeName = props[QStringLiteral(
"schemeName" )];
511 if ( props.contains( QStringLiteral(
"colors" ) ) )
512 colors = props[QStringLiteral(
"colors" )].toInt();
513 if ( props.contains( QStringLiteral(
"inverted" ) ) )
514 inverted = props[QStringLiteral(
"inverted" )].toInt();
525 QList<QColor> tmpPalette;
527 for (
int k =
mPalette.size() - 1; k >= 0; k-- )
549 return static_cast< double >( index ) / (
mPalette.size() - 1 );
554 if (
mPalette.isEmpty() || value < 0 || value > 1 || std::isnan( value ) )
557 int paletteEntry =
static_cast< int >( value *
mPalette.count() );
558 if ( paletteEntry >=
mPalette.count() )
559 paletteEntry =
mPalette.count() - 1;
578 map[QStringLiteral(
"colors" )] = QString::number(
mColors );
579 map[QStringLiteral(
"inverted" )] = QString::number(
mInverted );
580 map[QStringLiteral(
"rampType" )] =
type();
589 bool inverted,
bool doLoadFile )
592 , mVariantName( variantName )
597 if ( doLoadFile && ( variantName != QString() ||
mVariantList.isEmpty() ) )
602 const QString &
variantName,
bool inverted,
bool doLoadFile )
613 if ( doLoadFile && ( variantName != QString() ||
mVariantList.isEmpty() ) )
621 bool inverted =
false;
623 if ( props.contains( QStringLiteral(
"schemeName" ) ) )
624 schemeName = props[QStringLiteral(
"schemeName" )];
625 if ( props.contains( QStringLiteral(
"variantName" ) ) )
626 variantName = props[QStringLiteral(
"variantName" )];
627 if ( props.contains( QStringLiteral(
"inverted" ) ) )
628 inverted = props[QStringLiteral(
"inverted" )].toInt();
671 info[QStringLiteral(
"cpt-city-license" )] =
"<cpt-city>" + copyingFilename;
682 map[QStringLiteral(
"inverted" )] = QString::number(
mInverted );
683 map[QStringLiteral(
"rampType" )] =
type();
725 if ( filename.isNull() )
734 QMap< double, QPair<QColor, QColor> > colorMap =
740 QMap<double, QPair<QColor, QColor> >::const_iterator it, prev;
746 it = prev = colorMap.constBegin();
747 while ( it != colorMap.constEnd() )
750 if ( it != colorMap.constBegin() && ( it.value().first != it.value().second ) )
752 if ( it.value().first == prev.value().second )
768 it = prev = colorMap.constBegin();
769 while ( it != colorMap.constEnd() )
781 ( it.key() != 0.0 && it.key() != 1.0 ) )
791 if ( !
mStops.isEmpty() &&
mStops.at( 0 ).offset == 0.0 )
793 if ( !
mStops.isEmpty() &&
mStops.last().offset == 1.0 )
812 Q_FOREACH (
const QColor &
color, colors )
814 mColors << qMakePair( color, color.name() );
817 if ( mColors.isEmpty() )
818 mColors << qMakePair( QColor( 250, 75, 60 ), QStringLiteral(
"#fa4b3c" ) );
825 if ( mColors.isEmpty() )
826 mColors << qMakePair( QColor( 250, 75, 60 ), QStringLiteral(
"#fa4b3c" ) );
834 QString colorString = properties.value( QStringLiteral(
"preset_color_%1" ).arg( i ), QString() );
835 QString colorName = properties.value( QStringLiteral(
"preset_color_name_%1" ).arg( i ), QString() );
836 while ( !colorString.isEmpty() )
840 colorString = properties.value( QStringLiteral(
"preset_color_%1" ).arg( i ), QString() );
841 colorName = properties.value( QStringLiteral(
"preset_color_name_%1" ).arg( i ), QString() );
850 l.reserve( mColors.count() );
851 for (
int i = 0; i < mColors.count(); ++i )
853 l << mColors.at( i ).first;
860 if ( mColors.empty() )
862 return static_cast< double >( index ) / ( mColors.size() - 1 );
867 if ( value < 0 || value > 1 )
870 int colorCnt = mColors.count();
871 int colorIdx = std::min( static_cast< int >( value * colorCnt ), colorCnt - 1 );
873 if ( colorIdx >= 0 && colorIdx < colorCnt )
874 return mColors.at( colorIdx ).first;
883 for (
int k = mColors.size() - 1; k >= 0; k-- )
885 tmpColors << mColors.at( k );
898 for (
int i = 0; i < mColors.count(); ++i )
901 props.insert( QStringLiteral(
"preset_color_name_%1" ).arg( i ), mColors.at( i ).second );
903 props[QStringLiteral(
"rampType" )] =
type();
909 return mColors.count();
int valMin() const
Returns the minimum value for generated colors.
QgsStringMap copyingInfo() const
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.
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.
int colors() const
Returns the number of colors in the ramp.
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
#define DEFAULT_RANDOM_HUE_MIN
QgsGradientColorRamp * cloneGradientRamp() const
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 schemeName() const
#define DEFAULT_COLORBREWER_SCHEMENAME
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
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.
QString variantName() const
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.
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (e.g., authorship notes) ...
QString type() const override
Returns a string representing the color ramp type.
QString copyingFileName() const
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
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)
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.
int satMax() const
Returns the maximum saturation for generated colors.
#define DEFAULT_CPTCITY_SCHEMENAME
int hueMin() const
Returns the minimum hue for generated colors.
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.
int hueMax() const
Returns the maximum hue for generated colors.
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.
QString descFileName() const
Totally random color ramp.
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.
static QStringList listSchemes()
QString type() const override
Returns a string representing the color ramp type.
static QList< QColor > listSchemeColors(const QString &schemeName, int colors)
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.
int satMin() const
Returns the minimum saturation for generated colors.
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.
#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.
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
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...
QColor color2() const
Returns the gradient end color.
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 ...
QList< QColor > colors() const
Returns the list of colors used by the ramp.
QgsPresetSchemeColorRamp(const QList< QColor > &colors=QList< QColor >())
Constructor for QgsPresetSchemeColorRamp.
#define DEFAULT_CPTCITY_VARIANTNAME
QColor color1() const
Returns the gradient start color.
QgsStringMap properties() const override
Returns a string map containing all the color ramp's properties.
QString schemeName() const
Returns the name of the color brewer color scheme.
static QString findFileName(const QString &target, const QString &startDir, const QString &baseDir)
#define DEFAULT_COLORBREWER_COLORS
static QColor decodeColor(const QString &str)
QStringList variantList() const
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.