QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgscolorrampimpl.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscolorrampimpl.h
3 ---------------------
4 begin : November 2009
5 copyright : (C) 2009 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSCOLORRAMPIMPL_H
17#define QGSCOLORRAMPIMPL_H
18
19#include "qgis_core.h"
20#include <QColor>
21#include <QGradient>
22#include "qgis.h"
23#include "qgscolorscheme.h"
24#include "qgscolorramp.h"
25
26#ifndef SIP_RUN
28typedef QColor( *InterpolateColorFunc )( const QColor &c1, const QColor &c2, const double value, Qgis::AngularDirection direction );
30#endif
31
37class CORE_EXPORT QgsGradientStop
38{
39 public:
40
46 QgsGradientStop( double offset, const QColor &color );
47
49 double offset;
51 QColor color;
52
53 bool operator==( const QgsGradientStop &other ) const
54 {
55 return other.color == color && qgsDoubleNear( other.offset, offset ) && other.mColorSpec == mColorSpec && other.mDirection == mDirection;
56 }
57
58 bool operator!=( const QgsGradientStop &other ) const
59 {
60 return !( *this == other );
61 }
62
72 QColor::Spec colorSpec() const { return mColorSpec; }
73
85 void setColorSpec( QColor::Spec spec );
86
97 Qgis::AngularDirection direction() const { return mDirection; }
98
109 void setDirection( Qgis::AngularDirection direction ) { mDirection = direction; }
110
111 private:
112
113 QColor::Spec mColorSpec = QColor::Spec::Rgb;
115 InterpolateColorFunc mFunc = nullptr;
116
118};
119
121typedef QList<QgsGradientStop> QgsGradientStopsList;
122
123// these are the QGIS branding colors, exaggerated a bit to make a default ramp with greater color variation
124// then the official QGIS color gradient!
125#define DEFAULT_GRADIENT_COLOR1 QColor(69, 116, 40)
126#define DEFAULT_GRADIENT_COLOR2 QColor(188, 220, 60)
127
134class CORE_EXPORT QgsGradientColorRamp : public QgsColorRamp
135{
136 public:
137
146 QgsGradientColorRamp( const QColor &color1 = DEFAULT_GRADIENT_COLOR1,
147 const QColor &color2 = DEFAULT_GRADIENT_COLOR2,
148 bool discrete = false,
150
152 static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
153
154 int count() const override { return mStops.count() + 2; }
155 double value( int index ) const override;
156 QColor color( double value ) const override;
157
163 static QString typeString() { return QStringLiteral( "gradient" ); }
164
165 QString type() const override;
166 void invert() override;
167 QgsGradientColorRamp *clone() const override SIP_FACTORY;
168 QVariantMap properties() const override;
169
175 QColor color1() const { return mColor1; }
176
182 QColor color2() const { return mColor2; }
183
190 void setColor1( const QColor &color ) { mColor1 = color; }
191
198 void setColor2( const QColor &color ) { mColor2 = color; }
199
205 bool isDiscrete() const { return mDiscrete; }
206
214 void setDiscrete( bool discrete ) { mDiscrete = discrete; }
215
223 void convertToDiscrete( bool discrete );
224
232 void setStops( const QgsGradientStopsList &stops );
233
238 QgsGradientStopsList stops() const { return mStops; }
239
244 QgsStringMap info() const { return mInfo; }
245
251 void setInfo( const QgsStringMap &info ) { mInfo = info; }
252
259 void addStopsToGradient( QGradient *gradient, double opacity = 1 ) const;
260
270 QColor::Spec colorSpec() const { return mColorSpec; }
271
283 void setColorSpec( QColor::Spec spec );
284
295 Qgis::AngularDirection direction() const { return mDirection; }
296
307 void setDirection( Qgis::AngularDirection direction ) { mDirection = direction; }
308
309 protected:
310 QColor mColor1;
311 QColor mColor2;
315 QColor::Spec mColorSpec = QColor::Spec::Rgb;
317
318 InterpolateColorFunc mFunc = nullptr;
319};
320
322
323#define DEFAULT_RANDOM_COUNT 10
324#define DEFAULT_RANDOM_HUE_MIN 0
325#define DEFAULT_RANDOM_HUE_MAX 359
326#define DEFAULT_RANDOM_VAL_MIN 200
327#define DEFAULT_RANDOM_VAL_MAX 240
328#define DEFAULT_RANDOM_SAT_MIN 100
329#define DEFAULT_RANDOM_SAT_MAX 240
330
336class CORE_EXPORT QgsLimitedRandomColorRamp : public QgsColorRamp
337{
338 public:
339
351 int hueMin = DEFAULT_RANDOM_HUE_MIN, int hueMax = DEFAULT_RANDOM_HUE_MAX,
352 int satMin = DEFAULT_RANDOM_SAT_MIN, int satMax = DEFAULT_RANDOM_SAT_MAX,
353 int valMin = DEFAULT_RANDOM_VAL_MIN, int valMax = DEFAULT_RANDOM_VAL_MAX );
354
361 static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
362
363 double value( int index ) const override;
364 QColor color( double value ) const override;
365
371 static QString typeString() { return QStringLiteral( "random" ); }
372
373 QString type() const override;
374 QgsLimitedRandomColorRamp *clone() const override SIP_FACTORY;
375 QVariantMap properties() const override;
376 int count() const override { return mCount; }
377
381 static QList<QColor> randomColors( int count,
382 int hueMax = DEFAULT_RANDOM_HUE_MAX, int hueMin = DEFAULT_RANDOM_HUE_MIN,
383 int satMax = DEFAULT_RANDOM_SAT_MAX, int satMin = DEFAULT_RANDOM_SAT_MIN,
384 int valMax = DEFAULT_RANDOM_VAL_MAX, int valMin = DEFAULT_RANDOM_VAL_MIN );
385
390 void updateColors();
391
396 int hueMin() const { return mHueMin; }
397
402 int hueMax() const { return mHueMax; }
403
408 int satMin() const { return mSatMin; }
409
414 int satMax() const { return mSatMax; }
415
420 int valMin() const { return mValMin; }
421
426 int valMax() const { return mValMax; }
427
431 void setCount( int val ) { mCount = val; }
432
437 void setHueMin( int val ) { mHueMin = val; }
438
443 void setHueMax( int val ) { mHueMax = val; }
444
449 void setSatMin( int val ) { mSatMin = val; }
450
455 void setSatMax( int val ) { mSatMax = val; }
456
461 void setValMin( int val ) { mValMin = val; }
462
467 void setValMax( int val ) { mValMax = val; }
468
469 protected:
477 QList<QColor> mColors;
478};
479
486class CORE_EXPORT QgsRandomColorRamp: public QgsColorRamp
487{
488 public:
489
491
492 int count() const override;
493
494 double value( int index ) const override;
495
496 QColor color( double value ) const override;
497
504 virtual void setTotalColorCount( int colorCount );
505
511 static QString typeString() { return QStringLiteral( "randomcolors" ); }
512
513 QString type() const override;
514
515 QgsRandomColorRamp *clone() const override SIP_FACTORY;
516
517 QVariantMap properties() const override;
518
519 protected:
520
521 int mTotalColorCount = 0;
522 QList<QColor> mPrecalculatedColors;
523
524};
525
526
532class CORE_EXPORT QgsPresetSchemeColorRamp : public QgsColorRamp, public QgsColorScheme
533{
534 public:
535
540 QgsPresetSchemeColorRamp( const QList< QColor > &colors = QList< QColor >() );
541
547
554 static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
555
561 bool setColors( const QgsNamedColorList &colors, const QString & = QString(), const QColor & = QColor() ) override { mColors = colors; return true; }
562
567 QList< QColor > colors() const;
568
569 double value( int index ) const override;
570 QColor color( double value ) const override;
571
577 static QString typeString() { return QStringLiteral( "preset" ); }
578
579 QString type() const override;
580 void invert() override;
581 QgsPresetSchemeColorRamp *clone() const override SIP_FACTORY;
582 QVariantMap properties() const override;
583 int count() const override;
584
585 QString schemeName() const override { return QStringLiteral( "preset" ); }
586 QgsNamedColorList fetchColors( const QString &context = QString(), const QColor &baseColor = QColor() ) override;
587 bool isEditable() const override { return true; }
588
589 private:
590
591 QgsNamedColorList mColors;
592};
593
594
595#define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
596#define DEFAULT_COLORBREWER_COLORS 5
597
603class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
604{
605 public:
606
613 QgsColorBrewerColorRamp( const QString &schemeName = DEFAULT_COLORBREWER_SCHEMENAME,
614 int colors = DEFAULT_COLORBREWER_COLORS,
615 bool inverted = false );
616
623 static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
624
625 double value( int index ) const override;
626 QColor color( double value ) const override;
627
633 static QString typeString() { return QStringLiteral( "colorbrewer" ); }
634
635 QString type() const override { return QgsColorBrewerColorRamp::typeString(); }
636 void invert() override;
637 QgsColorBrewerColorRamp *clone() const override SIP_FACTORY;
638 QVariantMap properties() const override;
639 int count() const override { return mColors; }
640
645 QString schemeName() const { return mSchemeName; }
646
651 int colors() const { return mColors; }
652
659 void setSchemeName( const QString &schemeName ) { mSchemeName = schemeName; loadPalette(); }
660
667 void setColors( int colors ) { mColors = colors; loadPalette(); }
668
673 static QStringList listSchemeNames();
674
681 static QList<int> listSchemeVariants( const QString &schemeName );
682
683 protected:
684
686 void loadPalette();
687
688 QString mSchemeName;
690 QList<QColor> mPalette;
692};
693
694
695#define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
696#define DEFAULT_CPTCITY_VARIANTNAME "05"
697
705{
706 public:
707
715 QgsCptCityColorRamp( const QString &schemeName = DEFAULT_CPTCITY_SCHEMENAME,
716 const QString &variantName = DEFAULT_CPTCITY_VARIANTNAME,
717 bool inverted = false,
718 bool doLoadFile = true );
719
728 QgsCptCityColorRamp( const QString &schemeName, const QStringList &variantList,
729 const QString &variantName = QString(), bool inverted = false,
730 bool doLoadFile = true );
731
733 static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY; // cppcheck-suppress duplInheritedMember
734
740 static QString typeString() { return QStringLiteral( "cpt-city" ); } // cppcheck-suppress duplInheritedMember
741
742 QString type() const override;
743
744 void invert() override;
745
746 QgsCptCityColorRamp *clone() const override SIP_FACTORY;
747 void copy( const QgsCptCityColorRamp *other );
748 QgsGradientColorRamp *cloneGradientRamp() const SIP_FACTORY;
749
750 QVariantMap properties() const override;
751
752 QString schemeName() const { return mSchemeName; }
753 QString variantName() const { return mVariantName; }
754 QStringList variantList() const { return mVariantList; }
755
756 // lazy loading - have to call loadPalette() explicitly
757 void setSchemeName( const QString &schemeName ) { mSchemeName = schemeName; mFileLoaded = false; }
758 void setVariantName( const QString &variantName ) { mVariantName = variantName; mFileLoaded = false; }
759 void setVariantList( const QStringList &variantList ) { mVariantList = variantList; }
760 void setName( const QString &schemeName, const QString &variantName = QString(), const QStringList &variantList = QStringList() )
761 { mSchemeName = schemeName; mVariantName = variantName; mVariantList = variantList; mFileLoaded = false; }
762
763 void loadPalette() { loadFile(); }
764 bool hasMultiStops() const { return mMultiStops; }
765
772 static QString fileNameForVariant( const QString &schema, const QString &variant ) SIP_SKIP;
773
774 QString fileName() const;
775 bool loadFile();
776 bool fileLoaded() const { return mFileLoaded; }
777
778 QString copyingFileName() const;
779 QString descFileName() const;
780 QgsStringMap copyingInfo() const;
781
782 protected:
783 QString mSchemeName;
785 QStringList mVariantList;
786 bool mFileLoaded = false;
787 bool mMultiStops = false;
789
790};
791
792// clazy:excludeall=qstring-allocations
793
794#endif
AngularDirection
Angular directions.
Definition qgis.h:3175
@ CounterClockwise
Counter-clockwise direction.
Color ramp utilising "Color Brewer" preset color schemes.
static QString typeString()
Returns the string identifier for QgsColorBrewerColorRamp.
QString type() const override
Returns a string representing the color ramp type.
QString schemeName() const
Returns the name of the color brewer color scheme.
int colors() const
Returns the number of colors in the ramp.
void setSchemeName(const QString &schemeName)
Sets the name of the color brewer color scheme.
void setColors(int colors)
Sets the number of colors in the ramp.
Abstract base class for color ramps.
Abstract base class for color schemes.
A color ramp from the CPT City collection.
void setName(const QString &schemeName, const QString &variantName=QString(), const QStringList &variantList=QStringList())
void setSchemeName(const QString &schemeName)
void setVariantList(const QStringList &variantList)
QStringList variantList() const
void setVariantName(const QString &variantName)
static QString typeString()
Returns the string identifier for QgsCptCityColorRamp.
QString variantName() const
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
QgsGradientStopsList mStops
void setColor1(const QColor &color)
Sets the gradient start color.
void setColor2(const QColor &color)
Sets the gradient end color.
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...
int count() const override
Returns number of defined colors, or -1 if undefined.
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (e.g., authorship notes)
QColor::Spec colorSpec() const
Returns the color specification in which the color component interpolation will occur.
static QString typeString()
Returns the string identifier for QgsGradientColorRamp.
void setDirection(Qgis::AngularDirection direction)
Sets the direction to traverse the color wheel using when interpolating hue-based color specification...
Qgis::AngularDirection direction() const
Returns the direction to traverse the color wheel using when interpolating hue-based color specificat...
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
void setDiscrete(bool discrete)
Sets whether the gradient should use discrete interpolation, rather than smoothly interpolating betwe...
QColor color2() const
Returns the gradient end color.
Represents a color stop within a QgsGradientColorRamp color ramp.
Qgis::AngularDirection direction() const
Returns the direction to traverse the color wheel using when interpolating hue-based color specificat...
bool operator==(const QgsGradientStop &other) const
QColor::Spec colorSpec() const
Returns the color specification in which the color component interpolation will occur.
double offset
Relative positional offset, between 0 and 1.
bool operator!=(const QgsGradientStop &other) const
void setDirection(Qgis::AngularDirection direction)
Sets the direction to traverse the color wheel using when interpolating hue-based color specification...
QColor color
Gradient color at stop.
Constrained random color ramp, which returns random colors based on preset parameters.
static QString typeString()
Returns the string identifier for QgsLimitedRandomColorRamp.
void setSatMin(int val)
Sets the minimum saturation for generated colors.
void setHueMin(int val)
Sets the minimum hue for generated colors.
void setSatMax(int val)
Sets the maximum saturation for generated colors.
int valMax() const
Returns the maximum value for generated colors.
int satMax() const
Returns the maximum saturation for generated colors.
void setHueMax(int val)
Sets the maximum hue for generated colors.
void setCount(int val)
Sets the number of colors contained in the ramp.
int hueMax() const
Returns the maximum hue for generated colors.
int hueMin() const
Returns the minimum hue for generated colors.
void setValMax(int val)
Sets the maximum value for generated colors.
int valMin() const
Returns the minimum value for generated colors.
void setValMin(int val)
Sets the minimum value for generated colors.
int satMin() const
Returns the minimum saturation for generated colors.
A scheme based color ramp consisting of a list of predefined colors.
bool setColors(const QgsNamedColorList &colors, const QString &=QString(), const QColor &=QColor()) override
Sets the list of colors used by the ramp.
static QString typeString()
Returns the string identifier for QgsPresetSchemeColorRamp.
bool isEditable() const override
Returns whether the color scheme is editable.
Totally random color ramp.
QgsRandomColorRamp()=default
static QString typeString()
Returns the string identifier for QgsRandomColorRamp.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5917
QMap< QString, QString > QgsStringMap
Definition qgis.h:6455
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_FACTORY
Definition qgis_sip.h:76
#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
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)